]> git.imager.perl.org - imager.git/blob - Imager.xs
PNG re-work: read 16-bit PNG as 16-bit
[imager.git] / Imager.xs
1 #define PERL_NO_GET_CONTEXT
2 #ifdef __cplusplus
3 extern "C" {
4 #endif
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8 #define NEED_newRV_noinc
9 #define NEED_sv_2pv_nolen
10 #define NEED_sv_2pvbyte
11 #include "ppport.h"
12 #ifdef __cplusplus
13 }
14 #endif
15
16 #define i_int_hlines_testing() 1
17
18 #include "imager.h"
19 #include "feat.h"
20 #include "dynaload.h"
21 #include "regmach.h"
22 #include "imextdef.h"
23 #include "imextpltypes.h"
24 #include <float.h>
25
26 #if i_int_hlines_testing()
27 #include "imageri.h"
28 #endif
29
30 #include "imperl.h"
31
32 /* used to represent channel lists parameters */
33 typedef struct i_channel_list_tag {
34   int *channels;
35   int count;
36 } i_channel_list;
37
38 typedef struct {
39   size_t count;
40   const i_sample_t *samples;
41 } i_sample_list;
42
43 typedef struct {
44   size_t count;
45   const i_fsample_t *samples;
46 } i_fsample_list;
47
48 /*
49
50 Allocate memory that will be discarded when mortals are discarded.
51
52 */
53
54 static void *
55 malloc_temp(pTHX_ size_t size) {
56   SV *sv = sv_2mortal(newSV(size));
57
58   return SvPVX(sv);
59 }
60
61 /* These functions are all shared - then comes platform dependant code */
62 static int getstr(void *hv_t,char *key,char **store) {
63   dTHX;
64   SV** svpp;
65   HV* hv=(HV*)hv_t;
66
67   mm_log((1,"getstr(hv_t %p, key %s, store %p)\n",hv_t,key,store));
68
69   if ( !hv_exists(hv,key,strlen(key)) ) return 0;
70
71   svpp=hv_fetch(hv, key, strlen(key), 0);
72   *store=SvPV(*svpp, PL_na );
73
74   return 1;
75 }
76
77 static int getint(void *hv_t,char *key,int *store) {
78   dTHX;
79   SV** svpp;
80   HV* hv=(HV*)hv_t;  
81
82   mm_log((1,"getint(hv_t %p, key %s, store %p)\n",hv_t,key,store));
83
84   if ( !hv_exists(hv,key,strlen(key)) ) return 0;
85
86   svpp=hv_fetch(hv, key, strlen(key), 0);
87   *store=(int)SvIV(*svpp);
88   return 1;
89 }
90
91 static int getdouble(void *hv_t,char* key,double *store) {
92   dTHX;
93   SV** svpp;
94   HV* hv=(HV*)hv_t;
95
96   mm_log((1,"getdouble(hv_t %p, key %s, store %p)\n",hv_t,key,store));
97
98   if ( !hv_exists(hv,key,strlen(key)) ) return 0;
99   svpp=hv_fetch(hv, key, strlen(key), 0);
100   *store=(double)SvNV(*svpp);
101   return 1;
102 }
103
104 static int getvoid(void *hv_t,char* key,void **store) {
105   dTHX;
106   SV** svpp;
107   HV* hv=(HV*)hv_t;
108
109   mm_log((1,"getvoid(hv_t %p, key %s, store %p)\n",hv_t,key,store));
110
111   if ( !hv_exists(hv,key,strlen(key)) ) return 0;
112
113   svpp=hv_fetch(hv, key, strlen(key), 0);
114   *store = INT2PTR(void*, SvIV(*svpp));
115
116   return 1;
117 }
118
119 static int getobj(void *hv_t,char *key,char *type,void **store) {
120   dTHX;
121   SV** svpp;
122   HV* hv=(HV*)hv_t;
123
124   mm_log((1,"getobj(hv_t %p, key %s,type %s, store %p)\n",hv_t,key,type,store));
125
126   if ( !hv_exists(hv,key,strlen(key)) ) return 0;
127
128   svpp=hv_fetch(hv, key, strlen(key), 0);
129
130   if (sv_derived_from(*svpp,type)) {
131     IV tmp = SvIV((SV*)SvRV(*svpp));
132     *store = INT2PTR(void*, tmp);
133   } else {
134     mm_log((1,"getobj: key exists in hash but is not of correct type"));
135     return 0;
136   }
137
138   return 1;
139 }
140
141 UTIL_table_t i_UTIL_table={getstr,getint,getdouble,getvoid,getobj};
142
143 void my_SvREFCNT_dec(void *p) {
144   dTHX;
145   SvREFCNT_dec((SV*)p);
146 }
147
148
149 static void
150 i_log_entry(char *string, int level) {
151   mm_log((level, "%s", string));
152 }
153
154 static SV *
155 make_i_color_sv(pTHX_ const i_color *c) {
156   SV *sv;
157   i_color *col = mymalloc(sizeof(i_color));
158   *col = *c;
159   sv = sv_newmortal();
160   sv_setref_pv(sv, "Imager::Color", (void *)col);
161
162   return sv;
163 }
164
165 #define CBDATA_BUFSIZE 8192
166
167 struct cbdata {
168   /* the SVs we use to call back to Perl */
169   SV *writecb;
170   SV *readcb;
171   SV *seekcb;
172   SV *closecb;
173 };
174
175 static ssize_t
176 call_reader(struct cbdata *cbd, void *buf, size_t size, 
177             size_t maxread) {
178   dTHX;
179   int count;
180   int result;
181   SV *data;
182   dSP;
183
184   if (!SvOK(cbd->readcb)) {
185     mm_log((1, "read callback called but no readcb supplied\n"));
186     i_push_error(0, "read callback called but no readcb supplied");
187     return -1;
188   }
189
190   ENTER;
191   SAVETMPS;
192   EXTEND(SP, 2);
193   PUSHMARK(SP);
194   PUSHs(sv_2mortal(newSViv(size)));
195   PUSHs(sv_2mortal(newSViv(maxread)));
196   PUTBACK;
197
198   count = perl_call_sv(cbd->readcb, G_SCALAR);
199
200   SPAGAIN;
201
202   if (count != 1)
203     croak("Result of perl_call_sv(..., G_SCALAR) != 1");
204
205   data = POPs;
206
207   if (SvOK(data)) {
208     STRLEN len;
209     char *ptr = SvPVbyte(data, len);
210     if (len > maxread)
211       croak("Too much data returned in reader callback (wanted %d, got %d, expected %d)",
212       (int)size, (int)len, (int)maxread);
213     
214     memcpy(buf, ptr, len);
215     result = len;
216   }
217   else {
218     result = -1;
219   }
220
221   PUTBACK;
222   FREETMPS;
223   LEAVE;
224
225   return result;
226 }
227
228 static off_t
229 io_seeker(void *p, off_t offset, int whence) {
230   dTHX;
231   struct cbdata *cbd = p;
232   int count;
233   off_t result;
234   dSP;
235
236   if (!SvOK(cbd->seekcb)) {
237     mm_log((1, "seek callback called but no seekcb supplied\n"));
238     i_push_error(0, "seek callback called but no seekcb supplied");
239     return -1;
240   }
241
242   ENTER;
243   SAVETMPS;
244   EXTEND(SP, 2);
245   PUSHMARK(SP);
246   PUSHs(sv_2mortal(newSViv(offset)));
247   PUSHs(sv_2mortal(newSViv(whence)));
248   PUTBACK;
249
250   count = perl_call_sv(cbd->seekcb, G_SCALAR);
251
252   SPAGAIN;
253
254   if (count != 1)
255     croak("Result of perl_call_sv(..., G_SCALAR) != 1");
256
257   result = POPi;
258
259   PUTBACK;
260   FREETMPS;
261   LEAVE;
262
263   return result;
264 }
265
266 static ssize_t
267 io_writer(void *p, void const *data, size_t size) {
268   dTHX;
269   struct cbdata *cbd = p;
270   I32 count;
271   SV *sv;
272   dSP;
273   bool success;
274
275   if (!SvOK(cbd->writecb)) {
276     mm_log((1, "write callback called but no writecb supplied\n"));
277     i_push_error(0, "write callback called but no writecb supplied");
278     return -1;
279   }
280
281   ENTER;
282   SAVETMPS;
283   EXTEND(SP, 1);
284   PUSHMARK(SP);
285   PUSHs(sv_2mortal(newSVpv((char *)data, size)));
286   PUTBACK;
287
288   count = perl_call_sv(cbd->writecb, G_SCALAR);
289
290   SPAGAIN;
291   if (count != 1)
292     croak("Result of perl_call_sv(..., G_SCALAR) != 1");
293
294   sv = POPs;
295   success = SvTRUE(sv);
296
297
298   PUTBACK;
299   FREETMPS;
300   LEAVE;
301
302   return success ? size : -1;
303 }
304
305 static ssize_t 
306 io_reader(void *p, void *data, size_t size) {
307   struct cbdata *cbd = p;
308
309   return call_reader(cbd, data, size, size);
310 }
311
312 static int io_closer(void *p) {
313   dTHX;
314   struct cbdata *cbd = p;
315   int success = 1;
316
317   if (SvOK(cbd->closecb)) {
318     dSP;
319     I32 count;
320     SV *sv;
321
322     ENTER;
323     SAVETMPS;
324     PUSHMARK(SP);
325     PUTBACK;
326
327     count = perl_call_sv(cbd->closecb, G_SCALAR);
328
329     SPAGAIN;
330     
331     sv = POPs;
332     success = SvTRUE(sv);
333
334     PUTBACK;
335     FREETMPS;
336     LEAVE;
337   }
338
339   return success ? 0 : -1;
340 }
341
342 static void io_destroyer(void *p) {
343   dTHX;
344   struct cbdata *cbd = p;
345
346   SvREFCNT_dec(cbd->writecb);
347   SvREFCNT_dec(cbd->readcb);
348   SvREFCNT_dec(cbd->seekcb);
349   SvREFCNT_dec(cbd->closecb);
350   myfree(cbd);
351 }
352
353 static i_io_glue_t *
354 do_io_new_buffer(pTHX_ SV *data_sv) {
355   const char *data;
356   STRLEN length;
357
358   data = SvPVbyte(data_sv, length);
359   SvREFCNT_inc(data_sv);
360   return io_new_buffer(data, length, my_SvREFCNT_dec, data_sv);
361 }
362
363 static const char *
364 describe_sv(SV *sv) {
365   if (SvOK(sv)) {
366     if (SvROK(sv)) {
367       svtype type = SvTYPE(SvRV(sv));
368       switch (type) {
369       case SVt_PVCV: return "CV";
370       case SVt_PVGV: return "GV";
371       case SVt_PVLV: return "LV";
372       default: return "some reference";
373       }
374     }
375     else {
376       return "non-reference scalar";
377     }
378   }
379   else {
380     return "undef";
381   }
382 }
383
384 static i_io_glue_t *
385 do_io_new_cb(pTHX_ SV *writecb, SV *readcb, SV *seekcb, SV *closecb) {
386   struct cbdata *cbd;
387
388   cbd = mymalloc(sizeof(struct cbdata));
389   cbd->writecb = newSVsv(writecb);
390   cbd->readcb = newSVsv(readcb);
391   cbd->seekcb = newSVsv(seekcb);
392   cbd->closecb = newSVsv(closecb);
393
394   mm_log((1, "do_io_new_cb(writecb %p (%s), readcb %p (%s), seekcb %p (%s), closecb %p (%s))\n", writecb, describe_sv(writecb), readcb, describe_sv(readcb), seekcb, describe_sv(seekcb), closecb, describe_sv(closecb)));
395
396   return io_new_cb(cbd, io_reader, io_writer, io_seeker, io_closer, 
397                    io_destroyer);
398 }
399
400 struct value_name {
401   char *name;
402   int value;
403 };
404 static int lookup_name(struct value_name *names, int count, char *name, int def_value)
405 {
406   int i;
407   for (i = 0; i < count; ++i)
408     if (strEQ(names[i].name, name))
409       return names[i].value;
410
411   return def_value;
412 }
413 static struct value_name transp_names[] =
414 {
415   { "none", tr_none },
416   { "threshold", tr_threshold },
417   { "errdiff", tr_errdiff },
418   { "ordered", tr_ordered, },
419 };
420
421 static struct value_name make_color_names[] =
422 {
423   { "none", mc_none, },
424   { "webmap", mc_web_map, },
425   { "addi", mc_addi, },
426   { "mediancut", mc_median_cut, },
427   { "mono", mc_mono, },
428   { "monochrome", mc_mono, },
429   { "gray", mc_gray, },
430   { "gray4", mc_gray4, },
431   { "gray16", mc_gray16, },
432 };
433
434 static struct value_name translate_names[] =
435 {
436   { "giflib", pt_giflib, },
437   { "closest", pt_closest, },
438   { "perturb", pt_perturb, },
439   { "errdiff", pt_errdiff, },
440 };
441
442 static struct value_name errdiff_names[] =
443 {
444   { "floyd", ed_floyd, },
445   { "jarvis", ed_jarvis, },
446   { "stucki", ed_stucki, },
447   { "custom", ed_custom, },
448 };
449
450 static struct value_name orddith_names[] =
451 {
452   { "random", od_random, },
453   { "dot8", od_dot8, },
454   { "dot4", od_dot4, },
455   { "hline", od_hline, },
456   { "vline", od_vline, },
457   { "/line", od_slashline, },
458   { "slashline", od_slashline, },
459   { "\\line", od_backline, },
460   { "backline", od_backline, },
461   { "tiny", od_tiny, },
462   { "custom", od_custom, },
463 };
464
465 /* look through the hash for quantization options */
466 static void
467 ip_handle_quant_opts(pTHX_ i_quantize *quant, HV *hv)
468 {
469   /*** POSSIBLY BROKEN: do I need to unref the SV from hv_fetch ***/
470   SV **sv;
471   int i;
472   STRLEN len;
473   char *str;
474
475   quant->mc_colors = mymalloc(quant->mc_size * sizeof(i_color));
476
477   sv = hv_fetch(hv, "transp", 6, 0);
478   if (sv && *sv && (str = SvPV(*sv, len))) {
479     quant->transp = 
480       lookup_name(transp_names, sizeof(transp_names)/sizeof(*transp_names), 
481                   str, tr_none);
482     if (quant->transp != tr_none) {
483       quant->tr_threshold = 127;
484       sv = hv_fetch(hv, "tr_threshold", 12, 0);
485       if (sv && *sv)
486         quant->tr_threshold = SvIV(*sv);
487     }
488     if (quant->transp == tr_errdiff) {
489       sv = hv_fetch(hv, "tr_errdiff", 10, 0);
490       if (sv && *sv && (str = SvPV(*sv, len)))
491         quant->tr_errdiff = lookup_name(errdiff_names, sizeof(errdiff_names)/sizeof(*errdiff_names), str, ed_floyd);
492     }
493     if (quant->transp == tr_ordered) {
494       quant->tr_orddith = od_tiny;
495       sv = hv_fetch(hv, "tr_orddith", 10, 0);
496       if (sv && *sv && (str = SvPV(*sv, len)))
497         quant->tr_orddith = lookup_name(orddith_names, sizeof(orddith_names)/sizeof(*orddith_names), str, od_random);
498
499       if (quant->tr_orddith == od_custom) {
500         sv = hv_fetch(hv, "tr_map", 6, 0);
501         if (sv && *sv && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
502           AV *av = (AV*)SvRV(*sv);
503           len = av_len(av) + 1;
504           if (len > sizeof(quant->tr_custom))
505             len = sizeof(quant->tr_custom);
506           for (i = 0; i < len; ++i) {
507             SV **sv2 = av_fetch(av, i, 0);
508             if (sv2 && *sv2) {
509               quant->tr_custom[i] = SvIV(*sv2);
510             }
511           }
512           while (i < sizeof(quant->tr_custom))
513             quant->tr_custom[i++] = 0;
514         }
515       }
516     }
517   }
518   quant->make_colors = mc_median_cut;
519   sv = hv_fetch(hv, "make_colors", 11, 0);
520   if (sv && *sv && (str = SvPV(*sv, len))) {
521     quant->make_colors = 
522       lookup_name(make_color_names, sizeof(make_color_names)/sizeof(*make_color_names), str, mc_median_cut);
523   }
524   sv = hv_fetch(hv, "colors", 6, 0);
525   if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
526     /* needs to be an array of Imager::Color
527        note that the caller allocates the mc_color array and sets mc_size
528        to it's size */
529     AV *av = (AV *)SvRV(*sv);
530     quant->mc_count = av_len(av)+1;
531     if (quant->mc_count > quant->mc_size)
532       quant->mc_count = quant->mc_size;
533     for (i = 0; i < quant->mc_count; ++i) {
534       SV **sv1 = av_fetch(av, i, 0);
535       if (sv1 && *sv1 && SvROK(*sv1) && sv_derived_from(*sv1, "Imager::Color")) {
536         i_color *col = INT2PTR(i_color *, SvIV((SV*)SvRV(*sv1)));
537         quant->mc_colors[i] = *col;
538       }
539     }
540   }
541   sv = hv_fetch(hv, "max_colors", 10, 0);
542   if (sv && *sv) {
543     i = SvIV(*sv);
544     if (i <= quant->mc_size && i >= quant->mc_count)
545       quant->mc_size = i;
546   }
547
548   quant->translate = pt_closest;
549   sv = hv_fetch(hv, "translate", 9, 0);
550   if (sv && *sv && (str = SvPV(*sv, len))) {
551     quant->translate = lookup_name(translate_names, sizeof(translate_names)/sizeof(*translate_names), str, pt_closest);
552   }
553   sv = hv_fetch(hv, "errdiff", 7, 0);
554   if (sv && *sv && (str = SvPV(*sv, len))) {
555     quant->errdiff = lookup_name(errdiff_names, sizeof(errdiff_names)/sizeof(*errdiff_names), str, ed_floyd);
556   }
557   if (quant->translate == pt_errdiff && quant->errdiff == ed_custom) {
558     /* get the error diffusion map */
559     sv = hv_fetch(hv, "errdiff_width", 13, 0);
560     if (sv && *sv)
561       quant->ed_width = SvIV(*sv);
562     sv = hv_fetch(hv, "errdiff_height", 14, 0);
563     if (sv && *sv)
564       quant->ed_height = SvIV(*sv);
565     sv = hv_fetch(hv, "errdiff_orig", 12, 0);
566     if (sv && *sv)
567       quant->ed_orig = SvIV(*sv);
568     if (quant->ed_width > 0 && quant->ed_height > 0) {
569       int sum = 0;
570       quant->ed_map = mymalloc(sizeof(int)*quant->ed_width*quant->ed_height);
571       sv = hv_fetch(hv, "errdiff_map", 11, 0);
572       if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
573         AV *av = (AV*)SvRV(*sv);
574         len = av_len(av) + 1;
575         if (len > quant->ed_width * quant->ed_height)
576           len = quant->ed_width * quant->ed_height;
577         for (i = 0; i < len; ++i) {
578           SV **sv2 = av_fetch(av, i, 0);
579           if (sv2 && *sv2) {
580             quant->ed_map[i] = SvIV(*sv2);
581             sum += quant->ed_map[i];
582           }
583         }
584       }
585       if (!sum) {
586         /* broken map */
587         myfree(quant->ed_map);
588         quant->ed_map = 0;
589         quant->errdiff = ed_floyd;
590       }
591     }
592   }
593   sv = hv_fetch(hv, "perturb", 7, 0);
594   if (sv && *sv)
595     quant->perturb = SvIV(*sv);
596 }
597
598 static void
599 ip_cleanup_quant_opts(pTHX_ i_quantize *quant) {
600   myfree(quant->mc_colors);
601   if (quant->ed_map)
602     myfree(quant->ed_map);
603 }
604
605 /* copies the color map from the hv into the colors member of the HV */
606 static void
607 ip_copy_colors_back(pTHX_ HV *hv, i_quantize *quant) {
608   SV **sv;
609   AV *av;
610   int i;
611   SV *work;
612
613   sv = hv_fetch(hv, "colors", 6, 0);
614   if (!sv || !*sv || !SvROK(*sv) || SvTYPE(SvRV(*sv)) != SVt_PVAV) {
615     /* nothing to do */
616     return;
617   }
618
619   av = (AV *)SvRV(*sv);
620   av_clear(av);
621   av_extend(av, quant->mc_count+1);
622   for (i = 0; i < quant->mc_count; ++i) {
623     i_color *in = quant->mc_colors+i;
624     Imager__Color c = ICL_new_internal(in->rgb.r, in->rgb.g, in->rgb.b, 255);
625     work = sv_newmortal();
626     sv_setref_pv(work, "Imager::Color", (void *)c);
627     SvREFCNT_inc(work);
628     av_push(av, work);
629   }
630 }
631
632 /* loads the segments of a fountain fill into an array */
633 static i_fountain_seg *
634 load_fount_segs(pTHX_ AV *asegs, int *count) {
635   /* Each element of segs must contain:
636      [ start, middle, end, c0, c1, segtype, colortrans ]
637      start, middle, end are doubles from 0 to 1
638      c0, c1 are Imager::Color::Float or Imager::Color objects
639      segtype, colortrans are ints
640   */
641   int i, j;
642   AV *aseg;
643   i_fountain_seg *segs;
644   double work[3];
645   int worki[2];
646
647   *count = av_len(asegs)+1;
648   if (*count < 1) 
649     croak("i_fountain must have at least one segment");
650   segs = mymalloc(sizeof(i_fountain_seg) * *count);
651   for(i = 0; i < *count; i++) {
652     SV **sv1 = av_fetch(asegs, i, 0);
653     if (!sv1 || !*sv1 || !SvROK(*sv1) 
654         || SvTYPE(SvRV(*sv1)) != SVt_PVAV) {
655       myfree(segs);
656       croak("i_fountain: segs must be an arrayref of arrayrefs");
657     }
658     aseg = (AV *)SvRV(*sv1);
659     if (av_len(aseg) != 7-1) {
660       myfree(segs);
661       croak("i_fountain: a segment must have 7 members");
662     }
663     for (j = 0; j < 3; ++j) {
664       SV **sv2 = av_fetch(aseg, j, 0);
665       if (!sv2 || !*sv2) {
666         myfree(segs);
667         croak("i_fountain: XS error");
668       }
669       work[j] = SvNV(*sv2);
670     }
671     segs[i].start  = work[0];
672     segs[i].middle = work[1];
673     segs[i].end    = work[2];
674     for (j = 0; j < 2; ++j) {
675       SV **sv3 = av_fetch(aseg, 3+j, 0);
676       if (!sv3 || !*sv3 || !SvROK(*sv3) ||
677           (!sv_derived_from(*sv3, "Imager::Color")
678            && !sv_derived_from(*sv3, "Imager::Color::Float"))) {
679         myfree(segs);
680         croak("i_fountain: segs must contain colors in elements 3 and 4");
681       }
682       if (sv_derived_from(*sv3, "Imager::Color::Float")) {
683         segs[i].c[j] = *INT2PTR(i_fcolor *, SvIV((SV *)SvRV(*sv3)));
684       }
685       else {
686         i_color c = *INT2PTR(i_color *, SvIV((SV *)SvRV(*sv3)));
687         int ch;
688         for (ch = 0; ch < MAXCHANNELS; ++ch) {
689           segs[i].c[j].channel[ch] = c.channel[ch] / 255.0;
690         }
691       }
692     }
693     for (j = 0; j < 2; ++j) {
694       SV **sv2 = av_fetch(aseg, j+5, 0);
695       if (!sv2 || !*sv2) {
696         myfree(segs);
697         croak("i_fountain: XS error");
698       }
699       worki[j] = SvIV(*sv2);
700     }
701     segs[i].type = worki[0];
702     segs[i].color = worki[1];
703   }
704
705   return segs;
706 }
707
708 /* validates the indexes supplied to i_ppal
709
710 i_ppal() doesn't do that for speed, but I'm not comfortable doing that
711 for calls from perl.
712
713 */
714 static void
715 validate_i_ppal(i_img *im, i_palidx const *indexes, int count) {
716   int color_count = i_colorcount(im);
717   int i;
718
719   if (color_count == -1)
720     croak("i_plin() called on direct color image");
721   
722   for (i = 0; i < count; ++i) {
723     if (indexes[i] >= color_count) {
724       croak("i_plin() called with out of range color index %d (max %d)",
725         indexes[i], color_count-1);
726     }
727   }
728 }
729
730
731 /* I don't think ICLF_* names belong at the C interface
732    this makes the XS code think we have them, to let us avoid 
733    putting function bodies in the XS code
734 */
735 #define ICLF_new_internal(r, g, b, a) i_fcolor_new((r), (g), (b), (a))
736 #define ICLF_DESTROY(cl) i_fcolor_destroy(cl)
737
738 #ifdef IMAGER_LOG
739 #define i_log_enabled() 1
740 #else
741 #define i_log_enabled() 0
742 #endif
743
744 #if i_int_hlines_testing()
745
746 typedef i_int_hlines *Imager__Internal__Hlines;
747
748 static i_int_hlines *
749 i_int_hlines_new(i_img_dim start_y, i_img_dim count_y, i_img_dim start_x, i_img_dim count_x) {
750   i_int_hlines *result = mymalloc(sizeof(i_int_hlines));
751   i_int_init_hlines(result, start_y, count_y, start_x, count_x);
752
753   return result;
754 }
755
756 static i_int_hlines *
757 i_int_hlines_new_img(i_img *im) {
758   i_int_hlines *result = mymalloc(sizeof(i_int_hlines));
759   i_int_init_hlines_img(result, im);
760
761   return result;
762 }
763
764 static void
765 i_int_hlines_DESTROY(i_int_hlines *hlines) {
766   i_int_hlines_destroy(hlines);
767   myfree(hlines);
768 }
769
770 #define i_int_hlines_CLONE_SKIP(cls) 1
771
772 static int seg_compare(const void *vleft, const void *vright) {
773   const i_int_hline_seg *left = vleft;
774   const i_int_hline_seg *right = vright;
775
776   return left->minx - right->minx;
777 }
778
779 static SV *
780 i_int_hlines_dump(i_int_hlines *hlines) {
781   dTHX;
782   SV *dump = newSVpvf("start_y: %" i_DF " limit_y: %" i_DF " start_x: %" i_DF " limit_x: %" i_DF"\n",
783         i_DFc(hlines->start_y), i_DFc(hlines->limit_y), i_DFc(hlines->start_x), i_DFc(hlines->limit_x));
784   i_img_dim y;
785   
786   for (y = hlines->start_y; y < hlines->limit_y; ++y) {
787     i_int_hline_entry *entry = hlines->entries[y-hlines->start_y];
788     if (entry) {
789       int i;
790       /* sort the segments, if any */
791       if (entry->count)
792         qsort(entry->segs, entry->count, sizeof(i_int_hline_seg), seg_compare);
793
794       sv_catpvf(dump, " %" i_DF " (%" i_DF "):", i_DFc(y), i_DFc(entry->count));
795       for (i = 0; i < entry->count; ++i) {
796         sv_catpvf(dump, " [%" i_DF ", %" i_DF ")", i_DFc(entry->segs[i].minx), 
797                   i_DFc(entry->segs[i].x_limit));
798       }
799       sv_catpv(dump, "\n");
800     }
801   }
802
803   return dump;
804 }
805
806 #endif
807
808 static off_t
809 i_sv_off_t(pTHX_ SV *sv) {
810 #if LSEEKSIZE > IVSIZE
811   return (off_t)SvNV(sv);
812 #else
813   return (off_t)SvIV(sv);
814 #endif
815 }
816
817 static SV *
818 i_new_sv_off_t(pTHX_ off_t off) {
819 #if LSEEKSIZE > IVSIZE
820   return newSVnv(off);
821 #else
822   return newSViv(off);
823 #endif
824 }
825
826 static im_pl_ext_funcs im_perl_funcs =
827 {
828   IMAGER_PL_API_VERSION,
829   IMAGER_PL_API_LEVEL,
830   ip_handle_quant_opts,
831   ip_cleanup_quant_opts,
832   ip_copy_colors_back
833 };
834
835 #define PERL_PL_SET_GLOBAL_CALLBACKS \
836   sv_setiv(get_sv(PERL_PL_FUNCTION_TABLE_NAME, 1), PTR2IV(&im_perl_funcs));
837
838 #ifdef IMEXIF_ENABLE
839 #define i_exif_enabled() 1
840 #else
841 #define i_exif_enabled() 0
842 #endif
843
844 /* trying to use more C style names, map them here */
845 #define i_io_DESTROY(ig) io_glue_destroy(ig)
846
847 #define i_img_get_width(im) ((im)->xsize)
848 #define i_img_get_height(im) ((im)->ysize)
849
850 #define i_img_epsilonf() (DBL_EPSILON * 4)
851
852 /* avoid some xsubpp strangeness */
853 #define NEWLINE '\n'
854
855 MODULE = Imager         PACKAGE = Imager::Color PREFIX = ICL_
856
857 Imager::Color
858 ICL_new_internal(r,g,b,a)
859                unsigned char     r
860                unsigned char     g
861                unsigned char     b
862                unsigned char     a
863
864 void
865 ICL_DESTROY(cl)
866                Imager::Color    cl
867
868
869 void
870 ICL_set_internal(cl,r,g,b,a)
871                Imager::Color    cl
872                unsigned char     r
873                unsigned char     g
874                unsigned char     b
875                unsigned char     a
876            PPCODE:
877                ICL_set_internal(cl, r, g, b, a);
878                EXTEND(SP, 1);
879                PUSHs(ST(0));
880
881 void
882 ICL_info(cl)
883                Imager::Color    cl
884
885
886 void
887 ICL_rgba(cl)
888               Imager::Color     cl
889             PPCODE:
890                 EXTEND(SP, 4);
891                 PUSHs(sv_2mortal(newSVnv(cl->rgba.r)));
892                 PUSHs(sv_2mortal(newSVnv(cl->rgba.g)));
893                 PUSHs(sv_2mortal(newSVnv(cl->rgba.b)));
894                 PUSHs(sv_2mortal(newSVnv(cl->rgba.a)));
895
896 Imager::Color
897 i_hsv_to_rgb(c)
898         Imager::Color c
899       CODE:
900         RETVAL = mymalloc(sizeof(i_color));
901         *RETVAL = *c;
902         i_hsv_to_rgb(RETVAL);
903       OUTPUT:
904         RETVAL
905         
906 Imager::Color
907 i_rgb_to_hsv(c)
908         Imager::Color c
909       CODE:
910         RETVAL = mymalloc(sizeof(i_color));
911         *RETVAL = *c;
912         i_rgb_to_hsv(RETVAL);
913       OUTPUT:
914         RETVAL
915         
916
917
918 MODULE = Imager        PACKAGE = Imager::Color::Float  PREFIX=ICLF_
919
920 Imager::Color::Float
921 ICLF_new_internal(r, g, b, a)
922         double r
923         double g
924         double b
925         double a
926
927 void
928 ICLF_DESTROY(cl)
929         Imager::Color::Float    cl
930
931 void
932 ICLF_rgba(cl)
933         Imager::Color::Float    cl
934       PREINIT:
935         int ch;
936       PPCODE:
937         EXTEND(SP, MAXCHANNELS);
938         for (ch = 0; ch < MAXCHANNELS; ++ch) {
939         /* printf("%d: %g\n", ch, cl->channel[ch]); */
940           PUSHs(sv_2mortal(newSVnv(cl->channel[ch])));
941         }
942
943 void
944 ICLF_set_internal(cl,r,g,b,a)
945         Imager::Color::Float    cl
946         double     r
947         double     g
948         double     b
949         double     a
950       PPCODE:
951         cl->rgba.r = r;
952         cl->rgba.g = g;
953         cl->rgba.b = b;
954         cl->rgba.a = a;                
955         EXTEND(SP, 1);
956         PUSHs(ST(0));
957
958 Imager::Color::Float
959 i_hsv_to_rgb(c)
960         Imager::Color::Float c
961       CODE:
962         RETVAL = mymalloc(sizeof(i_fcolor));
963         *RETVAL = *c;
964         i_hsv_to_rgbf(RETVAL);
965       OUTPUT:
966         RETVAL
967         
968 Imager::Color::Float
969 i_rgb_to_hsv(c)
970         Imager::Color::Float c
971       CODE:
972         RETVAL = mymalloc(sizeof(i_fcolor));
973         *RETVAL = *c;
974         i_rgb_to_hsvf(RETVAL);
975       OUTPUT:
976         RETVAL
977
978 MODULE = Imager         PACKAGE = Imager::ImgRaw        PREFIX = IIM_
979
980 Imager::ImgRaw
981 IIM_new(x,y,ch)
982                i_img_dim     x
983                i_img_dim     y
984                int     ch
985
986 void
987 IIM_DESTROY(im)
988                Imager::ImgRaw    im
989
990
991
992 MODULE = Imager         PACKAGE = Imager
993
994 PROTOTYPES: ENABLE
995
996
997 Imager::IO
998 io_new_fd(fd)
999                          int     fd
1000
1001 Imager::IO
1002 io_new_bufchain()
1003
1004
1005 Imager::IO
1006 io_new_buffer(data_sv)
1007           SV   *data_sv
1008         CODE:
1009           RETVAL = do_io_new_buffer(aTHX_ data_sv);
1010         OUTPUT:
1011           RETVAL
1012
1013 Imager::IO
1014 io_new_cb(writecb, readcb, seekcb, closecb, maxwrite = CBDATA_BUFSIZE)
1015         SV *writecb;
1016         SV *readcb;
1017         SV *seekcb;
1018         SV *closecb;
1019         int maxwrite;
1020       CODE:
1021         RETVAL = do_io_new_cb(aTHX_ writecb, readcb, seekcb, closecb);
1022       OUTPUT:
1023         RETVAL
1024
1025 SV *
1026 io_slurp(ig)
1027         Imager::IO     ig
1028              PREINIT:
1029               unsigned char*    data;
1030               size_t    tlength;
1031              CODE:
1032               data    = NULL;
1033               tlength = io_slurp(ig, &data);
1034               RETVAL = newSVpv((char *)data,tlength);
1035               myfree(data);
1036              OUTPUT:
1037               RETVAL
1038
1039
1040 undef_int
1041 i_set_image_file_limits(width, height, bytes)
1042         i_img_dim width
1043         i_img_dim height
1044         size_t bytes
1045
1046 void
1047 i_get_image_file_limits()
1048       PREINIT:
1049         i_img_dim width, height;
1050         size_t bytes;
1051       PPCODE:
1052         if (i_get_image_file_limits(&width, &height, &bytes)) {
1053           EXTEND(SP, 3);
1054           PUSHs(sv_2mortal(newSViv(width)));
1055           PUSHs(sv_2mortal(newSViv(height)));
1056           PUSHs(sv_2mortal(newSVuv(bytes)));
1057         }
1058
1059 MODULE = Imager         PACKAGE = Imager::IO    PREFIX = io_
1060
1061 Imager::IO
1062 io_new_fd(class, fd)
1063         int fd
1064     CODE:
1065         RETVAL = io_new_fd(fd);
1066     OUTPUT:
1067         RETVAL
1068
1069 Imager::IO
1070 io_new_buffer(class, data_sv)
1071         SV *data_sv
1072     CODE:
1073         RETVAL = do_io_new_buffer(aTHX_ data_sv);
1074     OUTPUT:
1075         RETVAL
1076
1077 Imager::IO
1078 io_new_cb(class, writecb, readcb, seekcb, closecb)
1079         SV *writecb;
1080         SV *readcb;
1081         SV *seekcb;
1082         SV *closecb;
1083     CODE:
1084         RETVAL = do_io_new_cb(aTHX_ writecb, readcb, seekcb, closecb);
1085     OUTPUT:
1086         RETVAL
1087
1088 Imager::IO
1089 io_new_bufchain(class)
1090     CODE:
1091         RETVAL = io_new_bufchain();
1092     OUTPUT:
1093         RETVAL
1094
1095 SV *
1096 io_slurp(class, ig)
1097         Imager::IO     ig
1098     PREINIT:
1099         unsigned char*    data;
1100         size_t    tlength;
1101     CODE:
1102         data    = NULL;
1103         tlength = io_slurp(ig, &data);
1104         RETVAL = newSVpv((char *)data,tlength);
1105         myfree(data);
1106     OUTPUT:
1107         RETVAL
1108
1109 MODULE = Imager         PACKAGE = Imager::IO    PREFIX = i_io_
1110
1111 IV
1112 i_io_raw_write(ig, data_sv)
1113         Imager::IO ig
1114         SV *data_sv
1115       PREINIT:
1116         void *data;
1117         STRLEN size;
1118       CODE:
1119 #ifdef SvUTF8
1120         if (SvUTF8(data_sv)) {
1121           data_sv = sv_2mortal(newSVsv(data_sv));
1122           /* yes, we want this to croak() if the SV can't be downgraded */
1123           sv_utf8_downgrade(data_sv, FALSE);
1124         }
1125 #endif        
1126         data = SvPV(data_sv, size);
1127         RETVAL = i_io_raw_write(ig, data, size);
1128       OUTPUT:
1129         RETVAL
1130
1131 void
1132 i_io_raw_read(ig, buffer_sv, size)
1133         Imager::IO ig
1134         SV *buffer_sv
1135         IV size
1136       PREINIT:
1137         void *buffer;
1138         ssize_t result;
1139       PPCODE:
1140         if (size <= 0)
1141           croak("size negative in call to i_io_raw_read()");
1142         /* prevent an undefined value warning if they supplied an 
1143            undef buffer.
1144            Orginally conditional on !SvOK(), but this will prevent the
1145            downgrade from croaking */
1146         sv_setpvn(buffer_sv, "", 0);
1147 #ifdef SvUTF8
1148         if (SvUTF8(buffer_sv))
1149           sv_utf8_downgrade(buffer_sv, FALSE);
1150 #endif
1151         buffer = SvGROW(buffer_sv, size+1);
1152         result = i_io_raw_read(ig, buffer, size);
1153         if (result >= 0) {
1154           SvCUR_set(buffer_sv, result);
1155           *SvEND(buffer_sv) = '\0';
1156           SvPOK_only(buffer_sv);
1157           EXTEND(SP, 1);
1158           PUSHs(sv_2mortal(newSViv(result)));
1159         }
1160         ST(1) = buffer_sv;
1161         SvSETMAGIC(ST(1));
1162
1163 void
1164 i_io_raw_read2(ig, size)
1165         Imager::IO ig
1166         IV size
1167       PREINIT:
1168         SV *buffer_sv;
1169         void *buffer;
1170         ssize_t result;
1171       PPCODE:
1172         if (size <= 0)
1173           croak("size negative in call to i_io_read2()");
1174         buffer_sv = newSV(size);
1175         buffer = SvGROW(buffer_sv, size+1);
1176         result = i_io_raw_read(ig, buffer, size);
1177         if (result >= 0) {
1178           SvCUR_set(buffer_sv, result);
1179           *SvEND(buffer_sv) = '\0';
1180           SvPOK_only(buffer_sv);
1181           EXTEND(SP, 1);
1182           PUSHs(sv_2mortal(buffer_sv));
1183         }
1184         else {
1185           /* discard it */
1186           SvREFCNT_dec(buffer_sv);
1187         }
1188
1189 off_t
1190 i_io_raw_seek(ig, position, whence)
1191         Imager::IO ig
1192         off_t position
1193         int whence
1194
1195 int
1196 i_io_raw_close(ig)
1197         Imager::IO ig
1198
1199 void
1200 i_io_DESTROY(ig)
1201         Imager::IO     ig
1202
1203 int
1204 i_io_CLONE_SKIP(...)
1205     CODE:
1206         (void)items; /* avoid unused warning for XS variable */
1207         RETVAL = 1;
1208     OUTPUT:
1209         RETVAL
1210
1211 int
1212 i_io_getc(ig)
1213         Imager::IO ig
1214
1215 int
1216 i_io_putc(ig, c)
1217         Imager::IO ig
1218         int c
1219
1220 int
1221 i_io_close(ig)
1222         Imager::IO ig
1223
1224 int
1225 i_io_flush(ig)
1226         Imager::IO ig
1227
1228 int
1229 i_io_peekc(ig)
1230         Imager::IO ig
1231
1232 int
1233 i_io_seek(ig, off, whence)
1234         Imager::IO ig
1235         off_t off
1236         int whence
1237
1238 void
1239 i_io_peekn(ig, size)
1240         Imager::IO ig
1241         STRLEN size
1242       PREINIT:
1243         SV *buffer_sv;
1244         void *buffer;
1245         ssize_t result;
1246       PPCODE:
1247         buffer_sv = newSV(size+1);
1248         buffer = SvGROW(buffer_sv, size+1);
1249         result = i_io_peekn(ig, buffer, size);
1250         if (result >= 0) {
1251           SvCUR_set(buffer_sv, result);
1252           *SvEND(buffer_sv) = '\0';
1253           SvPOK_only(buffer_sv);
1254           EXTEND(SP, 1);
1255           PUSHs(sv_2mortal(buffer_sv));
1256         }
1257         else {
1258           /* discard it */
1259           SvREFCNT_dec(buffer_sv);
1260         }
1261
1262 void
1263 i_io_read(ig, buffer_sv, size)
1264         Imager::IO ig
1265         SV *buffer_sv
1266         IV size
1267       PREINIT:
1268         void *buffer;
1269         ssize_t result;
1270       PPCODE:
1271         if (size <= 0)
1272           croak("size negative in call to i_io_read()");
1273         /* prevent an undefined value warning if they supplied an 
1274            undef buffer.
1275            Orginally conditional on !SvOK(), but this will prevent the
1276            downgrade from croaking */
1277         sv_setpvn(buffer_sv, "", 0);
1278 #ifdef SvUTF8
1279         if (SvUTF8(buffer_sv))
1280           sv_utf8_downgrade(buffer_sv, FALSE);
1281 #endif
1282         buffer = SvGROW(buffer_sv, size+1);
1283         result = i_io_read(ig, buffer, size);
1284         if (result >= 0) {
1285           SvCUR_set(buffer_sv, result);
1286           *SvEND(buffer_sv) = '\0';
1287           SvPOK_only(buffer_sv);
1288           EXTEND(SP, 1);
1289           PUSHs(sv_2mortal(newSViv(result)));
1290         }
1291         ST(1) = buffer_sv;
1292         SvSETMAGIC(ST(1));
1293
1294 void
1295 i_io_read2(ig, size)
1296         Imager::IO ig
1297         STRLEN size
1298       PREINIT:
1299         SV *buffer_sv;
1300         void *buffer;
1301         ssize_t result;
1302       PPCODE:
1303         if (size == 0)
1304           croak("size zero in call to read2()");
1305         buffer_sv = newSV(size);
1306         buffer = SvGROW(buffer_sv, size+1);
1307         result = i_io_read(ig, buffer, size);
1308         if (result > 0) {
1309           SvCUR_set(buffer_sv, result);
1310           *SvEND(buffer_sv) = '\0';
1311           SvPOK_only(buffer_sv);
1312           EXTEND(SP, 1);
1313           PUSHs(sv_2mortal(buffer_sv));
1314         }
1315         else {
1316           /* discard it */
1317           SvREFCNT_dec(buffer_sv);
1318         }
1319
1320 void
1321 i_io_gets(ig, size = 8192, eol = NEWLINE)
1322         Imager::IO ig
1323         STRLEN size
1324         int eol
1325       PREINIT:
1326         SV *buffer_sv;
1327         void *buffer;
1328         ssize_t result;
1329       PPCODE:
1330         if (size < 2)
1331           croak("size too small in call to gets()");
1332         buffer_sv = sv_2mortal(newSV(size+1));
1333         buffer = SvPVX(buffer_sv);
1334         result = i_io_gets(ig, buffer, size+1, eol);
1335         if (result > 0) {
1336           SvCUR_set(buffer_sv, result);
1337           *SvEND(buffer_sv) = '\0';
1338           SvPOK_only(buffer_sv);
1339           EXTEND(SP, 1);
1340           PUSHs(buffer_sv);
1341         }
1342
1343 IV
1344 i_io_write(ig, data_sv)
1345         Imager::IO ig
1346         SV *data_sv
1347       PREINIT:
1348         void *data;
1349         STRLEN size;
1350       CODE:
1351 #ifdef SvUTF8
1352         if (SvUTF8(data_sv)) {
1353           data_sv = sv_2mortal(newSVsv(data_sv));
1354           /* yes, we want this to croak() if the SV can't be downgraded */
1355           sv_utf8_downgrade(data_sv, FALSE);
1356         }
1357 #endif        
1358         data = SvPV(data_sv, size);
1359         RETVAL = i_io_write(ig, data, size);
1360       OUTPUT:
1361         RETVAL
1362
1363 void
1364 i_io_dump(ig, flags = I_IO_DUMP_DEFAULT)
1365         Imager::IO ig
1366         int flags
1367
1368 bool
1369 i_io_set_buffered(ig, flag = 1)
1370         Imager::IO ig
1371         int flag
1372
1373 bool
1374 i_io_is_buffered(ig)
1375         Imager::IO ig
1376
1377 bool
1378 i_io_eof(ig)
1379         Imager::IO ig
1380
1381 bool
1382 i_io_error(ig)
1383         Imager::IO ig
1384
1385 MODULE = Imager         PACKAGE = Imager
1386
1387 PROTOTYPES: ENABLE
1388
1389 void
1390 i_list_formats()
1391              PREINIT:
1392               char*    item;
1393                int     i;
1394              PPCODE:
1395                i=0;
1396                while( (item=i_format_list[i++]) != NULL ) {
1397                       EXTEND(SP, 1);
1398                       PUSHs(sv_2mortal(newSVpv(item,0)));
1399                }
1400
1401 Imager::ImgRaw
1402 i_img_new()
1403
1404 Imager::ImgRaw
1405 i_img_empty(im,x,y)
1406     Imager::ImgRaw     im
1407                i_img_dim     x
1408                i_img_dim     y
1409
1410 Imager::ImgRaw
1411 i_img_empty_ch(im,x,y,ch)
1412     Imager::ImgRaw     im
1413                i_img_dim     x
1414                i_img_dim     y
1415                int     ch
1416
1417 Imager::ImgRaw
1418 i_sametype(im, x, y)
1419     Imager::ImgRaw im
1420                i_img_dim x
1421                i_img_dim y
1422
1423 Imager::ImgRaw
1424 i_sametype_chans(im, x, y, channels)
1425     Imager::ImgRaw im
1426                i_img_dim x
1427                i_img_dim y
1428                int channels
1429
1430 int
1431 i_init_log(name_sv,level)
1432               SV*    name_sv
1433                int     level
1434         PREINIT:
1435           const char *name = SvOK(name_sv) ? SvPV_nolen(name_sv) : NULL;
1436         CODE:
1437           RETVAL = i_init_log(name, level);
1438         OUTPUT:
1439           RETVAL
1440
1441 void
1442 i_log_entry(string,level)
1443               char*    string
1444                int     level
1445
1446 int
1447 i_log_enabled()
1448
1449 void
1450 i_img_exorcise(im)
1451     Imager::ImgRaw     im
1452
1453 void
1454 i_img_destroy(im)
1455     Imager::ImgRaw     im
1456
1457 void
1458 i_img_info(im)
1459     Imager::ImgRaw     im
1460              PREINIT:
1461                i_img_dim     info[4];
1462              PPCODE:
1463                i_img_info(im,info);
1464                EXTEND(SP, 4);
1465                PUSHs(sv_2mortal(newSViv(info[0])));
1466                PUSHs(sv_2mortal(newSViv(info[1])));
1467                PUSHs(sv_2mortal(newSViv(info[2])));
1468                PUSHs(sv_2mortal(newSViv(info[3])));
1469
1470
1471
1472
1473 void
1474 i_img_setmask(im,ch_mask)
1475     Imager::ImgRaw     im
1476                int     ch_mask
1477
1478 int
1479 i_img_getmask(im)
1480     Imager::ImgRaw     im
1481
1482 int
1483 i_img_getchannels(im)
1484     Imager::ImgRaw     im
1485
1486 void
1487 i_img_getdata(im)
1488     Imager::ImgRaw     im
1489              PPCODE:
1490                EXTEND(SP, 1);
1491                PUSHs(im->idata ? 
1492                      sv_2mortal(newSVpv((char *)im->idata, im->bytes)) 
1493                      : &PL_sv_undef);
1494
1495 IV
1496 i_img_get_width(im)
1497     Imager::ImgRaw      im
1498
1499 IV
1500 i_img_get_height(im)
1501     Imager::ImgRaw      im
1502
1503
1504 void
1505 i_img_is_monochrome(im)
1506         Imager::ImgRaw im
1507       PREINIT:
1508         int zero_is_white;
1509         int result;
1510       PPCODE:
1511         result = i_img_is_monochrome(im, &zero_is_white);
1512         if (result) {
1513           if (GIMME_V == G_ARRAY) {
1514             EXTEND(SP, 2);
1515             PUSHs(&PL_sv_yes);
1516             PUSHs(sv_2mortal(newSViv(zero_is_white)));
1517           }
1518           else {
1519             EXTEND(SP, 1);
1520             PUSHs(&PL_sv_yes);
1521           }
1522         }
1523
1524 void
1525 i_line(im,x1,y1,x2,y2,val,endp)
1526     Imager::ImgRaw     im
1527                i_img_dim     x1
1528                i_img_dim     y1
1529                i_img_dim     x2
1530                i_img_dim     y2
1531      Imager::Color     val
1532                int     endp
1533
1534 void
1535 i_line_aa(im,x1,y1,x2,y2,val,endp)
1536     Imager::ImgRaw     im
1537                i_img_dim     x1
1538                i_img_dim     y1
1539                i_img_dim     x2
1540                i_img_dim     y2
1541      Imager::Color     val
1542                int     endp
1543
1544 void
1545 i_box(im,x1,y1,x2,y2,val)
1546     Imager::ImgRaw     im
1547                i_img_dim     x1
1548                i_img_dim     y1
1549                i_img_dim     x2
1550                i_img_dim     y2
1551      Imager::Color     val
1552
1553 void
1554 i_box_filled(im,x1,y1,x2,y2,val)
1555     Imager::ImgRaw     im
1556                i_img_dim     x1
1557                i_img_dim     y1
1558                i_img_dim     x2
1559                i_img_dim     y2
1560            Imager::Color    val
1561
1562 int
1563 i_box_filledf(im,x1,y1,x2,y2,val)
1564     Imager::ImgRaw     im
1565                i_img_dim     x1
1566                i_img_dim     y1
1567                i_img_dim     x2
1568                i_img_dim     y2
1569            Imager::Color::Float    val
1570
1571 void
1572 i_box_cfill(im,x1,y1,x2,y2,fill)
1573     Imager::ImgRaw     im
1574                i_img_dim     x1
1575                i_img_dim     y1
1576                i_img_dim     x2
1577                i_img_dim     y2
1578            Imager::FillHandle    fill
1579
1580 void
1581 i_arc(im,x,y,rad,d1,d2,val)
1582     Imager::ImgRaw     im
1583                i_img_dim     x
1584                i_img_dim     y
1585              double     rad
1586              double     d1
1587              double     d2
1588            Imager::Color    val
1589
1590 void
1591 i_arc_aa(im,x,y,rad,d1,d2,val)
1592     Imager::ImgRaw     im
1593             double     x
1594             double     y
1595             double     rad
1596             double     d1
1597             double     d2
1598            Imager::Color    val
1599
1600 void
1601 i_arc_cfill(im,x,y,rad,d1,d2,fill)
1602     Imager::ImgRaw     im
1603                i_img_dim     x
1604                i_img_dim     y
1605              double     rad
1606              double     d1
1607              double     d2
1608            Imager::FillHandle    fill
1609
1610 void
1611 i_arc_aa_cfill(im,x,y,rad,d1,d2,fill)
1612     Imager::ImgRaw     im
1613             double     x
1614             double     y
1615             double     rad
1616             double     d1
1617             double     d2
1618            Imager::FillHandle   fill
1619
1620
1621 void
1622 i_circle_aa(im,x,y,rad,val)
1623     Imager::ImgRaw     im
1624              double     x
1625              double     y
1626              double     rad
1627            Imager::Color    val
1628
1629 int
1630 i_circle_out(im,x,y,rad,val)
1631     Imager::ImgRaw     im
1632              i_img_dim     x
1633              i_img_dim     y
1634              i_img_dim     rad
1635            Imager::Color    val
1636
1637 int
1638 i_circle_out_aa(im,x,y,rad,val)
1639     Imager::ImgRaw     im
1640              i_img_dim     x
1641              i_img_dim     y
1642              i_img_dim     rad
1643            Imager::Color    val
1644
1645 int
1646 i_arc_out(im,x,y,rad,d1,d2,val)
1647     Imager::ImgRaw     im
1648              i_img_dim     x
1649              i_img_dim     y
1650              i_img_dim     rad
1651              double d1
1652              double d2
1653            Imager::Color    val
1654
1655 int
1656 i_arc_out_aa(im,x,y,rad,d1,d2,val)
1657     Imager::ImgRaw     im
1658              i_img_dim     x
1659              i_img_dim     y
1660              i_img_dim     rad
1661              double d1
1662              double d2
1663            Imager::Color    val
1664
1665
1666 void
1667 i_bezier_multi(im,xc,yc,val)
1668     Imager::ImgRaw     im
1669              Imager::Color  val
1670              PREINIT:
1671              double   *x,*y;
1672              int       len;
1673              AV       *av1;
1674              AV       *av2;
1675              SV       *sv1;
1676              SV       *sv2;
1677              int i;
1678              PPCODE:
1679              ICL_info(val);
1680              if (!SvROK(ST(1))) croak("Imager: Parameter 1 to i_bezier_multi must be a reference to an array\n");
1681              if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 to i_bezier_multi must be a reference to an array\n");
1682              if (!SvROK(ST(2))) croak("Imager: Parameter 2 to i_bezier_multi must be a reference to an array\n");
1683              if (SvTYPE(SvRV(ST(2))) != SVt_PVAV) croak("Imager: Parameter 2 to i_bezier_multi must be a reference to an array\n");
1684              av1=(AV*)SvRV(ST(1));
1685              av2=(AV*)SvRV(ST(2));
1686              if (av_len(av1) != av_len(av2)) croak("Imager: x and y arrays to i_bezier_multi must be equal length\n");
1687              len=av_len(av1)+1;
1688              x=mymalloc( len*sizeof(double) );
1689              y=mymalloc( len*sizeof(double) );
1690              for(i=0;i<len;i++) {
1691                sv1=(*(av_fetch(av1,i,0)));
1692                sv2=(*(av_fetch(av2,i,0)));
1693                x[i]=(double)SvNV(sv1);
1694                y[i]=(double)SvNV(sv2);
1695              }
1696              i_bezier_multi(im,len,x,y,val);
1697              myfree(x);
1698              myfree(y);
1699
1700
1701 int
1702 i_poly_aa(im,xc,yc,val)
1703     Imager::ImgRaw     im
1704              Imager::Color  val
1705              PREINIT:
1706              double   *x,*y;
1707              int       len;
1708              AV       *av1;
1709              AV       *av2;
1710              SV       *sv1;
1711              SV       *sv2;
1712              int i;
1713              CODE:
1714              ICL_info(val);
1715              if (!SvROK(ST(1))) croak("Imager: Parameter 1 to i_poly_aa must be a reference to an array\n");
1716              if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 to i_poly_aa must be a reference to an array\n");
1717              if (!SvROK(ST(2))) croak("Imager: Parameter 1 to i_poly_aa must be a reference to an array\n");
1718              if (SvTYPE(SvRV(ST(2))) != SVt_PVAV) croak("Imager: Parameter 1 to i_poly_aa must be a reference to an array\n");
1719              av1=(AV*)SvRV(ST(1));
1720              av2=(AV*)SvRV(ST(2));
1721              if (av_len(av1) != av_len(av2)) croak("Imager: x and y arrays to i_poly_aa must be equal length\n");
1722              len=av_len(av1)+1;
1723              x=mymalloc( len*sizeof(double) );
1724              y=mymalloc( len*sizeof(double) );
1725              for(i=0;i<len;i++) {
1726                sv1=(*(av_fetch(av1,i,0)));
1727                sv2=(*(av_fetch(av2,i,0)));
1728                x[i]=(double)SvNV(sv1);
1729                y[i]=(double)SvNV(sv2);
1730              }
1731              RETVAL = i_poly_aa(im,len,x,y,val);
1732              myfree(x);
1733              myfree(y);
1734              OUTPUT:
1735                RETVAL
1736
1737 int
1738 i_poly_aa_cfill(im,xc,yc,fill)
1739     Imager::ImgRaw     im
1740      Imager::FillHandle     fill
1741              PREINIT:
1742              double   *x,*y;
1743              int       len;
1744              AV       *av1;
1745              AV       *av2;
1746              SV       *sv1;
1747              SV       *sv2;
1748              int i;
1749              CODE:
1750              if (!SvROK(ST(1))) croak("Imager: Parameter 1 to i_poly_aa_cfill must be a reference to an array\n");
1751              if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 to i_poly_aa_cfill must be a reference to an array\n");
1752              if (!SvROK(ST(2))) croak("Imager: Parameter 1 to i_poly_aa_cfill must be a reference to an array\n");
1753              if (SvTYPE(SvRV(ST(2))) != SVt_PVAV) croak("Imager: Parameter 1 to i_poly_aa_cfill must be a reference to an array\n");
1754              av1=(AV*)SvRV(ST(1));
1755              av2=(AV*)SvRV(ST(2));
1756              if (av_len(av1) != av_len(av2)) croak("Imager: x and y arrays to i_poly_aa_cfill must be equal length\n");
1757              len=av_len(av1)+1;
1758              x=mymalloc( len*sizeof(double) );
1759              y=mymalloc( len*sizeof(double) );
1760              for(i=0;i<len;i++) {
1761                sv1=(*(av_fetch(av1,i,0)));
1762                sv2=(*(av_fetch(av2,i,0)));
1763                x[i]=(double)SvNV(sv1);
1764                y[i]=(double)SvNV(sv2);
1765              }
1766              RETVAL = i_poly_aa_cfill(im,len,x,y,fill);
1767              myfree(x);
1768              myfree(y);
1769              OUTPUT:
1770                RETVAL
1771
1772
1773
1774 undef_int
1775 i_flood_fill(im,seedx,seedy,dcol)
1776     Imager::ImgRaw     im
1777                i_img_dim     seedx
1778                i_img_dim     seedy
1779      Imager::Color     dcol
1780
1781 undef_int
1782 i_flood_cfill(im,seedx,seedy,fill)
1783     Imager::ImgRaw     im
1784                i_img_dim     seedx
1785                i_img_dim     seedy
1786      Imager::FillHandle     fill
1787
1788 undef_int
1789 i_flood_fill_border(im,seedx,seedy,dcol, border)
1790     Imager::ImgRaw     im
1791                i_img_dim     seedx
1792                i_img_dim     seedy
1793      Imager::Color     dcol
1794      Imager::Color     border
1795
1796 undef_int
1797 i_flood_cfill_border(im,seedx,seedy,fill, border)
1798     Imager::ImgRaw     im
1799                i_img_dim     seedx
1800                i_img_dim     seedy
1801      Imager::FillHandle     fill
1802      Imager::Color     border
1803
1804
1805 void
1806 i_copyto(im,src,x1,y1,x2,y2,tx,ty)
1807     Imager::ImgRaw     im
1808     Imager::ImgRaw     src
1809                i_img_dim     x1
1810                i_img_dim     y1
1811                i_img_dim     x2
1812                i_img_dim     y2
1813                i_img_dim     tx
1814                i_img_dim     ty
1815
1816
1817 void
1818 i_copyto_trans(im,src,x1,y1,x2,y2,tx,ty,trans)
1819     Imager::ImgRaw     im
1820     Imager::ImgRaw     src
1821                i_img_dim     x1
1822                i_img_dim     y1
1823                i_img_dim     x2
1824                i_img_dim     y2
1825                i_img_dim     tx
1826                i_img_dim     ty
1827      Imager::Color     trans
1828
1829 Imager::ImgRaw
1830 i_copy(src)
1831     Imager::ImgRaw     src
1832
1833
1834 undef_int
1835 i_rubthru(im,src,tx,ty,src_minx,src_miny,src_maxx,src_maxy)
1836     Imager::ImgRaw     im
1837     Imager::ImgRaw     src
1838                i_img_dim     tx
1839                i_img_dim     ty
1840                i_img_dim     src_minx
1841                i_img_dim     src_miny
1842                i_img_dim     src_maxx
1843                i_img_dim     src_maxy
1844
1845 undef_int
1846 i_compose(out, src, out_left, out_top, src_left, src_top, width, height, combine = ic_normal, opacity = 0.0)
1847     Imager::ImgRaw out
1848     Imager::ImgRaw src
1849         i_img_dim out_left
1850         i_img_dim out_top
1851         i_img_dim src_left
1852         i_img_dim src_top
1853         i_img_dim width
1854         i_img_dim height
1855         int combine
1856         double opacity
1857
1858 undef_int
1859 i_compose_mask(out, src, mask, out_left, out_top, src_left, src_top, mask_left, mask_top, width, height, combine = ic_normal, opacity = 0.0)
1860     Imager::ImgRaw out
1861     Imager::ImgRaw src
1862     Imager::ImgRaw mask
1863         i_img_dim out_left
1864         i_img_dim out_top
1865         i_img_dim src_left
1866         i_img_dim src_top
1867         i_img_dim mask_left
1868         i_img_dim mask_top
1869         i_img_dim width
1870         i_img_dim height
1871         int combine
1872         double opacity
1873
1874 Imager::ImgRaw
1875 i_combine(src_av, channels_av = NULL)
1876         AV *src_av
1877         AV *channels_av
1878   PREINIT:
1879         i_img **imgs = NULL;
1880         STRLEN in_count;
1881         int *channels = NULL;
1882         int i;
1883         SV **psv;
1884         IV tmp;
1885   CODE:
1886         in_count = av_len(src_av) + 1;
1887         if (in_count > 0) {
1888           imgs = mymalloc(sizeof(i_img*) * in_count);
1889           channels = mymalloc(sizeof(int) * in_count);
1890           for (i = 0; i < in_count; ++i) {
1891             psv = av_fetch(src_av, i, 0);
1892             if (!psv || !*psv || !sv_derived_from(*psv, "Imager::ImgRaw")) {
1893               myfree(imgs);
1894               myfree(channels);
1895               croak("imgs must contain only images");
1896             }
1897             tmp = SvIV((SV*)SvRV(*psv));
1898             imgs[i] = INT2PTR(i_img*, tmp);
1899             if (channels_av &&
1900                 (psv = av_fetch(channels_av, i, 0)) != NULL &&
1901                 *psv) {
1902               channels[i] = SvIV(*psv);
1903             }
1904             else {
1905               channels[i] = 0;
1906             }
1907           }
1908         }
1909         RETVAL = i_combine(imgs, channels, in_count);
1910         myfree(imgs);
1911         myfree(channels);
1912   OUTPUT:
1913         RETVAL
1914
1915 undef_int
1916 i_flipxy(im, direction)
1917     Imager::ImgRaw     im
1918                int     direction
1919
1920 Imager::ImgRaw
1921 i_rotate90(im, degrees)
1922     Imager::ImgRaw      im
1923                int      degrees
1924
1925 Imager::ImgRaw
1926 i_rotate_exact(im, amount, ...)
1927     Imager::ImgRaw      im
1928             double      amount
1929       PREINIT:
1930         i_color *backp = NULL;
1931         i_fcolor *fbackp = NULL;
1932         int i;
1933         SV * sv1;
1934       CODE:
1935         /* extract the bg colors if any */
1936         /* yes, this is kind of strange */
1937         for (i = 2; i < items; ++i) {
1938           sv1 = ST(i);
1939           if (sv_derived_from(sv1, "Imager::Color")) {
1940             IV tmp = SvIV((SV*)SvRV(sv1));
1941             backp = INT2PTR(i_color *, tmp);
1942           }
1943           else if (sv_derived_from(sv1, "Imager::Color::Float")) {
1944             IV tmp = SvIV((SV*)SvRV(sv1));
1945             fbackp = INT2PTR(i_fcolor *, tmp);
1946           }
1947         }
1948         RETVAL = i_rotate_exact_bg(im, amount, backp, fbackp);
1949       OUTPUT:
1950         RETVAL
1951
1952 Imager::ImgRaw
1953 i_matrix_transform(im, xsize, ysize, matrix, ...)
1954     Imager::ImgRaw      im
1955                i_img_dim      xsize
1956                i_img_dim      ysize
1957       PREINIT:
1958         double matrix[9];
1959         AV *av;
1960         IV len;
1961         SV *sv1;
1962         int i;
1963         i_color *backp = NULL;
1964         i_fcolor *fbackp = NULL;
1965       CODE:
1966         if (!SvROK(ST(3)) || SvTYPE(SvRV(ST(3))) != SVt_PVAV)
1967           croak("i_matrix_transform: parameter 4 must be an array ref\n");
1968         av=(AV*)SvRV(ST(3));
1969         len=av_len(av)+1;
1970         if (len > 9)
1971           len = 9;
1972         for (i = 0; i < len; ++i) {
1973           sv1=(*(av_fetch(av,i,0)));
1974           matrix[i] = SvNV(sv1);
1975         }
1976         for (; i < 9; ++i)
1977           matrix[i] = 0;
1978         /* extract the bg colors if any */
1979         /* yes, this is kind of strange */
1980         for (i = 4; i < items; ++i) {
1981           sv1 = ST(i);
1982           if (sv_derived_from(sv1, "Imager::Color")) {
1983             IV tmp = SvIV((SV*)SvRV(sv1));
1984             backp = INT2PTR(i_color *, tmp);
1985           }
1986           else if (sv_derived_from(sv1, "Imager::Color::Float")) {
1987             IV tmp = SvIV((SV*)SvRV(sv1));
1988             fbackp = INT2PTR(i_fcolor *, tmp);
1989           }
1990         }
1991         RETVAL = i_matrix_transform_bg(im, xsize, ysize, matrix, backp, fbackp);
1992       OUTPUT:
1993         RETVAL
1994
1995 undef_int
1996 i_gaussian(im,stdev)
1997     Imager::ImgRaw     im
1998             double     stdev
1999
2000 void
2001 i_unsharp_mask(im,stdev,scale)
2002     Imager::ImgRaw     im
2003              double    stdev
2004              double    scale
2005
2006 int
2007 i_conv(im,coef)
2008         Imager::ImgRaw     im
2009         AV *coef
2010      PREINIT:
2011         double*    c_coef;
2012         int     len;
2013         SV* sv1;
2014         int i;
2015     CODE:
2016         len = av_len(coef) + 1;
2017         c_coef=mymalloc( len * sizeof(double) );
2018         for(i = 0; i  < len; i++) {
2019           sv1 = (*(av_fetch(coef, i, 0)));
2020           c_coef[i] = (double)SvNV(sv1);
2021         }
2022         RETVAL = i_conv(im, c_coef, len);
2023         myfree(c_coef);
2024     OUTPUT:
2025         RETVAL
2026
2027 Imager::ImgRaw
2028 i_convert(src, avmain)
2029     Imager::ImgRaw     src
2030     AV *avmain
2031         PREINIT:
2032           double *coeff;
2033           int outchan;
2034           int inchan;
2035           SV **temp;
2036           AV *avsub;
2037           int len;
2038           int i, j;
2039         CODE:
2040           outchan = av_len(avmain)+1;
2041           /* find the biggest */
2042           inchan = 0;
2043           for (j=0; j < outchan; ++j) {
2044             temp = av_fetch(avmain, j, 0);
2045             if (temp && SvROK(*temp) && SvTYPE(SvRV(*temp)) == SVt_PVAV) {
2046               avsub = (AV*)SvRV(*temp);
2047               len = av_len(avsub)+1;
2048               if (len > inchan)
2049                 inchan = len;
2050             }
2051           }
2052           coeff = mymalloc(sizeof(double) * outchan * inchan);
2053           for (j = 0; j < outchan; ++j) {
2054             avsub = (AV*)SvRV(*av_fetch(avmain, j, 0));
2055             len = av_len(avsub)+1;
2056             for (i = 0; i < len; ++i) {
2057               temp = av_fetch(avsub, i, 0);
2058               if (temp)
2059                 coeff[i+j*inchan] = SvNV(*temp);
2060               else
2061                 coeff[i+j*inchan] = 0;
2062             }
2063             while (i < inchan)
2064               coeff[i++ + j*inchan] = 0;
2065           }
2066           RETVAL = i_convert(src, coeff, outchan, inchan);
2067           myfree(coeff);
2068         OUTPUT:
2069           RETVAL
2070
2071
2072 void
2073 i_map(im, pmaps)
2074     Imager::ImgRaw     im
2075         PREINIT:
2076           unsigned int mask = 0;
2077           AV *avmain;
2078           AV *avsub;
2079           SV **temp;
2080           int len;
2081           int i, j;
2082           unsigned char (*maps)[256];
2083         CODE:
2084           if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVAV)
2085             croak("i_map: parameter 2 must be an arrayref\n");
2086           avmain = (AV*)SvRV(ST(1));
2087           len = av_len(avmain)+1;
2088           if (im->channels < len) len = im->channels;
2089
2090           maps = mymalloc( len * sizeof(unsigned char [256]) );
2091
2092           for (j=0; j<len ; j++) {
2093             temp = av_fetch(avmain, j, 0);
2094             if (temp && SvROK(*temp) && (SvTYPE(SvRV(*temp)) == SVt_PVAV) ) {
2095               avsub = (AV*)SvRV(*temp);
2096               if(av_len(avsub) != 255) continue;
2097               mask |= 1<<j;
2098               for (i=0; i<256 ; i++) {
2099                 int val;
2100                 temp = av_fetch(avsub, i, 0);
2101                 val = temp ? SvIV(*temp) : 0;
2102                 if (val<0) val = 0;
2103                 if (val>255) val = 255;
2104                 maps[j][i] = val;
2105               }
2106             }
2107           }
2108           i_map(im, maps, mask);
2109           myfree(maps);
2110
2111
2112
2113 float
2114 i_img_diff(im1,im2)
2115     Imager::ImgRaw     im1
2116     Imager::ImgRaw     im2
2117
2118 double
2119 i_img_diffd(im1,im2)
2120     Imager::ImgRaw     im1
2121     Imager::ImgRaw     im2
2122
2123 int
2124 i_img_samef(im1, im2, epsilon = i_img_epsilonf(), what=NULL)
2125     Imager::ImgRaw    im1
2126     Imager::ImgRaw    im2
2127     double epsilon
2128     const char *what
2129
2130 double
2131 i_img_epsilonf()
2132
2133 bool
2134 _is_color_object(sv)
2135         SV* sv
2136     CODE:
2137         SvGETMAGIC(sv);
2138         RETVAL = SvOK(sv) && SvROK(sv) &&
2139            (sv_derived_from(sv, "Imager::Color")
2140           || sv_derived_from(sv, "Imager::Color::Float"));
2141     OUTPUT:
2142         RETVAL
2143
2144 #ifdef HAVE_LIBTT
2145
2146
2147 Imager::Font::TT
2148 i_tt_new(fontname)
2149               char*     fontname
2150
2151
2152 MODULE = Imager         PACKAGE = Imager::Font::TT      PREFIX=TT_
2153
2154 #define TT_DESTROY(handle) i_tt_destroy(handle)
2155
2156 void
2157 TT_DESTROY(handle)
2158      Imager::Font::TT   handle
2159
2160 int
2161 TT_CLONE_SKIP(...)
2162     CODE:
2163         (void)items; /* avoid unused warning */
2164         RETVAL = 1;
2165     OUTPUT:
2166         RETVAL
2167
2168
2169 MODULE = Imager         PACKAGE = Imager
2170
2171
2172 undef_int
2173 i_tt_text(handle,im,xb,yb,cl,points,str_sv,len_ignored,smooth,utf8,align=1)
2174   Imager::Font::TT     handle
2175     Imager::ImgRaw     im
2176                i_img_dim     xb
2177                i_img_dim     yb
2178      Imager::Color     cl
2179              double     points
2180               SV *     str_sv
2181                int     smooth
2182                int     utf8
2183                int     align
2184              PREINIT:
2185                char *str;
2186                STRLEN len;
2187              CODE:
2188 #ifdef SvUTF8
2189                if (SvUTF8(str_sv))
2190                  utf8 = 1;
2191 #endif
2192                str = SvPV(str_sv, len);
2193                RETVAL = i_tt_text(handle, im, xb, yb, cl, points, str, 
2194                                   len, smooth, utf8, align);
2195              OUTPUT:
2196                RETVAL                
2197
2198
2199 undef_int
2200 i_tt_cp(handle,im,xb,yb,channel,points,str_sv,len_ignored,smooth,utf8,align=1)
2201   Imager::Font::TT     handle
2202     Imager::ImgRaw     im
2203                i_img_dim     xb
2204                i_img_dim     yb
2205                int     channel
2206              double     points
2207               SV *     str_sv
2208                int     smooth
2209                int     utf8
2210                int     align
2211              PREINIT:
2212                char *str;
2213                STRLEN len;
2214              CODE:
2215 #ifdef SvUTF8
2216                if (SvUTF8(str_sv))
2217                  utf8 = 1;
2218 #endif
2219                str = SvPV(str_sv, len);
2220                RETVAL = i_tt_cp(handle, im, xb, yb, channel, points, str, len,
2221                                 smooth, utf8, align);
2222              OUTPUT:
2223                 RETVAL
2224
2225
2226 void
2227 i_tt_bbox(handle,point,str_sv,len_ignored, utf8)
2228   Imager::Font::TT     handle
2229              double     point
2230                SV*    str_sv
2231                int     utf8
2232              PREINIT:
2233                i_img_dim cords[BOUNDING_BOX_COUNT];
2234                int rc;
2235                char *  str;
2236                STRLEN len;
2237                int i;
2238              PPCODE:
2239 #ifdef SvUTF8
2240                if (SvUTF8(ST(2)))
2241                  utf8 = 1;
2242 #endif
2243                str = SvPV(str_sv, len);
2244                if ((rc=i_tt_bbox(handle,point,str,len,cords, utf8))) {
2245                  EXTEND(SP, rc);
2246                  for (i = 0; i < rc; ++i) {
2247                    PUSHs(sv_2mortal(newSViv(cords[i])));
2248                  }
2249                }
2250
2251 void
2252 i_tt_has_chars(handle, text_sv, utf8)
2253         Imager::Font::TT handle
2254         SV  *text_sv
2255         int utf8
2256       PREINIT:
2257         char const *text;
2258         STRLEN len;
2259         char *work;
2260         size_t count;
2261         size_t i;
2262       PPCODE:
2263 #ifdef SvUTF8
2264         if (SvUTF8(text_sv))
2265           utf8 = 1;
2266 #endif
2267         text = SvPV(text_sv, len);
2268         work = mymalloc(len);
2269         count = i_tt_has_chars(handle, text, len, utf8, work);
2270         if (GIMME_V == G_ARRAY) {
2271           EXTEND(SP, count);
2272           for (i = 0; i < count; ++i) {
2273             PUSHs(boolSV(work[i]));
2274           }
2275         }
2276         else {
2277           EXTEND(SP, 1);
2278           PUSHs(sv_2mortal(newSVpv(work, count)));
2279         }
2280         myfree(work);
2281
2282 void
2283 i_tt_dump_names(handle)
2284         Imager::Font::TT handle
2285
2286 void
2287 i_tt_face_name(handle)
2288         Imager::Font::TT handle
2289       PREINIT:
2290         char name[255];
2291         size_t len;
2292       PPCODE:
2293         len = i_tt_face_name(handle, name, sizeof(name));
2294         if (len) {
2295           EXTEND(SP, 1);
2296           PUSHs(sv_2mortal(newSVpv(name, len-1)));
2297         }
2298
2299 void
2300 i_tt_glyph_name(handle, text_sv, utf8 = 0)
2301         Imager::Font::TT handle
2302         SV *text_sv
2303         int utf8
2304       PREINIT:
2305         char const *text;
2306         STRLEN work_len;
2307         size_t len;
2308         size_t outsize;
2309         char name[255];
2310       PPCODE:
2311 #ifdef SvUTF8
2312         if (SvUTF8(text_sv))
2313           utf8 = 1;
2314 #endif
2315         text = SvPV(text_sv, work_len);
2316         len = work_len;
2317         while (len) {
2318           unsigned long ch;
2319           if (utf8) {
2320             ch = i_utf8_advance(&text, &len);
2321             if (ch == ~0UL) {
2322               i_push_error(0, "invalid UTF8 character");
2323               break;
2324             }
2325           }
2326           else {
2327             ch = *text++;
2328             --len;
2329           }
2330           EXTEND(SP, 1);
2331           if ((outsize = i_tt_glyph_name(handle, ch, name, sizeof(name))) != 0) {
2332             PUSHs(sv_2mortal(newSVpv(name, 0)));
2333           }
2334           else {
2335             PUSHs(&PL_sv_undef);
2336           } 
2337         }
2338
2339 #endif 
2340
2341 const char *
2342 i_test_format_probe(ig, length)
2343         Imager::IO     ig
2344                int     length
2345
2346 Imager::ImgRaw
2347 i_readpnm_wiol(ig, allow_incomplete)
2348         Imager::IO     ig
2349                int     allow_incomplete
2350
2351
2352 void
2353 i_readpnm_multi_wiol(ig, allow_incomplete)
2354         Imager::IO ig
2355                int     allow_incomplete
2356       PREINIT:
2357         i_img **imgs;
2358         int count=0;
2359         int i;
2360       PPCODE:
2361         imgs = i_readpnm_multi_wiol(ig, &count, allow_incomplete);
2362         if (imgs) {
2363           EXTEND(SP, count);
2364           for (i = 0; i < count; ++i) {
2365             SV *sv = sv_newmortal();
2366             sv_setref_pv(sv, "Imager::ImgRaw", (void *)imgs[i]);
2367             PUSHs(sv);
2368           }
2369           myfree(imgs);
2370         }
2371
2372 undef_int
2373 i_writeppm_wiol(im, ig)
2374     Imager::ImgRaw     im
2375         Imager::IO     ig
2376
2377
2378
2379
2380
2381 Imager::ImgRaw
2382 i_readraw_wiol(ig,x,y,datachannels,storechannels,intrl)
2383         Imager::IO     ig
2384                i_img_dim     x
2385                i_img_dim     y
2386                int     datachannels
2387                int     storechannels
2388                int     intrl
2389
2390 undef_int
2391 i_writeraw_wiol(im,ig)
2392     Imager::ImgRaw     im
2393         Imager::IO     ig
2394
2395 undef_int
2396 i_writebmp_wiol(im,ig)
2397     Imager::ImgRaw     im
2398         Imager::IO     ig
2399
2400 Imager::ImgRaw
2401 i_readbmp_wiol(ig, allow_incomplete=0)
2402         Imager::IO     ig
2403         int            allow_incomplete
2404
2405
2406 undef_int
2407 i_writetga_wiol(im,ig, wierdpack, compress, idstring)
2408     Imager::ImgRaw     im
2409         Imager::IO     ig
2410                int     wierdpack
2411                int     compress
2412               char*    idstring
2413             PREINIT:
2414                 int idlen;
2415                CODE:
2416                 idlen  = SvCUR(ST(4));
2417                 RETVAL = i_writetga_wiol(im, ig, wierdpack, compress, idstring, idlen);
2418                 OUTPUT:
2419                 RETVAL
2420
2421
2422 Imager::ImgRaw
2423 i_readtga_wiol(ig, length)
2424         Imager::IO     ig
2425                int     length
2426
2427
2428
2429
2430 Imager::ImgRaw
2431 i_scaleaxis(im,Value,Axis)
2432     Imager::ImgRaw     im
2433              double     Value
2434                int     Axis
2435
2436 Imager::ImgRaw
2437 i_scale_nn(im,scx,scy)
2438     Imager::ImgRaw     im
2439              double    scx
2440              double    scy
2441
2442 Imager::ImgRaw
2443 i_scale_mixing(im, width, height)
2444     Imager::ImgRaw     im
2445                i_img_dim     width
2446                i_img_dim     height
2447
2448 Imager::ImgRaw
2449 i_haar(im)
2450     Imager::ImgRaw     im
2451
2452 int
2453 i_count_colors(im,maxc)
2454     Imager::ImgRaw     im
2455                int     maxc
2456
2457 void
2458 i_get_anonymous_color_histo(im, maxc = 0x40000000)
2459    Imager::ImgRaw  im
2460    int maxc
2461     PREINIT:
2462         int i;
2463         unsigned int * col_usage = NULL;
2464         int col_cnt;
2465     PPCODE:
2466         col_cnt = i_get_anonymous_color_histo(im, &col_usage, maxc);
2467         EXTEND(SP, col_cnt);
2468         for (i = 0; i < col_cnt; i++)  {
2469             PUSHs(sv_2mortal(newSViv( col_usage[i])));
2470         }
2471         myfree(col_usage);
2472         XSRETURN(col_cnt);
2473
2474
2475 void
2476 i_transform(im,opx,opy,parm)
2477     Imager::ImgRaw     im
2478              PREINIT:
2479              double* parm;
2480              int *opx;
2481              int *opy;
2482              int     opxl;
2483              int     opyl;
2484              int     parmlen;
2485              AV* av;
2486              SV* sv1;
2487              int i;
2488              i_img *result;
2489              PPCODE:
2490              if (!SvROK(ST(1))) croak("Imager: Parameter 1 must be a reference to an array\n");
2491              if (!SvROK(ST(2))) croak("Imager: Parameter 2 must be a reference to an array\n");
2492              if (!SvROK(ST(3))) croak("Imager: Parameter 3 must be a reference to an array\n");
2493              if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 must be a reference to an array\n");
2494              if (SvTYPE(SvRV(ST(2))) != SVt_PVAV) croak("Imager: Parameter 2 must be a reference to an array\n");
2495              if (SvTYPE(SvRV(ST(3))) != SVt_PVAV) croak("Imager: Parameter 3 must be a reference to an array\n");
2496              av=(AV*)SvRV(ST(1));
2497              opxl=av_len(av)+1;
2498              opx=mymalloc( opxl*sizeof(int) );
2499              for(i=0;i<opxl;i++) {
2500                sv1=(*(av_fetch(av,i,0)));
2501                opx[i]=(int)SvIV(sv1);
2502              }
2503              av=(AV*)SvRV(ST(2));
2504              opyl=av_len(av)+1;
2505              opy=mymalloc( opyl*sizeof(int) );
2506              for(i=0;i<opyl;i++) {
2507                sv1=(*(av_fetch(av,i,0)));
2508                opy[i]=(int)SvIV(sv1);
2509              }
2510              av=(AV*)SvRV(ST(3));
2511              parmlen=av_len(av)+1;
2512              parm=mymalloc( parmlen*sizeof(double) );
2513              for(i=0;i<parmlen;i++) { /* FIXME: Bug? */
2514                sv1=(*(av_fetch(av,i,0)));
2515                parm[i]=(double)SvNV(sv1);
2516              }
2517              result=i_transform(im,opx,opxl,opy,opyl,parm,parmlen);
2518              myfree(parm);
2519              myfree(opy);
2520              myfree(opx);
2521              if (result) {
2522                SV *result_sv = sv_newmortal();
2523                EXTEND(SP, 1);
2524                sv_setref_pv(result_sv, "Imager::ImgRaw", (void*)result);
2525                PUSHs(result_sv);
2526              }
2527
2528 void
2529 i_transform2(sv_width,sv_height,channels,sv_ops,av_n_regs,av_c_regs,av_in_imgs)
2530         SV *sv_width
2531         SV *sv_height
2532         SV *sv_ops
2533         AV *av_n_regs
2534         AV *av_c_regs
2535         AV *av_in_imgs
2536         int channels
2537              PREINIT:
2538              i_img_dim width;
2539              i_img_dim height;
2540              struct rm_op *ops;
2541              STRLEN ops_len;
2542              int ops_count;
2543              double *n_regs;
2544              int n_regs_count;
2545              i_color *c_regs;
2546              int c_regs_count;
2547              int in_imgs_count;
2548              i_img **in_imgs;
2549              SV *sv1;
2550              IV tmp;
2551              int i;
2552              i_img *result;
2553              PPCODE:
2554
2555              in_imgs_count = av_len(av_in_imgs)+1;
2556              for (i = 0; i < in_imgs_count; ++i) {
2557                sv1 = *av_fetch(av_in_imgs, i, 0);
2558                if (!sv_derived_from(sv1, "Imager::ImgRaw")) {
2559                  croak("sv_in_img must contain only images");
2560                }
2561              }
2562              if (in_imgs_count > 0) {
2563                in_imgs = mymalloc(in_imgs_count*sizeof(i_img*));
2564                for (i = 0; i < in_imgs_count; ++i) {              
2565                  sv1 = *av_fetch(av_in_imgs,i,0);
2566                  if (!sv_derived_from(sv1, "Imager::ImgRaw")) {
2567                    croak("Parameter 5 must contain only images");
2568                  }
2569                  tmp = SvIV((SV*)SvRV(sv1));
2570                  in_imgs[i] = INT2PTR(i_img*, tmp);
2571                }
2572              }
2573              else {
2574                /* no input images */
2575                in_imgs = NULL;
2576              }
2577              /* default the output size from the first input if possible */
2578              if (SvOK(sv_width))
2579                width = SvIV(sv_width);
2580              else if (in_imgs_count)
2581                width = in_imgs[0]->xsize;
2582              else
2583                croak("No output image width supplied");
2584
2585              if (SvOK(sv_height))
2586                height = SvIV(sv_height);
2587              else if (in_imgs_count)
2588                height = in_imgs[0]->ysize;
2589              else
2590                croak("No output image height supplied");
2591
2592              ops = (struct rm_op *)SvPV(sv_ops, ops_len);
2593              if (ops_len % sizeof(struct rm_op))
2594                  croak("Imager: Parameter 3 must be a bitmap of regops\n");
2595              ops_count = ops_len / sizeof(struct rm_op);
2596
2597              n_regs_count = av_len(av_n_regs)+1;
2598              n_regs = mymalloc(n_regs_count * sizeof(double));
2599              for (i = 0; i < n_regs_count; ++i) {
2600                sv1 = *av_fetch(av_n_regs,i,0);
2601                if (SvOK(sv1))
2602                  n_regs[i] = SvNV(sv1);
2603              }
2604              c_regs_count = av_len(av_c_regs)+1;
2605              c_regs = mymalloc(c_regs_count * sizeof(i_color));
2606              /* I don't bother initializing the colou?r registers */
2607
2608              result=i_transform2(width, height, channels, ops, ops_count, 
2609                                  n_regs, n_regs_count, 
2610                                  c_regs, c_regs_count, in_imgs, in_imgs_count);
2611              if (in_imgs)
2612                  myfree(in_imgs);
2613              myfree(n_regs);
2614              myfree(c_regs);
2615              if (result) {
2616                SV *result_sv = sv_newmortal();
2617                EXTEND(SP, 1);
2618                sv_setref_pv(result_sv, "Imager::ImgRaw", (void*)result);
2619                PUSHs(result_sv);
2620              }
2621
2622
2623 void
2624 i_contrast(im,intensity)
2625     Imager::ImgRaw     im
2626              float     intensity
2627
2628 void
2629 i_hardinvert(im)
2630     Imager::ImgRaw     im
2631
2632 void
2633 i_hardinvertall(im)
2634     Imager::ImgRaw     im
2635
2636 void
2637 i_noise(im,amount,type)
2638     Imager::ImgRaw     im
2639              float     amount
2640      unsigned char     type
2641
2642 void
2643 i_bumpmap(im,bump,channel,light_x,light_y,strength)
2644     Imager::ImgRaw     im
2645     Imager::ImgRaw     bump
2646                int     channel
2647          i_img_dim     light_x
2648          i_img_dim     light_y
2649          i_img_dim     strength
2650
2651
2652 void
2653 i_bumpmap_complex(im,bump,channel,tx,ty,Lx,Ly,Lz,cd,cs,n,Ia,Il,Is)
2654     Imager::ImgRaw     im
2655     Imager::ImgRaw     bump
2656                int     channel
2657                i_img_dim     tx
2658                i_img_dim     ty
2659              double     Lx
2660              double     Ly
2661              double     Lz
2662              float     cd
2663              float     cs
2664              float     n
2665      Imager::Color     Ia
2666      Imager::Color     Il
2667      Imager::Color     Is
2668
2669
2670
2671 void
2672 i_postlevels(im,levels)
2673     Imager::ImgRaw     im
2674              int       levels
2675
2676 void
2677 i_mosaic(im,size)
2678     Imager::ImgRaw     im
2679          i_img_dim     size
2680
2681 void
2682 i_watermark(im,wmark,tx,ty,pixdiff)
2683     Imager::ImgRaw     im
2684     Imager::ImgRaw     wmark
2685                i_img_dim     tx
2686                i_img_dim     ty
2687                int     pixdiff
2688
2689
2690 void
2691 i_autolevels(im,lsat,usat,skew)
2692     Imager::ImgRaw     im
2693              float     lsat
2694              float     usat
2695              float     skew
2696
2697 void
2698 i_radnoise(im,xo,yo,rscale,ascale)
2699     Imager::ImgRaw     im
2700              float     xo
2701              float     yo
2702              float     rscale
2703              float     ascale
2704
2705 void
2706 i_turbnoise(im, xo, yo, scale)
2707     Imager::ImgRaw     im
2708              float     xo
2709              float     yo
2710              float     scale
2711
2712
2713 void
2714 i_gradgen(im, ...)
2715     Imager::ImgRaw     im
2716       PREINIT:
2717         int num;
2718         i_img_dim *xo;
2719         i_img_dim *yo;
2720         i_color *ival;
2721         int dmeasure;
2722         int i;
2723         SV *sv;
2724         AV *axx;
2725         AV *ayy;
2726         AV *ac;
2727       CODE:
2728         if (items != 5)
2729             croak("Usage: i_gradgen(im, xo, yo, ival, dmeasure)");
2730         if (!SvROK(ST(1)) || ! SvTYPE(SvRV(ST(1))))
2731             croak("i_gradgen: Second argument must be an array ref");
2732         if (!SvROK(ST(2)) || ! SvTYPE(SvRV(ST(2))))
2733             croak("i_gradgen: Third argument must be an array ref");
2734         if (!SvROK(ST(3)) || ! SvTYPE(SvRV(ST(3))))
2735             croak("i_gradgen: Fourth argument must be an array ref");
2736         axx = (AV *)SvRV(ST(1));
2737         ayy = (AV *)SvRV(ST(2));
2738         ac  = (AV *)SvRV(ST(3));
2739         dmeasure = (int)SvIV(ST(4));
2740         
2741         num = av_len(axx) < av_len(ayy) ? av_len(axx) : av_len(ayy);
2742         num = num <= av_len(ac) ? num : av_len(ac);
2743         num++; 
2744         if (num < 2) croak("Usage: i_gradgen array refs must have more than 1 entry each");
2745         xo = mymalloc( sizeof(i_img_dim) * num );
2746         yo = mymalloc( sizeof(i_img_dim) * num );
2747         ival = mymalloc( sizeof(i_color) * num );
2748         for(i = 0; i<num; i++) {
2749           xo[i]   = (i_img_dim)SvIV(* av_fetch(axx, i, 0));
2750           yo[i]   = (i_img_dim)SvIV(* av_fetch(ayy, i, 0));
2751           sv = *av_fetch(ac, i, 0);
2752           if ( !sv_derived_from(sv, "Imager::Color") ) {
2753             free(axx); free(ayy); free(ac);
2754             croak("i_gradgen: Element of fourth argument is not derived from Imager::Color");
2755           }
2756           ival[i] = *INT2PTR(i_color *, SvIV((SV *)SvRV(sv)));
2757         }
2758         i_gradgen(im, num, xo, yo, ival, dmeasure);
2759         myfree(xo);
2760         myfree(yo);
2761         myfree(ival);
2762
2763 Imager::ImgRaw
2764 i_diff_image(im, im2, mindist=0)
2765     Imager::ImgRaw     im
2766     Imager::ImgRaw     im2
2767             double     mindist
2768
2769 undef_int
2770 i_fountain(im, xa, ya, xb, yb, type, repeat, combine, super_sample, ssample_param, segs)
2771     Imager::ImgRaw     im
2772             double     xa
2773             double     ya
2774             double     xb
2775             double     yb
2776                int     type
2777                int     repeat
2778                int     combine
2779                int     super_sample
2780             double     ssample_param
2781       PREINIT:
2782         AV *asegs;
2783         int count;
2784         i_fountain_seg *segs;
2785       CODE:
2786         if (!SvROK(ST(10)) || ! SvTYPE(SvRV(ST(10))))
2787             croak("i_fountain: argument 11 must be an array ref");
2788         
2789         asegs = (AV *)SvRV(ST(10));
2790         segs = load_fount_segs(aTHX_ asegs, &count);
2791         RETVAL = i_fountain(im, xa, ya, xb, yb, type, repeat, combine, 
2792                             super_sample, ssample_param, count, segs);
2793         myfree(segs);
2794       OUTPUT:
2795         RETVAL
2796
2797 Imager::FillHandle
2798 i_new_fill_fount(xa, ya, xb, yb, type, repeat, combine, super_sample, ssample_param, segs)
2799             double     xa
2800             double     ya
2801             double     xb
2802             double     yb
2803                int     type
2804                int     repeat
2805                int     combine
2806                int     super_sample
2807             double     ssample_param
2808       PREINIT:
2809         AV *asegs;
2810         int count;
2811         i_fountain_seg *segs;
2812       CODE:
2813         if (!SvROK(ST(9)) || ! SvTYPE(SvRV(ST(9))))
2814             croak("i_fountain: argument 11 must be an array ref");
2815         
2816         asegs = (AV *)SvRV(ST(9));
2817         segs = load_fount_segs(aTHX_ asegs, &count);
2818         RETVAL = i_new_fill_fount(xa, ya, xb, yb, type, repeat, combine, 
2819                                   super_sample, ssample_param, count, segs);
2820         myfree(segs);        
2821       OUTPUT:
2822         RETVAL
2823
2824 Imager::FillHandle
2825 i_new_fill_opacity(other_fill, alpha_mult)
2826     Imager::FillHandle other_fill
2827     double alpha_mult
2828
2829 void
2830 i_errors()
2831       PREINIT:
2832         i_errmsg *errors;
2833         int i;
2834         AV *av;
2835         SV *sv;
2836       PPCODE:
2837         errors = i_errors();
2838         i = 0;
2839         while (errors[i].msg) {
2840           av = newAV();
2841           sv = newSVpv(errors[i].msg, strlen(errors[i].msg));
2842           if (!av_store(av, 0, sv)) {
2843             SvREFCNT_dec(sv);
2844           }
2845           sv = newSViv(errors[i].code);
2846           if (!av_store(av, 1, sv)) {
2847             SvREFCNT_dec(sv);
2848           }
2849           PUSHs(sv_2mortal(newRV_noinc((SV*)av)));
2850           ++i;
2851         }
2852
2853 void
2854 i_clear_error()
2855
2856 void
2857 i_push_error(code, msg)
2858         int code
2859         const char *msg
2860
2861 undef_int
2862 i_nearest_color(im, ...)
2863     Imager::ImgRaw     im
2864       PREINIT:
2865         int num;
2866         i_img_dim *xo;
2867         i_img_dim *yo;
2868         i_color *ival;
2869         int dmeasure;
2870         int i;
2871         SV *sv;
2872         AV *axx;
2873         AV *ayy;
2874         AV *ac;
2875       CODE:
2876         if (items != 5)
2877             croak("Usage: i_nearest_color(im, xo, yo, ival, dmeasure)");
2878         if (!SvROK(ST(1)) || ! SvTYPE(SvRV(ST(1))))
2879             croak("i_nearest_color: Second argument must be an array ref");
2880         if (!SvROK(ST(2)) || ! SvTYPE(SvRV(ST(2))))
2881             croak("i_nearest_color: Third argument must be an array ref");
2882         if (!SvROK(ST(3)) || ! SvTYPE(SvRV(ST(3))))
2883             croak("i_nearest_color: Fourth argument must be an array ref");
2884         axx = (AV *)SvRV(ST(1));
2885         ayy = (AV *)SvRV(ST(2));
2886         ac  = (AV *)SvRV(ST(3));
2887         dmeasure = (int)SvIV(ST(4));
2888         
2889         num = av_len(axx) < av_len(ayy) ? av_len(axx) : av_len(ayy);
2890         num = num <= av_len(ac) ? num : av_len(ac);
2891         num++; 
2892         if (num < 2) croak("Usage: i_nearest_color array refs must have more than 1 entry each");
2893         xo = mymalloc( sizeof(i_img_dim) * num );
2894         yo = mymalloc( sizeof(i_img_dim) * num );
2895         ival = mymalloc( sizeof(i_color) * num );
2896         for(i = 0; i<num; i++) {
2897           xo[i]   = (i_img_dim)SvIV(* av_fetch(axx, i, 0));
2898           yo[i]   = (i_img_dim)SvIV(* av_fetch(ayy, i, 0));
2899           sv = *av_fetch(ac, i, 0);
2900           if ( !sv_derived_from(sv, "Imager::Color") ) {
2901             free(axx); free(ayy); free(ac);
2902             croak("i_nearest_color: Element of fourth argument is not derived from Imager::Color");
2903           }
2904           ival[i] = *INT2PTR(i_color *, SvIV((SV *)SvRV(sv)));
2905         }
2906         RETVAL = i_nearest_color(im, num, xo, yo, ival, dmeasure);
2907       OUTPUT:
2908         RETVAL
2909
2910 void
2911 malloc_state()
2912
2913 void
2914 DSO_open(filename)
2915              char*       filename
2916              PREINIT:
2917                void *rc;
2918                char *evstr;
2919              PPCODE:
2920                rc=DSO_open(filename,&evstr);
2921                if (rc!=NULL) {
2922                  if (evstr!=NULL) {
2923                    EXTEND(SP,2); 
2924                    PUSHs(sv_2mortal(newSViv(PTR2IV(rc))));
2925                    PUSHs(sv_2mortal(newSVpvn(evstr, strlen(evstr))));
2926                  } else {
2927                    EXTEND(SP,1);
2928                    PUSHs(sv_2mortal(newSViv(PTR2IV(rc))));
2929                  }
2930                }
2931
2932
2933 undef_int
2934 DSO_close(dso_handle)
2935              void*       dso_handle
2936
2937 void
2938 DSO_funclist(dso_handle_v)
2939              void*       dso_handle_v
2940              PREINIT:
2941                int i;
2942                DSO_handle *dso_handle;
2943                func_ptr *functions;
2944              PPCODE:
2945                dso_handle=(DSO_handle*)dso_handle_v;
2946                functions = DSO_funclist(dso_handle);
2947                i=0;
2948                while( functions[i].name != NULL) {
2949                  EXTEND(SP,1);
2950                  PUSHs(sv_2mortal(newSVpv(functions[i].name,0)));
2951                  EXTEND(SP,1);
2952                  PUSHs(sv_2mortal(newSVpv(functions[i++].pcode,0)));
2953                }
2954
2955 void
2956 DSO_call(handle,func_index,hv)
2957                void*  handle
2958                int    func_index
2959              PREINIT:
2960                HV* hv;
2961              PPCODE:
2962                if (!SvROK(ST(2))) croak("Imager: Parameter 2 must be a reference to a hash\n");        
2963                hv=(HV*)SvRV(ST(2));
2964                if (SvTYPE(hv)!=SVt_PVHV) croak("Imager: Parameter 2 must be a reference to a hash\n");
2965                DSO_call( (DSO_handle *)handle,func_index,hv);
2966
2967 SV *
2968 i_get_pixel(im, x, y)
2969         Imager::ImgRaw im
2970         i_img_dim x
2971         i_img_dim y;
2972       PREINIT:
2973         i_color *color;
2974       CODE:
2975         color = (i_color *)mymalloc(sizeof(i_color));
2976         if (i_gpix(im, x, y, color) == 0) {
2977           RETVAL = NEWSV(0, 0);
2978           sv_setref_pv(RETVAL, "Imager::Color", (void *)color);
2979         }
2980         else {
2981           myfree(color);
2982           RETVAL = &PL_sv_undef;
2983         }
2984       OUTPUT:
2985         RETVAL
2986         
2987
2988 int
2989 i_ppix(im, x, y, cl)
2990         Imager::ImgRaw im
2991         i_img_dim x
2992         i_img_dim y
2993         Imager::Color cl
2994
2995 Imager::ImgRaw
2996 i_img_pal_new(x, y, channels, maxpal)
2997         i_img_dim x
2998         i_img_dim y
2999         int     channels
3000         int     maxpal
3001
3002 Imager::ImgRaw
3003 i_img_to_pal(src, quant)
3004         Imager::ImgRaw src
3005       PREINIT:
3006         HV *hv;
3007         i_quantize quant;
3008       CODE:
3009         if (!SvROK(ST(1)) || ! SvTYPE(SvRV(ST(1))))
3010           croak("i_img_to_pal: second argument must be a hash ref");
3011         hv = (HV *)SvRV(ST(1));
3012         memset(&quant, 0, sizeof(quant));
3013         quant.version = 1;
3014         quant.mc_size = 256;
3015         ip_handle_quant_opts(aTHX_ &quant, hv);
3016         RETVAL = i_img_to_pal(src, &quant);
3017         if (RETVAL) {
3018           ip_copy_colors_back(aTHX_ hv, &quant);
3019         }
3020         ip_cleanup_quant_opts(aTHX_ &quant);
3021       OUTPUT:
3022         RETVAL
3023
3024 Imager::ImgRaw
3025 i_img_to_rgb(src)
3026         Imager::ImgRaw src
3027
3028 void
3029 i_img_make_palette(HV *quant_hv, ...)
3030       PREINIT:
3031         size_t count = items - 1;
3032         i_quantize quant;
3033         i_img **imgs = NULL;
3034         ssize_t i;
3035       PPCODE:
3036         if (count <= 0)
3037           croak("Please supply at least one image (%d)", (int)count);
3038         imgs = mymalloc(sizeof(i_img *) * count);
3039         for (i = 0; i < count; ++i) {
3040           SV *img_sv = ST(i + 1);
3041           if (SvROK(img_sv) && sv_derived_from(img_sv, "Imager::ImgRaw")) {
3042             imgs[i] = INT2PTR(i_img *, SvIV((SV*)SvRV(img_sv)));
3043           }
3044           else {
3045             myfree(imgs);
3046             croak("Image %d is not an image object", (int)i+1);
3047           }
3048         }
3049         memset(&quant, 0, sizeof(quant));
3050         quant.version = 1;
3051         quant.mc_size = 256;
3052         ip_handle_quant_opts(aTHX_ &quant, quant_hv);
3053         i_quant_makemap(&quant, imgs, count);
3054         EXTEND(SP, quant.mc_count);
3055         for (i = 0; i < quant.mc_count; ++i) {
3056           SV *sv_c = make_i_color_sv(aTHX_ quant.mc_colors + i);
3057           PUSHs(sv_c);
3058         }
3059         ip_cleanup_quant_opts(aTHX_ &quant);
3060         
3061
3062 void
3063 i_gpal(im, l, r, y)
3064         Imager::ImgRaw  im
3065         i_img_dim     l
3066         i_img_dim     r
3067         i_img_dim     y
3068       PREINIT:
3069         i_palidx *work;
3070         int count, i;
3071       PPCODE:
3072         if (l < r) {
3073           work = mymalloc((r-l) * sizeof(i_palidx));
3074           count = i_gpal(im, l, r, y, work);
3075           if (GIMME_V == G_ARRAY) {
3076             EXTEND(SP, count);
3077             for (i = 0; i < count; ++i) {
3078               PUSHs(sv_2mortal(newSViv(work[i])));
3079             }
3080           }
3081           else {
3082             EXTEND(SP, 1);
3083             PUSHs(sv_2mortal(newSVpv((char *)work, count * sizeof(i_palidx))));
3084           }
3085           myfree(work);
3086         }
3087         else {
3088           if (GIMME_V != G_ARRAY) {
3089             EXTEND(SP, 1);
3090             PUSHs(&PL_sv_undef);
3091           }
3092         }
3093
3094 int
3095 i_ppal(im, l, y, ...)
3096         Imager::ImgRaw  im
3097         i_img_dim     l
3098         i_img_dim     y
3099       PREINIT:
3100         i_palidx *work;
3101         i_img_dim i;
3102       CODE:
3103         if (items > 3) {
3104           work = malloc_temp(aTHX_ sizeof(i_palidx) * (items-3));
3105           for (i=0; i < items-3; ++i) {
3106             work[i] = SvIV(ST(i+3));
3107           }
3108           validate_i_ppal(im, work, items - 3);
3109           RETVAL = i_ppal(im, l, l+items-3, y, work);
3110         }
3111         else {
3112           RETVAL = 0;
3113         }
3114       OUTPUT:
3115         RETVAL
3116
3117 int
3118 i_ppal_p(im, l, y, data)
3119         Imager::ImgRaw  im
3120         i_img_dim     l
3121         i_img_dim     y
3122         SV *data
3123       PREINIT:
3124         i_palidx const *work;
3125         STRLEN len;
3126       CODE:
3127         work = (i_palidx const *)SvPV(data, len);
3128         len /= sizeof(i_palidx);
3129         if (len > 0) {
3130           validate_i_ppal(im, work, len);
3131           RETVAL = i_ppal(im, l, l+len, y, work);
3132         }
3133         else {
3134           RETVAL = 0;
3135         }
3136       OUTPUT:
3137         RETVAL
3138
3139 SV *
3140 i_addcolors(im, ...)
3141         Imager::ImgRaw  im
3142       PREINIT:
3143         int index;
3144         i_color *colors;
3145         int i;
3146       CODE:
3147         if (items < 2)
3148           croak("i_addcolors: no colors to add");
3149         colors = mymalloc((items-1) * sizeof(i_color));
3150         for (i=0; i < items-1; ++i) {
3151           if (sv_isobject(ST(i+1)) 
3152               && sv_derived_from(ST(i+1), "Imager::Color")) {
3153             IV tmp = SvIV((SV *)SvRV(ST(i+1)));
3154             colors[i] = *INT2PTR(i_color *, tmp);
3155           }
3156           else {
3157             myfree(colors);
3158             croak("i_addcolor: pixels must be Imager::Color objects");
3159           }
3160         }
3161         index = i_addcolors(im, colors, items-1);
3162         myfree(colors);
3163         if (index == 0) {
3164           RETVAL = newSVpv("0 but true", 0);
3165         }
3166         else if (index == -1) {
3167           RETVAL = &PL_sv_undef;
3168         }
3169         else {
3170           RETVAL = newSViv(index);
3171         }
3172       OUTPUT:
3173         RETVAL
3174
3175 undef_int 
3176 i_setcolors(im, index, ...)
3177         Imager::ImgRaw  im
3178         int index
3179       PREINIT:
3180         i_color *colors;
3181         int i;
3182       CODE:
3183         if (items < 3)
3184           croak("i_setcolors: no colors to add");
3185         colors = mymalloc((items-2) * sizeof(i_color));
3186         for (i=0; i < items-2; ++i) {
3187           if (sv_isobject(ST(i+2)) 
3188               && sv_derived_from(ST(i+2), "Imager::Color")) {
3189             IV tmp = SvIV((SV *)SvRV(ST(i+2)));
3190             colors[i] = *INT2PTR(i_color *, tmp);
3191           }
3192           else {
3193             myfree(colors);
3194             croak("i_setcolors: pixels must be Imager::Color objects");
3195           }
3196         }
3197         RETVAL = i_setcolors(im, index, colors, items-2);
3198         myfree(colors);
3199       OUTPUT:
3200         RETVAL
3201
3202 void
3203 i_getcolors(im, index, ...)
3204         Imager::ImgRaw im
3205         int index
3206       PREINIT:
3207         i_color *colors;
3208         int count = 1;
3209         int i;
3210       PPCODE:
3211         if (items > 3)
3212           croak("i_getcolors: too many arguments");
3213         if (items == 3)
3214           count = SvIV(ST(2));
3215         if (count < 1)
3216           croak("i_getcolors: count must be positive");
3217         colors = mymalloc(sizeof(i_color) * count);
3218         if (i_getcolors(im, index, colors, count)) {
3219           for (i = 0; i < count; ++i) {
3220             SV *sv = make_i_color_sv(aTHX_ colors+i);
3221             PUSHs(sv);
3222           }
3223         }
3224         myfree(colors);
3225
3226
3227 undef_neg_int
3228 i_colorcount(im)
3229         Imager::ImgRaw im
3230
3231 undef_neg_int
3232 i_maxcolors(im)
3233         Imager::ImgRaw im
3234
3235 SV *
3236 i_findcolor(im, color)
3237         Imager::ImgRaw im
3238         Imager::Color color
3239       PREINIT:
3240         i_palidx index;
3241       CODE:
3242         if (i_findcolor(im, color, &index)) {
3243           RETVAL = newSViv(index);
3244         }
3245         else {
3246           RETVAL = &PL_sv_undef;
3247         }
3248       OUTPUT:
3249         RETVAL
3250
3251 int
3252 i_img_bits(im)
3253         Imager::ImgRaw  im
3254
3255 int
3256 i_img_type(im)
3257         Imager::ImgRaw  im
3258
3259 int
3260 i_img_virtual(im)
3261         Imager::ImgRaw  im
3262
3263 void
3264 i_gsamp(im, l, r, y, channels)
3265         Imager::ImgRaw im
3266         i_img_dim l
3267         i_img_dim r
3268         i_img_dim y
3269         i_channel_list channels
3270       PREINIT:
3271         i_sample_t *data;
3272         i_img_dim count, i;
3273       PPCODE:
3274         if (l < r) {
3275           data = mymalloc(sizeof(i_sample_t) * (r-l) * channels.count); /* XXX: memleak? */
3276           count = i_gsamp(im, l, r, y, data, channels.channels, channels.count);
3277           if (GIMME_V == G_ARRAY) {
3278             EXTEND(SP, count);
3279             for (i = 0; i < count; ++i)
3280               PUSHs(sv_2mortal(newSViv(data[i])));
3281           }
3282           else {
3283             EXTEND(SP, 1);
3284             PUSHs(sv_2mortal(newSVpv((char *)data, count * sizeof(i_sample_t))));
3285           }
3286           myfree(data);
3287         }
3288         else {
3289           if (GIMME_V != G_ARRAY) {
3290             EXTEND(SP, 1);
3291             PUSHs(&PL_sv_undef);
3292           }
3293         }
3294
3295 undef_neg_int
3296 i_gsamp_bits(im, l, r, y, bits, target, offset, channels)
3297         Imager::ImgRaw im
3298         i_img_dim l
3299         i_img_dim r
3300         i_img_dim y
3301         int bits
3302         AV *target
3303         STRLEN offset
3304         i_channel_list channels
3305       PREINIT:
3306         unsigned *data;
3307         i_img_dim count, i;
3308       CODE:
3309         i_clear_error();
3310         if (items < 8)
3311           croak("No channel numbers supplied to g_samp()");
3312         if (l < r) {
3313           data = mymalloc(sizeof(unsigned) * (r-l) * channels.count);
3314           count = i_gsamp_bits(im, l, r, y, data, channels.channels, channels.count, bits);
3315           for (i = 0; i < count; ++i) {
3316             av_store(target, i+offset, newSVuv(data[i]));
3317           }
3318           myfree(data);
3319           RETVAL = count;
3320         }
3321         else {
3322           RETVAL = 0;
3323         }
3324       OUTPUT:
3325         RETVAL
3326
3327 undef_neg_int
3328 i_psamp_bits(im, l, y, bits, channels, data_av, data_offset = 0, pixel_count = -1)
3329         Imager::ImgRaw im
3330         i_img_dim l
3331         i_img_dim y
3332         int bits
3333         i_channel_list channels
3334         AV *data_av
3335         i_img_dim data_offset
3336         i_img_dim pixel_count
3337       PREINIT:
3338         STRLEN data_count;
3339         size_t data_used;
3340         unsigned *data;
3341         ptrdiff_t i;
3342       CODE:
3343         i_clear_error();
3344
3345         data_count = av_len(data_av) + 1;
3346         if (data_offset < 0) {
3347           croak("data_offset must be non-negative");
3348         }
3349         if (data_offset > data_count) {
3350           croak("data_offset greater than number of samples supplied");
3351         }
3352         if (pixel_count == -1 || 
3353             data_offset + pixel_count * channels.count > data_count) {
3354           pixel_count = (data_count - data_offset) / channels.count;
3355         }
3356
3357         data_used = pixel_count * channels.count;
3358         data = mymalloc(sizeof(unsigned) * data_count);
3359         for (i = 0; i < data_used; ++i)
3360           data[i] = SvUV(*av_fetch(data_av, data_offset + i, 0));
3361
3362         RETVAL = i_psamp_bits(im, l, l + pixel_count, y, data, channels.channels, 
3363                               channels.count, bits);
3364
3365         if (data)
3366           myfree(data);
3367       OUTPUT:
3368         RETVAL
3369
3370 undef_neg_int
3371 i_psamp(im, x, y, channels, data, offset = 0, width = -1)
3372         Imager::ImgRaw im
3373         i_img_dim x
3374         i_img_dim y
3375         i_channel_list channels
3376         i_sample_list data
3377         i_img_dim offset
3378         i_img_dim width
3379     PREINIT:
3380         i_img_dim r;
3381     CODE:
3382         i_clear_error();
3383         if (offset < 0) {
3384           i_push_error(0, "offset must be non-negative");
3385           XSRETURN_UNDEF;
3386         }
3387         if (offset > 0) {
3388           if (offset > data.count) {
3389             i_push_error(0, "offset greater than number of samples supplied");
3390             XSRETURN_UNDEF;
3391           }
3392           data.samples += offset;
3393           data.count -= offset;
3394         }
3395         if (width == -1 ||
3396             width * channels.count > data.count) {
3397           width = data.count / channels.count;
3398         }
3399         r = x + width;
3400         RETVAL = i_psamp(im, x, r, y, data.samples, channels.channels, channels.count);
3401     OUTPUT:
3402         RETVAL
3403
3404 undef_neg_int
3405 i_psampf(im, x, y, channels, data, offset = 0, width = -1)
3406         Imager::ImgRaw im
3407         i_img_dim x
3408         i_img_dim y
3409         i_channel_list channels
3410         i_fsample_list data
3411         i_img_dim offset
3412         i_img_dim width
3413     PREINIT:
3414         i_img_dim r;
3415     CODE:
3416         i_clear_error();
3417         if (offset < 0) {
3418           i_push_error(0, "offset must be non-negative");
3419           XSRETURN_UNDEF;
3420         }
3421         if (offset > 0) {
3422           if (offset > data.count) {
3423             i_push_error(0, "offset greater than number of samples supplied");
3424             XSRETURN_UNDEF;
3425           }
3426           data.samples += offset;
3427           data.count -= offset;
3428         }
3429         if (width == -1 ||
3430             width * channels.count > data.count) {
3431           width = data.count / channels.count;
3432         }
3433         r = x + width;
3434         RETVAL = i_psampf(im, x, r, y, data.samples, channels.channels, channels.count);
3435     OUTPUT:
3436         RETVAL
3437
3438 Imager::ImgRaw
3439 i_img_masked_new(targ, mask, x, y, w, h)
3440         Imager::ImgRaw targ
3441         i_img_dim x
3442         i_img_dim y
3443         i_img_dim w
3444         i_img_dim h
3445       PREINIT:
3446         i_img *mask;
3447       CODE:
3448         if (SvOK(ST(1))) {
3449           if (!sv_isobject(ST(1)) 
3450               || !sv_derived_from(ST(1), "Imager::ImgRaw")) {
3451             croak("i_img_masked_new: parameter 2 must undef or an image");
3452           }
3453           mask = INT2PTR(i_img *, SvIV((SV *)SvRV(ST(1))));
3454         }
3455         else
3456           mask = NULL;
3457         RETVAL = i_img_masked_new(targ, mask, x, y, w, h);
3458       OUTPUT:
3459         RETVAL
3460
3461 int
3462 i_plin(im, l, y, ...)
3463         Imager::ImgRaw  im
3464         i_img_dim     l
3465         i_img_dim     y
3466       PREINIT:
3467         i_color *work;
3468         STRLEN i;
3469         STRLEN len;
3470         size_t count;
3471       CODE:
3472         if (items > 3) {
3473           if (items == 4 && SvOK(ST(3)) && !SvROK(ST(3))) {
3474             /* supplied as a byte string */
3475             work = (i_color *)SvPV(ST(3), len);
3476             count = len / sizeof(i_color);
3477             if (count * sizeof(i_color) != len) {
3478               croak("i_plin: length of scalar argument must be multiple of sizeof i_color");
3479             }
3480             RETVAL = i_plin(im, l, l+count, y, work);
3481           }
3482           else {
3483             work = mymalloc(sizeof(i_color) * (items-3));
3484             for (i=0; i < items-3; ++i) {
3485               if (sv_isobject(ST(i+3)) 
3486                   && sv_derived_from(ST(i+3), "Imager::Color")) {
3487                 IV tmp = SvIV((SV *)SvRV(ST(i+3)));
3488                 work[i] = *INT2PTR(i_color *, tmp);
3489               }
3490               else {
3491                 myfree(work);
3492                 croak("i_plin: pixels must be Imager::Color objects");
3493               }
3494             }
3495             RETVAL = i_plin(im, l, l+items-3, y, work);
3496             myfree(work);
3497           }
3498         }
3499         else {
3500           RETVAL = 0;
3501         }
3502       OUTPUT:
3503         RETVAL
3504
3505 int
3506 i_ppixf(im, x, y, cl)
3507         Imager::ImgRaw im
3508         i_img_dim x
3509         i_img_dim y
3510         Imager::Color::Float cl
3511
3512 void
3513 i_gsampf(im, l, r, y, channels)
3514         Imager::ImgRaw im
3515         i_img_dim l
3516         i_img_dim r
3517         i_img_dim y
3518         i_channel_list channels
3519       PREINIT:
3520         i_fsample_t *data;
3521         i_img_dim count, i;
3522       PPCODE:
3523         if (l < r) {
3524           data = mymalloc(sizeof(i_fsample_t) * (r-l) * channels.count);
3525           count = i_gsampf(im, l, r, y, data, channels.channels, channels.count);
3526           if (GIMME_V == G_ARRAY) {
3527             EXTEND(SP, count);
3528             for (i = 0; i < count; ++i)
3529               PUSHs(sv_2mortal(newSVnv(data[i])));
3530           }
3531           else {
3532             EXTEND(SP, 1);
3533             PUSHs(sv_2mortal(newSVpv((void *)data, count * sizeof(i_fsample_t))));
3534           }
3535           myfree(data);
3536         }
3537         else {
3538           if (GIMME_V != G_ARRAY) {
3539             EXTEND(SP, 1);
3540             PUSHs(&PL_sv_undef);
3541           }
3542         }
3543
3544 int
3545 i_plinf(im, l, y, ...)
3546         Imager::ImgRaw  im
3547         i_img_dim     l
3548         i_img_dim     y
3549       PREINIT:
3550         i_fcolor *work;
3551         i_img_dim i;
3552         STRLEN len;
3553         size_t count;
3554       CODE:
3555         if (items > 3) {
3556           if (items == 4 && SvOK(ST(3)) && !SvROK(ST(3))) {
3557             /* supplied as a byte string */
3558             work = (i_fcolor *)SvPV(ST(3), len);
3559             count = len / sizeof(i_fcolor);
3560             if (count * sizeof(i_fcolor) != len) {
3561               croak("i_plin: length of scalar argument must be multiple of sizeof i_fcolor");
3562             }
3563             RETVAL = i_plinf(im, l, l+count, y, work);
3564           }
3565           else {
3566             work = mymalloc(sizeof(i_fcolor) * (items-3));
3567             for (i=0; i < items-3; ++i) {
3568               if (sv_isobject(ST(i+3)) 
3569                   && sv_derived_from(ST(i+3), "Imager::Color::Float")) {
3570                 IV tmp = SvIV((SV *)SvRV(ST(i+3)));
3571                 work[i] = *INT2PTR(i_fcolor *, tmp);
3572               }
3573               else {
3574                 myfree(work);
3575                 croak("i_plinf: pixels must be Imager::Color::Float objects");
3576               }
3577             }
3578             /**(char *)0 = 1;*/
3579             RETVAL = i_plinf(im, l, l+items-3, y, work);
3580             myfree(work);
3581           }
3582         }
3583         else {
3584           RETVAL = 0;
3585         }
3586       OUTPUT:
3587         RETVAL
3588
3589 SV *
3590 i_gpixf(im, x, y)
3591         Imager::ImgRaw im
3592         i_img_dim x
3593         i_img_dim y;
3594       PREINIT:
3595         i_fcolor *color;
3596       CODE:
3597         color = (i_fcolor *)mymalloc(sizeof(i_fcolor));
3598         if (i_gpixf(im, x, y, color) == 0) {
3599           RETVAL = NEWSV(0,0);
3600           sv_setref_pv(RETVAL, "Imager::Color::Float", (void *)color);
3601         }
3602         else {
3603           myfree(color);
3604           RETVAL = &PL_sv_undef;
3605         }
3606       OUTPUT:
3607         RETVAL
3608
3609 void
3610 i_glin(im, l, r, y)
3611         Imager::ImgRaw im
3612         i_img_dim l
3613         i_img_dim r
3614         i_img_dim y
3615       PREINIT:
3616         i_color *vals;
3617         i_img_dim count, i;
3618       PPCODE:
3619         if (l < r) {
3620           vals = mymalloc((r-l) * sizeof(i_color));
3621           memset(vals, 0, (r-l) * sizeof(i_color));
3622           count = i_glin(im, l, r, y, vals);
3623           if (GIMME_V == G_ARRAY) {
3624             EXTEND(SP, count);
3625             for (i = 0; i < count; ++i) {
3626               SV *sv = make_i_color_sv(aTHX_ vals+i);
3627               PUSHs(sv);
3628             }
3629           }
3630           else if (count) {
3631             EXTEND(SP, 1);
3632             PUSHs(sv_2mortal(newSVpv((void *)vals, count * sizeof(i_color))));
3633           }
3634           myfree(vals);
3635         }
3636
3637 void
3638 i_glinf(im, l, r, y)
3639         Imager::ImgRaw im
3640         i_img_dim l
3641         i_img_dim r
3642         i_img_dim y
3643       PREINIT:
3644         i_fcolor *vals;
3645         i_img_dim count, i;
3646         i_fcolor zero;
3647       PPCODE:
3648         for (i = 0; i < MAXCHANNELS; ++i)
3649           zero.channel[i] = 0;
3650         if (l < r) {
3651           vals = mymalloc((r-l) * sizeof(i_fcolor));
3652           for (i = 0; i < r-l; ++i)
3653             vals[i] = zero;
3654           count = i_glinf(im, l, r, y, vals);
3655           if (GIMME_V == G_ARRAY) {
3656             EXTEND(SP, count);
3657             for (i = 0; i < count; ++i) {
3658               SV *sv;
3659               i_fcolor *col = mymalloc(sizeof(i_fcolor));
3660               *col = vals[i];
3661               sv = sv_newmortal();
3662               sv_setref_pv(sv, "Imager::Color::Float", (void *)col);
3663               PUSHs(sv);
3664             }
3665           }
3666           else if (count) {
3667             EXTEND(SP, 1);
3668             PUSHs(sv_2mortal(newSVpv((void *)vals, count * sizeof(i_fcolor))));
3669           }
3670           myfree(vals);
3671         }
3672
3673 Imager::ImgRaw
3674 i_img_16_new(x, y, ch)
3675         i_img_dim x
3676         i_img_dim y
3677         int ch
3678
3679 Imager::ImgRaw
3680 i_img_to_rgb16(im)
3681        Imager::ImgRaw im
3682
3683 Imager::ImgRaw
3684 i_img_double_new(x, y, ch)
3685         i_img_dim x
3686         i_img_dim y
3687         int ch
3688
3689 Imager::ImgRaw
3690 i_img_to_drgb(im)
3691        Imager::ImgRaw im
3692
3693 undef_int
3694 i_tags_addn(im, name, code, idata)
3695         Imager::ImgRaw im
3696         int     code
3697         int     idata
3698       PREINIT:
3699         char *name;
3700         STRLEN len;
3701       CODE:
3702         if (SvOK(ST(1)))
3703           name = SvPV(ST(1), len);
3704         else
3705           name = NULL;
3706         RETVAL = i_tags_addn(&im->tags, name, code, idata);
3707       OUTPUT:
3708         RETVAL
3709
3710 undef_int
3711 i_tags_add(im, name, code, data, idata)
3712         Imager::ImgRaw  im
3713         int code
3714         int idata
3715       PREINIT:
3716         char *name;
3717         char *data;
3718         STRLEN len;
3719       CODE:
3720         if (SvOK(ST(1)))
3721           name = SvPV(ST(1), len);
3722         else
3723           name = NULL;
3724         if (SvOK(ST(3)))
3725           data = SvPV(ST(3), len);
3726         else {
3727           data = NULL;
3728           len = 0;
3729         }
3730         RETVAL = i_tags_add(&im->tags, name, code, data, len, idata);
3731       OUTPUT:
3732         RETVAL
3733
3734 SV *
3735 i_tags_find(im, name, start)
3736         Imager::ImgRaw  im
3737         char *name
3738         int start
3739       PREINIT:
3740         int entry;
3741       CODE:
3742         if (i_tags_find(&im->tags, name, start, &entry)) {
3743           if (entry == 0)
3744             RETVAL = newSVpv("0 but true", 0);
3745           else
3746             RETVAL = newSViv(entry);
3747         } else {
3748           RETVAL = &PL_sv_undef;
3749         }
3750       OUTPUT:
3751         RETVAL
3752
3753 SV *
3754 i_tags_findn(im, code, start)
3755         Imager::ImgRaw  im
3756         int             code
3757         int             start
3758       PREINIT:
3759         int entry;
3760       CODE:
3761         if (i_tags_findn(&im->tags, code, start, &entry)) {
3762           if (entry == 0)
3763             RETVAL = newSVpv("0 but true", 0);
3764           else
3765             RETVAL = newSViv(entry);
3766         }
3767         else {
3768           RETVAL = &PL_sv_undef;
3769         }
3770       OUTPUT:
3771         RETVAL
3772
3773 int
3774 i_tags_delete(im, entry)
3775         Imager::ImgRaw  im
3776         int             entry
3777       CODE:
3778         RETVAL = i_tags_delete(&im->tags, entry);
3779       OUTPUT:
3780         RETVAL
3781
3782 int
3783 i_tags_delbyname(im, name)
3784         Imager::ImgRaw  im
3785         char *          name
3786       CODE:
3787         RETVAL = i_tags_delbyname(&im->tags, name);
3788       OUTPUT:
3789         RETVAL
3790
3791 int
3792 i_tags_delbycode(im, code)
3793         Imager::ImgRaw  im
3794         int             code
3795       CODE:
3796         RETVAL = i_tags_delbycode(&im->tags, code);
3797       OUTPUT:
3798         RETVAL
3799
3800 void
3801 i_tags_get(im, index)
3802         Imager::ImgRaw  im
3803         int             index
3804       PPCODE:
3805         if (index >= 0 && index < im->tags.count) {
3806           i_img_tag *entry = im->tags.tags + index;
3807           EXTEND(SP, 5);
3808         
3809           if (entry->name) {
3810             PUSHs(sv_2mortal(newSVpv(entry->name, 0)));
3811           }
3812           else {
3813             PUSHs(sv_2mortal(newSViv(entry->code)));
3814           }
3815           if (entry->data) {
3816             PUSHs(sv_2mortal(newSVpvn(entry->data, entry->size)));
3817           }
3818           else {
3819             PUSHs(sv_2mortal(newSViv(entry->idata)));
3820           }
3821         }
3822
3823 void
3824 i_tags_get_string(im, what_sv)
3825         Imager::ImgRaw  im
3826         SV *what_sv
3827       PREINIT:
3828         char const *name = NULL;
3829         int code;
3830         char buffer[200];
3831       PPCODE:
3832         if (SvIOK(what_sv)) {
3833           code = SvIV(what_sv);
3834           name = NULL;
3835         }
3836         else {
3837           name = SvPV_nolen(what_sv);
3838           code = 0;
3839         }
3840         if (i_tags_get_string(&im->tags, name, code, buffer, sizeof(buffer))) {
3841           EXTEND(SP, 1);
3842           PUSHs(sv_2mortal(newSVpv(buffer, 0)));
3843         }
3844
3845 int
3846 i_tags_count(im)
3847         Imager::ImgRaw  im
3848       CODE:
3849         RETVAL = im->tags.count;
3850       OUTPUT:
3851         RETVAL
3852
3853
3854
3855 MODULE = Imager         PACKAGE = Imager::FillHandle PREFIX=IFILL_
3856
3857 void
3858 IFILL_DESTROY(fill)
3859         Imager::FillHandle fill
3860
3861 int
3862 IFILL_CLONE_SKIP(...)
3863     CODE:
3864         (void)items; /* avoid unused warning for XS variable */
3865         RETVAL = 1;
3866     OUTPUT:
3867         RETVAL
3868
3869 MODULE = Imager         PACKAGE = Imager
3870
3871 Imager::FillHandle
3872 i_new_fill_solid(cl, combine)
3873         Imager::Color cl
3874         int combine
3875
3876 Imager::FillHandle
3877 i_new_fill_solidf(cl, combine)
3878         Imager::Color::Float cl
3879         int combine
3880
3881 Imager::FillHandle
3882 i_new_fill_hatch(fg, bg, combine, hatch, cust_hatch, dx, dy)
3883         Imager::Color fg
3884         Imager::Color bg
3885         int combine
3886         int hatch
3887         i_img_dim dx
3888         i_img_dim dy
3889       PREINIT:
3890         unsigned char *cust_hatch;
3891         STRLEN len;
3892       CODE:
3893         if (SvOK(ST(4))) {
3894           cust_hatch = (unsigned char *)SvPV(ST(4), len);
3895         }
3896         else
3897           cust_hatch = NULL;
3898         RETVAL = i_new_fill_hatch(fg, bg, combine, hatch, cust_hatch, dx, dy);
3899       OUTPUT:
3900         RETVAL
3901
3902 Imager::FillHandle
3903 i_new_fill_hatchf(fg, bg, combine, hatch, cust_hatch, dx, dy)
3904         Imager::Color::Float fg
3905         Imager::Color::Float bg
3906         int combine
3907         int hatch
3908         i_img_dim dx
3909         i_img_dim dy
3910       PREINIT:
3911         unsigned char *cust_hatch;
3912         STRLEN len;
3913       CODE:
3914         if (SvOK(ST(4))) {
3915           cust_hatch = (unsigned char *)SvPV(ST(4), len);
3916         }
3917         else
3918           cust_hatch = NULL;
3919         RETVAL = i_new_fill_hatchf(fg, bg, combine, hatch, cust_hatch, dx, dy);
3920       OUTPUT:
3921         RETVAL
3922
3923 Imager::FillHandle
3924 i_new_fill_image(src, matrix, xoff, yoff, combine)
3925         Imager::ImgRaw src
3926         i_img_dim xoff
3927         i_img_dim yoff
3928         int combine
3929       PREINIT:
3930         double matrix[9];
3931         double *matrixp;
3932         AV *av;
3933         IV len;
3934         SV *sv1;
3935         int i;
3936       CODE:
3937         if (!SvOK(ST(1))) {
3938           matrixp = NULL;
3939         }
3940         else {
3941           if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVAV)
3942             croak("i_new_fill_image: parameter must be an arrayref");
3943           av=(AV*)SvRV(ST(1));
3944           len=av_len(av)+1;
3945           if (len > 9)
3946             len = 9;
3947           for (i = 0; i < len; ++i) {
3948             sv1=(*(av_fetch(av,i,0)));
3949             matrix[i] = SvNV(sv1);
3950           }
3951           for (; i < 9; ++i)
3952             matrix[i] = 0;
3953           matrixp = matrix;
3954         }
3955         RETVAL = i_new_fill_image(src, matrixp, xoff, yoff, combine);
3956       OUTPUT:
3957         RETVAL
3958
3959 MODULE = Imager  PACKAGE = Imager::Internal::Hlines  PREFIX=i_int_hlines_
3960
3961 # this class is only exposed for testing
3962
3963 int
3964 i_int_hlines_testing()
3965
3966 #if i_int_hlines_testing()
3967
3968 Imager::Internal::Hlines
3969 i_int_hlines_new(start_y, count_y, start_x, count_x)
3970         i_img_dim start_y
3971         int count_y
3972         i_img_dim start_x
3973         int count_x
3974
3975 Imager::Internal::Hlines
3976 i_int_hlines_new_img(im)
3977         Imager::ImgRaw im
3978
3979 void
3980 i_int_hlines_add(hlines, y, minx, width)
3981         Imager::Internal::Hlines hlines
3982         i_img_dim y
3983         i_img_dim minx
3984         i_img_dim width
3985
3986 void
3987 i_int_hlines_DESTROY(hlines)
3988         Imager::Internal::Hlines hlines
3989
3990 SV *
3991 i_int_hlines_dump(hlines)
3992         Imager::Internal::Hlines hlines
3993
3994 int
3995 i_int_hlines_CLONE_SKIP(cls)
3996
3997 #endif
3998
3999 BOOT:
4000         PERL_SET_GLOBAL_CALLBACKS;
4001         PERL_PL_SET_GLOBAL_CALLBACKS;