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