Added an Imager::log_entry() function and some extra logging
[imager.git] / palimg.c
CommitLineData
faa9b3e7
TC
1/*
2=head1 NAME
3
4 palimg.c - implements paletted images for Imager.
5
6=head1 SYNOPSIS
7
8=head1 DESCRIPTION
9
10Implements paletted images using the new image interface.
11
12=over
13
14=item IIM_base_8bit_pal
15
16Basic 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))
25static int i_ppix_p(i_img *im, int x, int y, i_color *val);
26static int i_gpix_p(i_img *im, int x, int y, i_color *val);
27static int i_glin_p(i_img *im, int l, int r, int y, i_color *vals);
28static int i_plin_p(i_img *im, int l, int r, int y, i_color *vals);
29static int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, int *chans, int chan_count);
30static int i_gpal_p(i_img *pm, int l, int r, int y, i_palidx *vals);
31static int i_ppal_p(i_img *pm, int l, int r, int y, i_palidx *vals);
32static int i_addcolors_p(i_img *im, i_color *color, int count);
33static int i_getcolors_p(i_img *im, int i, i_color *color, int count);
34static int i_colorcount_p(i_img *im);
35static int i_maxcolors_p(i_img *im);
36static int i_findcolor_p(i_img *im, i_color *color, i_palidx *entry);
37static int i_setcolors_p(i_img *im, int index, i_color *color, int count);
38
39static void i_destroy_p(i_img *im);
40
41static i_img IIM_base_8bit_pal =
42{
43 0, /* channels set */
44 0, 0, 0, /* xsize, ysize, bytes */
9a88a5e6 45 ~0U, /* ch_mask */
faa9b3e7
TC
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
79Creates a new paletted image.
80
81Currently 0 < maxpal <= 256
82
83=cut
84*/
85i_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 postive 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;
705fd961 113 memset(im->idata, 0, im->bytes);
faa9b3e7
TC
114 im->xsize = x;
115 im->ysize = y;
116
117 return im;
118}
119
120i_img *i_img_pal_new(int x, int y, int channels, int maxpal) {
7f882a01
AMH
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));
faa9b3e7
TC
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
130Converts paletted data in src to RGB data in targ
131
132Internal function.
133
134src must be a paletted image and targ must be an RGB image with the
135same width, height and channels.
136
137=cut
138*/
139static 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
152Converts im from a paletted image to an RGB image.
153
154The conversion is done in place.
155
156The conversion cannot be done for virtual images.
157
158=cut
159*/
160int 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
185Converts an RGB image to a paletted image
186
187=cut
188*/
189i_img *i_img_to_pal(i_img *src, i_quantize *quant) {
190 i_palidx *result;
191 i_img *im;
192
193 im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size);
194
195 quant_makemap(quant, &src, 1);
196 result = quant_translate(quant, src);
197
198 /* copy things over */
199 memcpy(im->idata, result, im->bytes);
200 PALEXT(im)->count = quant->mc_count;
201 memcpy(PALEXT(im)->pal, quant->mc_colors, sizeof(i_color) * quant->mc_count);
202
203 myfree(result);
204
205 return im;
206}
207
208/*
209=item i_img_to_rgb(i_img *src)
210
211=cut
212*/
213i_img *i_img_to_rgb(i_img *src) {
214 i_img *im = i_img_empty_ch(NULL, src->xsize, src->ysize, src->channels);
215 i_img_rgb_convert(im, src);
216
217 return im;
218}
219
220/*
221=item i_destroy_p(i_img *im)
222
223Destroys data related to a paletted image.
224
225=cut
226*/
227static void i_destroy_p(i_img *im) {
228 if (im) {
229 i_img_pal_ext *palext = im->ext_data;
230 if (palext) {
231 if (palext->pal)
232 myfree(palext->pal);
233 myfree(palext);
234 }
235 }
236}
237
238/*
239=item i_ppix_p(i_img *im, int x, int y, i_color *val)
240
241Write to a pixel in the image.
242
243Warning: converts the image to a RGB image if the color isn't already
244present in the image.
245
246=cut
247*/
63b018fd 248static int i_ppix_p(i_img *im, int x, int y, i_color *val) {
faa9b3e7
TC
249 i_palidx which;
250 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
251 return -1;
252 if (i_findcolor(im, val, &which)) {
253 ((i_palidx *)im->idata)[x + y * im->xsize] = which;
254 return 0;
255 }
256 else {
257 if (i_img_to_rgb_inplace(im)) {
258 return i_ppix(im, x, y, val);
259 }
260 else
261 return -1;
262 }
263}
264
265/*
266=item i_gpix(i_img *im, int x, int y, i_color *val)
267
268Retrieve a pixel, converting from a palette index to a color.
269
270=cut
271*/
63b018fd 272static int i_gpix_p(i_img *im, int x, int y, i_color *val) {
faa9b3e7
TC
273 i_palidx which;
274 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
275 return -1;
276 }
277 which = ((i_palidx *)im->idata)[x + y * im->xsize];
278 if (which > PALEXT(im)->count)
279 return -1;
280 *val = PALEXT(im)->pal[which];
281
282 return 0;
283}
284
285/*
286=item i_glinp(i_img *im, int l, int r, int y, i_color *vals)
287
288Retrieve a row of pixels.
289
290=cut
291*/
63b018fd 292static int i_glin_p(i_img *im, int l, int r, int y, i_color *vals) {
faa9b3e7
TC
293 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
294 int palsize = PALEXT(im)->count;
295 i_color *pal = PALEXT(im)->pal;
296 i_palidx *data;
297 int count, i;
298 if (r > im->xsize)
299 r = im->xsize;
300 data = ((i_palidx *)im->idata) + l + y * im->xsize;
301 count = r - l;
302 for (i = 0; i < count; ++i) {
303 i_palidx which = *data++;
304 if (which < palsize)
305 vals[i] = pal[which];
306 }
307 return count;
308 }
309 else {
310 return 0;
311 }
312}
313
314/*
315=item i_plin_p(i_img *im, int l, int r, int y, i_color *vals)
316
317Write a line of color data to the image.
318
319If any color value is not in the image when the image is converted to
320RGB.
321
322=cut
323*/
63b018fd 324static int i_plin_p(i_img *im, int l, int r, int y, i_color *vals) {
faa9b3e7
TC
325 int ch, count, i;
326 i_palidx *data;
327 i_palidx which;
328 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
329 if (r > im->xsize)
330 r = im->xsize;
331 data = ((i_palidx *)im->idata) + l + y * im->xsize;
332 count = r - l;
333 for (i = 0; i < count; ++i) {
334 if (i_findcolor(im, vals+i, &which)) {
335 ((i_palidx *)data)[i] = which;
336 }
337 else {
338 if (i_img_to_rgb_inplace(im)) {
339 return i+i_plin(im, l+i, r, y, vals+i);
340 }
341 }
342 }
343 return count;
344 }
345 else {
346 return 0;
347 }
348}
349
350/*
351=item i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps, int chans, int chan_count)
352
353=cut
354*/
63b018fd 355static int i_gsamp_p(i_img *im, int l, int r, int y, i_sample_t *samps,
faa9b3e7
TC
356 int *chans, int chan_count) {
357 int ch;
358 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
359 int palsize = PALEXT(im)->count;
360 i_color *pal = PALEXT(im)->pal;
361 i_palidx *data;
362 int count, i, w;
363 if (r > im->xsize)
364 r = im->xsize;
365 data = ((i_palidx *)im->idata) + l + y * im->xsize;
366 count = 0;
367 w = r - l;
368 if (chans) {
369 for (ch = 0; ch < chan_count; ++ch) {
370 if (chans[ch] < 0 || chans[ch] >= im->channels) {
371 i_push_errorf(0, "No channel %d in this image", chans[ch]);
372 }
373 }
374
375 for (i = 0; i < w; ++i) {
376 i_palidx which = *data++;
377 if (which < palsize) {
378 for (ch = 0; ch < chan_count; ++ch) {
379 *samps++ = pal[which].channel[chans[ch]];
380 ++count;
381 }
382 }
383 }
384 }
385 else {
386 for (i = 0; i < w; ++i) {
387 i_palidx which = *data++;
388 if (which < palsize) {
389 for (ch = 0; ch < chan_count; ++ch) {
390 *samps++ = pal[which].channel[ch];
391 ++count;
392 }
393 }
394 }
395 }
396 return count;
397 }
398 else {
399 return 0;
400 }
401}
402
403/*
404=item i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals)
405
406=cut
407*/
408
63b018fd 409static int i_gpal_p(i_img *im, int l, int r, int y, i_palidx *vals) {
faa9b3e7
TC
410 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
411 i_palidx *data;
412 int i, w;
413 if (r > im->xsize)
414 r = im->xsize;
415 data = ((i_palidx *)im->idata) + l + y * im->xsize;
416 w = r - l;
417 for (i = 0; i < w; ++i) {
418 *vals++ = *data++;
419 }
420 return i;
421 }
422 else {
423 return 0;
424 }
425}
426
427/*
428=item i_ppal_p(i_img *im, int l, int r, int y, i_palidx *vals)
429
430=cut
431*/
432
63b018fd 433static int i_ppal_p(i_img *im, int l, int r, int y, i_palidx *vals) {
faa9b3e7
TC
434 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
435 i_palidx *data;
436 int i, w;
437 if (r > im->xsize)
438 r = im->xsize;
439 data = ((i_palidx *)im->idata) + l + y * im->xsize;
440 w = r - l;
441 for (i = 0; i < w; ++i) {
442 *data++ = *vals++;
443 }
444 return i;
445 }
446 else {
447 return 0;
448 }
449}
450
451/*
452=item i_addcolors_p(i_img *im, i_color *color, int count)
453
454=cut
455*/
63b018fd 456static int i_addcolors_p(i_img *im, i_color *color, int count) {
faa9b3e7
TC
457 if (PALEXT(im)->count + count <= PALEXT(im)->alloc) {
458 int result = PALEXT(im)->count;
459 int index = result;
460
461 PALEXT(im)->count += count;
462 while (count) {
463 PALEXT(im)->pal[index++] = *color++;
464 --count;
465 }
466
467 return result;
468 }
469 else
470 return -1;
471}
472
473/*
474=item i_getcolors_p(i_img *im, int i, i_color *color, int count)
475
476=cut
477*/
63b018fd 478static int i_getcolors_p(i_img *im, int i, i_color *color, int count) {
faa9b3e7
TC
479 if (i >= 0 && i+count <= PALEXT(im)->count) {
480 while (count) {
481 *color++ = PALEXT(im)->pal[i++];
482 --count;
483 }
484 return 1;
485 }
486 else
487 return 0;
488}
489
490static int color_eq(i_img *im, i_color *c1, i_color *c2) {
491 int ch;
492 for (ch = 0; ch < im->channels; ++ch) {
493 if (c1->channel[ch] != c2->channel[ch])
494 return 0;
495 }
496 return 1;
497}
498
499/*
500=item i_colorcount_p(i_img *im)
501
502=cut
503*/
63b018fd 504static int i_colorcount_p(i_img *im) {
faa9b3e7
TC
505 return PALEXT(im)->count;
506}
507
508/*
509=item i_maxcolors_p(i_img *im)
510
511=cut
512*/
63b018fd 513static int i_maxcolors_p(i_img *im) {
faa9b3e7
TC
514 return PALEXT(im)->alloc;
515}
516
517/*
518=item i_setcolors_p(i_img *im, int index, i_color *colors, int count)
519
520=cut
521*/
63b018fd 522static int i_setcolors_p(i_img *im, int index, i_color *colors, int count) {
faa9b3e7
TC
523 if (index >= 0 && count >= 1 && index + count < PALEXT(im)->count) {
524 while (count) {
525 PALEXT(im)->pal[index++] = *colors++;
526 --count;
527 }
528 return 1;
529 }
530
531 return 0;
532}
533
534/*
535=item i_findcolor_p(i_img *im)
536
537=cut
538*/
63b018fd 539static int i_findcolor_p(i_img *im, i_color *color, i_palidx *entry) {
faa9b3e7
TC
540 if (PALEXT(im)->count) {
541 int i;
542 /* often the same color comes up several times in a row */
543 if (PALEXT(im)->last_found >= 0) {
544 if (color_eq(im, color, PALEXT(im)->pal + PALEXT(im)->last_found)) {
545 *entry = PALEXT(im)->last_found;
546 return 1;
547 }
548 }
549 for (i = 0; i < PALEXT(im)->count; ++i) {
550 if (color_eq(im, color, PALEXT(im)->pal + i)) {
551 PALEXT(im)->last_found = *entry = i;
552 return 1;
553 }
554 }
555 }
556 return 0;
557}
b8c2033e
AMH
558
559/*
560=back
561
562=head1 AUTHOR
563
564Tony Cook <tony@develop-help.com>
565
566=head1 SEE ALSO
567
568Imager(3)
569
570=cut
571*/