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