]> git.imager.perl.org - imager.git/blob - palimg.c
ecb14660c1a4c5e07054225d19fd2f1729b8a5d5
[imager.git] / palimg.c
1 /*
2 =head1 NAME
3
4   palimg.c - implements paletted images for Imager.
5
6 =head1 SYNOPSIS
7
8 =head1 DESCRIPTION
9
10 Implements paletted images using the new image interface.
11
12 =over
13
14 =item IIM_base_8bit_pal
15
16 Basic 8-bit/sample paletted image
17
18 =cut
19 */
20
21 #include "image.h"
22 #include "imagei.h"
23
24 #define PALEXT(im) ((i_img_pal_ext*)((im)->ext_data))
25 static int i_ppix_p(i_img *im, int x, int y, i_color *val);
26 static int i_gpix_p(i_img *im, int x, int y, i_color *val);
27 static int i_glin_p(i_img *im, int l, int r, int y, i_color *vals);
28 static int i_plin_p(i_img *im, int l, int r, int y, i_color *vals);
29 static int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, int const *chans, int chan_count);
30 static int i_gpal_p(i_img *pm, int l, int r, int y, i_palidx *vals);
31 static int i_ppal_p(i_img *pm, int l, int r, int y, i_palidx *vals);
32 static int i_addcolors_p(i_img *im, i_color *color, int count);
33 static int i_getcolors_p(i_img *im, int i, i_color *color, int count);
34 static int i_colorcount_p(i_img *im);
35 static int i_maxcolors_p(i_img *im);
36 static int i_findcolor_p(i_img *im, i_color *color, i_palidx *entry);
37 static int i_setcolors_p(i_img *im, int index, i_color *color, int count);
38
39 static void i_destroy_p(i_img *im);
40
41 static i_img IIM_base_8bit_pal =
42 {
43   0, /* channels set */
44   0, 0, 0, /* xsize, ysize, bytes */
45   ~0U, /* ch_mask */
46   i_8_bits, /* bits */
47   i_palette_type, /* type */
48   0, /* virtual */
49   NULL, /* idata */
50   { 0, 0, NULL }, /* tags */
51   NULL, /* ext_data */
52
53   i_ppix_p, /* i_f_ppix */
54   i_ppixf_fp, /* i_f_ppixf */
55   i_plin_p, /* i_f_plin */
56   i_plinf_fp, /* i_f_plinf */
57   i_gpix_p, /* i_f_gpix */
58   i_gpixf_fp, /* i_f_gpixf */
59   i_glin_p, /* i_f_glin */
60   i_glinf_fp, /* i_f_glinf */
61   i_gsamp_p, /* i_f_gsamp */
62   i_gsampf_fp, /* i_f_gsampf */
63
64   i_gpal_p, /* i_f_gpal */
65   i_ppal_p, /* i_f_ppal */
66   i_addcolors_p, /* i_f_addcolors */
67   i_getcolors_p, /* i_f_getcolors */
68   i_colorcount_p, /* i_f_colorcount */
69   i_maxcolors_p, /* i_f_maxcolors */
70   i_findcolor_p, /* i_f_findcolor */
71   i_setcolors_p, /* i_f_setcolors */
72
73   i_destroy_p, /* i_f_destroy */
74 };
75
76 /*
77 =item i_img_pal_new_low(i_img *im, int x, int y, int channels, int maxpal)
78
79 Creates a new paletted image.
80
81 Currently 0 < maxpal <= 256
82
83 =cut
84 */
85 i_img *i_img_pal_new_low(i_img *im, int x, int y, int channels, int maxpal) {
86   i_img_pal_ext *palext;
87   int bytes;
88
89   i_clear_error();
90   if (maxpal < 0 || maxpal > 256) {
91     i_push_error(0, "Maximum of 256 palette entries");
92     return NULL;
93   }
94   if (x < 1 || y < 1) {
95     i_push_error(0, "Image sizes must be positive");
96     return NULL;
97   }
98   if (channels < 1 || channels > MAXCHANNELS) {
99     i_push_errorf(0, "Channels must be positive and <= %d", MAXCHANNELS);
100     return NULL;
101   }
102   bytes = sizeof(i_palidx) * x * y;
103   if (bytes / y / sizeof(i_palidx) != x) {
104     i_push_errorf(0, "integer overflow calculating image allocation");
105     return NULL;
106   }
107
108   memcpy(im, &IIM_base_8bit_pal, sizeof(i_img));
109   palext = mymalloc(sizeof(i_img_pal_ext));
110   palext->pal = mymalloc(sizeof(i_color) * maxpal);
111   palext->count = 0;
112   palext->alloc = maxpal;
113   palext->last_found = -1;
114   im->ext_data = palext;
115   i_tags_new(&im->tags);
116   im->bytes = bytes;
117   im->idata = mymalloc(im->bytes);
118   im->channels = channels;
119   memset(im->idata, 0, im->bytes);
120   im->xsize = x;
121   im->ysize = y;
122   
123   return im;
124 }
125
126 i_img *i_img_pal_new(int x, int y, int channels, int maxpal) {
127   i_img *im;
128   mm_log((1, "i_img_pal_new(x %d, y %d, channels %d, maxpal %d)\n", x, y, channels, maxpal));
129   im = mymalloc(sizeof(i_img));
130   return i_img_pal_new_low(im, x, y, channels, maxpal);
131 }
132
133 /*
134 =item i_img_rgb_convert(i_img *targ, i_img *src)
135
136 Converts paletted data in src to RGB data in targ
137
138 Internal function.
139
140 src must be a paletted image and targ must be an RGB image with the
141 same width, height and channels.
142
143 =cut
144 */
145 static void i_img_rgb_convert(i_img *targ, i_img *src) {
146   i_color *row = mymalloc(sizeof(i_color) * targ->xsize);
147   int y;
148   for (y = 0; y < targ->ysize; ++y) {
149     i_glin(src, 0, src->xsize, y, row);
150     i_plin(targ, 0, src->xsize, y, row);
151   }
152   myfree(row);
153 }
154
155 /*
156 =item i_img_to_rgb_inplace(im)
157
158 Converts im from a paletted image to an RGB image.
159
160 The conversion is done in place.
161
162 The conversion cannot be done for virtual images.
163
164 =cut
165 */
166 int i_img_to_rgb_inplace(i_img *im) {
167   i_img temp;
168   i_color *pal;
169   int palsize;
170
171   if (im->virtual)
172     return 0;
173
174   if (im->type == i_direct_type)
175     return 1; /* trivial success */
176
177   i_img_empty_ch(&temp, im->xsize, im->ysize, im->channels);
178   i_img_rgb_convert(&temp, im);
179
180   /* nasty hack */
181   (im->i_f_destroy)(im);
182   myfree(im->idata);
183   *im = temp;
184
185   return 1;
186 }
187
188 /*
189 =item i_img_to_pal(i_img *im, i_quantize *quant)
190
191 Converts an RGB image to a paletted image
192
193 =cut
194 */
195 i_img *i_img_to_pal(i_img *src, i_quantize *quant) {
196   i_palidx *result;
197   i_img *im;
198
199   i_clear_error();
200   
201   quant_makemap(quant, &src, 1);
202   result = quant_translate(quant, src);
203
204   if (result) {
205
206     im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size);
207
208     /* copy things over */
209     memcpy(im->idata, result, im->bytes);
210     PALEXT(im)->count = quant->mc_count;
211     memcpy(PALEXT(im)->pal, quant->mc_colors, sizeof(i_color) * quant->mc_count);
212     
213     myfree(result);
214
215     return im;
216   }
217   else {
218     return NULL;
219   }
220 }
221
222 /*
223 =item i_img_to_rgb(i_img *src)
224
225 =cut
226 */
227 i_img *i_img_to_rgb(i_img *src) {
228   i_img *im = i_img_empty_ch(NULL, src->xsize, src->ysize, src->channels);
229   i_img_rgb_convert(im, src);
230
231   return im;
232 }
233
234 /*
235 =item i_destroy_p(i_img *im)
236
237 Destroys data related to a paletted image.
238
239 =cut
240 */
241 static void i_destroy_p(i_img *im) {
242   if (im) {
243     i_img_pal_ext *palext = im->ext_data;
244     if (palext) {
245       if (palext->pal)
246         myfree(palext->pal);
247       myfree(palext);
248     }
249   }
250 }
251
252 /*
253 =item i_ppix_p(i_img *im, int x, int y, i_color *val)
254
255 Write to a pixel in the image.
256
257 Warning: converts the image to a RGB image if the color isn't already
258 present in the image.
259
260 =cut
261 */
262 static int i_ppix_p(i_img *im, int x, int y, i_color *val) {
263   i_palidx which;
264   if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
265     return -1;
266   if (i_findcolor(im, val, &which)) {
267     ((i_palidx *)im->idata)[x + y * im->xsize] = which;
268     return 0;
269   }
270   else {
271     if (i_img_to_rgb_inplace(im)) {
272       return i_ppix(im, x, y, val);
273     }
274     else
275       return -1;
276   }
277 }
278
279 /*
280 =item i_gpix(i_img *im, int x, int y, i_color *val)
281
282 Retrieve a pixel, converting from a palette index to a color.
283
284 =cut
285 */
286 static int i_gpix_p(i_img *im, int x, int y, i_color *val) {
287   i_palidx which;
288   if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
289     return -1;
290   }
291   which = ((i_palidx *)im->idata)[x + y * im->xsize];
292   if (which > PALEXT(im)->count)
293     return -1;
294   *val = PALEXT(im)->pal[which];
295
296   return 0;
297 }
298
299 /*
300 =item i_glinp(i_img *im, int l, int r, int y, i_color *vals)
301
302 Retrieve a row of pixels.
303
304 =cut
305 */
306 static int i_glin_p(i_img *im, int l, int r, int y, i_color *vals) {
307   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
308     int palsize = PALEXT(im)->count;
309     i_color *pal = PALEXT(im)->pal;
310     i_palidx *data;
311     int count, i;
312     if (r > im->xsize)
313       r = im->xsize;
314     data = ((i_palidx *)im->idata) + l + y * im->xsize;
315     count = r - l;
316     for (i = 0; i < count; ++i) {
317       i_palidx which = *data++;
318       if (which < palsize)
319         vals[i] = pal[which];
320     }
321     return count;
322   }
323   else {
324     return 0;
325   }
326 }
327
328 /*
329 =item i_plin_p(i_img *im, int l, int r, int y, i_color *vals)
330
331 Write a line of color data to the image.
332
333 If any color value is not in the image when the image is converted to 
334 RGB.
335
336 =cut
337 */
338 static int i_plin_p(i_img *im, int l, int r, int y, i_color *vals) {
339   int ch, count, i;
340   i_palidx *data;
341   i_palidx which;
342   if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
343     if (r > im->xsize)
344       r = im->xsize;
345     data = ((i_palidx *)im->idata) + l + y * im->xsize;
346     count = r - l;
347     for (i = 0; i < count; ++i) {
348       if (i_findcolor(im, vals+i, &which)) {
349         ((i_palidx *)data)[i] = which;
350       }
351       else {
352         if (i_img_to_rgb_inplace(im)) {
353           return i+i_plin(im, l+i, r, y, vals+i);
354         }
355       }
356     }
357     return count;
358   }
359   else {
360     return 0;
361   }
362 }
363
364 /*
365 =item i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, int chans, int chan_count)
366
367 =cut
368 */
369 static int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, 
370               int const *chans, int chan_count) {
371   int ch;
372   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
373     int palsize = PALEXT(im)->count;
374     i_color *pal = PALEXT(im)->pal;
375     i_palidx *data;
376     int count, i, w;
377     if (r > im->xsize)
378       r = im->xsize;
379     data = ((i_palidx *)im->idata) + l + y * im->xsize;
380     count = 0;
381     w = r - l;
382     if (chans) {
383       for (ch = 0; ch < chan_count; ++ch) {
384         if (chans[ch] < 0 || chans[ch] >= im->channels) {
385           i_push_errorf(0, "No channel %d in this image", chans[ch]);
386         }
387       }
388
389       for (i = 0; i < w; ++i) {
390         i_palidx which = *data++;
391         if (which < palsize) {
392           for (ch = 0; ch < chan_count; ++ch) {
393             *samps++ = pal[which].channel[chans[ch]];
394             ++count;
395           }
396         }
397       }
398     }
399     else {
400       for (i = 0; i < w; ++i) {
401         i_palidx which = *data++;
402         if (which < palsize) {
403           for (ch = 0; ch < chan_count; ++ch) {
404             *samps++ = pal[which].channel[ch];
405             ++count;
406           }
407         }
408       }
409     }
410     return count;
411   }
412   else {
413     return 0;
414   }
415 }
416
417 /*
418 =item i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals)
419
420 =cut
421 */
422
423 static int i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals) {
424   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
425     i_palidx *data;
426     int i, w;
427     if (r > im->xsize)
428       r = im->xsize;
429     data = ((i_palidx *)im->idata) + l + y * im->xsize;
430     w = r - l;
431     for (i = 0; i < w; ++i) {
432       *vals++ = *data++;
433     }
434     return i;
435   }
436   else {
437     return 0;
438   }
439 }
440
441 /*
442 =item i_ppal_p(i_img *im, int l, int r, int y, i_palidx *vals)
443
444 =cut
445 */
446
447 static int i_ppal_p(i_img *im, int l, int r, int y, i_palidx *vals) {
448   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
449     i_palidx *data;
450     int i, w;
451     if (r > im->xsize)
452       r = im->xsize;
453     data = ((i_palidx *)im->idata) + l + y * im->xsize;
454     w = r - l;
455     for (i = 0; i < w; ++i) {
456       *data++ = *vals++;
457     }
458     return i;
459   }
460   else {
461     return 0;
462   }
463 }
464
465 /*
466 =item i_addcolors_p(i_img *im, i_color *color, int count)
467
468 =cut
469 */
470 static int i_addcolors_p(i_img *im, i_color *color, int count) {
471   if (PALEXT(im)->count + count <= PALEXT(im)->alloc) {
472     int result = PALEXT(im)->count;
473     int index = result;
474
475     PALEXT(im)->count += count;
476     while (count) {
477       PALEXT(im)->pal[index++] = *color++;
478       --count;
479     }
480
481     return result;
482   }
483   else
484     return -1;
485 }
486
487 /*
488 =item i_getcolors_p(i_img *im, int i, i_color *color, int count)
489
490 =cut
491 */
492 static int i_getcolors_p(i_img *im, int i, i_color *color, int count) {
493   if (i >= 0 && i+count <= PALEXT(im)->count) {
494     while (count) {
495       *color++ = PALEXT(im)->pal[i++];
496       --count;
497     }
498     return 1;
499   }
500   else
501     return 0;
502 }
503
504 static int color_eq(i_img *im, i_color *c1, i_color *c2) {
505   int ch;
506   for (ch = 0; ch < im->channels; ++ch) {
507     if (c1->channel[ch] != c2->channel[ch])
508       return 0;
509   }
510   return 1;
511 }
512
513 /*
514 =item i_colorcount_p(i_img *im)
515
516 =cut
517 */
518 static int i_colorcount_p(i_img *im) {
519   return PALEXT(im)->count;
520 }
521
522 /*
523 =item i_maxcolors_p(i_img *im)
524
525 =cut
526 */
527 static int i_maxcolors_p(i_img *im) {
528   return PALEXT(im)->alloc;
529 }
530
531 /*
532 =item i_setcolors_p(i_img *im, int index, i_color *colors, int count)
533
534 =cut
535 */
536 static int i_setcolors_p(i_img *im, int index, i_color *colors, int count) {
537   if (index >= 0 && count >= 1 && index + count < PALEXT(im)->count) {
538     while (count) {
539       PALEXT(im)->pal[index++] = *colors++;
540       --count;
541     }
542     return 1;
543   }
544
545   return 0;
546 }
547
548 /*
549 =item i_findcolor_p(i_img *im)
550
551 =cut
552 */
553 static int i_findcolor_p(i_img *im, i_color *color, i_palidx *entry) {
554   if (PALEXT(im)->count) {
555     int i;
556     /* often the same color comes up several times in a row */
557     if (PALEXT(im)->last_found >= 0) {
558       if (color_eq(im, color, PALEXT(im)->pal + PALEXT(im)->last_found)) {
559         *entry = PALEXT(im)->last_found;
560         return 1;
561       }
562     }
563     for (i = 0; i < PALEXT(im)->count; ++i) {
564       if (color_eq(im, color, PALEXT(im)->pal + i)) {
565         PALEXT(im)->last_found = *entry = i;
566         return 1;
567       }
568     }
569   }
570   return 0;
571 }
572
573 /*
574 =back
575
576 =head1 AUTHOR
577
578 Tony Cook <tony@develop-help.com>
579
580 =head1 SEE ALSO
581
582 Imager(3)
583
584 =cut
585 */