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