File: palimg.c

package info (click to toggle)
libimager-perl 1.012%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,376 kB
  • sloc: perl: 31,562; ansic: 27,846; makefile: 53; cpp: 4
file content (794 lines) | stat: -rw-r--r-- 18,433 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
/*
=head1 NAME

  palimg.c - implements paletted images for Imager.

=head1 SYNOPSIS

=head1 DESCRIPTION

Implements paletted images using the new image interface.

=over

=item IIM_base_8bit_pal

Basic 8-bit/sample paletted image

=cut
*/

#define IMAGER_NO_CONTEXT

#include "imager.h"
#include "imageri.h"

#define PALEXT(im) ((i_img_pal_ext*)((im)->ext_data))
static int i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val);
static int i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val);
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);
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);
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);
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);
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);
static int i_addcolors_p(i_img *im, const i_color *color, int count);
static int i_getcolors_p(i_img *im, int i, i_color *color, int count);
static int i_colorcount_p(i_img *im);
static int i_maxcolors_p(i_img *im);
static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry);
static int i_setcolors_p(i_img *im, int index, const i_color *color, int count);

static void i_destroy_p(i_img *im);
static i_img_dim 
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);
static i_img_dim 
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);

static i_img IIM_base_8bit_pal =
{
  0, /* channels set */
  0, 0, 0, /* xsize, ysize, bytes */
  ~0U, /* ch_mask */
  i_8_bits, /* bits */
  i_palette_type, /* type */
  0, /* virtual */
  NULL, /* idata */
  { 0, 0, NULL }, /* tags */
  NULL, /* ext_data */

  i_ppix_p, /* i_f_ppix */
  i_ppixf_fp, /* i_f_ppixf */
  i_plin_p, /* i_f_plin */
  i_plinf_fp, /* i_f_plinf */
  i_gpix_p, /* i_f_gpix */
  i_gpixf_fp, /* i_f_gpixf */
  i_glin_p, /* i_f_glin */
  i_glinf_fp, /* i_f_glinf */
  i_gsamp_p, /* i_f_gsamp */
  i_gsampf_fp, /* i_f_gsampf */

  i_gpal_p, /* i_f_gpal */
  i_ppal_p, /* i_f_ppal */
  i_addcolors_p, /* i_f_addcolors */
  i_getcolors_p, /* i_f_getcolors */
  i_colorcount_p, /* i_f_colorcount */
  i_maxcolors_p, /* i_f_maxcolors */
  i_findcolor_p, /* i_f_findcolor */
  i_setcolors_p, /* i_f_setcolors */

  i_destroy_p, /* i_f_destroy */

  i_gsamp_bits_fb,
  NULL, /* i_f_psamp_bits */
  
  i_psamp_p,
  i_psampf_p
};

/*
=item im_img_pal_new(ctx, C<x>, C<y>, C<channels>, C<maxpal>)
X<im_img_pal_new API>X<i_img_pal_new API>
=category Image creation/destruction
=synopsis i_img *img = im_img_pal_new(aIMCTX, width, height, channels, max_palette_size)
=synopsis i_img *img = i_img_pal_new(width, height, channels, max_palette_size)

Creates a new paletted image of the supplied dimensions.

C<maxpal> is the maximum palette size and should normally be 256.

Returns a new image or NULL on failure.

Also callable as C<i_img_pal_new(width, height, channels, max_palette_size)>.

=cut
*/
i_img *
im_img_pal_new(pIMCTX, i_img_dim x, i_img_dim y, int channels, int maxpal) {
  i_img *im;
  i_img_pal_ext *palext;
  size_t bytes, line_bytes;

  i_clear_error();
  if (maxpal < 1 || maxpal > 256) {
    i_push_error(0, "Maximum of 256 palette entries");
    return NULL;
  }
  if (x < 1 || y < 1) {
    i_push_error(0, "Image sizes must be positive");
    return NULL;
  }
  if (channels < 1 || channels > MAXCHANNELS) {
    im_push_errorf(aIMCTX, 0, "Channels must be positive and <= %d", MAXCHANNELS);
    return NULL;
  }
  bytes = sizeof(i_palidx) * x * y;
  if (bytes / y / sizeof(i_palidx) != x) {
    i_push_error(0, "integer overflow calculating image allocation");
    return NULL;
  }

  /* basic assumption: we can always allocate a buffer representing a
     line from the image, otherwise we're going to have trouble
     working with the image */
  line_bytes = sizeof(i_color) * x;
  if (line_bytes / x != sizeof(i_color)) {
    i_push_error(0, "integer overflow calculating scanline allocation");
    return NULL;
  }

  im = i_img_alloc();
  memcpy(im, &IIM_base_8bit_pal, sizeof(i_img));
  palext = mymalloc(sizeof(i_img_pal_ext));
  palext->pal = mymalloc(sizeof(i_color) * maxpal);
  palext->count = 0;
  palext->alloc = maxpal;
  palext->last_found = -1;
  im->ext_data = palext;
  i_tags_new(&im->tags);
  im->bytes = bytes;
  im->idata = mymalloc(im->bytes);
  im->channels = channels;
  memset(im->idata, 0, im->bytes);
  im->xsize = x;
  im->ysize = y;

  i_img_init(im);
  
  return im;
}

/*
=item i_img_rgb_convert(i_img *targ, i_img *src)

Converts paletted data in src to RGB data in targ

Internal function.

src must be a paletted image and targ must be an RGB image with the
same width, height and channels.

=cut
*/
static void i_img_rgb_convert(i_img *targ, i_img *src) {
  i_color *row = mymalloc(sizeof(i_color) * targ->xsize);
  i_img_dim y;
  for (y = 0; y < targ->ysize; ++y) {
    i_glin(src, 0, src->xsize, y, row);
    i_plin(targ, 0, src->xsize, y, row);
  }
  myfree(row);
}

/*
=item i_img_to_rgb_inplace(im)

Converts im from a paletted image to an RGB image.

The conversion is done in place.

The conversion cannot be done for virtual images.

=cut
*/
int
i_img_to_rgb_inplace(i_img *im) {
  i_img temp;
  dIMCTXim(im);

  if (im->virtual)
    return 0;

  if (im->type == i_direct_type)
    return 1; /* trivial success */

  i_img_empty_ch(&temp, im->xsize, im->ysize, im->channels);
  i_img_rgb_convert(&temp, im);

  /* nasty hack */
  i_img_exorcise(im);
  *im = temp;

  /* i_img_empty_ch() calls i_img_init() which takes a ref */
  im_context_refdec(aIMCTX, "img_destroy");

  return 1;
}

/*
=item i_img_to_pal(i_img *im, i_quantize *quant)

Converts an RGB image to a paletted image

=cut
*/
i_img *i_img_to_pal(i_img *src, i_quantize *quant) {
  i_palidx *result;
  i_img *im;
  dIMCTXim(src);

  i_clear_error();
  
  i_quant_makemap(quant, &src, 1);
  result = i_quant_translate(quant, src);

  if (result) {

    im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size);

    /* copy things over */
    memcpy(im->idata, result, im->bytes);
    PALEXT(im)->count = quant->mc_count;
    memcpy(PALEXT(im)->pal, quant->mc_colors, sizeof(i_color) * quant->mc_count);
    
    myfree(result);

    return im;
  }
  else {
    return NULL;
  }
}

/*
=item i_img_to_rgb(i_img *src)

=cut
*/
i_img *
i_img_to_rgb(i_img *src) {
  dIMCTXim(src);
  i_img *im = i_img_empty_ch(NULL, src->xsize, src->ysize, src->channels);
  i_img_rgb_convert(im, src);

  return im;
}

/*
=item i_destroy_p(i_img *im)

Destroys data related to a paletted image.

=cut
*/
static void i_destroy_p(i_img *im) {
  if (im) {
    i_img_pal_ext *palext = im->ext_data;
    if (palext) {
      if (palext->pal)
        myfree(palext->pal);
      myfree(palext);
    }
  }
}

/*
=item i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val)

Write to a pixel in the image.

Warning: converts the image to a RGB image if the color isn't already
present in the image.

=cut
*/
static int 
i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val) {
  const i_color *work_val = val;
  i_color workc;
  i_palidx which;
  const unsigned all_mask = ( 1 << im->channels ) - 1;

  if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
    return -1;

  if ((im->ch_mask & all_mask) != all_mask) {
    unsigned mask = 1;
    int ch;
    i_gpix(im, x, y, &workc);
    for (ch = 0; ch < im->channels; ++ch) {
      if (im->ch_mask & mask)
	workc.channel[ch] = val->channel[ch];
      mask <<= 1;
    }
    work_val = &workc;
  }

  if (i_findcolor(im, work_val, &which)) {
    ((i_palidx *)im->idata)[x + y * im->xsize] = which;
    return 0;
  }
  else {
    dIMCTXim(im);
    im_log((aIMCTX, 1, "i_ppix: color(%d,%d,%d) not found, converting to rgb\n",
	    val->channel[0], val->channel[1], val->channel[2]));
    if (i_img_to_rgb_inplace(im)) {
      return i_ppix(im, x, y, val);
    }
    else
      return -1;
  }
}

/*
=item i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val)

Retrieve a pixel, converting from a palette index to a color.

=cut
*/
static int i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val) {
  i_palidx which;
  if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
    return -1;
  }
  which = ((i_palidx *)im->idata)[x + y * im->xsize];
  if (which > PALEXT(im)->count)
    return -1;
  *val = PALEXT(im)->pal[which];

  return 0;
}

/*
=item i_glinp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals)

Retrieve a row of pixels.

=cut
*/
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) {
  if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
    int palsize = PALEXT(im)->count;
    i_color *pal = PALEXT(im)->pal;
    i_palidx *data;
    i_img_dim count, i;
    if (r > im->xsize)
      r = im->xsize;
    data = ((i_palidx *)im->idata) + l + y * im->xsize;
    count = r - l;
    for (i = 0; i < count; ++i) {
      i_palidx which = *data++;
      if (which < palsize)
        vals[i] = pal[which];
    }
    return count;
  }
  else {
    return 0;
  }
}

/*
=item i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals)

Write a line of color data to the image.

If any color value is not in the image when the image is converted to 
RGB.

=cut
*/
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) {
  i_img_dim count, i;
  i_palidx *data;
  i_palidx which;
  if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
    if (r > im->xsize)
      r = im->xsize;
    data = ((i_palidx *)im->idata) + l + y * im->xsize;
    count = r - l;
    for (i = 0; i < count; ++i) {
      if (i_findcolor(im, vals+i, &which)) {
        ((i_palidx *)data)[i] = which;
      }
      else {
        if (i_img_to_rgb_inplace(im)) {
          return i+i_plin(im, l+i, r, y, vals+i);
        }
      }
    }
    return count;
  }
  else {
    return 0;
  }
}

/*
=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)

=cut
*/
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) {
  int ch;
  if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
    int palsize = PALEXT(im)->count;
    i_color *pal = PALEXT(im)->pal;
    i_palidx *data;
    i_img_dim count, i, w;
    if (r > im->xsize)
      r = im->xsize;
    data = ((i_palidx *)im->idata) + l + y * im->xsize;
    count = 0;
    w = r - l;
    if (chans) {
      for (ch = 0; ch < chan_count; ++ch) {
        if (chans[ch] < 0 || chans[ch] >= im->channels) {
	  dIMCTXim(im);
          im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
        }
      }

      for (i = 0; i < w; ++i) {
        i_palidx which = *data++;
        if (which < palsize) {
          for (ch = 0; ch < chan_count; ++ch) {
            *samps++ = pal[which].channel[chans[ch]];
            ++count;
          }
        }
      }
    }
    else {
      if (chan_count <= 0 || chan_count > im->channels) {
	dIMCTXim(im);
	im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels", 
		      chan_count);
	return 0;
      }
      for (i = 0; i < w; ++i) {
        i_palidx which = *data++;
        if (which < palsize) {
          for (ch = 0; ch < chan_count; ++ch) {
            *samps++ = pal[which].channel[ch];
            ++count;
          }
        }
      }
    }
    return count;
  }
  else {
    return 0;
  }
}

/*
=item i_gpal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals)

=cut
*/

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) {
  if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
    i_palidx *data;
    i_img_dim i, w;
    if (r > im->xsize)
      r = im->xsize;
    data = ((i_palidx *)im->idata) + l + y * im->xsize;
    w = r - l;
    for (i = 0; i < w; ++i) {
      *vals++ = *data++;
    }
    return i;
  }
  else {
    return 0;
  }
}

/*
=item i_ppal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals)

=cut
*/

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) {
  if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
    i_palidx *data;
    i_img_dim i, w;
    if (r > im->xsize)
      r = im->xsize;
    data = ((i_palidx *)im->idata) + l + y * im->xsize;
    w = r - l;
    for (i = 0; i < w; ++i) {
      *data++ = *vals++;
    }
    return i;
  }
  else {
    return 0;
  }
}

/*
=item i_addcolors_p(i_img *im, const i_color *color, int count)

=cut
*/
static int i_addcolors_p(i_img *im, const i_color *color, int count) {
  if (PALEXT(im)->count + count <= PALEXT(im)->alloc) {
    int result = PALEXT(im)->count;
    int index = result;

    PALEXT(im)->count += count;
    while (count) {
      PALEXT(im)->pal[index++] = *color++;
      --count;
    }

    return result;
  }
  else
    return -1;
}

/*
=item i_getcolors_p(i_img *im, int i, i_color *color, int count)

=cut
*/
static int i_getcolors_p(i_img *im, int i, i_color *color, int count) {
  if (i >= 0 && i+count <= PALEXT(im)->count) {
    while (count) {
      *color++ = PALEXT(im)->pal[i++];
      --count;
    }
    return 1;
  }
  else
    return 0;
}

static int color_eq(i_img *im, const i_color *c1, const i_color *c2) {
  int ch;
  for (ch = 0; ch < im->channels; ++ch) {
    if (c1->channel[ch] != c2->channel[ch])
      return 0;
  }
  return 1;
}

/*
=item i_colorcount_p(i_img *im)

=cut
*/
static int i_colorcount_p(i_img *im) {
  return PALEXT(im)->count;
}

/*
=item i_maxcolors_p(i_img *im)

=cut
*/
static int i_maxcolors_p(i_img *im) {
  return PALEXT(im)->alloc;
}

/*
=item i_setcolors_p(i_img *im, int index, const i_color *colors, int count)

=cut
*/
static int i_setcolors_p(i_img *im, int index, const i_color *colors, int count) {
  if (index >= 0 && count >= 1 && index + count <= PALEXT(im)->count) {
    while (count) {
      PALEXT(im)->pal[index++] = *colors++;
      --count;
    }
    return 1;
  }

  return 0;
}

/*
=item i_findcolor_p(i_img *im)

=cut
*/
static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry) {
  if (PALEXT(im)->count) {
    int i;
    /* often the same color comes up several times in a row */
    if (PALEXT(im)->last_found >= 0) {
      if (color_eq(im, color, PALEXT(im)->pal + PALEXT(im)->last_found)) {
        *entry = PALEXT(im)->last_found;
        return 1;
      }
    }
    for (i = 0; i < PALEXT(im)->count; ++i) {
      if (color_eq(im, color, PALEXT(im)->pal + i)) {
        PALEXT(im)->last_found = *entry = i;
        return 1;
      }
    }
  }
  return 0;
}

/*
=item i_psamp_p(im, l, r, y, samps, chans, chan_count)

Implement psamp() for paletted images.

Since writing samples doesn't really work as a concept for paletted
images, this is slow.

Also, writing samples may convert the image to a direct image in the
process, so use i_ppix/i_gpix instead of directly calling the paletted
handlers.

=cut
*/

static i_img_dim 
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) {
  if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
    i_img_dim count = 0;
    int ch;

    if (r > im->xsize)
      r = im->xsize;
      
    if (chans) {
      /* make sure we have good channel numbers */
      for (ch = 0; ch < chan_count; ++ch) {
        if (chans[ch] < 0 || chans[ch] >= im->channels) {
	  dIMCTXim(im);
          im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
          return -1;
        }
      }
      while (l < r) {
	i_color c;
	
	i_gpix(im, l, y, &c);
	for (ch = 0; ch < chan_count; ++ch)
	  c.channel[chans[ch]] = *samps++;
	i_ppix(im, l, y, &c);
	count += chan_count;
	++l;
      }
    }
    else {
      if (chan_count <= 0 || chan_count > im->channels) {
	dIMCTXim(im);
	im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels", 
		      chan_count);
	return -1;
      }

      while (l < r) {
	i_color c;
	
	i_gpix(im, l, y, &c);
	for (ch = 0; ch < chan_count; ++ch)
	  c.channel[ch] = *samps++;
	i_ppix(im, l, y, &c);
	count += chan_count;
	++l;
      }
    }

    return count;
  }
  else {
    dIMCTXim(im);
    i_push_error(0, "Image position outside of image");
    return -1;
  }
}

/*
=item i_psampf_p(im, l, r, y, samps, chans, chan_count)

Implement psampf() for paletted images.

Since writing samples doesn't really work as a concept for paletted
images, this is slow.

Also, writing samples may convert the image to a direct image in the
process, so use i_ppixf/i_gpixf instead of directly calling the paletted
handlers.

=cut
*/

static i_img_dim 
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) {
  if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
    i_img_dim count = 0;
    int ch;

    if (r > im->xsize)
      r = im->xsize;
      
    if (chans) {
      /* make sure we have good channel numbers */
      for (ch = 0; ch < chan_count; ++ch) {
        if (chans[ch] < 0 || chans[ch] >= im->channels) {
	  dIMCTXim(im);
          im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
          return -1;
        }
      }
      while (l < r) {
	i_fcolor c;
	
	i_gpixf(im, l, y, &c);
	for (ch = 0; ch < chan_count; ++ch)
	  c.channel[chans[ch]] = *samps++;
	i_ppixf(im, l, y, &c);
	count += chan_count;
	++l;
      }
    }
    else {
      if (chan_count <= 0 || chan_count > im->channels) {
	dIMCTXim(im);
	im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels", 
		      chan_count);
	return -1;
      }

      while (l < r) {
	i_fcolor c;
	
	i_gpixf(im, l, y, &c);
	for (ch = 0; ch < chan_count; ++ch)
	  c.channel[ch] = *samps++;
	i_ppixf(im, l, y, &c);
	count += chan_count;
	++l;
      }
    }

    return count;
  }
  else {
    dIMCTXim(im);
    i_push_error(0, "Image position outside of image");
    return -1;
  }
}

/*
=back

=head1 AUTHOR

Tony Cook <tony@develop-help.com>

=head1 SEE ALSO

Imager(3)

=cut
*/