1 /* nxpm.c - load "normalized" XPM image
3 * Raster graphics library
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* AIX requires this to be the first thing in the file. */
26 # define alloca __builtin_alloca
34 # ifndef alloca /* predefined by HP cc +Olibcalls */
50 * Restricted support for XPM images.
52 * The images must be in the following "normalized" format:
57 * 2 ignored ( normally "static char *xpm[] = {" )
58 * 3 "width height color_count chars" where chars is 1 or 2
59 * 4 color definitions. Only c values with #rrggbb or #rrrrggggbbb
63 * - no comments or blank lines are allowed, except for the signature
64 * - all lines must have at most 256 characters
65 * - no white spaces allowed at left of each line
72 RImage
*RGetImageFromXPMData(RContext
* context
, char **data
)
75 unsigned char *color_table
[4];
76 unsigned short *symbol_table
;
77 unsigned char *r
, *g
, *b
, *a
;
78 int i
, j
, k
, line
= 0;
82 int w
, h
, ccount
, csize
;
84 if (sscanf(data
[line
++], "%i %i %i %i", &w
, &h
, &ccount
, &csize
) != 4
85 || w
<= 0 || h
<= 0 || ccount
<= 0 || csize
<= 0)
88 if (csize
!= 1 && csize
!= 2)
91 color_table
[0] = alloca(ccount
);
92 color_table
[1] = alloca(ccount
);
93 color_table
[2] = alloca(ccount
);
94 color_table
[3] = alloca(ccount
);
95 symbol_table
= alloca(ccount
* sizeof(unsigned short));
97 bsize
= csize
* w
+ 16;
99 if (!color_table
[0] || !color_table
[1] || !color_table
[2] || !color_table
[3] || !symbol_table
|| !bsize
) {
100 RErrorCode
= RERR_NOMEMORY
;
106 /* get color table */
107 for (i
= 0; i
< ccount
; i
++) {
108 symbol_table
[i
] = data
[line
][0];
110 symbol_table
[i
] |= data
[line
][1] << 8;
113 while (data
[line
][j
] != '#' && data
[line
][j
] != 0 && data
[line
][j
] != 'N')
116 if (data
[line
][j
] == '#') {
117 unsigned int red
, green
, blue
;
121 while (data
[line
][j
+ k
] != 0)
124 if (sscanf(&(data
[line
][j
]), "%2x%2x%2x", &red
, &green
, &blue
) != 3)
126 } else if (k
== 12) {
127 if (sscanf(&(data
[line
][j
]), "%4x%4x%4x", &red
, &green
, &blue
) != 3)
135 color_table
[0][i
] = red
;
136 color_table
[1][i
] = green
;
137 color_table
[2][i
] = blue
;
138 color_table
[3][i
] = 255;
139 } else if (strncmp(&(data
[line
][j
]), "None", 4) == 0 || strncmp(&(data
[line
][j
]), "none", 4) == 0) {
140 color_table
[3][i
] = 0;
148 image
= RCreateImage(w
, h
, transp
);
157 if (image
->format
== RRGBAFormat
)
162 for (i
= 0; i
< h
; i
++) {
164 for (j
= 0; j
< w
; j
++) {
165 color
= data
[line
][j
];
167 for (k
= 0; k
< ccount
; k
++) {
168 if (symbol_table
[k
] == color
)
174 *r
= color_table
[0][k
];
175 *g
= color_table
[1][k
];
176 *b
= color_table
[2][k
];
178 *a
= color_table
[3][k
];
190 for (j
= 0; j
< w
* 2; j
++) {
191 color
= data
[line
][j
++];
192 color
|= data
[line
][j
];
194 for (k
= 0; k
< ccount
; k
++) {
195 if (symbol_table
[k
] == color
)
201 *r
= color_table
[0][k
];
202 *g
= color_table
[1][k
];
203 *b
= color_table
[2][k
];
205 *a
= color_table
[3][k
];
226 RErrorCode
= RERR_BADIMAGEFILE
;
231 RReleaseImage(image
);
235 RImage
*RLoadXPM(RContext
* context
, char *file
, int index
)
237 RImage
*image
= NULL
;
238 char line
[LINEWIDTH
+ 1];
240 unsigned char *color_table
[4];
241 unsigned short *symbol_table
;
242 unsigned char *r
, *g
, *b
, *a
;
245 unsigned short color
;
247 int w
, h
, ccount
, csize
;
250 f
= fopen(file
, "rb");
252 RErrorCode
= RERR_OPEN
;
256 if (!fgets(line
, LINEWIDTH
, f
))
259 if (!fgets(line
, LINEWIDTH
, f
))
263 if (!fgets(line
, LINEWIDTH
, f
))
267 if (!fgets(line
, LINEWIDTH
, f
))
270 if (sscanf(line
, "\"%i %i %i %i\"", &w
, &h
, &ccount
, &csize
) != 4
271 || w
<= 0 || h
<= 0 || ccount
<= 0 || csize
<= 0)
274 if (csize
!= 1 && csize
!= 2)
277 color_table
[0] = alloca(ccount
);
278 color_table
[1] = alloca(ccount
);
279 color_table
[2] = alloca(ccount
);
280 color_table
[3] = alloca(ccount
);
281 symbol_table
= alloca(ccount
* sizeof(unsigned short));
283 bsize
= csize
* w
+ 16;
284 buffer
= alloca(bsize
);
286 if (!color_table
[0] || !color_table
[1] || !color_table
[2] || !color_table
[3] || !symbol_table
|| !bsize
) {
287 RErrorCode
= RERR_NOMEMORY
;
294 /* get color table */
295 for (i
= 0; i
< ccount
; i
++) {
296 if (!fgets(line
, LINEWIDTH
, f
))
299 if (!fgets(line
, LINEWIDTH
, f
))
302 symbol_table
[i
] = line
[1];
304 symbol_table
[i
] |= line
[2] << 8;
307 while (line
[j
] != '#' && line
[j
] != '"' && line
[j
] != 0 && line
[j
] != 'N')
310 if (line
[j
] == '#') {
311 unsigned int red
, green
, blue
;
315 while (line
[j
+ k
] != '"' && line
[j
+ k
] != 0)
318 if (sscanf(&(line
[j
]), "%2x%2x%2x", &red
, &green
, &blue
) != 3)
320 } else if (k
== 12) {
321 if (sscanf(&(line
[j
]), "%4x%4x%4x", &red
, &green
, &blue
) != 3)
329 color_table
[0][i
] = red
;
330 color_table
[1][i
] = green
;
331 color_table
[2][i
] = blue
;
332 color_table
[3][i
] = 255;
333 } else if (strncmp(&(line
[j
]), "None", 4) == 0 || strncmp(&(line
[j
]), "none", 4) == 0) {
334 color_table
[3][i
] = 0;
341 image
= RCreateImage(w
, h
, transp
);
351 if (image
->format
== RRGBAFormat
)
356 for (i
= 0; i
< h
; i
++) {
357 if (!fgets(buffer
, bsize
, f
))
359 if (buffer
[0] == '/')
360 if (!fgets(buffer
, bsize
, f
))
364 for (j
= 1; j
<= w
; j
++) {
367 for (k
= 0; k
< ccount
; k
++) {
368 if (symbol_table
[k
] == color
)
374 *r
= color_table
[0][k
];
375 *g
= color_table
[1][k
];
376 *b
= color_table
[2][k
];
378 *a
= color_table
[3][k
];
390 for (j
= 1; j
<= w
* 2; j
++) {
392 color
|= buffer
[j
] << 8;
394 for (k
= 0; k
< ccount
; k
++) {
395 if (symbol_table
[k
] == color
)
402 *r
= color_table
[0][k
];
403 *g
= color_table
[1][k
];
404 *b
= color_table
[2][k
];
406 *a
= color_table
[3][k
];
427 RErrorCode
= RERR_BADIMAGEFILE
;
433 RReleaseImage(image
);
437 RErrorCode
= RERR_BADIMAGEFILE
;
443 RReleaseImage(image
);
449 typedef struct XPMColor
{
454 struct XPMColor
*next
;
457 #define I2CHAR(i) ((i)<12 ? (i)+'0' : ((i)<38 ? (i)+'A'-12 : (i)+'a'-38))
458 #define CINDEX(xpmc) (((unsigned)(xpmc)->red)<<16|((unsigned)(xpmc)->green)<<8|((unsigned)(xpmc)->blue))
460 static XPMColor
*lookfor(XPMColor
* list
, int index
)
465 for (; list
!= NULL
; list
= list
->next
) {
466 if (CINDEX(list
) == index
)
473 * Looks for the color in the colormap and inserts if it is not found.
475 * list is a binary search list. The unbalancing problem is just ignored.
477 * Returns False on error
479 static Bool
addcolor(XPMColor
** list
, unsigned r
, unsigned g
, unsigned b
, int *colors
)
485 index
= r
<< 16 | g
<< 8 | b
;
488 tmpc
= lookfor(*list
, index
);
493 newc
= malloc(sizeof(XPMColor
));
497 RErrorCode
= RERR_NOMEMORY
;
513 static char *index2str(char *buffer
, int index
, int charsPerPixel
)
517 for (i
= 0; i
< charsPerPixel
; i
++) {
518 buffer
[i
] = I2CHAR(index
& 63);
526 static void outputcolormap(FILE * file
, XPMColor
* colormap
, int charsPerPixel
)
534 for (index
= 0; colormap
!= NULL
; colormap
= colormap
->next
, index
++) {
535 colormap
->index
= index
;
536 fprintf(file
, "\"%s c #%02x%02x%02x\",\n",
537 index2str(buf
, index
, charsPerPixel
), colormap
->red
, colormap
->green
, colormap
->blue
);
541 static void freecolormap(XPMColor
* colormap
)
546 tmp
= colormap
->next
;
552 /* save routine is common to internal support and library support */
553 Bool
RSaveXPM(RImage
* image
, char *filename
)
559 XPMColor
*colormap
= NULL
;
563 unsigned char *r
, *g
, *b
, *a
;
567 file
= fopen(filename
, "wb+");
569 RErrorCode
= RERR_OPEN
;
573 fprintf(file
, "/* XPM */\n");
575 fprintf(file
, "static char *image[] = {\n");
580 if (image
->format
== RRGBAFormat
)
585 /* first pass: make colormap for the image */
588 for (y
= 0; y
< image
->height
; y
++) {
589 for (x
= 0; x
< image
->width
; x
++) {
590 if (!a
|| *a
> 127) {
591 if (!addcolor(&colormap
, *r
, *g
, *b
, &colorCount
)) {
609 while ((1 << charsPerPixel
* 6) < colorCount
)
612 /* write header info */
613 fprintf(file
, "\"%i %i %i %i\",\n", image
->width
, image
->height
, colorCount
, charsPerPixel
);
615 /* write colormap data */
617 for (i
= 0; i
< charsPerPixel
; i
++)
621 fprintf(file
, "\"%s c None\",\n", transp
);
625 outputcolormap(file
, colormap
, charsPerPixel
);
630 if (image
->format
== RRGBAFormat
)
636 for (y
= 0; y
< image
->height
; y
++) {
640 for (x
= 0; x
< image
->width
; x
++) {
642 if (!a
|| *a
> 127) {
643 tmpc
= lookfor(colormap
, (unsigned)*r
<< 16 | (unsigned)*g
<< 8 | (unsigned)*b
);
645 fprintf(file
, index2str(buf
, tmpc
->index
, charsPerPixel
));
647 fprintf(file
, transp
);
662 if (y
< image
->height
- 1)
663 fprintf(file
, "\",\n");
665 fprintf(file
, "\"};\n");
672 if (ok
&& errno
== ENOSPC
) {
673 RErrorCode
= RERR_WRITE
;
676 freecolormap(colormap
);
678 return ok
? True
: False
;