summaryrefslogtreecommitdiff
path: root/src/xpm/XpmCrDataFI.c
blob: 81c742bd3dc789d55b7247766966bdb7962e237d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* Copyright 1990-92 GROUPE BULL -- See license conditions in file COPYRIGHT */
/*****************************************************************************\
* XpmCrDataFI.c:                                                              *
*                                                                             *
*  XPM library                                                                *
*  Scan an image and possibly its mask and create an XPM array                *
*                                                                             *
*  Developed by Arnaud Le Hors                                                *
\*****************************************************************************/

#include "xpmP.h"
#ifdef VMS
#include "sys$library:string.h"
#else
#if defined(SYSV) || defined(SVR4)
#include <string.h>
#else
#include <strings.h>
#endif
#endif

LFUNC(CreateTransparentColor, int, (char **dataptr, unsigned int *data_size,
				    char **colors, unsigned int cpp,
				    unsigned int mask_pixel,
				    char ***colorTable));

LFUNC(CreateOtherColors, int, (char **dataptr, unsigned int *data_size,
			       char **colors, XColor *xcolors,
			       unsigned int ncolors, unsigned int cpp,
			       unsigned int mask_pixel, char ***colorTable,
			       unsigned int ncolors2, Pixel *pixels,
			       char *rgb_fname));

LFUNC(CreatePixels, void, (char **dataptr, unsigned int width,
			   unsigned int height, unsigned int cpp,
			   unsigned int *pixels, char **colors));

LFUNC(CountExtensions, void, (XpmExtension *ext, unsigned int num,
			      unsigned int *ext_size,
			      unsigned int *ext_nlines));

LFUNC(CreateExtensions, void, (char **dataptr, unsigned int offset,
			       XpmExtension *ext, unsigned int num,
			       unsigned int ext_nlines));

int
XpmCreateDataFromImage(display, data_return, image, shapeimage, attributes)
    Display *display;
    char ***data_return;
    XImage *image;
    XImage *shapeimage;
    XpmAttributes *attributes;
{
    int ErrorStatus;
    xpmInternAttrib attrib;

    /*
     * initialize return values 
     */
    if (data_return)
	*data_return = NULL;

    xpmInitInternAttrib(&attrib);

    /*
     * Scan image then create data 
     */
    ErrorStatus = xpmScanImage(display, image, shapeimage,
			       attributes, &attrib);

    if (ErrorStatus == XpmSuccess)
	ErrorStatus = xpmCreateData(data_return, &attrib, attributes);

    xpmFreeInternAttrib(&attrib);

    return (ErrorStatus);
}


#undef RETURN
#define RETURN(status) \
  { if (header) { \
        for (l = 0; l < header_nlines; l++) \
	    if (header[l]) \
		free(header[l]); \
	free(header); \
    } \
    return(status); }

int
xpmCreateData(data_return, attrib, attributes)
    char ***data_return;
    xpmInternAttrib *attrib;
    XpmAttributes *attributes;
{
    /* calculation variables */
    int ErrorStatus;
    char buf[BUFSIZ];
    char **header = NULL, **data, **sptr, **sptr2, *s;
    unsigned int header_size, header_nlines;
    unsigned int data_size, data_nlines;
    unsigned int extensions = 0, ext_size = 0, ext_nlines = 0;
    unsigned int infos = 0, offset, l, n;

    *data_return = NULL;

    infos = attributes && (attributes->valuemask & XpmInfos);
    extensions = attributes && (attributes->valuemask & XpmExtensions)
	&& attributes->nextensions;

    /* compute the number of extensions lines and size */
    if (extensions)
	CountExtensions(attributes->extensions, attributes->nextensions,
			&ext_size, &ext_nlines);

    /*
     * alloc a temporary array of char pointer for the header section which
     * is the hints line + the color table lines 
     */
    header_nlines = 1 + attrib->ncolors;
    header_size = sizeof(char *) * header_nlines;
    header = (char **) calloc(header_size, sizeof(char *));
    if (!header)
	RETURN(XpmNoMemory);

    /*
     * print the hints line 
     */
    s = buf;
    sprintf(s, "%d %d %d %d", attrib->width, attrib->height,
	    attrib->ncolors, attrib->cpp);
    s += strlen(s);

    if (attributes && (attributes->valuemask & XpmHotspot)) {
	sprintf(s, " %d %d", attributes->x_hotspot, attributes->y_hotspot);
	s += strlen(s);
    }

    if (extensions)
	sprintf(s, " XPMEXT");

    l = strlen(buf) + 1;
    *header = (char *) malloc(l);
    if (!*header)
	RETURN(XpmNoMemory);
    header_size += l;
    strcpy(*header, buf);

    /*
     * print colors 
     */

    /* transparent color */
    if (attrib->mask_pixel != UNDEF_PIXEL) {
	ErrorStatus =
	    CreateTransparentColor(header + 1, &header_size,
				   attrib->colorStrings, attrib->cpp,
				   (infos ? attributes->mask_pixel : 0),
				   (infos ? attributes->colorTable : NULL));
	if (ErrorStatus != XpmSuccess)
	    RETURN(ErrorStatus);

	offset = 1;
    } else
	offset = 0;

    /* other colors */
    ErrorStatus =
	CreateOtherColors(header + 1 + offset, &header_size,
			  attrib->colorStrings + offset,
			  attrib->xcolors + offset, attrib->ncolors - offset,
			  attrib->cpp, (infos ? attributes->mask_pixel : 0),
			  (infos ? attributes->colorTable : NULL),
			  (infos ? attributes->ncolors : 0),
			  (infos ? attributes->pixels : NULL),
			  (attributes &&
			   (attributes->valuemask & XpmRgbFilename) ?
			   attributes->rgb_fname : NULL));
    if (ErrorStatus != XpmSuccess)
	RETURN(ErrorStatus);

    /*
     * now we know the size needed, alloc the data and copy the header lines 
     */
    offset = attrib->width * attrib->cpp + 1;
    data_size = header_size + (attrib->height + ext_nlines) * sizeof(char *)
	+ attrib->height * offset + ext_size;

    data = (char **) malloc(data_size);
    if (!data)
	RETURN(XpmNoMemory);

    data_nlines = header_nlines + attrib->height + ext_nlines;
    *data = (char *) (data + data_nlines);
    n = attrib->ncolors;
    for (l = 0, sptr = data, sptr2 = header; l <= n; l++, sptr++, sptr2++) {
	strcpy(*sptr, *sptr2);
	*(sptr + 1) = *sptr + strlen(*sptr2) + 1;
    }

    /*
     * print pixels 
     */
    data[header_nlines] = (char *) data + header_size +
	(attrib->height + ext_nlines) * sizeof(char *);

    CreatePixels(data + header_nlines, attrib->width, attrib->height,
		 attrib->cpp, attrib->pixelindex, attrib->colorStrings);

    /*
     * print extensions
     */
    if (extensions)
	CreateExtensions(data + header_nlines + attrib->height - 1, offset,
			 attributes->extensions, attributes->nextensions,
			 ext_nlines);

    *data_return = data;

    RETURN(XpmSuccess);
}


static int
CreateTransparentColor(dataptr, data_size, colors, cpp, mask_pixel, colorTable)
char **dataptr;
unsigned int *data_size;
char **colors;
unsigned int cpp;
unsigned int mask_pixel;
char ***colorTable;
{
    char buf[BUFSIZ];
    unsigned int key, l;
    char *s, *s2;

    strncpy(buf, *colors, cpp);
    s = buf + cpp;

    if (colorTable && mask_pixel != UNDEF_PIXEL) {
	for (key = 1; key <= NKEYS; key++) {
	    if (s2 = colorTable[mask_pixel][key]) {
		sprintf(s, "\t%s %s", xpmColorKeys[key - 1], s2);
		s += strlen(s);
	    }
	}
    } else
	sprintf(s, "\tc %s", TRANSPARENT_COLOR);

    l = strlen(buf) + 1;
    s = (char *) malloc(l);
    if (!s)
	return(XpmNoMemory);
    *data_size += l;
    strcpy(s, buf);
    *dataptr = s;
    return(XpmSuccess);
}

static int
CreateOtherColors(dataptr, data_size, colors, xcolors, ncolors, cpp,
		  mask_pixel, colorTable, ncolors2, pixels, rgb_fname)
char **dataptr;
unsigned int *data_size;
char **colors;
XColor *xcolors;
unsigned int ncolors;
unsigned int cpp;
unsigned int mask_pixel;
char ***colorTable;
unsigned int ncolors2;
Pixel *pixels;
char *rgb_fname;
{
    char buf[BUFSIZ];
    unsigned int a, b, c, d, key, l;
    char *s, *s2, *colorname;
    xpmRgbName rgbn[MAX_RGBNAMES];
    int rgbn_max = 0;

    /* read the rgb file if any was specified */
    if (rgb_fname)
	rgbn_max = xpmReadRgbNames(rgb_fname, rgbn);

    for (a = 0; a < ncolors; a++, colors++, xcolors++, dataptr++) {

	strncpy(buf, *colors, cpp);
	s = buf + cpp;

	c = 1;
	if (colorTable) {
	    d = 0;
	    for (b = 0; b < ncolors2; b++) {
		if (b == mask_pixel) {
		    d = 1;
		    continue;
		}
		if (pixels[b - d] == xcolors->pixel)
		    break;
	    }
	    if (b != ncolors2) {
		c = 0;
		for (key = 1; key <= NKEYS; key++) {
		    if (s2 = colorTable[b][key]) {
			sprintf(s, "\t%s %s", xpmColorKeys[key - 1], s2);
			s += strlen(s);
		    }
		}
	    }
	}
	if (c) {
	    colorname = NULL;
	    if (rgbn_max)
		colorname = xpmGetRgbName(rgbn, rgbn_max, xcolors->red,
					  xcolors->green, xcolors->blue);
	    if (colorname)
		sprintf(s, "\tc %s", colorname);
	    else
		sprintf(s, "\tc #%04X%04X%04X",
			xcolors->red, xcolors->green, xcolors->blue);
	    s += strlen(s);
	}
	l = strlen(buf) + 1;
	s = (char *) malloc(l);
	if (!s)
	    return(XpmNoMemory);
	*data_size += l;
	strcpy(s, buf);
	*dataptr = s;
    }
    xpmFreeRgbNames(rgbn, rgbn_max);
    return(XpmSuccess);
}

static void
CreatePixels(dataptr, width, height, cpp, pixels, colors)
char **dataptr;
unsigned int width;
unsigned int height;
unsigned int cpp;
unsigned int *pixels;
char **colors;
{
    char *s;
    unsigned int x, y, h, offset;

    h = height - 1;
    offset = width * cpp + 1;
    for (y = 0; /* test is inside loop */ ; y++, dataptr++) {
	s = *dataptr;
	for (x = 0; x < width; x++, pixels++) {
	    strncpy(s, colors[*pixels], cpp);
	    s += cpp;
	}
	*s = '\0';
	if (y >= h)
	    break;		/* LEAVE LOOP */
	*(dataptr + 1) = *dataptr + offset;
    }
}

static void
CountExtensions(ext, num, ext_size, ext_nlines)
XpmExtension *ext;
unsigned int num;
unsigned int *ext_size;
unsigned int *ext_nlines;
{
    unsigned int x, y, a, size, nlines;
    char **lines;

    size = 0;
    nlines = 0;
    for (x = 0; x < num; x++, ext++) {
	/* "+ 2" is for the name and the ending 0 */
	nlines += ext->nlines + 2;
	/* 8 = 7 (for "XPMEXT ") + 1 (for 0) */
	size += strlen(ext->name) + 8;
	a = ext->nlines;
	for (y = 0, lines = ext->lines; y < a; y++, lines++)
	    size += strlen(*lines) + 1;
    }
    *ext_size = size;
    *ext_nlines = nlines;
}

static void
CreateExtensions(dataptr, offset, ext, num, ext_nlines)
char **dataptr;
unsigned int offset;
XpmExtension *ext;
unsigned int num;
unsigned int ext_nlines;
{
    unsigned int x, y, a, b;
    char **sptr;

    *(dataptr + 1) = *dataptr + offset;
    dataptr++;
    a = 0;
    for (x = 0; x < num; x++, ext++) {
	sprintf(*dataptr, "XPMEXT %s", ext->name);
	a++;
	if (a < ext_nlines)
	    *(dataptr + 1) = *dataptr + strlen(ext->name) + 8;
	dataptr++;
	b = ext->nlines;
	for (y = 0, sptr = ext->lines; y < b; y++, sptr++) {
	    strcpy(*dataptr, *sptr);
	    a++;
	    if (a < ext_nlines)
		*(dataptr + 1) = *dataptr + strlen(*sptr) + 1;
	    dataptr++;
	}
    }
    *dataptr = 0;
}