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 FILE *f = fopen(fileName, "wb");
33 if (!f)
34 return FALSE;
36 fprintf(f, "P6 %d %d 255\n", me->width, me->height);
38 for (int y=0 ; y<me->height; y++)
39 {
40 for (int x=0 ; x<me->width ; x++)
41 {
42 unsigned long pix = me->getPixel(me, x, y) / 3;
43 unsigned char pixb = (unsigned char) (pix & 0xff);
44 fputc(pixb, f);
45 fputc(pixb, f);
46 fputc(pixb, f);
47 }
48 }
49 fclose(f);
50 return TRUE;
51 }
54 static void gDestroy(GrayMap *me)
55 {
56 if (me->pixels)
57 free(me->pixels);
58 if (me->rows)
59 free(me->rows);
60 free(me);
61 }
63 GrayMap *GrayMapCreate(int width, int height)
64 {
66 GrayMap *me = (GrayMap *)malloc(sizeof(GrayMap));
67 if (!me)
68 return NULL;
70 /** methods **/
71 me->setPixel = gSetPixel;
72 me->getPixel = gGetPixel;
73 me->writePPM = gWritePPM;
74 me->destroy = gDestroy;
76 /** fields **/
77 me->width = width;
78 me->height = height;
79 me->pixels = (unsigned long *)
80 malloc(sizeof(unsigned long) * width * height);
81 me->rows = (unsigned long **)
82 malloc(sizeof(unsigned long *) * height);
83 if (!me->pixels || !me->rows)
84 {
85 free(me);
86 return NULL;
87 }
89 unsigned long *row = me->pixels;
90 for (int i=0 ; i<height ; i++)
91 {
92 me->rows[i] = row;
93 row += width;
94 }
96 return me;
97 }
103 /*#########################################################################
104 ### P A C K E D P I X E L M A P
105 #########################################################################*/
109 static void ppSetPixel(PackedPixelMap *me, int x, int y, int r, int g, int b)
110 {
111 unsigned long *pix = me->rows[y] + x;
112 *pix = ((unsigned long)r)<<16 & 0xff0000L |
113 ((unsigned long)g)<< 8 & 0x00ff00L |
114 ((unsigned long)b) & 0x0000ffL;
115 }
117 static void ppSetPixelLong(PackedPixelMap *me, int x, int y, unsigned long rgb)
118 {
119 unsigned long *pix = me->rows[y] + x;
120 *pix = rgb;
121 }
123 static unsigned long ppGetPixel(PackedPixelMap *me, int x, int y)
124 {
125 unsigned long *pix = me->rows[y] + x;
126 return *pix;
127 }
131 static int ppWritePPM(PackedPixelMap *me, char *fileName)
132 {
133 if (!fileName)
134 return FALSE;
136 FILE *f = fopen(fileName, "wb");
137 if (!f)
138 return FALSE;
140 fprintf(f, "P6 %d %d 255\n", me->width, me->height);
142 for (int y=0 ; y<me->height; y++)
143 {
144 for (int x=0 ; x<me->width ; x++)
145 {
146 unsigned long rgb = me->getPixel(me, x, y);
147 unsigned char r = (unsigned char) ((rgb>>16) & 0xff);
148 unsigned char g = (unsigned char) ((rgb>> 8) & 0xff);
149 unsigned char b = (unsigned char) ((rgb ) & 0xff);
150 fputc(r, f);
151 fputc(g, f);
152 fputc(b, f);
153 }
154 }
155 fclose(f);
156 return TRUE;
157 }
160 static void ppDestroy(PackedPixelMap *me)
161 {
162 if (me->pixels)
163 free(me->pixels);
164 if (me->rows)
165 free(me->rows);
166 free(me);
167 }
171 PackedPixelMap *PackedPixelMapCreate(int width, int height)
172 {
174 PackedPixelMap *me = (PackedPixelMap *)malloc(sizeof(PackedPixelMap));
175 if (!me)
176 return NULL;
178 /** methods **/
179 me->setPixel = ppSetPixel;
180 me->setPixelLong = ppSetPixelLong;
181 me->getPixel = ppGetPixel;
182 me->writePPM = ppWritePPM;
183 me->destroy = ppDestroy;
186 /** fields **/
187 me->width = width;
188 me->height = height;
189 me->pixels = (unsigned long *)
190 malloc(sizeof(unsigned long) * width * height);
191 me->rows = (unsigned long **)
192 malloc(sizeof(unsigned long *) * height);
193 if (!me->pixels)
194 {
195 free(me);
196 return NULL;
197 }
199 unsigned long *row = me->pixels;
200 for (int i=0 ; i<height ; i++)
201 {
202 me->rows[i] = row;
203 row += width;
204 }
206 return me;
207 }
211 /*#########################################################################
212 ### R G B M A P
213 #########################################################################*/
217 static void rSetPixel(RgbMap *me, int x, int y, int r, int g, int b)
218 {
219 RGB *pix = me->rows[y] + x;
220 pix->r = r;
221 pix->g = g;
222 pix->b = b;
223 }
225 static void rSetPixelRGB(RgbMap *me, int x, int y, RGB rgb)
226 {
227 RGB *pix = me->rows[y] + x;
228 *pix = rgb;
229 }
231 static RGB rGetPixel(RgbMap *me, int x, int y)
232 {
233 RGB *pix = me->rows[y] + x;
234 return *pix;
235 }
239 static int rWritePPM(RgbMap *me, char *fileName)
240 {
241 if (!fileName)
242 return FALSE;
244 FILE *f = fopen(fileName, "wb");
245 if (!f)
246 return FALSE;
248 fprintf(f, "P6 %d %d 255\n", me->width, me->height);
250 for (int y=0 ; y<me->height; y++)
251 {
252 for (int x=0 ; x<me->width ; x++)
253 {
254 RGB rgb = me->getPixel(me, x, y);
255 fputc(rgb.r, f);
256 fputc(rgb.g, f);
257 fputc(rgb.b, f);
258 }
259 }
260 fclose(f);
261 return TRUE;
262 }
265 static void rDestroy(RgbMap *me)
266 {
267 if (me->pixels)
268 free(me->pixels);
269 if (me->rows)
270 free(me->rows);
271 free(me);
272 }
276 RgbMap *RgbMapCreate(int width, int height)
277 {
279 RgbMap *me = (RgbMap *)malloc(sizeof(RgbMap));
280 if (!me)
281 return NULL;
283 /** methods **/
284 me->setPixel = rSetPixel;
285 me->setPixelRGB = rSetPixelRGB;
286 me->getPixel = rGetPixel;
287 me->writePPM = rWritePPM;
288 me->destroy = rDestroy;
291 /** fields **/
292 me->width = width;
293 me->height = height;
294 me->pixels = (RGB *)
295 malloc(sizeof(RGB) * width * height);
296 me->rows = (RGB **)
297 malloc(sizeof(RGB *) * height);
298 if (!me->pixels)
299 {
300 free(me);
301 return NULL;
302 }
304 RGB *row = me->pixels;
305 for (int i=0 ; i<height ; i++)
306 {
307 me->rows[i] = row;
308 row += width;
309 }
311 return me;
312 }
317 /*#########################################################################
318 ### I N D E X E D M A P
319 #########################################################################*/
323 static void iSetPixel(IndexedMap *me, int x, int y, unsigned int index)
324 {
325 unsigned int *pix = me->rows[y] + x;
326 *pix = index;
327 }
330 static unsigned int iGetPixel(IndexedMap *me, int x, int y)
331 {
332 unsigned int *pix = me->rows[y] + x;
333 return *pix;
334 }
336 static RGB iGetPixelValue(IndexedMap *me, int x, int y)
337 {
338 unsigned int *pix = me->rows[y] + x;
339 RGB rgb = me->clut[((*pix)&0xff)];
340 return rgb;
341 }
345 static int iWritePPM(IndexedMap *me, char *fileName)
346 {
347 if (!fileName)
348 return FALSE;
350 FILE *f = fopen(fileName, "wb");
351 if (!f)
352 return FALSE;
354 fprintf(f, "P6 %d %d 255\n", me->width, me->height);
356 for (int y=0 ; y<me->height; y++)
357 {
358 for (int x=0 ; x<me->width ; x++)
359 {
360 RGB rgb = me->getPixelValue(me, x, y);
361 fputc(rgb.r, f);
362 fputc(rgb.g, f);
363 fputc(rgb.b, f);
364 }
365 }
366 fclose(f);
367 return TRUE;
368 }
371 static void iDestroy(IndexedMap *me)
372 {
373 if (me->pixels)
374 free(me->pixels);
375 if (me->rows)
376 free(me->rows);
377 free(me);
378 }
382 IndexedMap *IndexedMapCreate(int width, int height)
383 {
385 IndexedMap *me = (IndexedMap *)malloc(sizeof(IndexedMap));
386 if (!me)
387 return NULL;
389 /** methods **/
390 me->setPixel = iSetPixel;
391 me->getPixel = iGetPixel;
392 me->getPixelValue = iGetPixelValue;
393 me->writePPM = iWritePPM;
394 me->destroy = iDestroy;
397 /** fields **/
398 me->width = width;
399 me->height = height;
400 me->pixels = (unsigned int *)
401 malloc(sizeof(unsigned int) * width * height);
402 me->rows = (unsigned int **)
403 malloc(sizeof(unsigned int *) * height);
404 if (!me->pixels)
405 {
406 free(me);
407 return NULL;
408 }
410 unsigned int *row = me->pixels;
411 for (int i=0 ; i<height ; i++)
412 {
413 me->rows[i] = row;
414 row += width;
415 }
417 me->nrColors = 0;
419 RGB rgb;
420 rgb.r = rgb.g = rgb.b = 0;
421 for (int i=0; i<256 ; i++)
422 {
423 me->clut[i] = rgb;
424 }
426 return me;
427 }
434 /*#########################################################################
435 ### E N D O F F I L E
436 #########################################################################*/