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 ## R G B M A P
85 #########################################################################*/
87 RgbMap *gdkPixbufToRgbMap(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 RgbMap *rgbMap = RgbMapCreate(width, height);
99 if (!rgbMap)
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 rgbMap->setPixel(rgbMap, x, y, r, g, b);
117 p += n_channels;
118 }
119 row += rowstride;
120 }
122 return rgbMap;
123 }
125 GdkPixbuf *rgbMapToGdkPixbuf(RgbMap *rgbMap)
126 {
127 if (!rgbMap)
128 return NULL;
130 guchar *pixdata = (guchar *)
131 malloc(sizeof(guchar) * rgbMap->width * rgbMap->height * 3);
132 if (!pixdata)
133 return NULL;
135 int n_channels = 3;
136 int rowstride = rgbMap->width * 3;
138 GdkPixbuf *buf = gdk_pixbuf_new_from_data(pixdata, GDK_COLORSPACE_RGB,
139 0, 8, rgbMap->width, rgbMap->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<rgbMap->height ; y++)
146 {
147 guchar *p = pixdata + row;
148 for (x=0 ; x<rgbMap->width ; x++)
149 {
150 RGB rgb = rgbMap->getPixel(rgbMap, x, y);
151 p[0] = rgb.r & 0xff;
152 p[1] = rgb.g & 0xff;
153 p[2] = rgb.b & 0xff;
154 p += n_channels;
155 }
156 row += rowstride;
157 }
159 return buf;
160 }
162 /*#########################################################################
163 ## I N D E X E D M A P
164 #########################################################################*/
167 GdkPixbuf *indexedMapToGdkPixbuf(IndexedMap *iMap)
168 {
169 if (!iMap)
170 return NULL;
172 guchar *pixdata = (guchar *)
173 malloc(sizeof(guchar) * iMap->width * iMap->height * 3);
174 if (!pixdata)
175 return NULL;
177 int n_channels = 3;
178 int rowstride = iMap->width * 3;
180 GdkPixbuf *buf = gdk_pixbuf_new_from_data(pixdata, GDK_COLORSPACE_RGB,
181 0, 8, iMap->width, iMap->height,
182 rowstride, NULL, NULL);
184 //### Fill in the cells with RGB values
185 int x,y;
186 int row = 0;
187 for (y=0 ; y<iMap->height ; y++)
188 {
189 guchar *p = pixdata + row;
190 for (x=0 ; x<iMap->width ; x++)
191 {
192 RGB rgb = iMap->getPixelValue(iMap, x, y);
193 p[0] = rgb.r & 0xff;
194 p[1] = rgb.g & 0xff;
195 p[2] = rgb.b & 0xff;
196 p += n_channels;
197 }
198 row += rowstride;
199 }
201 return buf;
202 }
204 /*#########################################################################
205 ## E N D O F F I L E
206 #########################################################################*/