1 #include <stdlib.h>
3 #include "imagemap-gdk.h"
6 /*#########################################################################
7 ## G R A Y M A P
8 #########################################################################*/
10 GrayMap *gdkPixbufToGrayMap(GdkPixbuf *buf)
11 {
12 if (!buf)
13 return NULL;
15 int width = gdk_pixbuf_get_width(buf);
16 int height = gdk_pixbuf_get_height(buf);
17 guchar *pixdata = gdk_pixbuf_get_pixels(buf);
18 int rowstride = gdk_pixbuf_get_rowstride(buf);
19 int n_channels = gdk_pixbuf_get_n_channels(buf);
21 GrayMap *grayMap = GrayMapCreate(width, height);
22 if (!grayMap)
23 return NULL;
25 //### Fill in the odd cells with RGB values
26 int x,y;
27 int row = 0;
28 for (y=0 ; y<height ; y++)
29 {
30 guchar *p = pixdata + row;
31 for (x=0 ; x<width ; x++)
32 {
33 int alpha = (int)p[3];
34 int white = 3 * (255-alpha);
35 unsigned long sample = (int)p[0] + (int)p[1] +(int)p[2];
36 unsigned long bright = sample * alpha / 256 + white;
37 grayMap->setPixel(grayMap, x, y, bright);
38 p += n_channels;
39 }
40 row += rowstride;
41 }
43 return grayMap;
44 }
46 GdkPixbuf *grayMapToGdkPixbuf(GrayMap *grayMap)
47 {
48 if (!grayMap)
49 return NULL;
51 guchar *pixdata = (guchar *)
52 malloc(sizeof(guchar) * grayMap->width * grayMap->height * 3);
53 if (!pixdata)
54 return NULL;
56 int n_channels = 3;
57 int rowstride = grayMap->width * 3;
59 GdkPixbuf *buf = gdk_pixbuf_new_from_data(pixdata, GDK_COLORSPACE_RGB,
60 0, 8, grayMap->width, grayMap->height,
61 rowstride, NULL, NULL);
63 //### Fill in the odd cells with RGB values
64 int x,y;
65 int row = 0;
66 for (y=0 ; y<grayMap->height ; y++)
67 {
68 guchar *p = pixdata + row;
69 for (x=0 ; x<grayMap->width ; x++)
70 {
71 unsigned long pix = grayMap->getPixel(grayMap, x, y) / 3;
72 p[0] = p[1] = p[2] = (guchar)(pix & 0xff);
73 p += n_channels;
74 }
75 row += rowstride;
76 }
78 return buf;
79 }
83 /*#########################################################################
84 ## P A C K E D P I X E L M A P
85 #########################################################################*/
87 PackedPixelMap *gdkPixbufToPackedPixelMap(GdkPixbuf *buf)
88 {
89 if (!buf)
90 return NULL;
92 int width = gdk_pixbuf_get_width(buf);
93 int height = gdk_pixbuf_get_height(buf);
94 guchar *pixdata = gdk_pixbuf_get_pixels(buf);
95 int rowstride = gdk_pixbuf_get_rowstride(buf);
96 int n_channels = gdk_pixbuf_get_n_channels(buf);
98 PackedPixelMap *ppMap = PackedPixelMapCreate(width, height);
99 if (!ppMap)
100 return NULL;
102 //### Fill in the cells with RGB values
103 int x,y;
104 int row = 0;
105 for (y=0 ; y<height ; y++)
106 {
107 guchar *p = pixdata + row;
108 for (x=0 ; x<width ; x++)
109 {
110 int alpha = (int)p[3];
111 int white = 255 - alpha;
112 int r = (int)p[0]; r = r * alpha / 256 + white;
113 int g = (int)p[1]; g = g * alpha / 256 + white;
114 int b = (int)p[2]; b = b * alpha / 256 + white;
116 ppMap->setPixel(ppMap, x, y, r, g, b);
117 p += n_channels;
118 }
119 row += rowstride;
120 }
122 return ppMap;
123 }
125 GdkPixbuf *packedPixelMapToGdkPixbuf(PackedPixelMap *ppMap)
126 {
127 if (!ppMap)
128 return NULL;
130 guchar *pixdata = (guchar *)
131 malloc(sizeof(guchar) * ppMap->width * ppMap->height * 3);
132 if (!pixdata)
133 return NULL;
135 int n_channels = 3;
136 int rowstride = ppMap->width * 3;
138 GdkPixbuf *buf = gdk_pixbuf_new_from_data(pixdata, GDK_COLORSPACE_RGB,
139 0, 8, ppMap->width, ppMap->height,
140 rowstride, NULL, NULL);
142 //### Fill in the cells with RGB values
143 int x,y;
144 int row = 0;
145 for (y=0 ; y<ppMap->height ; y++)
146 {
147 guchar *p = pixdata + row;
148 for (x=0 ; x<ppMap->width ; x++)
149 {
150 unsigned long rgb = ppMap->getPixel(ppMap, x, y);
151 p[0] = (rgb >> 16) & 0xff;
152 p[1] = (rgb >> 8) & 0xff;
153 p[2] = (rgb ) & 0xff;
154 p += n_channels;
155 }
156 row += rowstride;
157 }
159 return buf;
160 }
164 /*#########################################################################
165 ## R G B M A P
166 #########################################################################*/
168 RgbMap *gdkPixbufToRgbMap(GdkPixbuf *buf)
169 {
170 if (!buf)
171 return NULL;
173 int width = gdk_pixbuf_get_width(buf);
174 int height = gdk_pixbuf_get_height(buf);
175 guchar *pixdata = gdk_pixbuf_get_pixels(buf);
176 int rowstride = gdk_pixbuf_get_rowstride(buf);
177 int n_channels = gdk_pixbuf_get_n_channels(buf);
179 RgbMap *rgbMap = RgbMapCreate(width, height);
180 if (!rgbMap)
181 return NULL;
183 //### Fill in the cells with RGB values
184 int x,y;
185 int row = 0;
186 for (y=0 ; y<height ; y++)
187 {
188 guchar *p = pixdata + row;
189 for (x=0 ; x<width ; x++)
190 {
191 int alpha = (int)p[3];
192 int white = 255 - alpha;
193 int r = (int)p[0]; r = r * alpha / 256 + white;
194 int g = (int)p[1]; g = g * alpha / 256 + white;
195 int b = (int)p[2]; b = b * alpha / 256 + white;
197 rgbMap->setPixel(rgbMap, x, y, r, g, b);
198 p += n_channels;
199 }
200 row += rowstride;
201 }
203 return rgbMap;
204 }
206 GdkPixbuf *rgbMapToGdkPixbuf(RgbMap *rgbMap)
207 {
208 if (!rgbMap)
209 return NULL;
211 guchar *pixdata = (guchar *)
212 malloc(sizeof(guchar) * rgbMap->width * rgbMap->height * 3);
213 if (!pixdata)
214 return NULL;
216 int n_channels = 3;
217 int rowstride = rgbMap->width * 3;
219 GdkPixbuf *buf = gdk_pixbuf_new_from_data(pixdata, GDK_COLORSPACE_RGB,
220 0, 8, rgbMap->width, rgbMap->height,
221 rowstride, NULL, NULL);
223 //### Fill in the cells with RGB values
224 int x,y;
225 int row = 0;
226 for (y=0 ; y<rgbMap->height ; y++)
227 {
228 guchar *p = pixdata + row;
229 for (x=0 ; x<rgbMap->width ; x++)
230 {
231 RGB rgb = rgbMap->getPixel(rgbMap, x, y);
232 p[0] = rgb.r & 0xff;
233 p[1] = rgb.g & 0xff;
234 p[2] = rgb.b & 0xff;
235 p += n_channels;
236 }
237 row += rowstride;
238 }
240 return buf;
241 }
243 /*#########################################################################
244 ## I N D E X E D M A P
245 #########################################################################*/
248 GdkPixbuf *indexedMapToGdkPixbuf(IndexedMap *iMap)
249 {
250 if (!iMap)
251 return NULL;
253 guchar *pixdata = (guchar *)
254 malloc(sizeof(guchar) * iMap->width * iMap->height * 3);
255 if (!pixdata)
256 return NULL;
258 int n_channels = 3;
259 int rowstride = iMap->width * 3;
261 GdkPixbuf *buf = gdk_pixbuf_new_from_data(pixdata, GDK_COLORSPACE_RGB,
262 0, 8, iMap->width, iMap->height,
263 rowstride, NULL, NULL);
265 //### Fill in the cells with RGB values
266 int x,y;
267 int row = 0;
268 for (y=0 ; y<iMap->height ; y++)
269 {
270 guchar *p = pixdata + row;
271 for (x=0 ; x<iMap->width ; x++)
272 {
273 RGB rgb = iMap->getPixelValue(iMap, x, y);
274 p[0] = rgb.r & 0xff;
275 p[1] = rgb.g & 0xff;
276 p[2] = rgb.b & 0xff;
277 p += n_channels;
278 }
279 row += rowstride;
280 }
282 return buf;
283 }
285 /*#########################################################################
286 ## E N D O F F I L E
287 #########################################################################*/