File Coverage

palimg.c
Criterion Covered Total %
statement 265 298 88.9
branch 188 242 77.6
condition n/a
subroutine n/a
pod n/a
total 453 540 83.8


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