]> git.imager.perl.org - imager.git/blob - palimg.c
- added support for registering file readers
[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 "imager.h"
22 #include "imageri.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, const 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, const 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, const i_palidx *vals);
32 static int i_addcolors_p(i_img *im, const 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, const i_color *color, i_palidx *entry);
37 static int i_setcolors_p(i_img *im, int index, const 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(im, x, y, channels, 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, line_bytes;
88
89   i_clear_error();
90   if (maxpal < 1 || 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_error(0, "integer overflow calculating image allocation");
105     return NULL;
106   }
107
108   /* basic assumption: we can always allocate a buffer representing a
109      line from the image, otherwise we're going to have trouble
110      working with the image */
111   line_bytes = sizeof(i_color) * x;
112   if (line_bytes / x != sizeof(i_color)) {
113     i_push_error(0, "integer overflow calculating scanline allocation");
114     return NULL;
115   }
116
117   memcpy(im, &IIM_base_8bit_pal, sizeof(i_img));
118   palext = mymalloc(sizeof(i_img_pal_ext));
119   palext->pal = mymalloc(sizeof(i_color) * maxpal);
120   palext->count = 0;
121   palext->alloc = maxpal;
122   palext->last_found = -1;
123   im->ext_data = palext;
124   i_tags_new(&im->tags);
125   im->bytes = bytes;
126   im->idata = mymalloc(im->bytes);
127   im->channels = channels;
128   memset(im->idata, 0, im->bytes);
129   im->xsize = x;
130   im->ysize = y;
131   
132   return im;
133 }
134
135 /*
136 =item i_img_pal_new(x, y, channels, maxpal)
137
138 =category Image creation
139
140 Creates a new paletted image of the supplied dimensions.
141
142 Returns a new image or NULL on failure.
143
144 =cut
145 */
146
147 i_img *i_img_pal_new(int x, int y, int channels, int maxpal) {
148   i_img *im;
149   mm_log((1, "i_img_pal_new(x %d, y %d, channels %d, maxpal %d)\n", x, y, channels, maxpal));
150   im = mymalloc(sizeof(i_img));
151   if (!i_img_pal_new_low(im, x, y, channels, maxpal)) {
152     myfree(im);
153     im = NULL;
154   }
155
156   return im;
157 }
158
159 /*
160 =item i_img_rgb_convert(i_img *targ, i_img *src)
161
162 Converts paletted data in src to RGB data in targ
163
164 Internal function.
165
166 src must be a paletted image and targ must be an RGB image with the
167 same width, height and channels.
168
169 =cut
170 */
171 static void i_img_rgb_convert(i_img *targ, i_img *src) {
172   i_color *row = mymalloc(sizeof(i_color) * targ->xsize);
173   int y;
174   for (y = 0; y < targ->ysize; ++y) {
175     i_glin(src, 0, src->xsize, y, row);
176     i_plin(targ, 0, src->xsize, y, row);
177   }
178   myfree(row);
179 }
180
181 /*
182 =item i_img_to_rgb_inplace(im)
183
184 Converts im from a paletted image to an RGB image.
185
186 The conversion is done in place.
187
188 The conversion cannot be done for virtual images.
189
190 =cut
191 */
192 int i_img_to_rgb_inplace(i_img *im) {
193   i_img temp;
194
195   if (im->virtual)
196     return 0;
197
198   if (im->type == i_direct_type)
199     return 1; /* trivial success */
200
201   i_img_empty_ch(&temp, im->xsize, im->ysize, im->channels);
202   i_img_rgb_convert(&temp, im);
203
204   /* nasty hack */
205   (im->i_f_destroy)(im);
206   myfree(im->idata);
207   *im = temp;
208
209   return 1;
210 }
211
212 /*
213 =item i_img_to_pal(i_img *im, i_quantize *quant)
214
215 Converts an RGB image to a paletted image
216
217 =cut
218 */
219 i_img *i_img_to_pal(i_img *src, i_quantize *quant) {
220   i_palidx *result;
221   i_img *im;
222
223   i_clear_error();
224   
225   i_quant_makemap(quant, &src, 1);
226   result = i_quant_translate(quant, src);
227
228   if (result) {
229
230     im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size);
231
232     /* copy things over */
233     memcpy(im->idata, result, im->bytes);
234     PALEXT(im)->count = quant->mc_count;
235     memcpy(PALEXT(im)->pal, quant->mc_colors, sizeof(i_color) * quant->mc_count);
236     
237     myfree(result);
238
239     return im;
240   }
241   else {
242     return NULL;
243   }
244 }
245
246 /*
247 =item i_img_to_rgb(i_img *src)
248
249 =cut
250 */
251 i_img *i_img_to_rgb(i_img *src) {
252   i_img *im = i_img_empty_ch(NULL, src->xsize, src->ysize, src->channels);
253   i_img_rgb_convert(im, src);
254
255   return im;
256 }
257
258 /*
259 =item i_destroy_p(i_img *im)
260
261 Destroys data related to a paletted image.
262
263 =cut
264 */
265 static void i_destroy_p(i_img *im) {
266   if (im) {
267     i_img_pal_ext *palext = im->ext_data;
268     if (palext) {
269       if (palext->pal)
270         myfree(palext->pal);
271       myfree(palext);
272     }
273   }
274 }
275
276 /*
277 =item i_ppix_p(i_img *im, int x, int y, const i_color *val)
278
279 Write to a pixel in the image.
280
281 Warning: converts the image to a RGB image if the color isn't already
282 present in the image.
283
284 =cut
285 */
286 static int 
287 i_ppix_p(i_img *im, int x, int y, const i_color *val) {
288   i_palidx which;
289   if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
290     return -1;
291   if (i_findcolor(im, val, &which)) {
292     ((i_palidx *)im->idata)[x + y * im->xsize] = which;
293     return 0;
294   }
295   else {
296     if (i_img_to_rgb_inplace(im)) {
297       return i_ppix(im, x, y, val);
298     }
299     else
300       return -1;
301   }
302 }
303
304 /*
305 =item i_gpix_p(i_img *im, int x, int y, i_color *val)
306
307 Retrieve a pixel, converting from a palette index to a color.
308
309 =cut
310 */
311 static int i_gpix_p(i_img *im, int x, int y, i_color *val) {
312   i_palidx which;
313   if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
314     return -1;
315   }
316   which = ((i_palidx *)im->idata)[x + y * im->xsize];
317   if (which > PALEXT(im)->count)
318     return -1;
319   *val = PALEXT(im)->pal[which];
320
321   return 0;
322 }
323
324 /*
325 =item i_glinp(i_img *im, int l, int r, int y, i_color *vals)
326
327 Retrieve a row of pixels.
328
329 =cut
330 */
331 static int i_glin_p(i_img *im, int l, int r, int y, i_color *vals) {
332   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
333     int palsize = PALEXT(im)->count;
334     i_color *pal = PALEXT(im)->pal;
335     i_palidx *data;
336     int count, i;
337     if (r > im->xsize)
338       r = im->xsize;
339     data = ((i_palidx *)im->idata) + l + y * im->xsize;
340     count = r - l;
341     for (i = 0; i < count; ++i) {
342       i_palidx which = *data++;
343       if (which < palsize)
344         vals[i] = pal[which];
345     }
346     return count;
347   }
348   else {
349     return 0;
350   }
351 }
352
353 /*
354 =item i_plin_p(i_img *im, int l, int r, int y, const i_color *vals)
355
356 Write a line of color data to the image.
357
358 If any color value is not in the image when the image is converted to 
359 RGB.
360
361 =cut
362 */
363 static int 
364 i_plin_p(i_img *im, int l, int r, int y, const i_color *vals) {
365   int count, i;
366   i_palidx *data;
367   i_palidx which;
368   if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
369     if (r > im->xsize)
370       r = im->xsize;
371     data = ((i_palidx *)im->idata) + l + y * im->xsize;
372     count = r - l;
373     for (i = 0; i < count; ++i) {
374       if (i_findcolor(im, vals+i, &which)) {
375         ((i_palidx *)data)[i] = which;
376       }
377       else {
378         if (i_img_to_rgb_inplace(im)) {
379           return i+i_plin(im, l+i, r, y, vals+i);
380         }
381       }
382     }
383     return count;
384   }
385   else {
386     return 0;
387   }
388 }
389
390 /*
391 =item i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, int chans, int chan_count)
392
393 =cut
394 */
395 static int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, 
396               int const *chans, int chan_count) {
397   int ch;
398   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
399     int palsize = PALEXT(im)->count;
400     i_color *pal = PALEXT(im)->pal;
401     i_palidx *data;
402     int count, i, w;
403     if (r > im->xsize)
404       r = im->xsize;
405     data = ((i_palidx *)im->idata) + l + y * im->xsize;
406     count = 0;
407     w = r - l;
408     if (chans) {
409       for (ch = 0; ch < chan_count; ++ch) {
410         if (chans[ch] < 0 || chans[ch] >= im->channels) {
411           i_push_errorf(0, "No channel %d in this image", chans[ch]);
412         }
413       }
414
415       for (i = 0; i < w; ++i) {
416         i_palidx which = *data++;
417         if (which < palsize) {
418           for (ch = 0; ch < chan_count; ++ch) {
419             *samps++ = pal[which].channel[chans[ch]];
420             ++count;
421           }
422         }
423       }
424     }
425     else {
426       for (i = 0; i < w; ++i) {
427         i_palidx which = *data++;
428         if (which < palsize) {
429           for (ch = 0; ch < chan_count; ++ch) {
430             *samps++ = pal[which].channel[ch];
431             ++count;
432           }
433         }
434       }
435     }
436     return count;
437   }
438   else {
439     return 0;
440   }
441 }
442
443 /*
444 =item i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals)
445
446 =cut
447 */
448
449 static int i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals) {
450   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
451     i_palidx *data;
452     int i, w;
453     if (r > im->xsize)
454       r = im->xsize;
455     data = ((i_palidx *)im->idata) + l + y * im->xsize;
456     w = r - l;
457     for (i = 0; i < w; ++i) {
458       *vals++ = *data++;
459     }
460     return i;
461   }
462   else {
463     return 0;
464   }
465 }
466
467 /*
468 =item i_ppal_p(i_img *im, int l, int r, int y, const i_palidx *vals)
469
470 =cut
471 */
472
473 static int i_ppal_p(i_img *im, int l, int r, int y, const i_palidx *vals) {
474   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
475     i_palidx *data;
476     int i, w;
477     if (r > im->xsize)
478       r = im->xsize;
479     data = ((i_palidx *)im->idata) + l + y * im->xsize;
480     w = r - l;
481     for (i = 0; i < w; ++i) {
482       *data++ = *vals++;
483     }
484     return i;
485   }
486   else {
487     return 0;
488   }
489 }
490
491 /*
492 =item i_addcolors_p(i_img *im, const i_color *color, int count)
493
494 =cut
495 */
496 static int i_addcolors_p(i_img *im, const i_color *color, int count) {
497   if (PALEXT(im)->count + count <= PALEXT(im)->alloc) {
498     int result = PALEXT(im)->count;
499     int index = result;
500
501     PALEXT(im)->count += count;
502     while (count) {
503       PALEXT(im)->pal[index++] = *color++;
504       --count;
505     }
506
507     return result;
508   }
509   else
510     return -1;
511 }
512
513 /*
514 =item i_getcolors_p(i_img *im, int i, i_color *color, int count)
515
516 =cut
517 */
518 static int i_getcolors_p(i_img *im, int i, i_color *color, int count) {
519   if (i >= 0 && i+count <= PALEXT(im)->count) {
520     while (count) {
521       *color++ = PALEXT(im)->pal[i++];
522       --count;
523     }
524     return 1;
525   }
526   else
527     return 0;
528 }
529
530 static int color_eq(i_img *im, const i_color *c1, const i_color *c2) {
531   int ch;
532   for (ch = 0; ch < im->channels; ++ch) {
533     if (c1->channel[ch] != c2->channel[ch])
534       return 0;
535   }
536   return 1;
537 }
538
539 /*
540 =item i_colorcount_p(i_img *im)
541
542 =cut
543 */
544 static int i_colorcount_p(i_img *im) {
545   return PALEXT(im)->count;
546 }
547
548 /*
549 =item i_maxcolors_p(i_img *im)
550
551 =cut
552 */
553 static int i_maxcolors_p(i_img *im) {
554   return PALEXT(im)->alloc;
555 }
556
557 /*
558 =item i_setcolors_p(i_img *im, int index, const i_color *colors, int count)
559
560 =cut
561 */
562 static int i_setcolors_p(i_img *im, int index, const i_color *colors, int count) {
563   if (index >= 0 && count >= 1 && index + count <= PALEXT(im)->count) {
564     while (count) {
565       PALEXT(im)->pal[index++] = *colors++;
566       --count;
567     }
568     return 1;
569   }
570
571   return 0;
572 }
573
574 /*
575 =item i_findcolor_p(i_img *im)
576
577 =cut
578 */
579 static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry) {
580   if (PALEXT(im)->count) {
581     int i;
582     /* often the same color comes up several times in a row */
583     if (PALEXT(im)->last_found >= 0) {
584       if (color_eq(im, color, PALEXT(im)->pal + PALEXT(im)->last_found)) {
585         *entry = PALEXT(im)->last_found;
586         return 1;
587       }
588     }
589     for (i = 0; i < PALEXT(im)->count; ++i) {
590       if (color_eq(im, color, PALEXT(im)->pal + i)) {
591         PALEXT(im)->last_found = *entry = i;
592         return 1;
593       }
594     }
595   }
596   return 0;
597 }
598
599 /*
600 =back
601
602 =head1 AUTHOR
603
604 Tony Cook <tony@develop-help.com>
605
606 =head1 SEE ALSO
607
608 Imager(3)
609
610 =cut
611 */