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