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