1 #include <stdlib.h>
3 #include "imagemap.h"
5 #include "io/sys.h"
7 /*#########################################################################
8 ### G R A Y M A P
9 #########################################################################*/
12 static void gSetPixel(GrayMap *me, int x, int y, unsigned long val)
13 {
14 if (val>765)
15 val = 765;
16 unsigned long *pix = me->rows[y] + x;
17 *pix = val;
18 }
20 static unsigned long gGetPixel(GrayMap *me, int x, int y)
21 {
22 unsigned long *pix = me->rows[y] + x;
23 return *pix;
24 }
27 static int gWritePPM(GrayMap *me, char *fileName)
28 {
29 if (!fileName)
30 return FALSE;
32 Inkscape::IO::dump_fopen_call( fileName, "C" );
33 FILE *f = Inkscape::IO::fopen_utf8name(fileName, "wb");
34 if (!f)
35 return FALSE;
37 fprintf(f, "P6 %d %d 255\n", me->width, me->height);
39 for (int y=0 ; y<me->height; y++)
40 {
41 for (int x=0 ; x<me->width ; x++)
42 {
43 unsigned long pix = me->getPixel(me, x, y) / 3;
44 unsigned char pixb = (unsigned char) (pix & 0xff);
45 fwrite(&pixb, 1, 1, f);
46 fwrite(&pixb, 1, 1, f);
47 fwrite(&pixb, 1, 1, f);
48 }
49 }
50 fclose(f);
51 return TRUE;
52 }
55 static void gDestroy(GrayMap *me)
56 {
57 if (me->pixels)
58 free(me->pixels);
59 if (me->rows)
60 free(me->rows);
61 free(me);
62 }
64 GrayMap *GrayMapCreate(int width, int height)
65 {
67 GrayMap *me = (GrayMap *)malloc(sizeof(GrayMap));
68 if (!me)
69 return NULL;
71 /** methods **/
72 me->setPixel = gSetPixel;
73 me->getPixel = gGetPixel;
74 me->writePPM = gWritePPM;
75 me->destroy = gDestroy;
77 /** fields **/
78 me->width = width;
79 me->height = height;
80 me->pixels = (unsigned long *)
81 malloc(sizeof(unsigned long) * width * height);
82 me->rows = (unsigned long **)
83 malloc(sizeof(unsigned long *) * height);
84 if (!me->pixels || !me->rows)
85 {
86 free(me);
87 return NULL;
88 }
90 unsigned long *row = me->pixels;
91 for (int i=0 ; i<height ; i++)
92 {
93 me->rows[i] = row;
94 row += width;
95 }
97 return me;
98 }
104 /*#########################################################################
105 ### P A C K E D P I X E L M A P
106 #########################################################################*/
110 static void ppSetPixel(PackedPixelMap *me, int x, int y, int r, int g, int b)
111 {
112 unsigned long *pix = me->rows[y] + x;
113 *pix = ((unsigned long)r)<<16 & 0xff0000L |
114 ((unsigned long)g)<< 8 & 0x00ff00L |
115 ((unsigned long)b) & 0x0000ffL;
116 }
118 static void ppSetPixelLong(PackedPixelMap *me, int x, int y, unsigned long rgb)
119 {
120 unsigned long *pix = me->rows[y] + x;
121 *pix = rgb;
122 }
124 static unsigned long ppGetPixel(PackedPixelMap *me, int x, int y)
125 {
126 unsigned long *pix = me->rows[y] + x;
127 return *pix;
128 }
132 static int ppWritePPM(PackedPixelMap *me, char *fileName)
133 {
134 if (!fileName)
135 return FALSE;
137 Inkscape::IO::dump_fopen_call(fileName, "D");
138 FILE *f = Inkscape::IO::fopen_utf8name(fileName, "wb");
139 if (!f)
140 return FALSE;
142 fprintf(f, "P6 %d %d 255\n", me->width, me->height);
144 for (int y=0 ; y<me->height; y++)
145 {
146 for (int x=0 ; x<me->width ; x++)
147 {
148 unsigned long rgb = me->getPixel(me, x, y);
149 unsigned char r = (unsigned char) ((rgb>>16) & 0xff);
150 unsigned char g = (unsigned char) ((rgb>> 8) & 0xff);
151 unsigned char b = (unsigned char) ((rgb ) & 0xff);
152 fputc(r, f);
153 fputc(g, f);
154 fputc(b, f);
155 }
156 }
157 fclose(f);
158 return TRUE;
159 }
162 static void ppDestroy(PackedPixelMap *me)
163 {
164 if (me->pixels)
165 free(me->pixels);
166 if (me->rows)
167 free(me->rows);
168 free(me);
169 }
173 PackedPixelMap *PackedPixelMapCreate(int width, int height)
174 {
176 PackedPixelMap *me = (PackedPixelMap *)malloc(sizeof(PackedPixelMap));
177 if (!me)
178 return NULL;
180 /** methods **/
181 me->setPixel = ppSetPixel;
182 me->setPixelLong = ppSetPixelLong;
183 me->getPixel = ppGetPixel;
184 me->writePPM = ppWritePPM;
185 me->destroy = ppDestroy;
188 /** fields **/
189 me->width = width;
190 me->height = height;
191 me->pixels = (unsigned long *)
192 malloc(sizeof(unsigned long) * width * height);
193 me->rows = (unsigned long **)
194 malloc(sizeof(unsigned long *) * height);
195 if (!me->pixels)
196 {
197 free(me);
198 return NULL;
199 }
201 unsigned long *row = me->pixels;
202 for (int i=0 ; i<height ; i++)
203 {
204 me->rows[i] = row;
205 row += width;
206 }
208 return me;
209 }
213 /*#########################################################################
214 ### R G B M A P
215 #########################################################################*/
219 static void rSetPixel(RgbMap *me, int x, int y, int r, int g, int b)
220 {
221 RGB *pix = me->rows[y] + x;
222 pix->r = r;
223 pix->g = g;
224 pix->b = b;
225 }
227 static void rSetPixelRGB(RgbMap *me, int x, int y, RGB rgb)
228 {
229 RGB *pix = me->rows[y] + x;
230 *pix = rgb;
231 }
233 static RGB rGetPixel(RgbMap *me, int x, int y)
234 {
235 RGB *pix = me->rows[y] + x;
236 return *pix;
237 }
241 static int rWritePPM(RgbMap *me, char *fileName)
242 {
243 if (!fileName)
244 return FALSE;
246 Inkscape::IO::dump_fopen_call(fileName, "D");
247 FILE *f = Inkscape::IO::fopen_utf8name(fileName, "wb");
248 if (!f)
249 return FALSE;
251 fprintf(f, "P6 %d %d 255\n", me->width, me->height);
253 for (int y=0 ; y<me->height; y++)
254 {
255 for (int x=0 ; x<me->width ; x++)
256 {
257 RGB rgb = me->getPixel(me, x, y);
258 fwrite(&(rgb.r), 1, 1, f);
259 fwrite(&(rgb.g), 1, 1, f);
260 fwrite(&(rgb.b), 1, 1, f);
261 }
262 }
263 fclose(f);
264 return TRUE;
265 }
268 static void rDestroy(RgbMap *me)
269 {
270 if (me->pixels)
271 free(me->pixels);
272 if (me->rows)
273 free(me->rows);
274 free(me);
275 }
279 RgbMap *RgbMapCreate(int width, int height)
280 {
282 RgbMap *me = (RgbMap *)malloc(sizeof(RgbMap));
283 if (!me)
284 return NULL;
286 /** methods **/
287 me->setPixel = rSetPixel;
288 me->setPixelRGB = rSetPixelRGB;
289 me->getPixel = rGetPixel;
290 me->writePPM = rWritePPM;
291 me->destroy = rDestroy;
294 /** fields **/
295 me->width = width;
296 me->height = height;
297 me->pixels = (RGB *)
298 malloc(sizeof(RGB) * width * height);
299 me->rows = (RGB **)
300 malloc(sizeof(RGB *) * height);
301 if (!me->pixels)
302 {
303 free(me);
304 return NULL;
305 }
307 RGB *row = me->pixels;
308 for (int i=0 ; i<height ; i++)
309 {
310 me->rows[i] = row;
311 row += width;
312 }
314 return me;
315 }
320 /*#########################################################################
321 ### I N D E X E D M A P
322 #########################################################################*/
326 static void iSetPixel(IndexedMap *me, int x, int y, unsigned int index)
327 {
328 unsigned int *pix = me->rows[y] + x;
329 *pix = index;
330 }
333 static unsigned int iGetPixel(IndexedMap *me, int x, int y)
334 {
335 unsigned int *pix = me->rows[y] + x;
336 return *pix;
337 }
339 static RGB iGetPixelValue(IndexedMap *me, int x, int y)
340 {
341 unsigned int *pix = me->rows[y] + x;
342 RGB rgb = me->clut[((*pix)&0xff)];
343 return rgb;
344 }
348 static int iWritePPM(IndexedMap *me, char *fileName)
349 {
350 if (!fileName)
351 return FALSE;
353 Inkscape::IO::dump_fopen_call(fileName, "D");
354 FILE *f = Inkscape::IO::fopen_utf8name(fileName, "wb");
355 if (!f)
356 return FALSE;
358 fprintf(f, "P6 %d %d 255\n", me->width, me->height);
360 for (int y=0 ; y<me->height; y++)
361 {
362 for (int x=0 ; x<me->width ; x++)
363 {
364 RGB rgb = me->getPixelValue(me, x, y);
365 fwrite(&(rgb.r), 1, 1, f);
366 fwrite(&(rgb.g), 1, 1, f);
367 fwrite(&(rgb.b), 1, 1, f);
368 }
369 }
370 fclose(f);
371 return TRUE;
372 }
375 static void iDestroy(IndexedMap *me)
376 {
377 if (me->pixels)
378 free(me->pixels);
379 if (me->rows)
380 free(me->rows);
381 free(me);
382 }
386 IndexedMap *IndexedMapCreate(int width, int height)
387 {
389 IndexedMap *me = (IndexedMap *)malloc(sizeof(IndexedMap));
390 if (!me)
391 return NULL;
393 /** methods **/
394 me->setPixel = iSetPixel;
395 me->getPixel = iGetPixel;
396 me->getPixelValue = iGetPixelValue;
397 me->writePPM = iWritePPM;
398 me->destroy = iDestroy;
401 /** fields **/
402 me->width = width;
403 me->height = height;
404 me->pixels = (unsigned int *)
405 malloc(sizeof(unsigned int) * width * height);
406 me->rows = (unsigned int **)
407 malloc(sizeof(unsigned int *) * height);
408 if (!me->pixels)
409 {
410 free(me);
411 return NULL;
412 }
414 unsigned int *row = me->pixels;
415 for (int i=0 ; i<height ; i++)
416 {
417 me->rows[i] = row;
418 row += width;
419 }
421 me->nrColors = 0;
423 RGB rgb;
424 rgb.r = rgb.g = rgb.b = 0;
425 for (int i=0; i<256 ; i++)
426 {
427 me->clut[i] = rgb;
428 }
430 return me;
431 }
438 /*#########################################################################
439 ### E N D O F F I L E
440 #########################################################################*/