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