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