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 ### R G B M A P
106 #########################################################################*/
110 static void rSetPixel(RgbMap *me, int x, int y, int r, int g, int b)
111 {
112 RGB *pix = me->rows[y] + x;
113 pix->r = r;
114 pix->g = g;
115 pix->b = b;
116 }
118 static void rSetPixelRGB(RgbMap *me, int x, int y, RGB rgb)
119 {
120 RGB *pix = me->rows[y] + x;
121 *pix = rgb;
122 }
124 static RGB rGetPixel(RgbMap *me, int x, int y)
125 {
126 RGB *pix = me->rows[y] + x;
127 return *pix;
128 }
132 static int rWritePPM(RgbMap *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 RGB rgb = me->getPixel(me, x, y);
149 fwrite(&(rgb.r), 1, 1, f);
150 fwrite(&(rgb.g), 1, 1, f);
151 fwrite(&(rgb.b), 1, 1, f);
152 }
153 }
154 fclose(f);
155 return TRUE;
156 }
159 static void rDestroy(RgbMap *me)
160 {
161 if (me->pixels)
162 free(me->pixels);
163 if (me->rows)
164 free(me->rows);
165 free(me);
166 }
170 RgbMap *RgbMapCreate(int width, int height)
171 {
173 RgbMap *me = (RgbMap *)malloc(sizeof(RgbMap));
174 if (!me)
175 return NULL;
177 /** methods **/
178 me->setPixel = rSetPixel;
179 me->setPixelRGB = rSetPixelRGB;
180 me->getPixel = rGetPixel;
181 me->writePPM = rWritePPM;
182 me->destroy = rDestroy;
185 /** fields **/
186 me->width = width;
187 me->height = height;
188 me->pixels = (RGB *)
189 malloc(sizeof(RGB) * width * height);
190 me->rows = (RGB **)
191 malloc(sizeof(RGB *) * height);
192 if (!me->pixels)
193 {
194 free(me);
195 return NULL;
196 }
198 RGB *row = me->pixels;
199 for (int i=0 ; i<height ; i++)
200 {
201 me->rows[i] = row;
202 row += width;
203 }
205 return me;
206 }
211 /*#########################################################################
212 ### I N D E X E D M A P
213 #########################################################################*/
217 static void iSetPixel(IndexedMap *me, int x, int y, unsigned int index)
218 {
219 unsigned int *pix = me->rows[y] + x;
220 *pix = index;
221 }
224 static unsigned int iGetPixel(IndexedMap *me, int x, int y)
225 {
226 unsigned int *pix = me->rows[y] + x;
227 return *pix;
228 }
230 static RGB iGetPixelValue(IndexedMap *me, int x, int y)
231 {
232 unsigned int *pix = me->rows[y] + x;
233 RGB rgb = me->clut[((*pix)&0xff)];
234 return rgb;
235 }
239 static int iWritePPM(IndexedMap *me, char *fileName)
240 {
241 if (!fileName)
242 return FALSE;
244 Inkscape::IO::dump_fopen_call(fileName, "D");
245 FILE *f = Inkscape::IO::fopen_utf8name(fileName, "wb");
246 if (!f)
247 return FALSE;
249 fprintf(f, "P6 %d %d 255\n", me->width, me->height);
251 for (int y=0 ; y<me->height; y++)
252 {
253 for (int x=0 ; x<me->width ; x++)
254 {
255 RGB rgb = me->getPixelValue(me, x, y);
256 fwrite(&(rgb.r), 1, 1, f);
257 fwrite(&(rgb.g), 1, 1, f);
258 fwrite(&(rgb.b), 1, 1, f);
259 }
260 }
261 fclose(f);
262 return TRUE;
263 }
266 static void iDestroy(IndexedMap *me)
267 {
268 if (me->pixels)
269 free(me->pixels);
270 if (me->rows)
271 free(me->rows);
272 free(me);
273 }
277 IndexedMap *IndexedMapCreate(int width, int height)
278 {
280 IndexedMap *me = (IndexedMap *)malloc(sizeof(IndexedMap));
281 if (!me)
282 return NULL;
284 /** methods **/
285 me->setPixel = iSetPixel;
286 me->getPixel = iGetPixel;
287 me->getPixelValue = iGetPixelValue;
288 me->writePPM = iWritePPM;
289 me->destroy = iDestroy;
292 /** fields **/
293 me->width = width;
294 me->height = height;
295 me->pixels = (unsigned int *)
296 malloc(sizeof(unsigned int) * width * height);
297 me->rows = (unsigned int **)
298 malloc(sizeof(unsigned int *) * height);
299 if (!me->pixels)
300 {
301 free(me);
302 return NULL;
303 }
305 unsigned int *row = me->pixels;
306 for (int i=0 ; i<height ; i++)
307 {
308 me->rows[i] = row;
309 row += width;
310 }
312 me->nrColors = 0;
314 RGB rgb;
315 rgb.r = rgb.g = rgb.b = 0;
316 for (int i=0; i<256 ; i++)
317 {
318 me->clut[i] = rgb;
319 }
321 return me;
322 }
329 /*#########################################################################
330 ### E N D O F F I L E
331 #########################################################################*/