]> git.imager.perl.org - imager.git/blob - Imager.xs
doc cleanups for Imager::Fill
[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 #define IIM_new i_img_8_new
839 #define IIM_DESTROY i_img_destroy
840
841 #ifdef IMEXIF_ENABLE
842 #define i_exif_enabled() 1
843 #else
844 #define i_exif_enabled() 0
845 #endif
846
847 /* trying to use more C style names, map them here */
848 #define i_io_DESTROY(ig) io_glue_destroy(ig)
849
850 #define i_img_get_width(im) ((im)->xsize)
851 #define i_img_get_height(im) ((im)->ysize)
852
853 #define i_img_epsilonf() (DBL_EPSILON * 4)
854
855 /* avoid some xsubpp strangeness */
856 #define NEWLINE '\n'
857
858 MODULE = Imager         PACKAGE = Imager::Color PREFIX = ICL_
859
860 Imager::Color
861 ICL_new_internal(r,g,b,a)
862                unsigned char     r
863                unsigned char     g
864                unsigned char     b
865                unsigned char     a
866
867 void
868 ICL_DESTROY(cl)
869                Imager::Color    cl
870
871
872 void
873 ICL_set_internal(cl,r,g,b,a)
874                Imager::Color    cl
875                unsigned char     r
876                unsigned char     g
877                unsigned char     b
878                unsigned char     a
879            PPCODE:
880                ICL_set_internal(cl, r, g, b, a);
881                EXTEND(SP, 1);
882                PUSHs(ST(0));
883
884 void
885 ICL_info(cl)
886                Imager::Color    cl
887
888
889 void
890 ICL_rgba(cl)
891               Imager::Color     cl
892             PPCODE:
893                 EXTEND(SP, 4);
894                 PUSHs(sv_2mortal(newSVnv(cl->rgba.r)));
895                 PUSHs(sv_2mortal(newSVnv(cl->rgba.g)));
896                 PUSHs(sv_2mortal(newSVnv(cl->rgba.b)));
897                 PUSHs(sv_2mortal(newSVnv(cl->rgba.a)));
898
899 Imager::Color
900 i_hsv_to_rgb(c)
901         Imager::Color c
902       CODE:
903         RETVAL = mymalloc(sizeof(i_color));
904         *RETVAL = *c;
905         i_hsv_to_rgb(RETVAL);
906       OUTPUT:
907         RETVAL
908         
909 Imager::Color
910 i_rgb_to_hsv(c)
911         Imager::Color c
912       CODE:
913         RETVAL = mymalloc(sizeof(i_color));
914         *RETVAL = *c;
915         i_rgb_to_hsv(RETVAL);
916       OUTPUT:
917         RETVAL
918         
919
920
921 MODULE = Imager        PACKAGE = Imager::Color::Float  PREFIX=ICLF_
922
923 Imager::Color::Float
924 ICLF_new_internal(r, g, b, a)
925         double r
926         double g
927         double b
928         double a
929
930 void
931 ICLF_DESTROY(cl)
932         Imager::Color::Float    cl
933
934 void
935 ICLF_rgba(cl)
936         Imager::Color::Float    cl
937       PREINIT:
938         int ch;
939       PPCODE:
940         EXTEND(SP, MAXCHANNELS);
941         for (ch = 0; ch < MAXCHANNELS; ++ch) {
942         /* printf("%d: %g\n", ch, cl->channel[ch]); */
943           PUSHs(sv_2mortal(newSVnv(cl->channel[ch])));
944         }
945
946 void
947 ICLF_set_internal(cl,r,g,b,a)
948         Imager::Color::Float    cl
949         double     r
950         double     g
951         double     b
952         double     a
953       PPCODE:
954         cl->rgba.r = r;
955         cl->rgba.g = g;
956         cl->rgba.b = b;
957         cl->rgba.a = a;                
958         EXTEND(SP, 1);
959         PUSHs(ST(0));
960
961 Imager::Color::Float
962 i_hsv_to_rgb(c)
963         Imager::Color::Float c
964       CODE:
965         RETVAL = mymalloc(sizeof(i_fcolor));
966         *RETVAL = *c;
967         i_hsv_to_rgbf(RETVAL);
968       OUTPUT:
969         RETVAL
970         
971 Imager::Color::Float
972 i_rgb_to_hsv(c)
973         Imager::Color::Float c
974       CODE:
975         RETVAL = mymalloc(sizeof(i_fcolor));
976         *RETVAL = *c;
977         i_rgb_to_hsvf(RETVAL);
978       OUTPUT:
979         RETVAL
980
981 MODULE = Imager         PACKAGE = Imager::ImgRaw        PREFIX = IIM_
982
983 Imager::ImgRaw
984 IIM_new(x,y,ch)
985                i_img_dim     x
986                i_img_dim     y
987                int     ch
988
989 void
990 IIM_DESTROY(im)
991                Imager::ImgRaw    im
992
993
994
995 MODULE = Imager         PACKAGE = Imager
996
997 PROTOTYPES: ENABLE
998
999
1000 Imager::IO
1001 io_new_fd(fd)
1002                          int     fd
1003
1004 Imager::IO
1005 io_new_bufchain()
1006
1007
1008 Imager::IO
1009 io_new_buffer(data_sv)
1010           SV   *data_sv
1011         CODE:
1012           RETVAL = do_io_new_buffer(aTHX_ data_sv);
1013         OUTPUT:
1014           RETVAL
1015
1016 Imager::IO
1017 io_new_cb(writecb, readcb, seekcb, closecb, maxwrite = CBDATA_BUFSIZE)
1018         SV *writecb;
1019         SV *readcb;
1020         SV *seekcb;
1021         SV *closecb;
1022         int maxwrite;
1023       CODE:
1024         RETVAL = do_io_new_cb(aTHX_ writecb, readcb, seekcb, closecb);
1025       OUTPUT:
1026         RETVAL
1027
1028 SV *
1029 io_slurp(ig)
1030         Imager::IO     ig
1031              PREINIT:
1032               unsigned char*    data;
1033               size_t    tlength;
1034              CODE:
1035               data    = NULL;
1036               tlength = io_slurp(ig, &data);
1037               RETVAL = newSVpv((char *)data,tlength);
1038               myfree(data);
1039              OUTPUT:
1040               RETVAL
1041
1042
1043 undef_int
1044 i_set_image_file_limits(width, height, bytes)
1045         i_img_dim width
1046         i_img_dim height
1047         size_t bytes
1048
1049 void
1050 i_get_image_file_limits()
1051       PREINIT:
1052         i_img_dim width, height;
1053         size_t bytes;
1054       PPCODE:
1055         if (i_get_image_file_limits(&width, &height, &bytes)) {
1056           EXTEND(SP, 3);
1057           PUSHs(sv_2mortal(newSViv(width)));
1058           PUSHs(sv_2mortal(newSViv(height)));
1059           PUSHs(sv_2mortal(newSVuv(bytes)));
1060         }
1061
1062 bool
1063 i_int_check_image_file_limits(width, height, channels, sample_size)
1064         i_img_dim width
1065         i_img_dim height
1066         int channels
1067         size_t sample_size
1068   PROTOTYPE: DISABLE
1069
1070 MODULE = Imager         PACKAGE = Imager::IO    PREFIX = io_
1071
1072 Imager::IO
1073 io_new_fd(class, fd)
1074         int fd
1075     CODE:
1076         RETVAL = io_new_fd(fd);
1077     OUTPUT:
1078         RETVAL
1079
1080 Imager::IO
1081 io_new_buffer(class, data_sv)
1082         SV *data_sv
1083     CODE:
1084         RETVAL = do_io_new_buffer(aTHX_ data_sv);
1085     OUTPUT:
1086         RETVAL
1087
1088 Imager::IO
1089 io_new_cb(class, writecb, readcb, seekcb, closecb)
1090         SV *writecb;
1091         SV *readcb;
1092         SV *seekcb;
1093         SV *closecb;
1094     CODE:
1095         RETVAL = do_io_new_cb(aTHX_ writecb, readcb, seekcb, closecb);
1096     OUTPUT:
1097         RETVAL
1098
1099 Imager::IO
1100 io_new_bufchain(class)
1101     CODE:
1102         RETVAL = io_new_bufchain();
1103     OUTPUT:
1104         RETVAL
1105
1106 SV *
1107 io_slurp(class, ig)
1108         Imager::IO     ig
1109     PREINIT:
1110         unsigned char*    data;
1111         size_t    tlength;
1112     CODE:
1113         data    = NULL;
1114         tlength = io_slurp(ig, &data);
1115         RETVAL = newSVpv((char *)data,tlength);
1116         myfree(data);
1117     OUTPUT:
1118         RETVAL
1119
1120 MODULE = Imager         PACKAGE = Imager::IO    PREFIX = i_io_
1121
1122 IV
1123 i_io_raw_write(ig, data_sv)
1124         Imager::IO ig
1125         SV *data_sv
1126       PREINIT:
1127         void *data;
1128         STRLEN size;
1129       CODE:
1130 #ifdef SvUTF8
1131         if (SvUTF8(data_sv)) {
1132           data_sv = sv_2mortal(newSVsv(data_sv));
1133           /* yes, we want this to croak() if the SV can't be downgraded */
1134           sv_utf8_downgrade(data_sv, FALSE);
1135         }
1136 #endif        
1137         data = SvPV(data_sv, size);
1138         RETVAL = i_io_raw_write(ig, data, size);
1139       OUTPUT:
1140         RETVAL
1141
1142 void
1143 i_io_raw_read(ig, buffer_sv, size)
1144         Imager::IO ig
1145         SV *buffer_sv
1146         IV size
1147       PREINIT:
1148         void *buffer;
1149         ssize_t result;
1150       PPCODE:
1151         if (size <= 0)
1152           croak("size negative in call to i_io_raw_read()");
1153         /* prevent an undefined value warning if they supplied an 
1154            undef buffer.
1155            Orginally conditional on !SvOK(), but this will prevent the
1156            downgrade from croaking */
1157         sv_setpvn(buffer_sv, "", 0);
1158 #ifdef SvUTF8
1159         if (SvUTF8(buffer_sv))
1160           sv_utf8_downgrade(buffer_sv, FALSE);
1161 #endif
1162         buffer = SvGROW(buffer_sv, size+1);
1163         result = i_io_raw_read(ig, buffer, size);
1164         if (result >= 0) {
1165           SvCUR_set(buffer_sv, result);
1166           *SvEND(buffer_sv) = '\0';
1167           SvPOK_only(buffer_sv);
1168           EXTEND(SP, 1);
1169           PUSHs(sv_2mortal(newSViv(result)));
1170         }
1171         ST(1) = buffer_sv;
1172         SvSETMAGIC(ST(1));
1173
1174 void
1175 i_io_raw_read2(ig, size)
1176         Imager::IO ig
1177         IV size
1178       PREINIT:
1179         SV *buffer_sv;
1180         void *buffer;
1181         ssize_t result;
1182       PPCODE:
1183         if (size <= 0)
1184           croak("size negative in call to i_io_read2()");
1185         buffer_sv = newSV(size);
1186         buffer = SvGROW(buffer_sv, size+1);
1187         result = i_io_raw_read(ig, buffer, size);
1188         if (result >= 0) {
1189           SvCUR_set(buffer_sv, result);
1190           *SvEND(buffer_sv) = '\0';
1191           SvPOK_only(buffer_sv);
1192           EXTEND(SP, 1);
1193           PUSHs(sv_2mortal(buffer_sv));
1194         }
1195         else {
1196           /* discard it */
1197           SvREFCNT_dec(buffer_sv);
1198         }
1199
1200 off_t
1201 i_io_raw_seek(ig, position, whence)
1202         Imager::IO ig
1203         off_t position
1204         int whence
1205
1206 int
1207 i_io_raw_close(ig)
1208         Imager::IO ig
1209
1210 void
1211 i_io_DESTROY(ig)
1212         Imager::IO     ig
1213
1214 int
1215 i_io_CLONE_SKIP(...)
1216     CODE:
1217         (void)items; /* avoid unused warning for XS variable */
1218         RETVAL = 1;
1219     OUTPUT:
1220         RETVAL
1221
1222 int
1223 i_io_getc(ig)
1224         Imager::IO ig
1225
1226 int
1227 i_io_putc(ig, c)
1228         Imager::IO ig
1229         int c
1230
1231 int
1232 i_io_close(ig)
1233         Imager::IO ig
1234
1235 int
1236 i_io_flush(ig)
1237         Imager::IO ig
1238
1239 int
1240 i_io_peekc(ig)
1241         Imager::IO ig
1242
1243 int
1244 i_io_seek(ig, off, whence)
1245         Imager::IO ig
1246         off_t off
1247         int whence
1248
1249 void
1250 i_io_peekn(ig, size)
1251         Imager::IO ig
1252         STRLEN size
1253       PREINIT:
1254         SV *buffer_sv;
1255         void *buffer;
1256         ssize_t result;
1257       PPCODE:
1258         buffer_sv = newSV(size+1);
1259         buffer = SvGROW(buffer_sv, size+1);
1260         result = i_io_peekn(ig, buffer, size);
1261         if (result >= 0) {
1262           SvCUR_set(buffer_sv, result);
1263           *SvEND(buffer_sv) = '\0';
1264           SvPOK_only(buffer_sv);
1265           EXTEND(SP, 1);
1266           PUSHs(sv_2mortal(buffer_sv));
1267         }
1268         else {
1269           /* discard it */
1270           SvREFCNT_dec(buffer_sv);
1271         }
1272
1273 void
1274 i_io_read(ig, buffer_sv, size)
1275         Imager::IO ig
1276         SV *buffer_sv
1277         IV size
1278       PREINIT:
1279         void *buffer;
1280         ssize_t result;
1281       PPCODE:
1282         if (size <= 0)
1283           croak("size negative in call to i_io_read()");
1284         /* prevent an undefined value warning if they supplied an 
1285            undef buffer.
1286            Orginally conditional on !SvOK(), but this will prevent the
1287            downgrade from croaking */
1288         sv_setpvn(buffer_sv, "", 0);
1289 #ifdef SvUTF8
1290         if (SvUTF8(buffer_sv))
1291           sv_utf8_downgrade(buffer_sv, FALSE);
1292 #endif
1293         buffer = SvGROW(buffer_sv, size+1);
1294         result = i_io_read(ig, buffer, size);
1295         if (result >= 0) {
1296           SvCUR_set(buffer_sv, result);
1297           *SvEND(buffer_sv) = '\0';
1298           SvPOK_only(buffer_sv);
1299           EXTEND(SP, 1);
1300           PUSHs(sv_2mortal(newSViv(result)));
1301         }
1302         ST(1) = buffer_sv;
1303         SvSETMAGIC(ST(1));
1304
1305 void
1306 i_io_read2(ig, size)
1307         Imager::IO ig
1308         STRLEN size
1309       PREINIT:
1310         SV *buffer_sv;
1311         void *buffer;
1312         ssize_t result;
1313       PPCODE:
1314         if (size == 0)
1315           croak("size zero in call to read2()");
1316         buffer_sv = newSV(size);
1317         buffer = SvGROW(buffer_sv, size+1);
1318         result = i_io_read(ig, buffer, size);
1319         if (result > 0) {
1320           SvCUR_set(buffer_sv, result);
1321           *SvEND(buffer_sv) = '\0';
1322           SvPOK_only(buffer_sv);
1323           EXTEND(SP, 1);
1324           PUSHs(sv_2mortal(buffer_sv));
1325         }
1326         else {
1327           /* discard it */
1328           SvREFCNT_dec(buffer_sv);
1329         }
1330
1331 void
1332 i_io_gets(ig, size = 8192, eol = NEWLINE)
1333         Imager::IO ig
1334         STRLEN size
1335         int eol
1336       PREINIT:
1337         SV *buffer_sv;
1338         void *buffer;
1339         ssize_t result;
1340       PPCODE:
1341         if (size < 2)
1342           croak("size too small in call to gets()");
1343         buffer_sv = sv_2mortal(newSV(size+1));
1344         buffer = SvPVX(buffer_sv);
1345         result = i_io_gets(ig, buffer, size+1, eol);
1346         if (result > 0) {
1347           SvCUR_set(buffer_sv, result);
1348           *SvEND(buffer_sv) = '\0';
1349           SvPOK_only(buffer_sv);
1350           EXTEND(SP, 1);
1351           PUSHs(buffer_sv);
1352         }
1353
1354 IV
1355 i_io_write(ig, data_sv)
1356         Imager::IO ig
1357         SV *data_sv
1358       PREINIT:
1359         void *data;
1360         STRLEN size;
1361       CODE:
1362 #ifdef SvUTF8
1363         if (SvUTF8(data_sv)) {
1364           data_sv = sv_2mortal(newSVsv(data_sv));
1365           /* yes, we want this to croak() if the SV can't be downgraded */
1366           sv_utf8_downgrade(data_sv, FALSE);
1367         }
1368 #endif        
1369         data = SvPV(data_sv, size);
1370         RETVAL = i_io_write(ig, data, size);
1371       OUTPUT:
1372         RETVAL
1373
1374 void
1375 i_io_dump(ig, flags = I_IO_DUMP_DEFAULT)
1376         Imager::IO ig
1377         int flags
1378
1379 bool
1380 i_io_set_buffered(ig, flag = 1)
1381         Imager::IO ig
1382         int flag
1383
1384 bool
1385 i_io_is_buffered(ig)
1386         Imager::IO ig
1387
1388 bool
1389 i_io_eof(ig)
1390         Imager::IO ig
1391
1392 bool
1393 i_io_error(ig)
1394         Imager::IO ig
1395
1396 MODULE = Imager         PACKAGE = Imager
1397
1398 PROTOTYPES: ENABLE
1399
1400 void
1401 i_list_formats()
1402              PREINIT:
1403               char*    item;
1404                int     i;
1405              PPCODE:
1406                i=0;
1407                while( (item=i_format_list[i++]) != NULL ) {
1408                       EXTEND(SP, 1);
1409                       PUSHs(sv_2mortal(newSVpv(item,0)));
1410                }
1411
1412 Imager::ImgRaw
1413 i_sametype(im, x, y)
1414     Imager::ImgRaw im
1415                i_img_dim x
1416                i_img_dim y
1417
1418 Imager::ImgRaw
1419 i_sametype_chans(im, x, y, channels)
1420     Imager::ImgRaw im
1421                i_img_dim x
1422                i_img_dim y
1423                int channels
1424
1425 int
1426 i_init_log(name_sv,level)
1427               SV*    name_sv
1428                int     level
1429         PREINIT:
1430           const char *name = SvOK(name_sv) ? SvPV_nolen(name_sv) : NULL;
1431         CODE:
1432           RETVAL = i_init_log(name, level);
1433         OUTPUT:
1434           RETVAL
1435
1436 void
1437 i_log_entry(string,level)
1438               char*    string
1439                int     level
1440
1441 int
1442 i_log_enabled()
1443
1444 void
1445 i_img_info(im)
1446     Imager::ImgRaw     im
1447              PREINIT:
1448                i_img_dim     info[4];
1449              PPCODE:
1450                i_img_info(im,info);
1451                EXTEND(SP, 4);
1452                PUSHs(sv_2mortal(newSViv(info[0])));
1453                PUSHs(sv_2mortal(newSViv(info[1])));
1454                PUSHs(sv_2mortal(newSViv(info[2])));
1455                PUSHs(sv_2mortal(newSViv(info[3])));
1456
1457
1458
1459
1460 void
1461 i_img_setmask(im,ch_mask)
1462     Imager::ImgRaw     im
1463                int     ch_mask
1464
1465 int
1466 i_img_getmask(im)
1467     Imager::ImgRaw     im
1468
1469 int
1470 i_img_getchannels(im)
1471     Imager::ImgRaw     im
1472
1473 void
1474 i_img_getdata(im)
1475     Imager::ImgRaw     im
1476              PPCODE:
1477                EXTEND(SP, 1);
1478                PUSHs(im->idata ? 
1479                      sv_2mortal(newSVpv((char *)im->idata, im->bytes)) 
1480                      : &PL_sv_undef);
1481
1482 IV
1483 i_img_get_width(im)
1484     Imager::ImgRaw      im
1485
1486 IV
1487 i_img_get_height(im)
1488     Imager::ImgRaw      im
1489
1490
1491 void
1492 i_img_is_monochrome(im)
1493         Imager::ImgRaw im
1494       PREINIT:
1495         int zero_is_white;
1496         int result;
1497       PPCODE:
1498         result = i_img_is_monochrome(im, &zero_is_white);
1499         if (result) {
1500           if (GIMME_V == G_ARRAY) {
1501             EXTEND(SP, 2);
1502             PUSHs(&PL_sv_yes);
1503             PUSHs(sv_2mortal(newSViv(zero_is_white)));
1504           }
1505           else {
1506             EXTEND(SP, 1);
1507             PUSHs(&PL_sv_yes);
1508           }
1509         }
1510
1511 void
1512 i_line(im,x1,y1,x2,y2,val,endp)
1513     Imager::ImgRaw     im
1514                i_img_dim     x1
1515                i_img_dim     y1
1516                i_img_dim     x2
1517                i_img_dim     y2
1518      Imager::Color     val
1519                int     endp
1520
1521 void
1522 i_line_aa(im,x1,y1,x2,y2,val,endp)
1523     Imager::ImgRaw     im
1524                i_img_dim     x1
1525                i_img_dim     y1
1526                i_img_dim     x2
1527                i_img_dim     y2
1528      Imager::Color     val
1529                int     endp
1530
1531 void
1532 i_box(im,x1,y1,x2,y2,val)
1533     Imager::ImgRaw     im
1534                i_img_dim     x1
1535                i_img_dim     y1
1536                i_img_dim     x2
1537                i_img_dim     y2
1538      Imager::Color     val
1539
1540 void
1541 i_box_filled(im,x1,y1,x2,y2,val)
1542     Imager::ImgRaw     im
1543                i_img_dim     x1
1544                i_img_dim     y1
1545                i_img_dim     x2
1546                i_img_dim     y2
1547            Imager::Color    val
1548
1549 int
1550 i_box_filledf(im,x1,y1,x2,y2,val)
1551     Imager::ImgRaw     im
1552                i_img_dim     x1
1553                i_img_dim     y1
1554                i_img_dim     x2
1555                i_img_dim     y2
1556            Imager::Color::Float    val
1557
1558 void
1559 i_box_cfill(im,x1,y1,x2,y2,fill)
1560     Imager::ImgRaw     im
1561                i_img_dim     x1
1562                i_img_dim     y1
1563                i_img_dim     x2
1564                i_img_dim     y2
1565            Imager::FillHandle    fill
1566
1567 void
1568 i_arc(im,x,y,rad,d1,d2,val)
1569     Imager::ImgRaw     im
1570                i_img_dim     x
1571                i_img_dim     y
1572              double     rad
1573              double     d1
1574              double     d2
1575            Imager::Color    val
1576
1577 void
1578 i_arc_aa(im,x,y,rad,d1,d2,val)
1579     Imager::ImgRaw     im
1580             double     x
1581             double     y
1582             double     rad
1583             double     d1
1584             double     d2
1585            Imager::Color    val
1586
1587 void
1588 i_arc_cfill(im,x,y,rad,d1,d2,fill)
1589     Imager::ImgRaw     im
1590                i_img_dim     x
1591                i_img_dim     y
1592              double     rad
1593              double     d1
1594              double     d2
1595            Imager::FillHandle    fill
1596
1597 void
1598 i_arc_aa_cfill(im,x,y,rad,d1,d2,fill)
1599     Imager::ImgRaw     im
1600             double     x
1601             double     y
1602             double     rad
1603             double     d1
1604             double     d2
1605            Imager::FillHandle   fill
1606
1607
1608 void
1609 i_circle_aa(im,x,y,rad,val)
1610     Imager::ImgRaw     im
1611              double     x
1612              double     y
1613              double     rad
1614            Imager::Color    val
1615
1616 int
1617 i_circle_out(im,x,y,rad,val)
1618     Imager::ImgRaw     im
1619              i_img_dim     x
1620              i_img_dim     y
1621              i_img_dim     rad
1622            Imager::Color    val
1623
1624 int
1625 i_circle_out_aa(im,x,y,rad,val)
1626     Imager::ImgRaw     im
1627              i_img_dim     x
1628              i_img_dim     y
1629              i_img_dim     rad
1630            Imager::Color    val
1631
1632 int
1633 i_arc_out(im,x,y,rad,d1,d2,val)
1634     Imager::ImgRaw     im
1635              i_img_dim     x
1636              i_img_dim     y
1637              i_img_dim     rad
1638              double d1
1639              double d2
1640            Imager::Color    val
1641
1642 int
1643 i_arc_out_aa(im,x,y,rad,d1,d2,val)
1644     Imager::ImgRaw     im
1645              i_img_dim     x
1646              i_img_dim     y
1647              i_img_dim     rad
1648              double d1
1649              double d2
1650            Imager::Color    val
1651
1652
1653 void
1654 i_bezier_multi(im,xc,yc,val)
1655     Imager::ImgRaw     im
1656              Imager::Color  val
1657              PREINIT:
1658              double   *x,*y;
1659              int       len;
1660              AV       *av1;
1661              AV       *av2;
1662              SV       *sv1;
1663              SV       *sv2;
1664              int i;
1665              PPCODE:
1666              ICL_info(val);
1667              if (!SvROK(ST(1))) croak("Imager: Parameter 1 to i_bezier_multi must be a reference to an array\n");
1668              if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 to i_bezier_multi must be a reference to an array\n");
1669              if (!SvROK(ST(2))) croak("Imager: Parameter 2 to i_bezier_multi must be a reference to an array\n");
1670              if (SvTYPE(SvRV(ST(2))) != SVt_PVAV) croak("Imager: Parameter 2 to i_bezier_multi must be a reference to an array\n");
1671              av1=(AV*)SvRV(ST(1));
1672              av2=(AV*)SvRV(ST(2));
1673              if (av_len(av1) != av_len(av2)) croak("Imager: x and y arrays to i_bezier_multi must be equal length\n");
1674              len=av_len(av1)+1;
1675              x=mymalloc( len*sizeof(double) );
1676              y=mymalloc( len*sizeof(double) );
1677              for(i=0;i<len;i++) {
1678                sv1=(*(av_fetch(av1,i,0)));
1679                sv2=(*(av_fetch(av2,i,0)));
1680                x[i]=(double)SvNV(sv1);
1681                y[i]=(double)SvNV(sv2);
1682              }
1683              i_bezier_multi(im,len,x,y,val);
1684              myfree(x);
1685              myfree(y);
1686
1687
1688 int
1689 i_poly_aa(im,xc,yc,val)
1690     Imager::ImgRaw     im
1691              Imager::Color  val
1692              PREINIT:
1693              double   *x,*y;
1694              int       len;
1695              AV       *av1;
1696              AV       *av2;
1697              SV       *sv1;
1698              SV       *sv2;
1699              int i;
1700              CODE:
1701              ICL_info(val);
1702              if (!SvROK(ST(1))) croak("Imager: Parameter 1 to i_poly_aa must be a reference to an array\n");
1703              if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 to i_poly_aa must be a reference to an array\n");
1704              if (!SvROK(ST(2))) croak("Imager: Parameter 1 to i_poly_aa must be a reference to an array\n");
1705              if (SvTYPE(SvRV(ST(2))) != SVt_PVAV) croak("Imager: Parameter 1 to i_poly_aa must be a reference to an array\n");
1706              av1=(AV*)SvRV(ST(1));
1707              av2=(AV*)SvRV(ST(2));
1708              if (av_len(av1) != av_len(av2)) croak("Imager: x and y arrays to i_poly_aa must be equal length\n");
1709              len=av_len(av1)+1;
1710              x=mymalloc( len*sizeof(double) );
1711              y=mymalloc( len*sizeof(double) );
1712              for(i=0;i<len;i++) {
1713                sv1=(*(av_fetch(av1,i,0)));
1714                sv2=(*(av_fetch(av2,i,0)));
1715                x[i]=(double)SvNV(sv1);
1716                y[i]=(double)SvNV(sv2);
1717              }
1718              RETVAL = i_poly_aa(im,len,x,y,val);
1719              myfree(x);
1720              myfree(y);
1721              OUTPUT:
1722                RETVAL
1723
1724 int
1725 i_poly_aa_cfill(im,xc,yc,fill)
1726     Imager::ImgRaw     im
1727      Imager::FillHandle     fill
1728              PREINIT:
1729              double   *x,*y;
1730              int       len;
1731              AV       *av1;
1732              AV       *av2;
1733              SV       *sv1;
1734              SV       *sv2;
1735              int i;
1736              CODE:
1737              if (!SvROK(ST(1))) croak("Imager: Parameter 1 to i_poly_aa_cfill must be a reference to an array\n");
1738              if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 to i_poly_aa_cfill must be a reference to an array\n");
1739              if (!SvROK(ST(2))) croak("Imager: Parameter 1 to i_poly_aa_cfill must be a reference to an array\n");
1740              if (SvTYPE(SvRV(ST(2))) != SVt_PVAV) croak("Imager: Parameter 1 to i_poly_aa_cfill must be a reference to an array\n");
1741              av1=(AV*)SvRV(ST(1));
1742              av2=(AV*)SvRV(ST(2));
1743              if (av_len(av1) != av_len(av2)) croak("Imager: x and y arrays to i_poly_aa_cfill must be equal length\n");
1744              len=av_len(av1)+1;
1745              x=mymalloc( len*sizeof(double) );
1746              y=mymalloc( len*sizeof(double) );
1747              for(i=0;i<len;i++) {
1748                sv1=(*(av_fetch(av1,i,0)));
1749                sv2=(*(av_fetch(av2,i,0)));
1750                x[i]=(double)SvNV(sv1);
1751                y[i]=(double)SvNV(sv2);
1752              }
1753              RETVAL = i_poly_aa_cfill(im,len,x,y,fill);
1754              myfree(x);
1755              myfree(y);
1756              OUTPUT:
1757                RETVAL
1758
1759
1760
1761 undef_int
1762 i_flood_fill(im,seedx,seedy,dcol)
1763     Imager::ImgRaw     im
1764                i_img_dim     seedx
1765                i_img_dim     seedy
1766      Imager::Color     dcol
1767
1768 undef_int
1769 i_flood_cfill(im,seedx,seedy,fill)
1770     Imager::ImgRaw     im
1771                i_img_dim     seedx
1772                i_img_dim     seedy
1773      Imager::FillHandle     fill
1774
1775 undef_int
1776 i_flood_fill_border(im,seedx,seedy,dcol, border)
1777     Imager::ImgRaw     im
1778                i_img_dim     seedx
1779                i_img_dim     seedy
1780      Imager::Color     dcol
1781      Imager::Color     border
1782
1783 undef_int
1784 i_flood_cfill_border(im,seedx,seedy,fill, border)
1785     Imager::ImgRaw     im
1786                i_img_dim     seedx
1787                i_img_dim     seedy
1788      Imager::FillHandle     fill
1789      Imager::Color     border
1790
1791
1792 void
1793 i_copyto(im,src,x1,y1,x2,y2,tx,ty)
1794     Imager::ImgRaw     im
1795     Imager::ImgRaw     src
1796                i_img_dim     x1
1797                i_img_dim     y1
1798                i_img_dim     x2
1799                i_img_dim     y2
1800                i_img_dim     tx
1801                i_img_dim     ty
1802
1803
1804 void
1805 i_copyto_trans(im,src,x1,y1,x2,y2,tx,ty,trans)
1806     Imager::ImgRaw     im
1807     Imager::ImgRaw     src
1808                i_img_dim     x1
1809                i_img_dim     y1
1810                i_img_dim     x2
1811                i_img_dim     y2
1812                i_img_dim     tx
1813                i_img_dim     ty
1814      Imager::Color     trans
1815
1816 Imager::ImgRaw
1817 i_copy(src)
1818     Imager::ImgRaw     src
1819
1820
1821 undef_int
1822 i_rubthru(im,src,tx,ty,src_minx,src_miny,src_maxx,src_maxy)
1823     Imager::ImgRaw     im
1824     Imager::ImgRaw     src
1825                i_img_dim     tx
1826                i_img_dim     ty
1827                i_img_dim     src_minx
1828                i_img_dim     src_miny
1829                i_img_dim     src_maxx
1830                i_img_dim     src_maxy
1831
1832 undef_int
1833 i_compose(out, src, out_left, out_top, src_left, src_top, width, height, combine = ic_normal, opacity = 0.0)
1834     Imager::ImgRaw out
1835     Imager::ImgRaw src
1836         i_img_dim out_left
1837         i_img_dim out_top
1838         i_img_dim src_left
1839         i_img_dim src_top
1840         i_img_dim width
1841         i_img_dim height
1842         int combine
1843         double opacity
1844
1845 undef_int
1846 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)
1847     Imager::ImgRaw out
1848     Imager::ImgRaw src
1849     Imager::ImgRaw mask
1850         i_img_dim out_left
1851         i_img_dim out_top
1852         i_img_dim src_left
1853         i_img_dim src_top
1854         i_img_dim mask_left
1855         i_img_dim mask_top
1856         i_img_dim width
1857         i_img_dim height
1858         int combine
1859         double opacity
1860
1861 Imager::ImgRaw
1862 i_combine(src_av, channels_av = NULL)
1863         AV *src_av
1864         AV *channels_av
1865   PREINIT:
1866         i_img **imgs = NULL;
1867         STRLEN in_count;
1868         int *channels = NULL;
1869         int i;
1870         SV **psv;
1871         IV tmp;
1872   CODE:
1873         in_count = av_len(src_av) + 1;
1874         if (in_count > 0) {
1875           imgs = mymalloc(sizeof(i_img*) * in_count);
1876           channels = mymalloc(sizeof(int) * in_count);
1877           for (i = 0; i < in_count; ++i) {
1878             psv = av_fetch(src_av, i, 0);
1879             if (!psv || !*psv || !sv_derived_from(*psv, "Imager::ImgRaw")) {
1880               myfree(imgs);
1881               myfree(channels);
1882               croak("imgs must contain only images");
1883             }
1884             tmp = SvIV((SV*)SvRV(*psv));
1885             imgs[i] = INT2PTR(i_img*, tmp);
1886             if (channels_av &&
1887                 (psv = av_fetch(channels_av, i, 0)) != NULL &&
1888                 *psv) {
1889               channels[i] = SvIV(*psv);
1890             }
1891             else {
1892               channels[i] = 0;
1893             }
1894           }
1895         }
1896         RETVAL = i_combine(imgs, channels, in_count);
1897         myfree(imgs);
1898         myfree(channels);
1899   OUTPUT:
1900         RETVAL
1901
1902 undef_int
1903 i_flipxy(im, direction)
1904     Imager::ImgRaw     im
1905                int     direction
1906
1907 Imager::ImgRaw
1908 i_rotate90(im, degrees)
1909     Imager::ImgRaw      im
1910                int      degrees
1911
1912 Imager::ImgRaw
1913 i_rotate_exact(im, amount, ...)
1914     Imager::ImgRaw      im
1915             double      amount
1916       PREINIT:
1917         i_color *backp = NULL;
1918         i_fcolor *fbackp = NULL;
1919         int i;
1920         SV * sv1;
1921       CODE:
1922         /* extract the bg colors if any */
1923         /* yes, this is kind of strange */
1924         for (i = 2; i < items; ++i) {
1925           sv1 = ST(i);
1926           if (sv_derived_from(sv1, "Imager::Color")) {
1927             IV tmp = SvIV((SV*)SvRV(sv1));
1928             backp = INT2PTR(i_color *, tmp);
1929           }
1930           else if (sv_derived_from(sv1, "Imager::Color::Float")) {
1931             IV tmp = SvIV((SV*)SvRV(sv1));
1932             fbackp = INT2PTR(i_fcolor *, tmp);
1933           }
1934         }
1935         RETVAL = i_rotate_exact_bg(im, amount, backp, fbackp);
1936       OUTPUT:
1937         RETVAL
1938
1939 Imager::ImgRaw
1940 i_matrix_transform(im, xsize, ysize, matrix, ...)
1941     Imager::ImgRaw      im
1942                i_img_dim      xsize
1943                i_img_dim      ysize
1944       PREINIT:
1945         double matrix[9];
1946         AV *av;
1947         IV len;
1948         SV *sv1;
1949         int i;
1950         i_color *backp = NULL;
1951         i_fcolor *fbackp = NULL;
1952       CODE:
1953         if (!SvROK(ST(3)) || SvTYPE(SvRV(ST(3))) != SVt_PVAV)
1954           croak("i_matrix_transform: parameter 4 must be an array ref\n");
1955         av=(AV*)SvRV(ST(3));
1956         len=av_len(av)+1;
1957         if (len > 9)
1958           len = 9;
1959         for (i = 0; i < len; ++i) {
1960           sv1=(*(av_fetch(av,i,0)));
1961           matrix[i] = SvNV(sv1);
1962         }
1963         for (; i < 9; ++i)
1964           matrix[i] = 0;
1965         /* extract the bg colors if any */
1966         /* yes, this is kind of strange */
1967         for (i = 4; i < items; ++i) {
1968           sv1 = ST(i);
1969           if (sv_derived_from(sv1, "Imager::Color")) {
1970             IV tmp = SvIV((SV*)SvRV(sv1));
1971             backp = INT2PTR(i_color *, tmp);
1972           }
1973           else if (sv_derived_from(sv1, "Imager::Color::Float")) {
1974             IV tmp = SvIV((SV*)SvRV(sv1));
1975             fbackp = INT2PTR(i_fcolor *, tmp);
1976           }
1977         }
1978         RETVAL = i_matrix_transform_bg(im, xsize, ysize, matrix, backp, fbackp);
1979       OUTPUT:
1980         RETVAL
1981
1982 undef_int
1983 i_gaussian(im,stdev)
1984     Imager::ImgRaw     im
1985             double     stdev
1986
1987 void
1988 i_unsharp_mask(im,stdev,scale)
1989     Imager::ImgRaw     im
1990              double    stdev
1991              double    scale
1992
1993 int
1994 i_conv(im,coef)
1995         Imager::ImgRaw     im
1996         AV *coef
1997      PREINIT:
1998         double*    c_coef;
1999         int     len;
2000         SV* sv1;
2001         int i;
2002     CODE:
2003         len = av_len(coef) + 1;
2004         c_coef=mymalloc( len * sizeof(double) );
2005         for(i = 0; i  < len; i++) {
2006           sv1 = (*(av_fetch(coef, i, 0)));
2007           c_coef[i] = (double)SvNV(sv1);
2008         }
2009         RETVAL = i_conv(im, c_coef, len);
2010         myfree(c_coef);
2011     OUTPUT:
2012         RETVAL
2013
2014 Imager::ImgRaw
2015 i_convert(src, avmain)
2016     Imager::ImgRaw     src
2017     AV *avmain
2018         PREINIT:
2019           double *coeff;
2020           int outchan;
2021           int inchan;
2022           SV **temp;
2023           AV *avsub;
2024           int len;
2025           int i, j;
2026         CODE:
2027           outchan = av_len(avmain)+1;
2028           /* find the biggest */
2029           inchan = 0;
2030           for (j=0; j < outchan; ++j) {
2031             temp = av_fetch(avmain, j, 0);
2032             if (temp && SvROK(*temp) && SvTYPE(SvRV(*temp)) == SVt_PVAV) {
2033               avsub = (AV*)SvRV(*temp);
2034               len = av_len(avsub)+1;
2035               if (len > inchan)
2036                 inchan = len;
2037             }
2038             else {
2039               i_push_errorf(0, "invalid matrix: element %d is not an array ref", j);
2040               XSRETURN(0);
2041             }
2042           }
2043           coeff = mymalloc(sizeof(double) * outchan * inchan);
2044           for (j = 0; j < outchan; ++j) {
2045             avsub = (AV*)SvRV(*av_fetch(avmain, j, 0));
2046             len = av_len(avsub)+1;
2047             for (i = 0; i < len; ++i) {
2048               temp = av_fetch(avsub, i, 0);
2049               if (temp)
2050                 coeff[i+j*inchan] = SvNV(*temp);
2051               else
2052                 coeff[i+j*inchan] = 0;
2053             }
2054             while (i < inchan)
2055               coeff[i++ + j*inchan] = 0;
2056           }
2057           RETVAL = i_convert(src, coeff, outchan, inchan);
2058           myfree(coeff);
2059         OUTPUT:
2060           RETVAL
2061
2062
2063 void
2064 i_map(im, pmaps)
2065     Imager::ImgRaw     im
2066         PREINIT:
2067           unsigned int mask = 0;
2068           AV *avmain;
2069           AV *avsub;
2070           SV **temp;
2071           int len;
2072           int i, j;
2073           unsigned char (*maps)[256];
2074         CODE:
2075           if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVAV)
2076             croak("i_map: parameter 2 must be an arrayref\n");
2077           avmain = (AV*)SvRV(ST(1));
2078           len = av_len(avmain)+1;
2079           if (im->channels < len) len = im->channels;
2080
2081           maps = mymalloc( len * sizeof(unsigned char [256]) );
2082
2083           for (j=0; j<len ; j++) {
2084             temp = av_fetch(avmain, j, 0);
2085             if (temp && SvROK(*temp) && (SvTYPE(SvRV(*temp)) == SVt_PVAV) ) {
2086               avsub = (AV*)SvRV(*temp);
2087               if(av_len(avsub) != 255) continue;
2088               mask |= 1<<j;
2089               for (i=0; i<256 ; i++) {
2090                 int val;
2091                 temp = av_fetch(avsub, i, 0);
2092                 val = temp ? SvIV(*temp) : 0;
2093                 if (val<0) val = 0;
2094                 if (val>255) val = 255;
2095                 maps[j][i] = val;
2096               }
2097             }
2098           }
2099           i_map(im, maps, mask);
2100           myfree(maps);
2101
2102
2103
2104 float
2105 i_img_diff(im1,im2)
2106     Imager::ImgRaw     im1
2107     Imager::ImgRaw     im2
2108
2109 double
2110 i_img_diffd(im1,im2)
2111     Imager::ImgRaw     im1
2112     Imager::ImgRaw     im2
2113
2114 int
2115 i_img_samef(im1, im2, epsilon = i_img_epsilonf(), what=NULL)
2116     Imager::ImgRaw    im1
2117     Imager::ImgRaw    im2
2118     double epsilon
2119     const char *what
2120
2121 double
2122 i_img_epsilonf()
2123
2124 bool
2125 _is_color_object(sv)
2126         SV* sv
2127     CODE:
2128         SvGETMAGIC(sv);
2129         RETVAL = SvOK(sv) && SvROK(sv) &&
2130            (sv_derived_from(sv, "Imager::Color")
2131           || sv_derived_from(sv, "Imager::Color::Float"));
2132     OUTPUT:
2133         RETVAL
2134
2135 #ifdef HAVE_LIBTT
2136
2137
2138 Imager::Font::TT
2139 i_tt_new(fontname)
2140               char*     fontname
2141
2142
2143 MODULE = Imager         PACKAGE = Imager::Font::TT      PREFIX=TT_
2144
2145 #define TT_DESTROY(handle) i_tt_destroy(handle)
2146
2147 void
2148 TT_DESTROY(handle)
2149      Imager::Font::TT   handle
2150
2151 int
2152 TT_CLONE_SKIP(...)
2153     CODE:
2154         (void)items; /* avoid unused warning */
2155         RETVAL = 1;
2156     OUTPUT:
2157         RETVAL
2158
2159
2160 MODULE = Imager         PACKAGE = Imager
2161
2162
2163 undef_int
2164 i_tt_text(handle,im,xb,yb,cl,points,str_sv,len_ignored,smooth,utf8,align=1)
2165   Imager::Font::TT     handle
2166     Imager::ImgRaw     im
2167                i_img_dim     xb
2168                i_img_dim     yb
2169      Imager::Color     cl
2170              double     points
2171               SV *     str_sv
2172                int     smooth
2173                int     utf8
2174                int     align
2175              PREINIT:
2176                char *str;
2177                STRLEN len;
2178              CODE:
2179 #ifdef SvUTF8
2180                if (SvUTF8(str_sv))
2181                  utf8 = 1;
2182 #endif
2183                str = SvPV(str_sv, len);
2184                RETVAL = i_tt_text(handle, im, xb, yb, cl, points, str, 
2185                                   len, smooth, utf8, align);
2186              OUTPUT:
2187                RETVAL                
2188
2189
2190 undef_int
2191 i_tt_cp(handle,im,xb,yb,channel,points,str_sv,len_ignored,smooth,utf8,align=1)
2192   Imager::Font::TT     handle
2193     Imager::ImgRaw     im
2194                i_img_dim     xb
2195                i_img_dim     yb
2196                int     channel
2197              double     points
2198               SV *     str_sv
2199                int     smooth
2200                int     utf8
2201                int     align
2202              PREINIT:
2203                char *str;
2204                STRLEN len;
2205              CODE:
2206 #ifdef SvUTF8
2207                if (SvUTF8(str_sv))
2208                  utf8 = 1;
2209 #endif
2210                str = SvPV(str_sv, len);
2211                RETVAL = i_tt_cp(handle, im, xb, yb, channel, points, str, len,
2212                                 smooth, utf8, align);
2213              OUTPUT:
2214                 RETVAL
2215
2216
2217 void
2218 i_tt_bbox(handle,point,str_sv,len_ignored, utf8)
2219   Imager::Font::TT     handle
2220              double     point
2221                SV*    str_sv
2222                int     utf8
2223              PREINIT:
2224                i_img_dim cords[BOUNDING_BOX_COUNT];
2225                int rc;
2226                char *  str;
2227                STRLEN len;
2228                int i;
2229              PPCODE:
2230 #ifdef SvUTF8
2231                if (SvUTF8(ST(2)))
2232                  utf8 = 1;
2233 #endif
2234                str = SvPV(str_sv, len);
2235                if ((rc=i_tt_bbox(handle,point,str,len,cords, utf8))) {
2236                  EXTEND(SP, rc);
2237                  for (i = 0; i < rc; ++i) {
2238                    PUSHs(sv_2mortal(newSViv(cords[i])));
2239                  }
2240                }
2241
2242 void
2243 i_tt_has_chars(handle, text_sv, utf8)
2244         Imager::Font::TT handle
2245         SV  *text_sv
2246         int utf8
2247       PREINIT:
2248         char const *text;
2249         STRLEN len;
2250         char *work;
2251         size_t count;
2252         size_t i;
2253       PPCODE:
2254 #ifdef SvUTF8
2255         if (SvUTF8(text_sv))
2256           utf8 = 1;
2257 #endif
2258         text = SvPV(text_sv, len);
2259         work = mymalloc(len);
2260         count = i_tt_has_chars(handle, text, len, utf8, work);
2261         if (GIMME_V == G_ARRAY) {
2262           EXTEND(SP, count);
2263           for (i = 0; i < count; ++i) {
2264             PUSHs(boolSV(work[i]));
2265           }
2266         }
2267         else {
2268           EXTEND(SP, 1);
2269           PUSHs(sv_2mortal(newSVpv(work, count)));
2270         }
2271         myfree(work);
2272
2273 void
2274 i_tt_dump_names(handle)
2275         Imager::Font::TT handle
2276
2277 void
2278 i_tt_face_name(handle)
2279         Imager::Font::TT handle
2280       PREINIT:
2281         char name[255];
2282         size_t len;
2283       PPCODE:
2284         len = i_tt_face_name(handle, name, sizeof(name));
2285         if (len) {
2286           EXTEND(SP, 1);
2287           PUSHs(sv_2mortal(newSVpv(name, len-1)));
2288         }
2289
2290 void
2291 i_tt_glyph_name(handle, text_sv, utf8 = 0)
2292         Imager::Font::TT handle
2293         SV *text_sv
2294         int utf8
2295       PREINIT:
2296         char const *text;
2297         STRLEN work_len;
2298         size_t len;
2299         size_t outsize;
2300         char name[255];
2301       PPCODE:
2302 #ifdef SvUTF8
2303         if (SvUTF8(text_sv))
2304           utf8 = 1;
2305 #endif
2306         text = SvPV(text_sv, work_len);
2307         len = work_len;
2308         while (len) {
2309           unsigned long ch;
2310           if (utf8) {
2311             ch = i_utf8_advance(&text, &len);
2312             if (ch == ~0UL) {
2313               i_push_error(0, "invalid UTF8 character");
2314               break;
2315             }
2316           }
2317           else {
2318             ch = *text++;
2319             --len;
2320           }
2321           EXTEND(SP, 1);
2322           if ((outsize = i_tt_glyph_name(handle, ch, name, sizeof(name))) != 0) {
2323             PUSHs(sv_2mortal(newSVpv(name, 0)));
2324           }
2325           else {
2326             PUSHs(&PL_sv_undef);
2327           } 
2328         }
2329
2330 #endif 
2331
2332 const char *
2333 i_test_format_probe(ig, length)
2334         Imager::IO     ig
2335                int     length
2336
2337 Imager::ImgRaw
2338 i_readpnm_wiol(ig, allow_incomplete)
2339         Imager::IO     ig
2340                int     allow_incomplete
2341
2342
2343 void
2344 i_readpnm_multi_wiol(ig, allow_incomplete)
2345         Imager::IO ig
2346                int     allow_incomplete
2347       PREINIT:
2348         i_img **imgs;
2349         int count=0;
2350         int i;
2351       PPCODE:
2352         imgs = i_readpnm_multi_wiol(ig, &count, allow_incomplete);
2353         if (imgs) {
2354           EXTEND(SP, count);
2355           for (i = 0; i < count; ++i) {
2356             SV *sv = sv_newmortal();
2357             sv_setref_pv(sv, "Imager::ImgRaw", (void *)imgs[i]);
2358             PUSHs(sv);
2359           }
2360           myfree(imgs);
2361         }
2362
2363 undef_int
2364 i_writeppm_wiol(im, ig)
2365     Imager::ImgRaw     im
2366         Imager::IO     ig
2367
2368
2369
2370
2371
2372 Imager::ImgRaw
2373 i_readraw_wiol(ig,x,y,datachannels,storechannels,intrl)
2374         Imager::IO     ig
2375                i_img_dim     x
2376                i_img_dim     y
2377                int     datachannels
2378                int     storechannels
2379                int     intrl
2380
2381 undef_int
2382 i_writeraw_wiol(im,ig)
2383     Imager::ImgRaw     im
2384         Imager::IO     ig
2385
2386 undef_int
2387 i_writebmp_wiol(im,ig)
2388     Imager::ImgRaw     im
2389         Imager::IO     ig
2390
2391 Imager::ImgRaw
2392 i_readbmp_wiol(ig, allow_incomplete=0)
2393         Imager::IO     ig
2394         int            allow_incomplete
2395
2396
2397 undef_int
2398 i_writetga_wiol(im,ig, wierdpack, compress, idstring)
2399     Imager::ImgRaw     im
2400         Imager::IO     ig
2401                int     wierdpack
2402                int     compress
2403               char*    idstring
2404             PREINIT:
2405                 int idlen;
2406                CODE:
2407                 idlen  = SvCUR(ST(4));
2408                 RETVAL = i_writetga_wiol(im, ig, wierdpack, compress, idstring, idlen);
2409                 OUTPUT:
2410                 RETVAL
2411
2412
2413 Imager::ImgRaw
2414 i_readtga_wiol(ig, length)
2415         Imager::IO     ig
2416                int     length
2417
2418
2419
2420
2421 Imager::ImgRaw
2422 i_scaleaxis(im,Value,Axis)
2423     Imager::ImgRaw     im
2424              double     Value
2425                int     Axis
2426
2427 Imager::ImgRaw
2428 i_scale_nn(im,scx,scy)
2429     Imager::ImgRaw     im
2430              double    scx
2431              double    scy
2432
2433 Imager::ImgRaw
2434 i_scale_mixing(im, width, height)
2435     Imager::ImgRaw     im
2436                i_img_dim     width
2437                i_img_dim     height
2438
2439 Imager::ImgRaw
2440 i_haar(im)
2441     Imager::ImgRaw     im
2442
2443 int
2444 i_count_colors(im,maxc)
2445     Imager::ImgRaw     im
2446                int     maxc
2447
2448 void
2449 i_get_anonymous_color_histo(im, maxc = 0x40000000)
2450    Imager::ImgRaw  im
2451    int maxc
2452     PREINIT:
2453         int i;
2454         unsigned int * col_usage = NULL;
2455         int col_cnt;
2456     PPCODE:
2457         col_cnt = i_get_anonymous_color_histo(im, &col_usage, maxc);
2458         EXTEND(SP, col_cnt);
2459         for (i = 0; i < col_cnt; i++)  {
2460             PUSHs(sv_2mortal(newSViv( col_usage[i])));
2461         }
2462         myfree(col_usage);
2463         XSRETURN(col_cnt);
2464
2465
2466 void
2467 i_transform(im,opx,opy,parm)
2468     Imager::ImgRaw     im
2469              PREINIT:
2470              double* parm;
2471              int *opx;
2472              int *opy;
2473              int     opxl;
2474              int     opyl;
2475              int     parmlen;
2476              AV* av;
2477              SV* sv1;
2478              int i;
2479              i_img *result;
2480              PPCODE:
2481              if (!SvROK(ST(1))) croak("Imager: Parameter 1 must be a reference to an array\n");
2482              if (!SvROK(ST(2))) croak("Imager: Parameter 2 must be a reference to an array\n");
2483              if (!SvROK(ST(3))) croak("Imager: Parameter 3 must be a reference to an array\n");
2484              if (SvTYPE(SvRV(ST(1))) != SVt_PVAV) croak("Imager: Parameter 1 must be a reference to an array\n");
2485              if (SvTYPE(SvRV(ST(2))) != SVt_PVAV) croak("Imager: Parameter 2 must be a reference to an array\n");
2486              if (SvTYPE(SvRV(ST(3))) != SVt_PVAV) croak("Imager: Parameter 3 must be a reference to an array\n");
2487              av=(AV*)SvRV(ST(1));
2488              opxl=av_len(av)+1;
2489              opx=mymalloc( opxl*sizeof(int) );
2490              for(i=0;i<opxl;i++) {
2491                sv1=(*(av_fetch(av,i,0)));
2492                opx[i]=(int)SvIV(sv1);
2493              }
2494              av=(AV*)SvRV(ST(2));
2495              opyl=av_len(av)+1;
2496              opy=mymalloc( opyl*sizeof(int) );
2497              for(i=0;i<opyl;i++) {
2498                sv1=(*(av_fetch(av,i,0)));
2499                opy[i]=(int)SvIV(sv1);
2500              }
2501              av=(AV*)SvRV(ST(3));
2502              parmlen=av_len(av)+1;
2503              parm=mymalloc( parmlen*sizeof(double) );
2504              for(i=0;i<parmlen;i++) { /* FIXME: Bug? */
2505                sv1=(*(av_fetch(av,i,0)));
2506                parm[i]=(double)SvNV(sv1);
2507              }
2508              result=i_transform(im,opx,opxl,opy,opyl,parm,parmlen);
2509              myfree(parm);
2510              myfree(opy);
2511              myfree(opx);
2512              if (result) {
2513                SV *result_sv = sv_newmortal();
2514                EXTEND(SP, 1);
2515                sv_setref_pv(result_sv, "Imager::ImgRaw", (void*)result);
2516                PUSHs(result_sv);
2517              }
2518
2519 void
2520 i_transform2(sv_width,sv_height,channels,sv_ops,av_n_regs,av_c_regs,av_in_imgs)
2521         SV *sv_width
2522         SV *sv_height
2523         SV *sv_ops
2524         AV *av_n_regs
2525         AV *av_c_regs
2526         AV *av_in_imgs
2527         int channels
2528              PREINIT:
2529              i_img_dim width;
2530              i_img_dim height;
2531              struct rm_op *ops;
2532              STRLEN ops_len;
2533              int ops_count;
2534              double *n_regs;
2535              int n_regs_count;
2536              i_color *c_regs;
2537              int c_regs_count;
2538              int in_imgs_count;
2539              i_img **in_imgs;
2540              SV *sv1;
2541              IV tmp;
2542              int i;
2543              i_img *result;
2544              PPCODE:
2545
2546              in_imgs_count = av_len(av_in_imgs)+1;
2547              for (i = 0; i < in_imgs_count; ++i) {
2548                sv1 = *av_fetch(av_in_imgs, i, 0);
2549                if (!sv_derived_from(sv1, "Imager::ImgRaw")) {
2550                  croak("sv_in_img must contain only images");
2551                }
2552              }
2553              if (in_imgs_count > 0) {
2554                in_imgs = mymalloc(in_imgs_count*sizeof(i_img*));
2555                for (i = 0; i < in_imgs_count; ++i) {              
2556                  sv1 = *av_fetch(av_in_imgs,i,0);
2557                  if (!sv_derived_from(sv1, "Imager::ImgRaw")) {
2558                    croak("Parameter 5 must contain only images");
2559                  }
2560                  tmp = SvIV((SV*)SvRV(sv1));
2561                  in_imgs[i] = INT2PTR(i_img*, tmp);
2562                }
2563              }
2564              else {
2565                /* no input images */
2566                in_imgs = NULL;
2567              }
2568              /* default the output size from the first input if possible */
2569              if (SvOK(sv_width))
2570                width = SvIV(sv_width);
2571              else if (in_imgs_count)
2572                width = in_imgs[0]->xsize;
2573              else
2574                croak("No output image width supplied");
2575
2576              if (SvOK(sv_height))
2577                height = SvIV(sv_height);
2578              else if (in_imgs_count)
2579                height = in_imgs[0]->ysize;
2580              else
2581                croak("No output image height supplied");
2582
2583              ops = (struct rm_op *)SvPV(sv_ops, ops_len);
2584              if (ops_len % sizeof(struct rm_op))
2585                  croak("Imager: Parameter 3 must be a bitmap of regops\n");
2586              ops_count = ops_len / sizeof(struct rm_op);
2587
2588              n_regs_count = av_len(av_n_regs)+1;
2589              n_regs = mymalloc(n_regs_count * sizeof(double));
2590              for (i = 0; i < n_regs_count; ++i) {
2591                sv1 = *av_fetch(av_n_regs,i,0);
2592                if (SvOK(sv1))
2593                  n_regs[i] = SvNV(sv1);
2594              }
2595              c_regs_count = av_len(av_c_regs)+1;
2596              c_regs = mymalloc(c_regs_count * sizeof(i_color));
2597              /* I don't bother initializing the colou?r registers */
2598
2599              result=i_transform2(width, height, channels, ops, ops_count, 
2600                                  n_regs, n_regs_count, 
2601                                  c_regs, c_regs_count, in_imgs, in_imgs_count);
2602              if (in_imgs)
2603                  myfree(in_imgs);
2604              myfree(n_regs);
2605              myfree(c_regs);
2606              if (result) {
2607                SV *result_sv = sv_newmortal();
2608                EXTEND(SP, 1);
2609                sv_setref_pv(result_sv, "Imager::ImgRaw", (void*)result);
2610                PUSHs(result_sv);
2611              }
2612
2613
2614 void
2615 i_contrast(im,intensity)
2616     Imager::ImgRaw     im
2617              float     intensity
2618
2619 void
2620 i_hardinvert(im)
2621     Imager::ImgRaw     im
2622
2623 void
2624 i_hardinvertall(im)
2625     Imager::ImgRaw     im
2626
2627 void
2628 i_noise(im,amount,type)
2629     Imager::ImgRaw     im
2630              float     amount
2631      unsigned char     type
2632
2633 void
2634 i_bumpmap(im,bump,channel,light_x,light_y,strength)
2635     Imager::ImgRaw     im
2636     Imager::ImgRaw     bump
2637                int     channel
2638          i_img_dim     light_x
2639          i_img_dim     light_y
2640          i_img_dim     strength
2641
2642
2643 void
2644 i_bumpmap_complex(im,bump,channel,tx,ty,Lx,Ly,Lz,cd,cs,n,Ia,Il,Is)
2645     Imager::ImgRaw     im
2646     Imager::ImgRaw     bump
2647                int     channel
2648                i_img_dim     tx
2649                i_img_dim     ty
2650              double     Lx
2651              double     Ly
2652              double     Lz
2653              float     cd
2654              float     cs
2655              float     n
2656      Imager::Color     Ia
2657      Imager::Color     Il
2658      Imager::Color     Is
2659
2660
2661
2662 void
2663 i_postlevels(im,levels)
2664     Imager::ImgRaw     im
2665              int       levels
2666
2667 void
2668 i_mosaic(im,size)
2669     Imager::ImgRaw     im
2670          i_img_dim     size
2671
2672 void
2673 i_watermark(im,wmark,tx,ty,pixdiff)
2674     Imager::ImgRaw     im
2675     Imager::ImgRaw     wmark
2676                i_img_dim     tx
2677                i_img_dim     ty
2678                int     pixdiff
2679
2680
2681 void
2682 i_autolevels(im,lsat,usat,skew)
2683     Imager::ImgRaw     im
2684              float     lsat
2685              float     usat
2686              float     skew
2687
2688 void
2689 i_radnoise(im,xo,yo,rscale,ascale)
2690     Imager::ImgRaw     im
2691              float     xo
2692              float     yo
2693              float     rscale
2694              float     ascale
2695
2696 void
2697 i_turbnoise(im, xo, yo, scale)
2698     Imager::ImgRaw     im
2699              float     xo
2700              float     yo
2701              float     scale
2702
2703
2704 void
2705 i_gradgen(im, ...)
2706     Imager::ImgRaw     im
2707       PREINIT:
2708         int num;
2709         i_img_dim *xo;
2710         i_img_dim *yo;
2711         i_color *ival;
2712         int dmeasure;
2713         int i;
2714         SV *sv;
2715         AV *axx;
2716         AV *ayy;
2717         AV *ac;
2718       CODE:
2719         if (items != 5)
2720             croak("Usage: i_gradgen(im, xo, yo, ival, dmeasure)");
2721         if (!SvROK(ST(1)) || ! SvTYPE(SvRV(ST(1))))
2722             croak("i_gradgen: Second argument must be an array ref");
2723         if (!SvROK(ST(2)) || ! SvTYPE(SvRV(ST(2))))
2724             croak("i_gradgen: Third argument must be an array ref");
2725         if (!SvROK(ST(3)) || ! SvTYPE(SvRV(ST(3))))
2726             croak("i_gradgen: Fourth argument must be an array ref");
2727         axx = (AV *)SvRV(ST(1));
2728         ayy = (AV *)SvRV(ST(2));
2729         ac  = (AV *)SvRV(ST(3));
2730         dmeasure = (int)SvIV(ST(4));
2731         
2732         num = av_len(axx) < av_len(ayy) ? av_len(axx) : av_len(ayy);
2733         num = num <= av_len(ac) ? num : av_len(ac);
2734         num++; 
2735         if (num < 2) croak("Usage: i_gradgen array refs must have more than 1 entry each");
2736         xo = mymalloc( sizeof(i_img_dim) * num );
2737         yo = mymalloc( sizeof(i_img_dim) * num );
2738         ival = mymalloc( sizeof(i_color) * num );
2739         for(i = 0; i<num; i++) {
2740           xo[i]   = (i_img_dim)SvIV(* av_fetch(axx, i, 0));
2741           yo[i]   = (i_img_dim)SvIV(* av_fetch(ayy, i, 0));
2742           sv = *av_fetch(ac, i, 0);
2743           if ( !sv_derived_from(sv, "Imager::Color") ) {
2744             free(axx); free(ayy); free(ac);
2745             croak("i_gradgen: Element of fourth argument is not derived from Imager::Color");
2746           }
2747           ival[i] = *INT2PTR(i_color *, SvIV((SV *)SvRV(sv)));
2748         }
2749         i_gradgen(im, num, xo, yo, ival, dmeasure);
2750         myfree(xo);
2751         myfree(yo);
2752         myfree(ival);
2753
2754 Imager::ImgRaw
2755 i_diff_image(im, im2, mindist=0)
2756     Imager::ImgRaw     im
2757     Imager::ImgRaw     im2
2758             double     mindist
2759
2760 undef_int
2761 i_fountain(im, xa, ya, xb, yb, type, repeat, combine, super_sample, ssample_param, segs)
2762     Imager::ImgRaw     im
2763             double     xa
2764             double     ya
2765             double     xb
2766             double     yb
2767                int     type
2768                int     repeat
2769                int     combine
2770                int     super_sample
2771             double     ssample_param
2772       PREINIT:
2773         AV *asegs;
2774         int count;
2775         i_fountain_seg *segs;
2776       CODE:
2777         if (!SvROK(ST(10)) || ! SvTYPE(SvRV(ST(10))))
2778             croak("i_fountain: argument 11 must be an array ref");
2779         
2780         asegs = (AV *)SvRV(ST(10));
2781         segs = load_fount_segs(aTHX_ asegs, &count);
2782         RETVAL = i_fountain(im, xa, ya, xb, yb, type, repeat, combine, 
2783                             super_sample, ssample_param, count, segs);
2784         myfree(segs);
2785       OUTPUT:
2786         RETVAL
2787
2788 Imager::FillHandle
2789 i_new_fill_fount(xa, ya, xb, yb, type, repeat, combine, super_sample, ssample_param, segs)
2790             double     xa
2791             double     ya
2792             double     xb
2793             double     yb
2794                int     type
2795                int     repeat
2796                int     combine
2797                int     super_sample
2798             double     ssample_param
2799       PREINIT:
2800         AV *asegs;
2801         int count;
2802         i_fountain_seg *segs;
2803       CODE:
2804         if (!SvROK(ST(9)) || ! SvTYPE(SvRV(ST(9))))
2805             croak("i_fountain: argument 11 must be an array ref");
2806         
2807         asegs = (AV *)SvRV(ST(9));
2808         segs = load_fount_segs(aTHX_ asegs, &count);
2809         RETVAL = i_new_fill_fount(xa, ya, xb, yb, type, repeat, combine, 
2810                                   super_sample, ssample_param, count, segs);
2811         myfree(segs);        
2812       OUTPUT:
2813         RETVAL
2814
2815 Imager::FillHandle
2816 i_new_fill_opacity(other_fill, alpha_mult)
2817     Imager::FillHandle other_fill
2818     double alpha_mult
2819
2820 void
2821 i_errors()
2822       PREINIT:
2823         i_errmsg *errors;
2824         int i;
2825         AV *av;
2826         SV *sv;
2827       PPCODE:
2828         errors = i_errors();
2829         i = 0;
2830         while (errors[i].msg) {
2831           av = newAV();
2832           sv = newSVpv(errors[i].msg, strlen(errors[i].msg));
2833           if (!av_store(av, 0, sv)) {
2834             SvREFCNT_dec(sv);
2835           }
2836           sv = newSViv(errors[i].code);
2837           if (!av_store(av, 1, sv)) {
2838             SvREFCNT_dec(sv);
2839           }
2840           PUSHs(sv_2mortal(newRV_noinc((SV*)av)));
2841           ++i;
2842         }
2843
2844 void
2845 i_clear_error()
2846
2847 void
2848 i_push_error(code, msg)
2849         int code
2850         const char *msg
2851
2852 undef_int
2853 i_nearest_color(im, ...)
2854     Imager::ImgRaw     im
2855       PREINIT:
2856         int num;
2857         i_img_dim *xo;
2858         i_img_dim *yo;
2859         i_color *ival;
2860         int dmeasure;
2861         int i;
2862         SV *sv;
2863         AV *axx;
2864         AV *ayy;
2865         AV *ac;
2866       CODE:
2867         if (items != 5)
2868             croak("Usage: i_nearest_color(im, xo, yo, ival, dmeasure)");
2869         if (!SvROK(ST(1)) || ! SvTYPE(SvRV(ST(1))))
2870             croak("i_nearest_color: Second argument must be an array ref");
2871         if (!SvROK(ST(2)) || ! SvTYPE(SvRV(ST(2))))
2872             croak("i_nearest_color: Third argument must be an array ref");
2873         if (!SvROK(ST(3)) || ! SvTYPE(SvRV(ST(3))))
2874             croak("i_nearest_color: Fourth argument must be an array ref");
2875         axx = (AV *)SvRV(ST(1));
2876         ayy = (AV *)SvRV(ST(2));
2877         ac  = (AV *)SvRV(ST(3));
2878         dmeasure = (int)SvIV(ST(4));
2879         
2880         num = av_len(axx) < av_len(ayy) ? av_len(axx) : av_len(ayy);
2881         num = num <= av_len(ac) ? num : av_len(ac);
2882         num++; 
2883         if (num < 2) croak("Usage: i_nearest_color array refs must have more than 1 entry each");
2884         xo = mymalloc( sizeof(i_img_dim) * num );
2885         yo = mymalloc( sizeof(i_img_dim) * num );
2886         ival = mymalloc( sizeof(i_color) * num );
2887         for(i = 0; i<num; i++) {
2888           xo[i]   = (i_img_dim)SvIV(* av_fetch(axx, i, 0));
2889           yo[i]   = (i_img_dim)SvIV(* av_fetch(ayy, i, 0));
2890           sv = *av_fetch(ac, i, 0);
2891           if ( !sv_derived_from(sv, "Imager::Color") ) {
2892             free(axx); free(ayy); free(ac);
2893             croak("i_nearest_color: Element of fourth argument is not derived from Imager::Color");
2894           }
2895           ival[i] = *INT2PTR(i_color *, SvIV((SV *)SvRV(sv)));
2896         }
2897         RETVAL = i_nearest_color(im, num, xo, yo, ival, dmeasure);
2898       OUTPUT:
2899         RETVAL
2900
2901 void
2902 malloc_state()
2903
2904 void
2905 DSO_open(filename)
2906              char*       filename
2907              PREINIT:
2908                void *rc;
2909                char *evstr;
2910              PPCODE:
2911                rc=DSO_open(filename,&evstr);
2912                if (rc!=NULL) {
2913                  if (evstr!=NULL) {
2914                    EXTEND(SP,2); 
2915                    PUSHs(sv_2mortal(newSViv(PTR2IV(rc))));
2916                    PUSHs(sv_2mortal(newSVpvn(evstr, strlen(evstr))));
2917                  } else {
2918                    EXTEND(SP,1);
2919                    PUSHs(sv_2mortal(newSViv(PTR2IV(rc))));
2920                  }
2921                }
2922
2923
2924 undef_int
2925 DSO_close(dso_handle)
2926              void*       dso_handle
2927
2928 void
2929 DSO_funclist(dso_handle_v)
2930              void*       dso_handle_v
2931              PREINIT:
2932                int i;
2933                DSO_handle *dso_handle;
2934                func_ptr *functions;
2935              PPCODE:
2936                dso_handle=(DSO_handle*)dso_handle_v;
2937                functions = DSO_funclist(dso_handle);
2938                i=0;
2939                while( functions[i].name != NULL) {
2940                  EXTEND(SP,1);
2941                  PUSHs(sv_2mortal(newSVpv(functions[i].name,0)));
2942                  EXTEND(SP,1);
2943                  PUSHs(sv_2mortal(newSVpv(functions[i++].pcode,0)));
2944                }
2945
2946 void
2947 DSO_call(handle,func_index,hv)
2948                void*  handle
2949                int    func_index
2950              PREINIT:
2951                HV* hv;
2952              PPCODE:
2953                if (!SvROK(ST(2))) croak("Imager: Parameter 2 must be a reference to a hash\n");        
2954                hv=(HV*)SvRV(ST(2));
2955                if (SvTYPE(hv)!=SVt_PVHV) croak("Imager: Parameter 2 must be a reference to a hash\n");
2956                DSO_call( (DSO_handle *)handle,func_index,hv);
2957
2958 SV *
2959 i_get_pixel(im, x, y)
2960         Imager::ImgRaw im
2961         i_img_dim x
2962         i_img_dim y;
2963       PREINIT:
2964         i_color *color;
2965       CODE:
2966         color = (i_color *)mymalloc(sizeof(i_color));
2967         if (i_gpix(im, x, y, color) == 0) {
2968           RETVAL = NEWSV(0, 0);
2969           sv_setref_pv(RETVAL, "Imager::Color", (void *)color);
2970         }
2971         else {
2972           myfree(color);
2973           RETVAL = &PL_sv_undef;
2974         }
2975       OUTPUT:
2976         RETVAL
2977         
2978
2979 int
2980 i_ppix(im, x, y, cl)
2981         Imager::ImgRaw im
2982         i_img_dim x
2983         i_img_dim y
2984         Imager::Color cl
2985
2986 Imager::ImgRaw
2987 i_img_pal_new(x, y, channels, maxpal)
2988         i_img_dim x
2989         i_img_dim y
2990         int     channels
2991         int     maxpal
2992
2993 Imager::ImgRaw
2994 i_img_to_pal(src, quant)
2995         Imager::ImgRaw src
2996       PREINIT:
2997         HV *hv;
2998         i_quantize quant;
2999       CODE:
3000         if (!SvROK(ST(1)) || ! SvTYPE(SvRV(ST(1))))
3001           croak("i_img_to_pal: second argument must be a hash ref");
3002         hv = (HV *)SvRV(ST(1));
3003         memset(&quant, 0, sizeof(quant));
3004         quant.version = 1;
3005         quant.mc_size = 256;
3006         ip_handle_quant_opts(aTHX_ &quant, hv);
3007         RETVAL = i_img_to_pal(src, &quant);
3008         if (RETVAL) {
3009           ip_copy_colors_back(aTHX_ hv, &quant);
3010         }
3011         ip_cleanup_quant_opts(aTHX_ &quant);
3012       OUTPUT:
3013         RETVAL
3014
3015 Imager::ImgRaw
3016 i_img_to_rgb(src)
3017         Imager::ImgRaw src
3018
3019 void
3020 i_img_make_palette(HV *quant_hv, ...)
3021       PREINIT:
3022         size_t count = items - 1;
3023         i_quantize quant;
3024         i_img **imgs = NULL;
3025         ssize_t i;
3026       PPCODE:
3027         if (count <= 0)
3028           croak("Please supply at least one image (%d)", (int)count);
3029         imgs = mymalloc(sizeof(i_img *) * count);
3030         for (i = 0; i < count; ++i) {
3031           SV *img_sv = ST(i + 1);
3032           if (SvROK(img_sv) && sv_derived_from(img_sv, "Imager::ImgRaw")) {
3033             imgs[i] = INT2PTR(i_img *, SvIV((SV*)SvRV(img_sv)));
3034           }
3035           else {
3036             myfree(imgs);
3037             croak("Image %d is not an image object", (int)i+1);
3038           }
3039         }
3040         memset(&quant, 0, sizeof(quant));
3041         quant.version = 1;
3042         quant.mc_size = 256;
3043         ip_handle_quant_opts(aTHX_ &quant, quant_hv);
3044         i_quant_makemap(&quant, imgs, count);
3045         EXTEND(SP, quant.mc_count);
3046         for (i = 0; i < quant.mc_count; ++i) {
3047           SV *sv_c = make_i_color_sv(aTHX_ quant.mc_colors + i);
3048           PUSHs(sv_c);
3049         }
3050         ip_cleanup_quant_opts(aTHX_ &quant);
3051         
3052
3053 void
3054 i_gpal(im, l, r, y)
3055         Imager::ImgRaw  im
3056         i_img_dim     l
3057         i_img_dim     r
3058         i_img_dim     y
3059       PREINIT:
3060         i_palidx *work;
3061         int count, i;
3062       PPCODE:
3063         if (l < r) {
3064           work = mymalloc((r-l) * sizeof(i_palidx));
3065           count = i_gpal(im, l, r, y, work);
3066           if (GIMME_V == G_ARRAY) {
3067             EXTEND(SP, count);
3068             for (i = 0; i < count; ++i) {
3069               PUSHs(sv_2mortal(newSViv(work[i])));
3070             }
3071           }
3072           else {
3073             EXTEND(SP, 1);
3074             PUSHs(sv_2mortal(newSVpv((char *)work, count * sizeof(i_palidx))));
3075           }
3076           myfree(work);
3077         }
3078         else {
3079           if (GIMME_V != G_ARRAY) {
3080             EXTEND(SP, 1);
3081             PUSHs(&PL_sv_undef);
3082           }
3083         }
3084
3085 int
3086 i_ppal(im, l, y, ...)
3087         Imager::ImgRaw  im
3088         i_img_dim     l
3089         i_img_dim     y
3090       PREINIT:
3091         i_palidx *work;
3092         i_img_dim i;
3093       CODE:
3094         if (items > 3) {
3095           work = malloc_temp(aTHX_ sizeof(i_palidx) * (items-3));
3096           for (i=0; i < items-3; ++i) {
3097             work[i] = SvIV(ST(i+3));
3098           }
3099           validate_i_ppal(im, work, items - 3);
3100           RETVAL = i_ppal(im, l, l+items-3, y, work);
3101         }
3102         else {
3103           RETVAL = 0;
3104         }
3105       OUTPUT:
3106         RETVAL
3107
3108 int
3109 i_ppal_p(im, l, y, data)
3110         Imager::ImgRaw  im
3111         i_img_dim     l
3112         i_img_dim     y
3113         SV *data
3114       PREINIT:
3115         i_palidx const *work;
3116         STRLEN len;
3117       CODE:
3118         work = (i_palidx const *)SvPV(data, len);
3119         len /= sizeof(i_palidx);
3120         if (len > 0) {
3121           validate_i_ppal(im, work, len);
3122           RETVAL = i_ppal(im, l, l+len, y, work);
3123         }
3124         else {
3125           RETVAL = 0;
3126         }
3127       OUTPUT:
3128         RETVAL
3129
3130 SV *
3131 i_addcolors(im, ...)
3132         Imager::ImgRaw  im
3133       PREINIT:
3134         int index;
3135         i_color *colors;
3136         int i;
3137       CODE:
3138         if (items < 2)
3139           croak("i_addcolors: no colors to add");
3140         colors = mymalloc((items-1) * sizeof(i_color));
3141         for (i=0; i < items-1; ++i) {
3142           if (sv_isobject(ST(i+1)) 
3143               && sv_derived_from(ST(i+1), "Imager::Color")) {
3144             IV tmp = SvIV((SV *)SvRV(ST(i+1)));
3145             colors[i] = *INT2PTR(i_color *, tmp);
3146           }
3147           else {
3148             myfree(colors);
3149             croak("i_addcolor: pixels must be Imager::Color objects");
3150           }
3151         }
3152         index = i_addcolors(im, colors, items-1);
3153         myfree(colors);
3154         if (index == 0) {
3155           RETVAL = newSVpv("0 but true", 0);
3156         }
3157         else if (index == -1) {
3158           RETVAL = &PL_sv_undef;
3159         }
3160         else {
3161           RETVAL = newSViv(index);
3162         }
3163       OUTPUT:
3164         RETVAL
3165
3166 undef_int 
3167 i_setcolors(im, index, ...)
3168         Imager::ImgRaw  im
3169         int index
3170       PREINIT:
3171         i_color *colors;
3172         int i;
3173       CODE:
3174         if (items < 3)
3175           croak("i_setcolors: no colors to add");
3176         colors = mymalloc((items-2) * sizeof(i_color));
3177         for (i=0; i < items-2; ++i) {
3178           if (sv_isobject(ST(i+2)) 
3179               && sv_derived_from(ST(i+2), "Imager::Color")) {
3180             IV tmp = SvIV((SV *)SvRV(ST(i+2)));
3181             colors[i] = *INT2PTR(i_color *, tmp);
3182           }
3183           else {
3184             myfree(colors);
3185             croak("i_setcolors: pixels must be Imager::Color objects");
3186           }
3187         }
3188         RETVAL = i_setcolors(im, index, colors, items-2);
3189         myfree(colors);
3190       OUTPUT:
3191         RETVAL
3192
3193 void
3194 i_getcolors(im, index, ...)
3195         Imager::ImgRaw im
3196         int index
3197       PREINIT:
3198         i_color *colors;
3199         int count = 1;
3200         int i;
3201       PPCODE:
3202         if (items > 3)
3203           croak("i_getcolors: too many arguments");
3204         if (items == 3)
3205           count = SvIV(ST(2));
3206         if (count < 1)
3207           croak("i_getcolors: count must be positive");
3208         colors = mymalloc(sizeof(i_color) * count);
3209         if (i_getcolors(im, index, colors, count)) {
3210           for (i = 0; i < count; ++i) {
3211             SV *sv = make_i_color_sv(aTHX_ colors+i);
3212             PUSHs(sv);
3213           }
3214         }
3215         myfree(colors);
3216
3217
3218 undef_neg_int
3219 i_colorcount(im)
3220         Imager::ImgRaw im
3221
3222 undef_neg_int
3223 i_maxcolors(im)
3224         Imager::ImgRaw im
3225
3226 SV *
3227 i_findcolor(im, color)
3228         Imager::ImgRaw im
3229         Imager::Color color
3230       PREINIT:
3231         i_palidx index;
3232       CODE:
3233         if (i_findcolor(im, color, &index)) {
3234           RETVAL = newSViv(index);
3235         }
3236         else {
3237           RETVAL = &PL_sv_undef;
3238         }
3239       OUTPUT:
3240         RETVAL
3241
3242 int
3243 i_img_bits(im)
3244         Imager::ImgRaw  im
3245
3246 int
3247 i_img_type(im)
3248         Imager::ImgRaw  im
3249
3250 int
3251 i_img_virtual(im)
3252         Imager::ImgRaw  im
3253
3254 void
3255 i_gsamp(im, l, r, y, channels)
3256         Imager::ImgRaw im
3257         i_img_dim l
3258         i_img_dim r
3259         i_img_dim y
3260         i_channel_list channels
3261       PREINIT:
3262         i_sample_t *data;
3263         i_img_dim count, i;
3264       PPCODE:
3265         if (l < r) {
3266           data = mymalloc(sizeof(i_sample_t) * (r-l) * channels.count); /* XXX: memleak? */
3267           count = i_gsamp(im, l, r, y, data, channels.channels, channels.count);
3268           if (GIMME_V == G_ARRAY) {
3269             EXTEND(SP, count);
3270             for (i = 0; i < count; ++i)
3271               PUSHs(sv_2mortal(newSViv(data[i])));
3272           }
3273           else {
3274             EXTEND(SP, 1);
3275             PUSHs(sv_2mortal(newSVpv((char *)data, count * sizeof(i_sample_t))));
3276           }
3277           myfree(data);
3278         }
3279         else {
3280           if (GIMME_V != G_ARRAY) {
3281             EXTEND(SP, 1);
3282             PUSHs(&PL_sv_undef);
3283           }
3284         }
3285
3286 undef_neg_int
3287 i_gsamp_bits(im, l, r, y, bits, target, offset, channels)
3288         Imager::ImgRaw im
3289         i_img_dim l
3290         i_img_dim r
3291         i_img_dim y
3292         int bits
3293         AV *target
3294         STRLEN offset
3295         i_channel_list channels
3296       PREINIT:
3297         unsigned *data;
3298         i_img_dim count, i;
3299       CODE:
3300         i_clear_error();
3301         if (items < 8)
3302           croak("No channel numbers supplied to g_samp()");
3303         if (l < r) {
3304           data = mymalloc(sizeof(unsigned) * (r-l) * channels.count);
3305           count = i_gsamp_bits(im, l, r, y, data, channels.channels, channels.count, bits);
3306           for (i = 0; i < count; ++i) {
3307             av_store(target, i+offset, newSVuv(data[i]));
3308           }
3309           myfree(data);
3310           RETVAL = count;
3311         }
3312         else {
3313           RETVAL = 0;
3314         }
3315       OUTPUT:
3316         RETVAL
3317
3318 undef_neg_int
3319 i_psamp_bits(im, l, y, bits, channels, data_av, data_offset = 0, pixel_count = -1)
3320         Imager::ImgRaw im
3321         i_img_dim l
3322         i_img_dim y
3323         int bits
3324         i_channel_list channels
3325         AV *data_av
3326         i_img_dim data_offset
3327         i_img_dim pixel_count
3328       PREINIT:
3329         STRLEN data_count;
3330         size_t data_used;
3331         unsigned *data;
3332         ptrdiff_t i;
3333       CODE:
3334         i_clear_error();
3335
3336         data_count = av_len(data_av) + 1;
3337         if (data_offset < 0) {
3338           croak("data_offset must be non-negative");
3339         }
3340         if (data_offset > data_count) {
3341           croak("data_offset greater than number of samples supplied");
3342         }
3343         if (pixel_count == -1 || 
3344             data_offset + pixel_count * channels.count > data_count) {
3345           pixel_count = (data_count - data_offset) / channels.count;
3346         }
3347
3348         data_used = pixel_count * channels.count;
3349         data = mymalloc(sizeof(unsigned) * data_count);
3350         for (i = 0; i < data_used; ++i)
3351           data[i] = SvUV(*av_fetch(data_av, data_offset + i, 0));
3352
3353         RETVAL = i_psamp_bits(im, l, l + pixel_count, y, data, channels.channels, 
3354                               channels.count, bits);
3355
3356         if (data)
3357           myfree(data);
3358       OUTPUT:
3359         RETVAL
3360
3361 undef_neg_int
3362 i_psamp(im, x, y, channels, data, offset = 0, width = -1)
3363         Imager::ImgRaw im
3364         i_img_dim x
3365         i_img_dim y
3366         i_channel_list channels
3367         i_sample_list data
3368         i_img_dim offset
3369         i_img_dim width
3370     PREINIT:
3371         i_img_dim r;
3372     CODE:
3373         i_clear_error();
3374         if (offset < 0) {
3375           i_push_error(0, "offset must be non-negative");
3376           XSRETURN_UNDEF;
3377         }
3378         if (offset > 0) {
3379           if (offset > data.count) {
3380             i_push_error(0, "offset greater than number of samples supplied");
3381             XSRETURN_UNDEF;
3382           }
3383           data.samples += offset;
3384           data.count -= offset;
3385         }
3386         if (width == -1 ||
3387             width * channels.count > data.count) {
3388           width = data.count / channels.count;
3389         }
3390         r = x + width;
3391         RETVAL = i_psamp(im, x, r, y, data.samples, channels.channels, channels.count);
3392     OUTPUT:
3393         RETVAL
3394
3395 undef_neg_int
3396 i_psampf(im, x, y, channels, data, offset = 0, width = -1)
3397         Imager::ImgRaw im
3398         i_img_dim x
3399         i_img_dim y
3400         i_channel_list channels
3401         i_fsample_list data
3402         i_img_dim offset
3403         i_img_dim width
3404     PREINIT:
3405         i_img_dim r;
3406     CODE:
3407         i_clear_error();
3408         if (offset < 0) {
3409           i_push_error(0, "offset must be non-negative");
3410           XSRETURN_UNDEF;
3411         }
3412         if (offset > 0) {
3413           if (offset > data.count) {
3414             i_push_error(0, "offset greater than number of samples supplied");
3415             XSRETURN_UNDEF;
3416           }
3417           data.samples += offset;
3418           data.count -= offset;
3419         }
3420         if (width == -1 ||
3421             width * channels.count > data.count) {
3422           width = data.count / channels.count;
3423         }
3424         r = x + width;
3425         RETVAL = i_psampf(im, x, r, y, data.samples, channels.channels, channels.count);
3426     OUTPUT:
3427         RETVAL
3428
3429 Imager::ImgRaw
3430 i_img_masked_new(targ, mask, x, y, w, h)
3431         Imager::ImgRaw targ
3432         i_img_dim x
3433         i_img_dim y
3434         i_img_dim w
3435         i_img_dim h
3436       PREINIT:
3437         i_img *mask;
3438       CODE:
3439         if (SvOK(ST(1))) {
3440           if (!sv_isobject(ST(1)) 
3441               || !sv_derived_from(ST(1), "Imager::ImgRaw")) {
3442             croak("i_img_masked_new: parameter 2 must undef or an image");
3443           }
3444           mask = INT2PTR(i_img *, SvIV((SV *)SvRV(ST(1))));
3445         }
3446         else
3447           mask = NULL;
3448         RETVAL = i_img_masked_new(targ, mask, x, y, w, h);
3449       OUTPUT:
3450         RETVAL
3451
3452 int
3453 i_plin(im, l, y, ...)
3454         Imager::ImgRaw  im
3455         i_img_dim     l
3456         i_img_dim     y
3457       PREINIT:
3458         i_color *work;
3459         STRLEN i;
3460         STRLEN len;
3461         size_t count;
3462       CODE:
3463         if (items > 3) {
3464           if (items == 4 && SvOK(ST(3)) && !SvROK(ST(3))) {
3465             /* supplied as a byte string */
3466             work = (i_color *)SvPV(ST(3), len);
3467             count = len / sizeof(i_color);
3468             if (count * sizeof(i_color) != len) {
3469               croak("i_plin: length of scalar argument must be multiple of sizeof i_color");
3470             }
3471             RETVAL = i_plin(im, l, l+count, y, work);
3472           }
3473           else {
3474             work = mymalloc(sizeof(i_color) * (items-3));
3475             for (i=0; i < items-3; ++i) {
3476               if (sv_isobject(ST(i+3)) 
3477                   && sv_derived_from(ST(i+3), "Imager::Color")) {
3478                 IV tmp = SvIV((SV *)SvRV(ST(i+3)));
3479                 work[i] = *INT2PTR(i_color *, tmp);
3480               }
3481               else {
3482                 myfree(work);
3483                 croak("i_plin: pixels must be Imager::Color objects");
3484               }
3485             }
3486             RETVAL = i_plin(im, l, l+items-3, y, work);
3487             myfree(work);
3488           }
3489         }
3490         else {
3491           RETVAL = 0;
3492         }
3493       OUTPUT:
3494         RETVAL
3495
3496 int
3497 i_ppixf(im, x, y, cl)
3498         Imager::ImgRaw im
3499         i_img_dim x
3500         i_img_dim y
3501         Imager::Color::Float cl
3502
3503 void
3504 i_gsampf(im, l, r, y, channels)
3505         Imager::ImgRaw im
3506         i_img_dim l
3507         i_img_dim r
3508         i_img_dim y
3509         i_channel_list channels
3510       PREINIT:
3511         i_fsample_t *data;
3512         i_img_dim count, i;
3513       PPCODE:
3514         if (l < r) {
3515           data = mymalloc(sizeof(i_fsample_t) * (r-l) * channels.count);
3516           count = i_gsampf(im, l, r, y, data, channels.channels, channels.count);
3517           if (GIMME_V == G_ARRAY) {
3518             EXTEND(SP, count);
3519             for (i = 0; i < count; ++i)
3520               PUSHs(sv_2mortal(newSVnv(data[i])));
3521           }
3522           else {
3523             EXTEND(SP, 1);
3524             PUSHs(sv_2mortal(newSVpv((void *)data, count * sizeof(i_fsample_t))));
3525           }
3526           myfree(data);
3527         }
3528         else {
3529           if (GIMME_V != G_ARRAY) {
3530             EXTEND(SP, 1);
3531             PUSHs(&PL_sv_undef);
3532           }
3533         }
3534
3535 int
3536 i_plinf(im, l, y, ...)
3537         Imager::ImgRaw  im
3538         i_img_dim     l
3539         i_img_dim     y
3540       PREINIT:
3541         i_fcolor *work;
3542         i_img_dim i;
3543         STRLEN len;
3544         size_t count;
3545       CODE:
3546         if (items > 3) {
3547           if (items == 4 && SvOK(ST(3)) && !SvROK(ST(3))) {
3548             /* supplied as a byte string */
3549             work = (i_fcolor *)SvPV(ST(3), len);
3550             count = len / sizeof(i_fcolor);
3551             if (count * sizeof(i_fcolor) != len) {
3552               croak("i_plin: length of scalar argument must be multiple of sizeof i_fcolor");
3553             }
3554             RETVAL = i_plinf(im, l, l+count, y, work);
3555           }
3556           else {
3557             work = mymalloc(sizeof(i_fcolor) * (items-3));
3558             for (i=0; i < items-3; ++i) {
3559               if (sv_isobject(ST(i+3)) 
3560                   && sv_derived_from(ST(i+3), "Imager::Color::Float")) {
3561                 IV tmp = SvIV((SV *)SvRV(ST(i+3)));
3562                 work[i] = *INT2PTR(i_fcolor *, tmp);
3563               }
3564               else {
3565                 myfree(work);
3566                 croak("i_plinf: pixels must be Imager::Color::Float objects");
3567               }
3568             }
3569             /**(char *)0 = 1;*/
3570             RETVAL = i_plinf(im, l, l+items-3, y, work);
3571             myfree(work);
3572           }
3573         }
3574         else {
3575           RETVAL = 0;
3576         }
3577       OUTPUT:
3578         RETVAL
3579
3580 SV *
3581 i_gpixf(im, x, y)
3582         Imager::ImgRaw im
3583         i_img_dim x
3584         i_img_dim y;
3585       PREINIT:
3586         i_fcolor *color;
3587       CODE:
3588         color = (i_fcolor *)mymalloc(sizeof(i_fcolor));
3589         if (i_gpixf(im, x, y, color) == 0) {
3590           RETVAL = NEWSV(0,0);
3591           sv_setref_pv(RETVAL, "Imager::Color::Float", (void *)color);
3592         }
3593         else {
3594           myfree(color);
3595           RETVAL = &PL_sv_undef;
3596         }
3597       OUTPUT:
3598         RETVAL
3599
3600 void
3601 i_glin(im, l, r, y)
3602         Imager::ImgRaw im
3603         i_img_dim l
3604         i_img_dim r
3605         i_img_dim y
3606       PREINIT:
3607         i_color *vals;
3608         i_img_dim count, i;
3609       PPCODE:
3610         if (l < r) {
3611           vals = mymalloc((r-l) * sizeof(i_color));
3612           memset(vals, 0, (r-l) * sizeof(i_color));
3613           count = i_glin(im, l, r, y, vals);
3614           if (GIMME_V == G_ARRAY) {
3615             EXTEND(SP, count);
3616             for (i = 0; i < count; ++i) {
3617               SV *sv = make_i_color_sv(aTHX_ vals+i);
3618               PUSHs(sv);
3619             }
3620           }
3621           else if (count) {
3622             EXTEND(SP, 1);
3623             PUSHs(sv_2mortal(newSVpv((void *)vals, count * sizeof(i_color))));
3624           }
3625           myfree(vals);
3626         }
3627
3628 void
3629 i_glinf(im, l, r, y)
3630         Imager::ImgRaw im
3631         i_img_dim l
3632         i_img_dim r
3633         i_img_dim y
3634       PREINIT:
3635         i_fcolor *vals;
3636         i_img_dim count, i;
3637         i_fcolor zero;
3638       PPCODE:
3639         for (i = 0; i < MAXCHANNELS; ++i)
3640           zero.channel[i] = 0;
3641         if (l < r) {
3642           vals = mymalloc((r-l) * sizeof(i_fcolor));
3643           for (i = 0; i < r-l; ++i)
3644             vals[i] = zero;
3645           count = i_glinf(im, l, r, y, vals);
3646           if (GIMME_V == G_ARRAY) {
3647             EXTEND(SP, count);
3648             for (i = 0; i < count; ++i) {
3649               SV *sv;
3650               i_fcolor *col = mymalloc(sizeof(i_fcolor));
3651               *col = vals[i];
3652               sv = sv_newmortal();
3653               sv_setref_pv(sv, "Imager::Color::Float", (void *)col);
3654               PUSHs(sv);
3655             }
3656           }
3657           else if (count) {
3658             EXTEND(SP, 1);
3659             PUSHs(sv_2mortal(newSVpv((void *)vals, count * sizeof(i_fcolor))));
3660           }
3661           myfree(vals);
3662         }
3663
3664 Imager::ImgRaw
3665 i_img_8_new(x, y, ch)
3666         i_img_dim x
3667         i_img_dim y
3668         int ch
3669
3670 Imager::ImgRaw
3671 i_img_16_new(x, y, ch)
3672         i_img_dim x
3673         i_img_dim y
3674         int ch
3675
3676 Imager::ImgRaw
3677 i_img_to_rgb16(im)
3678        Imager::ImgRaw im
3679
3680 Imager::ImgRaw
3681 i_img_double_new(x, y, ch)
3682         i_img_dim x
3683         i_img_dim y
3684         int ch
3685
3686 Imager::ImgRaw
3687 i_img_to_drgb(im)
3688        Imager::ImgRaw im
3689
3690 undef_int
3691 i_tags_addn(im, name, code, idata)
3692         Imager::ImgRaw im
3693         int     code
3694         int     idata
3695       PREINIT:
3696         char *name;
3697         STRLEN len;
3698       CODE:
3699         if (SvOK(ST(1)))
3700           name = SvPV(ST(1), len);
3701         else
3702           name = NULL;
3703         RETVAL = i_tags_addn(&im->tags, name, code, idata);
3704       OUTPUT:
3705         RETVAL
3706
3707 undef_int
3708 i_tags_add(im, name, code, data, idata)
3709         Imager::ImgRaw  im
3710         int code
3711         int idata
3712       PREINIT:
3713         char *name;
3714         char *data;
3715         STRLEN len;
3716       CODE:
3717         if (SvOK(ST(1)))
3718           name = SvPV(ST(1), len);
3719         else
3720           name = NULL;
3721         if (SvOK(ST(3)))
3722           data = SvPV(ST(3), len);
3723         else {
3724           data = NULL;
3725           len = 0;
3726         }
3727         RETVAL = i_tags_add(&im->tags, name, code, data, len, idata);
3728       OUTPUT:
3729         RETVAL
3730
3731 SV *
3732 i_tags_find(im, name, start)
3733         Imager::ImgRaw  im
3734         char *name
3735         int start
3736       PREINIT:
3737         int entry;
3738       CODE:
3739         if (i_tags_find(&im->tags, name, start, &entry)) {
3740           if (entry == 0)
3741             RETVAL = newSVpv("0 but true", 0);
3742           else
3743             RETVAL = newSViv(entry);
3744         } else {
3745           RETVAL = &PL_sv_undef;
3746         }
3747       OUTPUT:
3748         RETVAL
3749
3750 SV *
3751 i_tags_findn(im, code, start)
3752         Imager::ImgRaw  im
3753         int             code
3754         int             start
3755       PREINIT:
3756         int entry;
3757       CODE:
3758         if (i_tags_findn(&im->tags, code, start, &entry)) {
3759           if (entry == 0)
3760             RETVAL = newSVpv("0 but true", 0);
3761           else
3762             RETVAL = newSViv(entry);
3763         }
3764         else {
3765           RETVAL = &PL_sv_undef;
3766         }
3767       OUTPUT:
3768         RETVAL
3769
3770 int
3771 i_tags_delete(im, entry)
3772         Imager::ImgRaw  im
3773         int             entry
3774       CODE:
3775         RETVAL = i_tags_delete(&im->tags, entry);
3776       OUTPUT:
3777         RETVAL
3778
3779 int
3780 i_tags_delbyname(im, name)
3781         Imager::ImgRaw  im
3782         char *          name
3783       CODE:
3784         RETVAL = i_tags_delbyname(&im->tags, name);
3785       OUTPUT:
3786         RETVAL
3787
3788 int
3789 i_tags_delbycode(im, code)
3790         Imager::ImgRaw  im
3791         int             code
3792       CODE:
3793         RETVAL = i_tags_delbycode(&im->tags, code);
3794       OUTPUT:
3795         RETVAL
3796
3797 void
3798 i_tags_get(im, index)
3799         Imager::ImgRaw  im
3800         int             index
3801       PPCODE:
3802         if (index >= 0 && index < im->tags.count) {
3803           i_img_tag *entry = im->tags.tags + index;
3804           EXTEND(SP, 5);
3805         
3806           if (entry->name) {
3807             PUSHs(sv_2mortal(newSVpv(entry->name, 0)));
3808           }
3809           else {
3810             PUSHs(sv_2mortal(newSViv(entry->code)));
3811           }
3812           if (entry->data) {
3813             PUSHs(sv_2mortal(newSVpvn(entry->data, entry->size)));
3814           }
3815           else {
3816             PUSHs(sv_2mortal(newSViv(entry->idata)));
3817           }
3818         }
3819
3820 void
3821 i_tags_get_string(im, what_sv)
3822         Imager::ImgRaw  im
3823         SV *what_sv
3824       PREINIT:
3825         char const *name = NULL;
3826         int code;
3827         char buffer[200];
3828       PPCODE:
3829         if (SvIOK(what_sv)) {
3830           code = SvIV(what_sv);
3831           name = NULL;
3832         }
3833         else {
3834           name = SvPV_nolen(what_sv);
3835           code = 0;
3836         }
3837         if (i_tags_get_string(&im->tags, name, code, buffer, sizeof(buffer))) {
3838           EXTEND(SP, 1);
3839           PUSHs(sv_2mortal(newSVpv(buffer, 0)));
3840         }
3841
3842 int
3843 i_tags_count(im)
3844         Imager::ImgRaw  im
3845       CODE:
3846         RETVAL = im->tags.count;
3847       OUTPUT:
3848         RETVAL
3849
3850
3851
3852 MODULE = Imager         PACKAGE = Imager::FillHandle PREFIX=IFILL_
3853
3854 void
3855 IFILL_DESTROY(fill)
3856         Imager::FillHandle fill
3857
3858 int
3859 IFILL_CLONE_SKIP(...)
3860     CODE:
3861         (void)items; /* avoid unused warning for XS variable */
3862         RETVAL = 1;
3863     OUTPUT:
3864         RETVAL
3865
3866 MODULE = Imager         PACKAGE = Imager
3867
3868 Imager::FillHandle
3869 i_new_fill_solid(cl, combine)
3870         Imager::Color cl
3871         int combine
3872
3873 Imager::FillHandle
3874 i_new_fill_solidf(cl, combine)
3875         Imager::Color::Float cl
3876         int combine
3877
3878 Imager::FillHandle
3879 i_new_fill_hatch(fg, bg, combine, hatch, cust_hatch, dx, dy)
3880         Imager::Color fg
3881         Imager::Color bg
3882         int combine
3883         int hatch
3884         i_img_dim dx
3885         i_img_dim dy
3886       PREINIT:
3887         unsigned char *cust_hatch;
3888         STRLEN len;
3889       CODE:
3890         if (SvOK(ST(4))) {
3891           cust_hatch = (unsigned char *)SvPV(ST(4), len);
3892         }
3893         else
3894           cust_hatch = NULL;
3895         RETVAL = i_new_fill_hatch(fg, bg, combine, hatch, cust_hatch, dx, dy);
3896       OUTPUT:
3897         RETVAL
3898
3899 Imager::FillHandle
3900 i_new_fill_hatchf(fg, bg, combine, hatch, cust_hatch, dx, dy)
3901         Imager::Color::Float fg
3902         Imager::Color::Float bg
3903         int combine
3904         int hatch
3905         i_img_dim dx
3906         i_img_dim dy
3907       PREINIT:
3908         unsigned char *cust_hatch;
3909         STRLEN len;
3910       CODE:
3911         if (SvOK(ST(4))) {
3912           cust_hatch = (unsigned char *)SvPV(ST(4), len);
3913         }
3914         else
3915           cust_hatch = NULL;
3916         RETVAL = i_new_fill_hatchf(fg, bg, combine, hatch, cust_hatch, dx, dy);
3917       OUTPUT:
3918         RETVAL
3919
3920 Imager::FillHandle
3921 i_new_fill_image(src, matrix, xoff, yoff, combine)
3922         Imager::ImgRaw src
3923         i_img_dim xoff
3924         i_img_dim yoff
3925         int combine
3926       PREINIT:
3927         double matrix[9];
3928         double *matrixp;
3929         AV *av;
3930         IV len;
3931         SV *sv1;
3932         int i;
3933       CODE:
3934         if (!SvOK(ST(1))) {
3935           matrixp = NULL;
3936         }
3937         else {
3938           if (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVAV)
3939             croak("i_new_fill_image: parameter must be an arrayref");
3940           av=(AV*)SvRV(ST(1));
3941           len=av_len(av)+1;
3942           if (len > 9)
3943             len = 9;
3944           for (i = 0; i < len; ++i) {
3945             sv1=(*(av_fetch(av,i,0)));
3946             matrix[i] = SvNV(sv1);
3947           }
3948           for (; i < 9; ++i)
3949             matrix[i] = 0;
3950           matrixp = matrix;
3951         }
3952         RETVAL = i_new_fill_image(src, matrixp, xoff, yoff, combine);
3953       OUTPUT:
3954         RETVAL
3955
3956 MODULE = Imager  PACKAGE = Imager::Internal::Hlines  PREFIX=i_int_hlines_
3957
3958 # this class is only exposed for testing
3959
3960 int
3961 i_int_hlines_testing()
3962
3963 #if i_int_hlines_testing()
3964
3965 Imager::Internal::Hlines
3966 i_int_hlines_new(start_y, count_y, start_x, count_x)
3967         i_img_dim start_y
3968         int count_y
3969         i_img_dim start_x
3970         int count_x
3971
3972 Imager::Internal::Hlines
3973 i_int_hlines_new_img(im)
3974         Imager::ImgRaw im
3975
3976 void
3977 i_int_hlines_add(hlines, y, minx, width)
3978         Imager::Internal::Hlines hlines
3979         i_img_dim y
3980         i_img_dim minx
3981         i_img_dim width
3982
3983 void
3984 i_int_hlines_DESTROY(hlines)
3985         Imager::Internal::Hlines hlines
3986
3987 SV *
3988 i_int_hlines_dump(hlines)
3989         Imager::Internal::Hlines hlines
3990
3991 int
3992 i_int_hlines_CLONE_SKIP(cls)
3993
3994 #endif
3995
3996 BOOT:
3997         PERL_SET_GLOBAL_CALLBACKS;
3998         PERL_PL_SET_GLOBAL_CALLBACKS;