]> git.imager.perl.org - imager.git/blame - palimg.c
change note for the has_chars and MakeMapObject changes
[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
696cb85d
TC
21#define IMAGER_NO_CONTEXT
22
92bda632
TC
23#include "imager.h"
24#include "imageri.h"
faa9b3e7
TC
25
26#define PALEXT(im) ((i_img_pal_ext*)((im)->ext_data))
8d14daab
TC
27static int i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val);
28static int i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val);
29static i_img_dim i_glin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals);
30static i_img_dim i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals);
31static i_img_dim i_gsamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samps, int const *chans, int chan_count);
32static i_img_dim i_gpal_p(i_img *pm, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals);
33static i_img_dim i_ppal_p(i_img *pm, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals);
97ac0a96 34static int i_addcolors_p(i_img *im, const i_color *color, int count);
faa9b3e7
TC
35static int i_getcolors_p(i_img *im, int i, i_color *color, int count);
36static int i_colorcount_p(i_img *im);
37static int i_maxcolors_p(i_img *im);
97ac0a96
TC
38static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry);
39static int i_setcolors_p(i_img *im, int index, const i_color *color, int count);
faa9b3e7
TC
40
41static void i_destroy_p(i_img *im);
836d9f54
TC
42static i_img_dim
43i_psamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_sample_t *samps, const int *chans, int chan_count);
44static i_img_dim
45i_psampf_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_fsample_t *samps, const int *chans, int chan_count);
faa9b3e7
TC
46
47static i_img IIM_base_8bit_pal =
48{
49 0, /* channels set */
50 0, 0, 0, /* xsize, ysize, bytes */
9a88a5e6 51 ~0U, /* ch_mask */
faa9b3e7
TC
52 i_8_bits, /* bits */
53 i_palette_type, /* type */
54 0, /* virtual */
55 NULL, /* idata */
56 { 0, 0, NULL }, /* tags */
57 NULL, /* ext_data */
58
59 i_ppix_p, /* i_f_ppix */
60 i_ppixf_fp, /* i_f_ppixf */
61 i_plin_p, /* i_f_plin */
62 i_plinf_fp, /* i_f_plinf */
63 i_gpix_p, /* i_f_gpix */
64 i_gpixf_fp, /* i_f_gpixf */
65 i_glin_p, /* i_f_glin */
66 i_glinf_fp, /* i_f_glinf */
67 i_gsamp_p, /* i_f_gsamp */
68 i_gsampf_fp, /* i_f_gsampf */
69
70 i_gpal_p, /* i_f_gpal */
71 i_ppal_p, /* i_f_ppal */
72 i_addcolors_p, /* i_f_addcolors */
73 i_getcolors_p, /* i_f_getcolors */
74 i_colorcount_p, /* i_f_colorcount */
75 i_maxcolors_p, /* i_f_maxcolors */
76 i_findcolor_p, /* i_f_findcolor */
77 i_setcolors_p, /* i_f_setcolors */
78
79 i_destroy_p, /* i_f_destroy */
bd8052a6
TC
80
81 i_gsamp_bits_fb,
82 NULL, /* i_f_psamp_bits */
836d9f54
TC
83
84 i_psamp_p,
85 i_psampf_p
faa9b3e7
TC
86};
87
88/*
abffffed
TC
89=item im_img_pal_new(ctx, C<x>, C<y>, C<channels>, C<maxpal>)
90X<im_img_pal_new API>X<i_img_pal_new API>
bd8052a6 91=category Image creation/destruction
abffffed 92=synopsis i_img *img = im_img_pal_new(aIMCTX, width, height, channels, max_palette_size)
bd8052a6
TC
93=synopsis i_img *img = i_img_pal_new(width, height, channels, max_palette_size)
94
95Creates a new paletted image of the supplied dimensions.
faa9b3e7 96
5715f7c3 97C<maxpal> is the maximum palette size and should normally be 256.
faa9b3e7 98
bd8052a6 99Returns a new image or NULL on failure.
faa9b3e7 100
abffffed
TC
101Also callable as C<i_img_pal_new(width, height, channels, max_palette_size)>.
102
faa9b3e7
TC
103=cut
104*/
bd8052a6 105i_img *
696cb85d 106im_img_pal_new(pIMCTX, i_img_dim x, i_img_dim y, int channels, int maxpal) {
bd8052a6 107 i_img *im;
faa9b3e7 108 i_img_pal_ext *palext;
8d14daab 109 size_t bytes, line_bytes;
faa9b3e7
TC
110
111 i_clear_error();
d443d6be 112 if (maxpal < 1 || maxpal > 256) {
faa9b3e7
TC
113 i_push_error(0, "Maximum of 256 palette entries");
114 return NULL;
115 }
116 if (x < 1 || y < 1) {
117 i_push_error(0, "Image sizes must be positive");
118 return NULL;
119 }
120 if (channels < 1 || channels > MAXCHANNELS) {
da3e5be2 121 im_push_errorf(aIMCTX, 0, "Channels must be positive and <= %d", MAXCHANNELS);
faa9b3e7
TC
122 return NULL;
123 }
653ea321
TC
124 bytes = sizeof(i_palidx) * x * y;
125 if (bytes / y / sizeof(i_palidx) != x) {
f0960b14
TC
126 i_push_error(0, "integer overflow calculating image allocation");
127 return NULL;
128 }
129
130 /* basic assumption: we can always allocate a buffer representing a
131 line from the image, otherwise we're going to have trouble
132 working with the image */
133 line_bytes = sizeof(i_color) * x;
134 if (line_bytes / x != sizeof(i_color)) {
135 i_push_error(0, "integer overflow calculating scanline allocation");
653ea321
TC
136 return NULL;
137 }
faa9b3e7 138
bd8052a6 139 im = i_img_alloc();
faa9b3e7
TC
140 memcpy(im, &IIM_base_8bit_pal, sizeof(i_img));
141 palext = mymalloc(sizeof(i_img_pal_ext));
142 palext->pal = mymalloc(sizeof(i_color) * maxpal);
143 palext->count = 0;
144 palext->alloc = maxpal;
145 palext->last_found = -1;
146 im->ext_data = palext;
147 i_tags_new(&im->tags);
653ea321 148 im->bytes = bytes;
faa9b3e7
TC
149 im->idata = mymalloc(im->bytes);
150 im->channels = channels;
705fd961 151 memset(im->idata, 0, im->bytes);
faa9b3e7
TC
152 im->xsize = x;
153 im->ysize = y;
4dc7c46b 154
bd8052a6
TC
155 i_img_init(im);
156
4dc7c46b 157 return im;
faa9b3e7
TC
158}
159
160/*
161=item i_img_rgb_convert(i_img *targ, i_img *src)
162
163Converts paletted data in src to RGB data in targ
164
165Internal function.
166
167src must be a paletted image and targ must be an RGB image with the
168same width, height and channels.
169
170=cut
171*/
172static void i_img_rgb_convert(i_img *targ, i_img *src) {
173 i_color *row = mymalloc(sizeof(i_color) * targ->xsize);
8d14daab 174 i_img_dim y;
faa9b3e7
TC
175 for (y = 0; y < targ->ysize; ++y) {
176 i_glin(src, 0, src->xsize, y, row);
177 i_plin(targ, 0, src->xsize, y, row);
178 }
179 myfree(row);
180}
181
182/*
183=item i_img_to_rgb_inplace(im)
184
185Converts im from a paletted image to an RGB image.
186
187The conversion is done in place.
188
189The conversion cannot be done for virtual images.
190
191=cut
192*/
696cb85d
TC
193int
194i_img_to_rgb_inplace(i_img *im) {
faa9b3e7 195 i_img temp;
696cb85d 196 dIMCTXim(im);
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 */
9f37b04d 208 i_img_exorcise(im);
faa9b3e7
TC
209 *im = temp;
210
9f37b04d
TC
211 /* i_img_empty_ch() calls i_img_init() which takes a ref */
212 im_context_refdec(aIMCTX, "img_destroy");
213
faa9b3e7
TC
214 return 1;
215}
216
217/*
218=item i_img_to_pal(i_img *im, i_quantize *quant)
219
220Converts an RGB image to a paletted image
221
222=cut
223*/
224i_img *i_img_to_pal(i_img *src, i_quantize *quant) {
225 i_palidx *result;
226 i_img *im;
696cb85d 227 dIMCTXim(src);
faa9b3e7 228
1501d9b3
TC
229 i_clear_error();
230
92bda632
TC
231 i_quant_makemap(quant, &src, 1);
232 result = i_quant_translate(quant, src);
faa9b3e7 233
1501d9b3 234 if (result) {
faa9b3e7 235
1501d9b3 236 im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size);
faa9b3e7 237
1501d9b3
TC
238 /* copy things over */
239 memcpy(im->idata, result, im->bytes);
240 PALEXT(im)->count = quant->mc_count;
241 memcpy(PALEXT(im)->pal, quant->mc_colors, sizeof(i_color) * quant->mc_count);
242
243 myfree(result);
244
245 return im;
246 }
247 else {
248 return NULL;
249 }
faa9b3e7
TC
250}
251
252/*
253=item i_img_to_rgb(i_img *src)
254
255=cut
256*/
696cb85d
TC
257i_img *
258i_img_to_rgb(i_img *src) {
259 dIMCTXim(src);
faa9b3e7
TC
260 i_img *im = i_img_empty_ch(NULL, src->xsize, src->ysize, src->channels);
261 i_img_rgb_convert(im, src);
262
263 return im;
264}
265
266/*
267=item i_destroy_p(i_img *im)
268
269Destroys data related to a paletted image.
270
271=cut
272*/
273static void i_destroy_p(i_img *im) {
274 if (im) {
275 i_img_pal_ext *palext = im->ext_data;
276 if (palext) {
277 if (palext->pal)
278 myfree(palext->pal);
279 myfree(palext);
280 }
281 }
282}
283
284/*
8d14daab 285=item i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val)
faa9b3e7
TC
286
287Write to a pixel in the image.
288
289Warning: converts the image to a RGB image if the color isn't already
290present in the image.
291
292=cut
293*/
97ac0a96 294static int
8d14daab 295i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val) {
836d9f54
TC
296 const i_color *work_val = val;
297 i_color workc;
faa9b3e7 298 i_palidx which;
836d9f54
TC
299 const unsigned all_mask = ( 1 << im->channels ) - 1;
300
faa9b3e7
TC
301 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
302 return -1;
836d9f54
TC
303
304 if ((im->ch_mask & all_mask) != all_mask) {
305 unsigned mask = 1;
306 int ch;
307 i_gpix(im, x, y, &workc);
308 for (ch = 0; ch < im->channels; ++ch) {
309 if (im->ch_mask & mask)
310 workc.channel[ch] = val->channel[ch];
311 mask <<= 1;
312 }
313 work_val = &workc;
314 }
315
316 if (i_findcolor(im, work_val, &which)) {
faa9b3e7
TC
317 ((i_palidx *)im->idata)[x + y * im->xsize] = which;
318 return 0;
319 }
320 else {
da3e5be2
TC
321 dIMCTXim(im);
322 im_log((aIMCTX, 1, "i_ppix: color(%d,%d,%d) not found, converting to rgb\n",
836d9f54 323 val->channel[0], val->channel[1], val->channel[2]));
faa9b3e7
TC
324 if (i_img_to_rgb_inplace(im)) {
325 return i_ppix(im, x, y, val);
326 }
327 else
328 return -1;
329 }
330}
331
332/*
8d14daab 333=item i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val)
faa9b3e7
TC
334
335Retrieve a pixel, converting from a palette index to a color.
336
337=cut
338*/
8d14daab 339static int i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val) {
faa9b3e7
TC
340 i_palidx which;
341 if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
342 return -1;
343 }
344 which = ((i_palidx *)im->idata)[x + y * im->xsize];
345 if (which > PALEXT(im)->count)
346 return -1;
347 *val = PALEXT(im)->pal[which];
348
349 return 0;
350}
351
352/*
8d14daab 353=item i_glinp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals)
faa9b3e7
TC
354
355Retrieve a row of pixels.
356
357=cut
358*/
8d14daab 359static i_img_dim i_glin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals) {
faa9b3e7
TC
360 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
361 int palsize = PALEXT(im)->count;
362 i_color *pal = PALEXT(im)->pal;
363 i_palidx *data;
8d14daab 364 i_img_dim count, i;
faa9b3e7
TC
365 if (r > im->xsize)
366 r = im->xsize;
367 data = ((i_palidx *)im->idata) + l + y * im->xsize;
368 count = r - l;
369 for (i = 0; i < count; ++i) {
370 i_palidx which = *data++;
371 if (which < palsize)
372 vals[i] = pal[which];
373 }
374 return count;
375 }
376 else {
377 return 0;
378 }
379}
380
381/*
8d14daab 382=item i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals)
faa9b3e7
TC
383
384Write a line of color data to the image.
385
386If any color value is not in the image when the image is converted to
387RGB.
388
389=cut
390*/
8d14daab
TC
391static i_img_dim
392i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals) {
393 i_img_dim count, i;
faa9b3e7
TC
394 i_palidx *data;
395 i_palidx which;
396 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
397 if (r > im->xsize)
398 r = im->xsize;
399 data = ((i_palidx *)im->idata) + l + y * im->xsize;
400 count = r - l;
401 for (i = 0; i < count; ++i) {
402 if (i_findcolor(im, vals+i, &which)) {
403 ((i_palidx *)data)[i] = which;
404 }
405 else {
406 if (i_img_to_rgb_inplace(im)) {
407 return i+i_plin(im, l+i, r, y, vals+i);
408 }
409 }
410 }
411 return count;
412 }
413 else {
414 return 0;
415 }
416}
417
418/*
8d14daab 419=item i_gsamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samps, int chans, int chan_count)
faa9b3e7
TC
420
421=cut
422*/
8d14daab 423static i_img_dim i_gsamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_sample_t *samps,
18accb2a 424 int const *chans, int chan_count) {
faa9b3e7
TC
425 int ch;
426 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
427 int palsize = PALEXT(im)->count;
428 i_color *pal = PALEXT(im)->pal;
429 i_palidx *data;
8d14daab 430 i_img_dim count, i, w;
faa9b3e7
TC
431 if (r > im->xsize)
432 r = im->xsize;
433 data = ((i_palidx *)im->idata) + l + y * im->xsize;
434 count = 0;
435 w = r - l;
436 if (chans) {
437 for (ch = 0; ch < chan_count; ++ch) {
438 if (chans[ch] < 0 || chans[ch] >= im->channels) {
da3e5be2
TC
439 dIMCTXim(im);
440 im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
faa9b3e7
TC
441 }
442 }
443
444 for (i = 0; i < w; ++i) {
445 i_palidx which = *data++;
446 if (which < palsize) {
447 for (ch = 0; ch < chan_count; ++ch) {
448 *samps++ = pal[which].channel[chans[ch]];
449 ++count;
450 }
451 }
452 }
453 }
454 else {
c7481ae1 455 if (chan_count <= 0 || chan_count > im->channels) {
da3e5be2
TC
456 dIMCTXim(im);
457 im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels",
c7481ae1
TC
458 chan_count);
459 return 0;
460 }
faa9b3e7
TC
461 for (i = 0; i < w; ++i) {
462 i_palidx which = *data++;
463 if (which < palsize) {
464 for (ch = 0; ch < chan_count; ++ch) {
465 *samps++ = pal[which].channel[ch];
466 ++count;
467 }
468 }
469 }
470 }
471 return count;
472 }
473 else {
474 return 0;
475 }
476}
477
478/*
8d14daab 479=item i_gpal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals)
faa9b3e7
TC
480
481=cut
482*/
483
8d14daab 484static i_img_dim i_gpal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals) {
faa9b3e7
TC
485 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
486 i_palidx *data;
8d14daab 487 i_img_dim i, w;
faa9b3e7
TC
488 if (r > im->xsize)
489 r = im->xsize;
490 data = ((i_palidx *)im->idata) + l + y * im->xsize;
491 w = r - l;
492 for (i = 0; i < w; ++i) {
493 *vals++ = *data++;
494 }
495 return i;
496 }
497 else {
498 return 0;
499 }
500}
501
502/*
8d14daab 503=item i_ppal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals)
faa9b3e7
TC
504
505=cut
506*/
507
8d14daab 508static i_img_dim i_ppal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals) {
faa9b3e7
TC
509 if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
510 i_palidx *data;
8d14daab 511 i_img_dim i, w;
faa9b3e7
TC
512 if (r > im->xsize)
513 r = im->xsize;
514 data = ((i_palidx *)im->idata) + l + y * im->xsize;
515 w = r - l;
516 for (i = 0; i < w; ++i) {
517 *data++ = *vals++;
518 }
519 return i;
520 }
521 else {
522 return 0;
523 }
524}
525
526/*
97ac0a96 527=item i_addcolors_p(i_img *im, const i_color *color, int count)
faa9b3e7
TC
528
529=cut
530*/
97ac0a96 531static int i_addcolors_p(i_img *im, const i_color *color, int count) {
faa9b3e7
TC
532 if (PALEXT(im)->count + count <= PALEXT(im)->alloc) {
533 int result = PALEXT(im)->count;
534 int index = result;
535
536 PALEXT(im)->count += count;
537 while (count) {
538 PALEXT(im)->pal[index++] = *color++;
539 --count;
540 }
541
542 return result;
543 }
544 else
545 return -1;
546}
547
548/*
549=item i_getcolors_p(i_img *im, int i, i_color *color, int count)
550
551=cut
552*/
63b018fd 553static int i_getcolors_p(i_img *im, int i, i_color *color, int count) {
faa9b3e7
TC
554 if (i >= 0 && i+count <= PALEXT(im)->count) {
555 while (count) {
556 *color++ = PALEXT(im)->pal[i++];
557 --count;
558 }
559 return 1;
560 }
561 else
562 return 0;
563}
564
97ac0a96 565static int color_eq(i_img *im, const i_color *c1, const i_color *c2) {
faa9b3e7
TC
566 int ch;
567 for (ch = 0; ch < im->channels; ++ch) {
568 if (c1->channel[ch] != c2->channel[ch])
569 return 0;
570 }
571 return 1;
572}
573
574/*
575=item i_colorcount_p(i_img *im)
576
577=cut
578*/
63b018fd 579static int i_colorcount_p(i_img *im) {
faa9b3e7
TC
580 return PALEXT(im)->count;
581}
582
583/*
584=item i_maxcolors_p(i_img *im)
585
586=cut
587*/
63b018fd 588static int i_maxcolors_p(i_img *im) {
faa9b3e7
TC
589 return PALEXT(im)->alloc;
590}
591
592/*
97ac0a96 593=item i_setcolors_p(i_img *im, int index, const i_color *colors, int count)
faa9b3e7
TC
594
595=cut
596*/
97ac0a96 597static int i_setcolors_p(i_img *im, int index, const i_color *colors, int count) {
8efd1577 598 if (index >= 0 && count >= 1 && index + count <= PALEXT(im)->count) {
faa9b3e7
TC
599 while (count) {
600 PALEXT(im)->pal[index++] = *colors++;
601 --count;
602 }
603 return 1;
604 }
605
606 return 0;
607}
608
609/*
610=item i_findcolor_p(i_img *im)
611
612=cut
613*/
97ac0a96 614static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry) {
faa9b3e7
TC
615 if (PALEXT(im)->count) {
616 int i;
617 /* often the same color comes up several times in a row */
618 if (PALEXT(im)->last_found >= 0) {
619 if (color_eq(im, color, PALEXT(im)->pal + PALEXT(im)->last_found)) {
620 *entry = PALEXT(im)->last_found;
621 return 1;
622 }
623 }
624 for (i = 0; i < PALEXT(im)->count; ++i) {
625 if (color_eq(im, color, PALEXT(im)->pal + i)) {
626 PALEXT(im)->last_found = *entry = i;
627 return 1;
628 }
629 }
630 }
631 return 0;
632}
b8c2033e 633
836d9f54
TC
634/*
635=item i_psamp_p(im, l, r, y, samps, chans, chan_count)
636
637Implement psamp() for paletted images.
638
639Since writing samples doesn't really work as a concept for paletted
640images, this is slow.
641
642Also, writing samples may convert the image to a direct image in the
643process, so use i_ppix/i_gpix instead of directly calling the paletted
644handlers.
645
646=cut
647*/
648
649static i_img_dim
650i_psamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y,
651 const i_sample_t *samps, const int *chans, int chan_count) {
652 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
653 i_img_dim count = 0;
654 int ch;
655
656 if (r > im->xsize)
657 r = im->xsize;
658
659 if (chans) {
660 /* make sure we have good channel numbers */
661 for (ch = 0; ch < chan_count; ++ch) {
662 if (chans[ch] < 0 || chans[ch] >= im->channels) {
da3e5be2
TC
663 dIMCTXim(im);
664 im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
836d9f54
TC
665 return -1;
666 }
667 }
668 while (l < r) {
669 i_color c;
670
671 i_gpix(im, l, y, &c);
672 for (ch = 0; ch < chan_count; ++ch)
673 c.channel[chans[ch]] = *samps++;
674 i_ppix(im, l, y, &c);
675 count += chan_count;
676 ++l;
677 }
678 }
679 else {
680 if (chan_count <= 0 || chan_count > im->channels) {
da3e5be2
TC
681 dIMCTXim(im);
682 im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels",
836d9f54
TC
683 chan_count);
684 return -1;
685 }
686
687 while (l < r) {
688 i_color c;
689
690 i_gpix(im, l, y, &c);
691 for (ch = 0; ch < chan_count; ++ch)
692 c.channel[ch] = *samps++;
693 i_ppix(im, l, y, &c);
694 count += chan_count;
695 ++l;
696 }
697 }
698
699 return count;
700 }
701 else {
696cb85d 702 dIMCTXim(im);
836d9f54
TC
703 i_push_error(0, "Image position outside of image");
704 return -1;
705 }
706}
707
708/*
709=item i_psampf_p(im, l, r, y, samps, chans, chan_count)
710
711Implement psampf() for paletted images.
712
713Since writing samples doesn't really work as a concept for paletted
714images, this is slow.
715
716Also, writing samples may convert the image to a direct image in the
717process, so use i_ppixf/i_gpixf instead of directly calling the paletted
718handlers.
719
720=cut
721*/
722
723static i_img_dim
724i_psampf_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y,
725 const i_fsample_t *samps, const int *chans, int chan_count) {
726 if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
727 i_img_dim count = 0;
728 int ch;
729
730 if (r > im->xsize)
731 r = im->xsize;
732
733 if (chans) {
734 /* make sure we have good channel numbers */
735 for (ch = 0; ch < chan_count; ++ch) {
736 if (chans[ch] < 0 || chans[ch] >= im->channels) {
da3e5be2
TC
737 dIMCTXim(im);
738 im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
836d9f54
TC
739 return -1;
740 }
741 }
742 while (l < r) {
743 i_fcolor c;
744
745 i_gpixf(im, l, y, &c);
746 for (ch = 0; ch < chan_count; ++ch)
747 c.channel[chans[ch]] = *samps++;
748 i_ppixf(im, l, y, &c);
749 count += chan_count;
750 ++l;
751 }
752 }
753 else {
754 if (chan_count <= 0 || chan_count > im->channels) {
da3e5be2
TC
755 dIMCTXim(im);
756 im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels",
836d9f54
TC
757 chan_count);
758 return -1;
759 }
760
761 while (l < r) {
762 i_fcolor c;
763
764 i_gpixf(im, l, y, &c);
765 for (ch = 0; ch < chan_count; ++ch)
766 c.channel[ch] = *samps++;
767 i_ppixf(im, l, y, &c);
768 count += chan_count;
769 ++l;
770 }
771 }
772
773 return count;
774 }
775 else {
696cb85d 776 dIMCTXim(im);
836d9f54
TC
777 i_push_error(0, "Image position outside of image");
778 return -1;
779 }
780}
781
b8c2033e
AMH
782/*
783=back
784
785=head1 AUTHOR
786
787Tony Cook <tony@develop-help.com>
788
789=head1 SEE ALSO
790
791Imager(3)
792
793=cut
794*/