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