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