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