1
2 /*
3 add ability to load xpm files to gd, requires the xpm
4 library.
5 Caolan.McNamara@ul.ie
6 http://www.csn.ul.ie/~caolan
7 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "gd.h"
12 #include "gdhelpers.h"
13
14 #ifdef HAVE_XPM
15
16 #include <X11/xpm.h>
17
gdImageCreateFromXpm(char * filename)18 gdImagePtr gdImageCreateFromXpm (char *filename)
19 {
20 XpmInfo info;
21 XpmImage image;
22 int i, j, k, number;
23 char buf[5];
24 gdImagePtr im = 0;
25 int *pointer;
26 int red = 0, green = 0, blue = 0;
27 int *colors;
28 int ret;
29
30 ret = XpmReadFileToXpmImage(filename, &image, &info);
31 if (ret != XpmSuccess) {
32 return 0;
33 }
34
35 if (!(im = gdImageCreate(image.width, image.height))) {
36 goto done;
37 }
38
39 number = image.ncolors;
40 colors = (int *) safe_emalloc(number, sizeof(int), 0);
41 for (i = 0; i < number; i++) {
42 switch (strlen (image.colorTable[i].c_color)) {
43 case 4:
44 buf[1] = '\0';
45 buf[0] = image.colorTable[i].c_color[1];
46 red = strtol(buf, NULL, 16);
47
48 buf[0] = image.colorTable[i].c_color[2];
49 green = strtol(buf, NULL, 16);
50
51 buf[0] = image.colorTable[i].c_color[3];
52 blue = strtol(buf, NULL, 16);
53 break;
54
55 case 7:
56 buf[2] = '\0';
57 buf[0] = image.colorTable[i].c_color[1];
58 buf[1] = image.colorTable[i].c_color[2];
59 red = strtol(buf, NULL, 16);
60
61 buf[0] = image.colorTable[i].c_color[3];
62 buf[1] = image.colorTable[i].c_color[4];
63 green = strtol(buf, NULL, 16);
64
65 buf[0] = image.colorTable[i].c_color[5];
66 buf[1] = image.colorTable[i].c_color[6];
67 blue = strtol(buf, NULL, 16);
68 break;
69
70 case 10:
71 buf[3] = '\0';
72 buf[0] = image.colorTable[i].c_color[1];
73 buf[1] = image.colorTable[i].c_color[2];
74 buf[2] = image.colorTable[i].c_color[3];
75 red = strtol(buf, NULL, 16);
76 red /= 64;
77
78 buf[0] = image.colorTable[i].c_color[4];
79 buf[1] = image.colorTable[i].c_color[5];
80 buf[2] = image.colorTable[i].c_color[6];
81 green = strtol(buf, NULL, 16);
82 green /= 64;
83
84 buf[0] = image.colorTable[i].c_color[7];
85 buf[1] = image.colorTable[i].c_color[8];
86 buf[2] = image.colorTable[i].c_color[9];
87 blue = strtol(buf, NULL, 16);
88 blue /= 64;
89 break;
90
91 case 13:
92 buf[4] = '\0';
93 buf[0] = image.colorTable[i].c_color[1];
94 buf[1] = image.colorTable[i].c_color[2];
95 buf[2] = image.colorTable[i].c_color[3];
96 buf[3] = image.colorTable[i].c_color[4];
97 red = strtol(buf, NULL, 16);
98 red /= 256;
99
100 buf[0] = image.colorTable[i].c_color[5];
101 buf[1] = image.colorTable[i].c_color[6];
102 buf[2] = image.colorTable[i].c_color[7];
103 buf[3] = image.colorTable[i].c_color[8];
104 green = strtol(buf, NULL, 16);
105 green /= 256;
106
107 buf[0] = image.colorTable[i].c_color[9];
108 buf[1] = image.colorTable[i].c_color[10];
109 buf[2] = image.colorTable[i].c_color[11];
110 buf[3] = image.colorTable[i].c_color[12];
111 blue = strtol(buf, NULL, 16);
112 blue /= 256;
113 break;
114 }
115
116
117 colors[i] = gdImageColorResolve(im, red, green, blue);
118 }
119
120 pointer = (int *) image.data;
121 for (i = 0; i < image.height; i++) {
122 for (j = 0; j < image.width; j++) {
123 k = *pointer++;
124 gdImageSetPixel(im, j, i, colors[k]);
125 }
126 }
127
128 gdFree(colors);
129 done:
130 XpmFreeXpmImage(&image);
131 XpmFreeXpmInfo(&info);
132 return im;
133 }
134 #endif
135