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 :
18 : gdImagePtr gdImageCreateFromXpm (char *filename)
19 4 : {
20 : XpmInfo info;
21 : XpmImage image;
22 : int i, j, k, number;
23 : char buf[5];
24 4 : gdImagePtr im = 0;
25 : char *apixel;
26 : int *pointer;
27 4 : int red = 0, green = 0, blue = 0;
28 : int *colors;
29 : int ret;
30 :
31 4 : ret = XpmReadFileToXpmImage(filename, &image, &info);
32 4 : if (ret != XpmSuccess) {
33 0 : return 0;
34 : }
35 :
36 4 : if (!(im = gdImageCreate(image.width, image.height))) {
37 0 : return 0;
38 : }
39 :
40 4 : number = image.ncolors;
41 4 : colors = (int *) safe_emalloc(number, sizeof(int), 0);
42 6144 : for (i = 0; i < number; i++) {
43 6140 : switch (strlen (image.colorTable[i].c_color)) {
44 : case 4:
45 4 : buf[1] = '\0';
46 4 : buf[0] = image.colorTable[i].c_color[1];
47 4 : red = strtol(buf, NULL, 16);
48 :
49 4 : buf[0] = image.colorTable[i].c_color[2];
50 4 : green = strtol(buf, NULL, 16);
51 :
52 4 : buf[0] = image.colorTable[i].c_color[3];
53 4 : blue = strtol(buf, NULL, 16);
54 4 : break;
55 :
56 : case 7:
57 6136 : buf[2] = '\0';
58 6136 : buf[0] = image.colorTable[i].c_color[1];
59 6136 : buf[1] = image.colorTable[i].c_color[2];
60 6136 : red = strtol(buf, NULL, 16);
61 :
62 6136 : buf[0] = image.colorTable[i].c_color[3];
63 6136 : buf[1] = image.colorTable[i].c_color[4];
64 6136 : green = strtol(buf, NULL, 16);
65 :
66 6136 : buf[0] = image.colorTable[i].c_color[5];
67 6136 : buf[1] = image.colorTable[i].c_color[6];
68 6136 : blue = strtol(buf, NULL, 16);
69 6136 : break;
70 :
71 : case 10:
72 0 : buf[3] = '\0';
73 0 : buf[0] = image.colorTable[i].c_color[1];
74 0 : buf[1] = image.colorTable[i].c_color[2];
75 0 : buf[2] = image.colorTable[i].c_color[3];
76 0 : red = strtol(buf, NULL, 16);
77 0 : red /= 64;
78 :
79 0 : buf[0] = image.colorTable[i].c_color[4];
80 0 : buf[1] = image.colorTable[i].c_color[5];
81 0 : buf[2] = image.colorTable[i].c_color[6];
82 0 : green = strtol(buf, NULL, 16);
83 0 : green /= 64;
84 :
85 0 : buf[0] = image.colorTable[i].c_color[7];
86 0 : buf[1] = image.colorTable[i].c_color[8];
87 0 : buf[2] = image.colorTable[i].c_color[9];
88 0 : blue = strtol(buf, NULL, 16);
89 0 : blue /= 64;
90 0 : break;
91 :
92 : case 13:
93 0 : buf[4] = '\0';
94 0 : buf[0] = image.colorTable[i].c_color[1];
95 0 : buf[1] = image.colorTable[i].c_color[2];
96 0 : buf[2] = image.colorTable[i].c_color[3];
97 0 : buf[3] = image.colorTable[i].c_color[4];
98 0 : red = strtol(buf, NULL, 16);
99 0 : red /= 256;
100 :
101 0 : buf[0] = image.colorTable[i].c_color[5];
102 0 : buf[1] = image.colorTable[i].c_color[6];
103 0 : buf[2] = image.colorTable[i].c_color[7];
104 0 : buf[3] = image.colorTable[i].c_color[8];
105 0 : green = strtol(buf, NULL, 16);
106 0 : green /= 256;
107 :
108 0 : buf[0] = image.colorTable[i].c_color[9];
109 0 : buf[1] = image.colorTable[i].c_color[10];
110 0 : buf[2] = image.colorTable[i].c_color[11];
111 0 : buf[3] = image.colorTable[i].c_color[12];
112 0 : blue = strtol(buf, NULL, 16);
113 0 : blue /= 256;
114 : break;
115 : }
116 :
117 :
118 6140 : colors[i] = gdImageColorResolve(im, red, green, blue);
119 6140 : if (colors[i] == -1) {
120 0 : php_gd_error("ARRRGH");
121 : }
122 : }
123 :
124 4 : apixel = (char *) gdMalloc(image.cpp + 1);
125 4 : apixel[image.cpp] = '\0';
126 :
127 4 : pointer = (int *) image.data;
128 204 : for (i = 0; i < image.height; i++) {
129 10200 : for (j = 0; j < image.width; j++) {
130 10000 : k = *pointer++;
131 10000 : gdImageSetPixel(im, j, i, colors[k]);
132 : }
133 : }
134 :
135 4 : gdFree(apixel);
136 4 : gdFree(colors);
137 4 : return im;
138 : }
139 : #endif
|