]> git.imager.perl.org - imager.git/blob - palimg.c
we no longer try to deal with buggy giflibs
[imager.git] / palimg.c
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<x>, C<y>, C<channels>, C<maxpal>)
90 X<im_img_pal_new API>X<i_img_pal_new API>
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<maxpal> 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<i_img_pal_new(width, height, channels, max_palette_size)>.
102
103 =cut
104 */
105 i_img *
106 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   i_clear_error();
112   if (maxpal < 1 || maxpal > 256) {
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) {
121     im_push_errorf(aIMCTX, 0, "Channels must be positive and <= %d", MAXCHANNELS);
122     return NULL;
123   }
124   bytes = sizeof(i_palidx) * x * y;
125   if (bytes / y / sizeof(i_palidx) != x) {
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");
136     return NULL;
137   }
138
139   im = i_img_alloc();
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);
148   im->bytes = bytes;
149   im->idata = mymalloc(im->bytes);
150   im->channels = channels;
151   memset(im->idata, 0, im->bytes);
152   im->xsize = x;
153   im->ysize = y;
154
155   i_img_init(im);
156   
157   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 static void i_img_rgb_convert(i_img *targ, i_img *src) {
173   i_color *row = mymalloc(sizeof(i_color) * targ->xsize);
174   i_img_dim y;
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
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 i_img_to_rgb_inplace(i_img *im) {
195   i_img temp;
196   dIMCTXim(im);
197
198   if (im->virtual)
199     return 0;
200
201   if (im->type == i_direct_type)
202     return 1; /* trivial success */
203
204   i_img_empty_ch(&temp, im->xsize, im->ysize, im->channels);
205   i_img_rgb_convert(&temp, im);
206
207   /* nasty hack */
208   (im->i_f_destroy)(im);
209   myfree(im->idata);
210   *im = temp;
211
212   return 1;
213 }
214
215 /*
216 =item i_img_to_pal(i_img *im, i_quantize *quant)
217
218 Converts an RGB image to a paletted image
219
220 =cut
221 */
222 i_img *i_img_to_pal(i_img *src, i_quantize *quant) {
223   i_palidx *result;
224   i_img *im;
225   dIMCTXim(src);
226
227   i_clear_error();
228   
229   i_quant_makemap(quant, &src, 1);
230   result = i_quant_translate(quant, src);
231
232   if (result) {
233
234     im = i_img_pal_new(src->xsize, src->ysize, src->channels, quant->mc_size);
235
236     /* copy things over */
237     memcpy(im->idata, result, im->bytes);
238     PALEXT(im)->count = quant->mc_count;
239     memcpy(PALEXT(im)->pal, quant->mc_colors, sizeof(i_color) * quant->mc_count);
240     
241     myfree(result);
242
243     return im;
244   }
245   else {
246     return NULL;
247   }
248 }
249
250 /*
251 =item i_img_to_rgb(i_img *src)
252
253 =cut
254 */
255 i_img *
256 i_img_to_rgb(i_img *src) {
257   dIMCTXim(src);
258   i_img *im = i_img_empty_ch(NULL, src->xsize, src->ysize, src->channels);
259   i_img_rgb_convert(im, src);
260
261   return im;
262 }
263
264 /*
265 =item i_destroy_p(i_img *im)
266
267 Destroys data related to a paletted image.
268
269 =cut
270 */
271 static void i_destroy_p(i_img *im) {
272   if (im) {
273     i_img_pal_ext *palext = im->ext_data;
274     if (palext) {
275       if (palext->pal)
276         myfree(palext->pal);
277       myfree(palext);
278     }
279   }
280 }
281
282 /*
283 =item i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val)
284
285 Write to a pixel in the image.
286
287 Warning: converts the image to a RGB image if the color isn't already
288 present in the image.
289
290 =cut
291 */
292 static int 
293 i_ppix_p(i_img *im, i_img_dim x, i_img_dim y, const i_color *val) {
294   const i_color *work_val = val;
295   i_color workc;
296   i_palidx which;
297   const unsigned all_mask = ( 1 << im->channels ) - 1;
298
299   if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
300     return -1;
301
302   if ((im->ch_mask & all_mask) != all_mask) {
303     unsigned mask = 1;
304     int ch;
305     i_gpix(im, x, y, &workc);
306     for (ch = 0; ch < im->channels; ++ch) {
307       if (im->ch_mask & mask)
308         workc.channel[ch] = val->channel[ch];
309       mask <<= 1;
310     }
311     work_val = &workc;
312   }
313
314   if (i_findcolor(im, work_val, &which)) {
315     ((i_palidx *)im->idata)[x + y * im->xsize] = which;
316     return 0;
317   }
318   else {
319     dIMCTXim(im);
320     im_log((aIMCTX, 1, "i_ppix: color(%d,%d,%d) not found, converting to rgb\n",
321             val->channel[0], val->channel[1], val->channel[2]));
322     if (i_img_to_rgb_inplace(im)) {
323       return i_ppix(im, x, y, val);
324     }
325     else
326       return -1;
327   }
328 }
329
330 /*
331 =item i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val)
332
333 Retrieve a pixel, converting from a palette index to a color.
334
335 =cut
336 */
337 static int i_gpix_p(i_img *im, i_img_dim x, i_img_dim y, i_color *val) {
338   i_palidx which;
339   if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
340     return -1;
341   }
342   which = ((i_palidx *)im->idata)[x + y * im->xsize];
343   if (which > PALEXT(im)->count)
344     return -1;
345   *val = PALEXT(im)->pal[which];
346
347   return 0;
348 }
349
350 /*
351 =item i_glinp(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_color *vals)
352
353 Retrieve a row of pixels.
354
355 =cut
356 */
357 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) {
358   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
359     int palsize = PALEXT(im)->count;
360     i_color *pal = PALEXT(im)->pal;
361     i_palidx *data;
362     i_img_dim count, i;
363     if (r > im->xsize)
364       r = im->xsize;
365     data = ((i_palidx *)im->idata) + l + y * im->xsize;
366     count = r - l;
367     for (i = 0; i < count; ++i) {
368       i_palidx which = *data++;
369       if (which < palsize)
370         vals[i] = pal[which];
371     }
372     return count;
373   }
374   else {
375     return 0;
376   }
377 }
378
379 /*
380 =item i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals)
381
382 Write a line of color data to the image.
383
384 If any color value is not in the image when the image is converted to 
385 RGB.
386
387 =cut
388 */
389 static i_img_dim 
390 i_plin_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_color *vals) {
391   i_img_dim count, i;
392   i_palidx *data;
393   i_palidx which;
394   if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
395     if (r > im->xsize)
396       r = im->xsize;
397     data = ((i_palidx *)im->idata) + l + y * im->xsize;
398     count = r - l;
399     for (i = 0; i < count; ++i) {
400       if (i_findcolor(im, vals+i, &which)) {
401         ((i_palidx *)data)[i] = which;
402       }
403       else {
404         if (i_img_to_rgb_inplace(im)) {
405           return i+i_plin(im, l+i, r, y, vals+i);
406         }
407       }
408     }
409     return count;
410   }
411   else {
412     return 0;
413   }
414 }
415
416 /*
417 =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)
418
419 =cut
420 */
421 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, 
422               int const *chans, int chan_count) {
423   int ch;
424   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
425     int palsize = PALEXT(im)->count;
426     i_color *pal = PALEXT(im)->pal;
427     i_palidx *data;
428     i_img_dim count, i, w;
429     if (r > im->xsize)
430       r = im->xsize;
431     data = ((i_palidx *)im->idata) + l + y * im->xsize;
432     count = 0;
433     w = r - l;
434     if (chans) {
435       for (ch = 0; ch < chan_count; ++ch) {
436         if (chans[ch] < 0 || chans[ch] >= im->channels) {
437           dIMCTXim(im);
438           im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
439         }
440       }
441
442       for (i = 0; i < w; ++i) {
443         i_palidx which = *data++;
444         if (which < palsize) {
445           for (ch = 0; ch < chan_count; ++ch) {
446             *samps++ = pal[which].channel[chans[ch]];
447             ++count;
448           }
449         }
450       }
451     }
452     else {
453       if (chan_count <= 0 || chan_count > im->channels) {
454         dIMCTXim(im);
455         im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels", 
456                       chan_count);
457         return 0;
458       }
459       for (i = 0; i < w; ++i) {
460         i_palidx which = *data++;
461         if (which < palsize) {
462           for (ch = 0; ch < chan_count; ++ch) {
463             *samps++ = pal[which].channel[ch];
464             ++count;
465           }
466         }
467       }
468     }
469     return count;
470   }
471   else {
472     return 0;
473   }
474 }
475
476 /*
477 =item i_gpal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, i_palidx *vals)
478
479 =cut
480 */
481
482 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) {
483   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
484     i_palidx *data;
485     i_img_dim i, w;
486     if (r > im->xsize)
487       r = im->xsize;
488     data = ((i_palidx *)im->idata) + l + y * im->xsize;
489     w = r - l;
490     for (i = 0; i < w; ++i) {
491       *vals++ = *data++;
492     }
493     return i;
494   }
495   else {
496     return 0;
497   }
498 }
499
500 /*
501 =item i_ppal_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y, const i_palidx *vals)
502
503 =cut
504 */
505
506 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) {
507   if (y >= 0 && y < im->ysize && l < im->xsize && l >= 0) {
508     i_palidx *data;
509     i_img_dim i, w;
510     if (r > im->xsize)
511       r = im->xsize;
512     data = ((i_palidx *)im->idata) + l + y * im->xsize;
513     w = r - l;
514     for (i = 0; i < w; ++i) {
515       *data++ = *vals++;
516     }
517     return i;
518   }
519   else {
520     return 0;
521   }
522 }
523
524 /*
525 =item i_addcolors_p(i_img *im, const i_color *color, int count)
526
527 =cut
528 */
529 static int i_addcolors_p(i_img *im, const i_color *color, int count) {
530   if (PALEXT(im)->count + count <= PALEXT(im)->alloc) {
531     int result = PALEXT(im)->count;
532     int index = result;
533
534     PALEXT(im)->count += count;
535     while (count) {
536       PALEXT(im)->pal[index++] = *color++;
537       --count;
538     }
539
540     return result;
541   }
542   else
543     return -1;
544 }
545
546 /*
547 =item i_getcolors_p(i_img *im, int i, i_color *color, int count)
548
549 =cut
550 */
551 static int i_getcolors_p(i_img *im, int i, i_color *color, int count) {
552   if (i >= 0 && i+count <= PALEXT(im)->count) {
553     while (count) {
554       *color++ = PALEXT(im)->pal[i++];
555       --count;
556     }
557     return 1;
558   }
559   else
560     return 0;
561 }
562
563 static int color_eq(i_img *im, const i_color *c1, const i_color *c2) {
564   int ch;
565   for (ch = 0; ch < im->channels; ++ch) {
566     if (c1->channel[ch] != c2->channel[ch])
567       return 0;
568   }
569   return 1;
570 }
571
572 /*
573 =item i_colorcount_p(i_img *im)
574
575 =cut
576 */
577 static int i_colorcount_p(i_img *im) {
578   return PALEXT(im)->count;
579 }
580
581 /*
582 =item i_maxcolors_p(i_img *im)
583
584 =cut
585 */
586 static int i_maxcolors_p(i_img *im) {
587   return PALEXT(im)->alloc;
588 }
589
590 /*
591 =item i_setcolors_p(i_img *im, int index, const i_color *colors, int count)
592
593 =cut
594 */
595 static int i_setcolors_p(i_img *im, int index, const i_color *colors, int count) {
596   if (index >= 0 && count >= 1 && index + count <= PALEXT(im)->count) {
597     while (count) {
598       PALEXT(im)->pal[index++] = *colors++;
599       --count;
600     }
601     return 1;
602   }
603
604   return 0;
605 }
606
607 /*
608 =item i_findcolor_p(i_img *im)
609
610 =cut
611 */
612 static int i_findcolor_p(i_img *im, const i_color *color, i_palidx *entry) {
613   if (PALEXT(im)->count) {
614     int i;
615     /* often the same color comes up several times in a row */
616     if (PALEXT(im)->last_found >= 0) {
617       if (color_eq(im, color, PALEXT(im)->pal + PALEXT(im)->last_found)) {
618         *entry = PALEXT(im)->last_found;
619         return 1;
620       }
621     }
622     for (i = 0; i < PALEXT(im)->count; ++i) {
623       if (color_eq(im, color, PALEXT(im)->pal + i)) {
624         PALEXT(im)->last_found = *entry = i;
625         return 1;
626       }
627     }
628   }
629   return 0;
630 }
631
632 /*
633 =item i_psamp_p(im, l, r, y, samps, chans, chan_count)
634
635 Implement psamp() for paletted images.
636
637 Since writing samples doesn't really work as a concept for paletted
638 images, this is slow.
639
640 Also, writing samples may convert the image to a direct image in the
641 process, so use i_ppix/i_gpix instead of directly calling the paletted
642 handlers.
643
644 =cut
645 */
646
647 static i_img_dim 
648 i_psamp_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y,
649           const i_sample_t *samps, const int *chans, int chan_count) {
650   if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
651     i_img_dim count = 0;
652     int ch;
653
654     if (r > im->xsize)
655       r = im->xsize;
656       
657     if (chans) {
658       /* make sure we have good channel numbers */
659       for (ch = 0; ch < chan_count; ++ch) {
660         if (chans[ch] < 0 || chans[ch] >= im->channels) {
661           dIMCTXim(im);
662           im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
663           return -1;
664         }
665       }
666       while (l < r) {
667         i_color c;
668         
669         i_gpix(im, l, y, &c);
670         for (ch = 0; ch < chan_count; ++ch)
671           c.channel[chans[ch]] = *samps++;
672         i_ppix(im, l, y, &c);
673         count += chan_count;
674         ++l;
675       }
676     }
677     else {
678       if (chan_count <= 0 || chan_count > im->channels) {
679         dIMCTXim(im);
680         im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels", 
681                       chan_count);
682         return -1;
683       }
684
685       while (l < r) {
686         i_color c;
687         
688         i_gpix(im, l, y, &c);
689         for (ch = 0; ch < chan_count; ++ch)
690           c.channel[ch] = *samps++;
691         i_ppix(im, l, y, &c);
692         count += chan_count;
693         ++l;
694       }
695     }
696
697     return count;
698   }
699   else {
700     dIMCTXim(im);
701     i_push_error(0, "Image position outside of image");
702     return -1;
703   }
704 }
705
706 /*
707 =item i_psampf_p(im, l, r, y, samps, chans, chan_count)
708
709 Implement psampf() for paletted images.
710
711 Since writing samples doesn't really work as a concept for paletted
712 images, this is slow.
713
714 Also, writing samples may convert the image to a direct image in the
715 process, so use i_ppixf/i_gpixf instead of directly calling the paletted
716 handlers.
717
718 =cut
719 */
720
721 static i_img_dim 
722 i_psampf_p(i_img *im, i_img_dim l, i_img_dim r, i_img_dim y,
723           const i_fsample_t *samps, const int *chans, int chan_count) {
724   if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
725     i_img_dim count = 0;
726     int ch;
727
728     if (r > im->xsize)
729       r = im->xsize;
730       
731     if (chans) {
732       /* make sure we have good channel numbers */
733       for (ch = 0; ch < chan_count; ++ch) {
734         if (chans[ch] < 0 || chans[ch] >= im->channels) {
735           dIMCTXim(im);
736           im_push_errorf(aIMCTX, 0, "No channel %d in this image", chans[ch]);
737           return -1;
738         }
739       }
740       while (l < r) {
741         i_fcolor c;
742         
743         i_gpixf(im, l, y, &c);
744         for (ch = 0; ch < chan_count; ++ch)
745           c.channel[chans[ch]] = *samps++;
746         i_ppixf(im, l, y, &c);
747         count += chan_count;
748         ++l;
749       }
750     }
751     else {
752       if (chan_count <= 0 || chan_count > im->channels) {
753         dIMCTXim(im);
754         im_push_errorf(aIMCTX, 0, "chan_count %d out of range, must be >0, <= channels", 
755                       chan_count);
756         return -1;
757       }
758
759       while (l < r) {
760         i_fcolor c;
761         
762         i_gpixf(im, l, y, &c);
763         for (ch = 0; ch < chan_count; ++ch)
764           c.channel[ch] = *samps++;
765         i_ppixf(im, l, y, &c);
766         count += chan_count;
767         ++l;
768       }
769     }
770
771     return count;
772   }
773   else {
774     dIMCTXim(im);
775     i_push_error(0, "Image position outside of image");
776     return -1;
777   }
778 }
779
780 /*
781 =back
782
783 =head1 AUTHOR
784
785 Tony Cook <tony@develop-help.com>
786
787 =head1 SEE ALSO
788
789 Imager(3)
790
791 =cut
792 */