-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathImager.xs
4490 lines (3937 loc) · 102 KB
/
Imager.xs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define PERL_NO_GET_CONTEXT
#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#define NEED_newRV_noinc
#define NEED_sv_2pv_nolen
#define NEED_sv_2pvbyte
#include "ppport.h"
#ifdef __cplusplus
}
#endif
#define i_int_hlines_testing() 1
#include "imager.h"
#include "feat.h"
#include "dynaload.h"
#include "regmach.h"
#include "imextdef.h"
#include "imextpltypes.h"
#include "imperlio.h"
#include <float.h>
#if i_int_hlines_testing()
#include "imageri.h"
#endif
#include "imperl.h"
#define ARRAY_COUNT(array) (sizeof(array)/sizeof(*array))
/*
Context object management
*/
#ifdef PERL_IMPLICIT_CONTEXT
#define MY_CXT_KEY "Imager::_context" XS_VERSION
typedef struct {
im_context_t ctx;
} my_cxt_t;
START_MY_CXT
static void
start_context(pTHX) {
dMY_CXT;
MY_CXT.ctx = im_context_new();
sv_setref_pv(get_sv("Imager::_context", GV_ADD), "Imager::Context", MY_CXT.ctx);
}
static im_context_t
perl_get_context(void) {
dTHX;
dMY_CXT;
return MY_CXT.ctx;
}
#else
static im_context_t perl_context;
static void
start_context(pTHX) {
perl_context = im_context_new();
/* just so it gets destroyed */
sv_setref_pv(get_sv("Imager::_context", GV_ADD), "Imager::Context", perl_context);
}
static im_context_t
perl_get_context(void) {
return perl_context;
}
#endif
typedef im_context_t Imager__Context;
static void
S_im_context_DESTROY(pTHX_ im_context_t ctx) {
#ifdef PERL_IMPLICIT_CONTEXT
dMY_CXT;
if (ctx == MY_CXT.ctx)
MY_CXT.ctx = NULL;
#else
perl_context = NULL;
#endif
im_context_refdec(ctx, "DESTROY");
}
#define im_context_DESTROY(ctx) S_im_context_DESTROY(aTHX_ (ctx))
/* used to represent channel lists parameters */
typedef struct i_channel_list_tag {
int *channels;
int count;
} i_channel_list;
typedef struct {
size_t count;
const i_sample_t *samples;
} i_sample_list;
typedef struct {
size_t count;
const i_fsample_t *samples;
} i_fsample_list;
typedef struct {
size_t count;
const i_polygon_t *polygons;
} i_polygon_list;
typedef struct {
SV *sv;
SV *rsv;
size_t count;
i_trim_colors_t *colors;
} i_trim_color_list;
/*
Allocate memory that will be discarded when mortals are discarded.
*/
static void *
malloc_temp(pTHX_ size_t size) {
void *result;
Newx(result, size, char);
SAVEFREEPV(result);
return result;
}
static void *
calloc_temp(pTHX_ size_t size) {
void *result;
Newxz(result, size, char);
SAVEFREEPV(result);
return result;
}
/* for use with the T_AVARRAY typemap */
#define doublePtr(size) ((double *)calloc_temp(aTHX_ sizeof(double) * (size)))
#define SvDouble(sv, pname) (SvNV(sv))
#define intPtr(size) ((int *)calloc_temp(aTHX_ sizeof(int) * (size)))
#define SvInt(sv, pname) (SvIV(sv))
#define i_img_dimPtr(size) ((i_img_dim *)calloc_temp(aTHX_ sizeof(i_img_dim) * (size)))
#define SvI_img_dim(sv, pname) (SvIV(sv))
#define i_colorPtr(size) ((i_color *)calloc_temp(aTHX_ sizeof(i_color) * (size)))
#define SvI_color(sv, pname) S_sv_to_i_color(aTHX_ sv, pname)
static i_color
S_sv_to_i_color(pTHX_ SV *sv, const char *pname) {
if (!sv_derived_from(sv, "Imager::Color")) {
croak("%s: not a color object", pname);
}
return *INT2PTR(i_color *, SvIV((SV *)SvRV(sv)));
}
/* These functions are all shared - then comes platform dependant code */
static int getstr(void *hv_t,char *key,char **store) {
dTHX;
SV** svpp;
HV* hv=(HV*)hv_t;
mm_log((1,"getstr(hv_t %p, key %s, store %p)\n",hv_t,key,store));
if ( !hv_exists(hv,key,strlen(key)) ) return 0;
svpp=hv_fetch(hv, key, strlen(key), 0);
*store=SvPV(*svpp, PL_na );
return 1;
}
static int getint(void *hv_t,char *key,int *store) {
dTHX;
SV** svpp;
HV* hv=(HV*)hv_t;
mm_log((1,"getint(hv_t %p, key %s, store %p)\n",hv_t,key,store));
if ( !hv_exists(hv,key,strlen(key)) ) return 0;
svpp=hv_fetch(hv, key, strlen(key), 0);
*store=(int)SvIV(*svpp);
return 1;
}
static int getdouble(void *hv_t,char* key,double *store) {
dTHX;
SV** svpp;
HV* hv=(HV*)hv_t;
mm_log((1,"getdouble(hv_t %p, key %s, store %p)\n",hv_t,key,store));
if ( !hv_exists(hv,key,strlen(key)) ) return 0;
svpp=hv_fetch(hv, key, strlen(key), 0);
*store=(double)SvNV(*svpp);
return 1;
}
static int getvoid(void *hv_t,char* key,void **store) {
dTHX;
SV** svpp;
HV* hv=(HV*)hv_t;
mm_log((1,"getvoid(hv_t %p, key %s, store %p)\n",hv_t,key,store));
if ( !hv_exists(hv,key,strlen(key)) ) return 0;
svpp=hv_fetch(hv, key, strlen(key), 0);
*store = INT2PTR(void*, SvIV(*svpp));
return 1;
}
static int getobj(void *hv_t,char *key,char *type,void **store) {
dTHX;
SV** svpp;
HV* hv=(HV*)hv_t;
mm_log((1,"getobj(hv_t %p, key %s,type %s, store %p)\n",hv_t,key,type,store));
if ( !hv_exists(hv,key,strlen(key)) ) return 0;
svpp=hv_fetch(hv, key, strlen(key), 0);
if (sv_derived_from(*svpp,type)) {
IV tmp = SvIV((SV*)SvRV(*svpp));
*store = INT2PTR(void*, tmp);
} else {
mm_log((1,"getobj: key exists in hash but is not of correct type"));
return 0;
}
return 1;
}
UTIL_table_t i_UTIL_table={getstr,getint,getdouble,getvoid,getobj};
static void
free_buffer(void *p) {
myfree(p);
}
static void
i_log_entry(char *string, int level) {
mm_log((level, "%s", string));
}
static SV *
make_i_color_sv(pTHX_ const i_color *c) {
SV *sv;
i_color *col = mymalloc(sizeof(i_color));
*col = *c;
sv = newSV(0);
sv_setref_pv(sv, "Imager::Color", (void *)col);
return sv;
}
static SV *
make_i_color_sv_mortal(pTHX_ const i_color *c) {
return sv_2mortal(make_i_color_sv(aTHX_ c));
}
static SV *
make_i_fcolor_sv(pTHX_ const i_fcolor *c) {
SV *sv;
i_fcolor *col = mymalloc(sizeof(i_fcolor));
*col = *c;
sv = newSV(0);
sv_setref_pv(sv, "Imager::Color::Float", (void *)col);
return sv;
}
static SV *
make_i_fcolor_sv_mortal(pTHX_ const i_fcolor *c) {
return sv_2mortal(make_i_fcolor_sv(aTHX_ c));
}
#define CBDATA_BUFSIZE 8192
struct cbdata {
/* the SVs we use to call back to Perl */
SV *writecb;
SV *readcb;
SV *seekcb;
SV *closecb;
};
static ssize_t
call_reader(struct cbdata *cbd, void *buf, size_t size,
size_t maxread) {
dTHX;
int count;
int result;
SV *data;
dSP;
if (!SvOK(cbd->readcb)) {
mm_log((1, "read callback called but no readcb supplied\n"));
i_push_error(0, "read callback called but no readcb supplied");
return -1;
}
ENTER;
SAVETMPS;
EXTEND(SP, 2);
PUSHMARK(SP);
PUSHs(sv_2mortal(newSViv(size)));
PUSHs(sv_2mortal(newSViv(maxread)));
PUTBACK;
count = perl_call_sv(cbd->readcb, G_SCALAR);
SPAGAIN;
if (count != 1)
croak("Result of perl_call_sv(..., G_SCALAR) != 1");
data = POPs;
if (SvOK(data)) {
STRLEN len;
char *ptr = SvPVbyte(data, len);
if (len > maxread)
croak("Too much data returned in reader callback (wanted %d, got %d, expected %d)",
(int)size, (int)len, (int)maxread);
memcpy(buf, ptr, len);
result = len;
}
else {
result = -1;
}
PUTBACK;
FREETMPS;
LEAVE;
return result;
}
static off_t
io_seeker(void *p, off_t offset, int whence) {
dTHX;
struct cbdata *cbd = p;
int count;
off_t result;
dSP;
if (!SvOK(cbd->seekcb)) {
mm_log((1, "seek callback called but no seekcb supplied\n"));
i_push_error(0, "seek callback called but no seekcb supplied");
return -1;
}
ENTER;
SAVETMPS;
EXTEND(SP, 2);
PUSHMARK(SP);
PUSHs(sv_2mortal(newSViv(offset)));
PUSHs(sv_2mortal(newSViv(whence)));
PUTBACK;
count = perl_call_sv(cbd->seekcb, G_SCALAR);
SPAGAIN;
if (count != 1)
croak("Result of perl_call_sv(..., G_SCALAR) != 1");
result = POPi;
PUTBACK;
FREETMPS;
LEAVE;
return result;
}
static ssize_t
io_writer(void *p, void const *data, size_t size) {
dTHX;
struct cbdata *cbd = p;
I32 count;
SV *sv;
dSP;
bool success;
if (!SvOK(cbd->writecb)) {
mm_log((1, "write callback called but no writecb supplied\n"));
i_push_error(0, "write callback called but no writecb supplied");
return -1;
}
ENTER;
SAVETMPS;
EXTEND(SP, 1);
PUSHMARK(SP);
PUSHs(sv_2mortal(newSVpv((char *)data, size)));
PUTBACK;
count = perl_call_sv(cbd->writecb, G_SCALAR);
SPAGAIN;
if (count != 1)
croak("Result of perl_call_sv(..., G_SCALAR) != 1");
sv = POPs;
success = SvTRUE(sv);
PUTBACK;
FREETMPS;
LEAVE;
return success ? size : -1;
}
static ssize_t
io_reader(void *p, void *data, size_t size) {
struct cbdata *cbd = p;
return call_reader(cbd, data, size, size);
}
static int io_closer(void *p) {
dTHX;
struct cbdata *cbd = p;
int success = 1;
if (SvOK(cbd->closecb)) {
dSP;
I32 count;
ENTER;
SAVETMPS;
PUSHMARK(SP);
PUTBACK;
count = perl_call_sv(cbd->closecb, G_SCALAR);
SPAGAIN;
if (count) {
SV *sv = POPs;
success = SvTRUE(sv);
}
else
success = 0;
PUTBACK;
FREETMPS;
LEAVE;
}
return success ? 0 : -1;
}
static void io_destroyer(void *p) {
dTHX;
struct cbdata *cbd = p;
SvREFCNT_dec(cbd->writecb);
SvREFCNT_dec(cbd->readcb);
SvREFCNT_dec(cbd->seekcb);
SvREFCNT_dec(cbd->closecb);
myfree(cbd);
}
static bool
im_SvREFSCALAR(SV *sv) {
svtype type = SvTYPE(sv);
switch (type) {
case SVt_PV:
case SVt_PVIV:
case SVt_PVNV:
case SVt_PVMG:
case SVt_IV:
case SVt_NV:
case SVt_PVLV:
#if PERL_VERSION > 10
case SVt_REGEXP:
#endif
return 1;
default:
return 0;
}
}
static const char *
describe_sv(SV *sv) {
if (SvOK(sv)) {
if (SvROK(sv)) {
svtype type = SvTYPE(SvRV(sv));
switch (type) {
case SVt_PVCV: return "CV";
case SVt_PVGV: return "GV";
case SVt_PVLV: return "LV";
default: return "some reference";
}
}
else {
return "non-reference scalar";
}
}
else {
return "undef";
}
}
static i_io_glue_t *
do_io_new_buffer(pTHX_ SV *data_sv) {
const char *data;
char *data_copy;
STRLEN length;
SV *sv;
SvGETMAGIC(data_sv);
if (SvROK(data_sv)) {
if (im_SvREFSCALAR(SvRV(data_sv))) {
sv = SvRV(data_sv);
}
else {
i_push_errorf(0, "data is not a scalar or a reference to scalar");
return NULL;
}
}
else {
sv = data_sv;
}
/* previously this would keep the SV around, but this is unsafe in
many ways, so always copy the bytes */
data = SvPVbyte(sv, length);
data_copy = mymalloc(length);
memcpy(data_copy, data, length);
return io_new_buffer(data_copy, length, free_buffer, data_copy);
}
static i_io_glue_t *
do_io_new_cb(pTHX_ SV *writecb, SV *readcb, SV *seekcb, SV *closecb) {
struct cbdata *cbd;
cbd = mymalloc(sizeof(struct cbdata));
cbd->writecb = newSVsv(writecb);
cbd->readcb = newSVsv(readcb);
cbd->seekcb = newSVsv(seekcb);
cbd->closecb = newSVsv(closecb);
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)));
return io_new_cb(cbd, io_reader, io_writer, io_seeker, io_closer,
io_destroyer);
}
struct value_name {
const char *name;
int value;
};
static int
lookup_name(const struct value_name *names, int count, char *name, int def_value, int push_errors, const char *id, int *failed)
{
int i;
if (push_errors)
*failed = 0;
for (i = 0; i < count; ++i)
if (strEQ(names[i].name, name))
return names[i].value;
if (push_errors) {
i_push_errorf(0, "unknown value '%s' for %s", name, id);
*failed = 1;
}
return def_value;
}
static struct value_name transp_names[] =
{
{ "none", tr_none },
{ "threshold", tr_threshold },
{ "errdiff", tr_errdiff },
{ "ordered", tr_ordered, },
};
static struct value_name make_color_names[] =
{
{ "none", mc_none, },
{ "webmap", mc_web_map, },
{ "addi", mc_addi, },
{ "mediancut", mc_median_cut, },
{ "mono", mc_mono, },
{ "monochrome", mc_mono, },
{ "gray", mc_gray, },
{ "gray4", mc_gray4, },
{ "gray16", mc_gray16, },
};
static struct value_name translate_names[] =
{
{ "giflib", pt_giflib, },
{ "closest", pt_closest, },
{ "perturb", pt_perturb, },
{ "errdiff", pt_errdiff, },
};
static struct value_name errdiff_names[] =
{
{ "floyd", ed_floyd, },
{ "jarvis", ed_jarvis, },
{ "stucki", ed_stucki, },
{ "custom", ed_custom, },
};
static struct value_name orddith_names[] =
{
{ "random", od_random, },
{ "dot8", od_dot8, },
{ "dot4", od_dot4, },
{ "hline", od_hline, },
{ "vline", od_vline, },
{ "/line", od_slashline, },
{ "slashline", od_slashline, },
{ "\\line", od_backline, },
{ "backline", od_backline, },
{ "tiny", od_tiny, },
{ "custom", od_custom, },
};
/* look through the hash for quantization options */
static int
ip_handle_quant_opts_low(pTHX_ i_quantize *quant, HV *hv, int push_errors)
{
SV **sv;
int i;
STRLEN len;
char *str;
int failed = 0;
quant->mc_colors = mymalloc(quant->mc_size * sizeof(i_color));
sv = hv_fetch(hv, "transp", 6, 0);
if (sv && *sv && (str = SvPV(*sv, len))) {
quant->transp =
lookup_name(transp_names, sizeof(transp_names)/sizeof(*transp_names),
str, tr_none, push_errors, "transp", &failed);
if (failed)
return 0;
if (quant->transp != tr_none) {
quant->tr_threshold = 127;
sv = hv_fetch(hv, "tr_threshold", 12, 0);
if (sv && *sv)
quant->tr_threshold = SvIV(*sv);
}
if (quant->transp == tr_errdiff) {
sv = hv_fetch(hv, "tr_errdiff", 10, 0);
if (sv && *sv && (str = SvPV(*sv, len)))
quant->tr_errdiff = lookup_name(errdiff_names, sizeof(errdiff_names)/sizeof(*errdiff_names), str, ed_floyd, push_errors, "tr_errdiff", &failed);
if (failed)
return 0;
}
if (quant->transp == tr_ordered) {
quant->tr_orddith = od_tiny;
sv = hv_fetch(hv, "tr_orddith", 10, 0);
if (sv && *sv && (str = SvPV(*sv, len))) {
quant->tr_orddith = lookup_name(orddith_names, sizeof(orddith_names)/sizeof(*orddith_names), str, od_random, push_errors, "tr_orddith", &failed);
if (failed)
return 0;
}
if (quant->tr_orddith == od_custom) {
sv = hv_fetch(hv, "tr_map", 6, 0);
if (sv && *sv && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
AV *av = (AV*)SvRV(*sv);
len = av_len(av) + 1;
if (len > sizeof(quant->tr_custom))
len = sizeof(quant->tr_custom);
for (i = 0; i < len; ++i) {
SV **sv2 = av_fetch(av, i, 0);
if (sv2 && *sv2) {
quant->tr_custom[i] = SvIV(*sv2);
}
}
while (i < sizeof(quant->tr_custom))
quant->tr_custom[i++] = 0;
}
}
}
}
quant->make_colors = mc_median_cut;
sv = hv_fetch(hv, "make_colors", 11, 0);
if (sv && *sv && (str = SvPV(*sv, len))) {
quant->make_colors =
lookup_name(make_color_names, sizeof(make_color_names)/sizeof(*make_color_names), str, mc_median_cut, push_errors, "make_colors", &failed);
if (failed)
return 0;
}
sv = hv_fetch(hv, "colors", 6, 0);
if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
/* needs to be an array of Imager::Color
note that the caller allocates the mc_color array and sets mc_size
to it's size */
AV *av = (AV *)SvRV(*sv);
quant->mc_count = av_len(av)+1;
if (quant->mc_count > quant->mc_size)
quant->mc_count = quant->mc_size;
for (i = 0; i < quant->mc_count; ++i) {
SV **sv1 = av_fetch(av, i, 0);
if (sv1 && *sv1 && SvROK(*sv1) && sv_derived_from(*sv1, "Imager::Color")) {
i_color *col = INT2PTR(i_color *, SvIV((SV*)SvRV(*sv1)));
quant->mc_colors[i] = *col;
}
else if (push_errors) {
i_push_errorf(0, "colors[%d] isn't an Imager::Color object", i);
return 0;
}
}
}
sv = hv_fetch(hv, "max_colors", 10, 0);
if (sv && *sv) {
i = SvIV(*sv);
if (i <= quant->mc_size && i >= quant->mc_count)
quant->mc_size = i;
}
quant->translate = pt_closest;
sv = hv_fetch(hv, "translate", 9, 0);
if (sv && *sv && (str = SvPV(*sv, len))) {
quant->translate = lookup_name(translate_names, sizeof(translate_names)/sizeof(*translate_names), str, pt_closest, push_errors, "translate", &failed);
if (failed)
return 0;
}
sv = hv_fetch(hv, "errdiff", 7, 0);
if (sv && *sv && (str = SvPV(*sv, len))) {
quant->errdiff = lookup_name(errdiff_names, sizeof(errdiff_names)/sizeof(*errdiff_names), str, ed_floyd, push_errors, "errdiff", &failed);
if (failed)
return 0;
}
if (quant->translate == pt_errdiff && quant->errdiff == ed_custom) {
/* get the error diffusion map */
sv = hv_fetch(hv, "errdiff_width", 13, 0);
if (sv && *sv)
quant->ed_width = SvIV(*sv);
sv = hv_fetch(hv, "errdiff_height", 14, 0);
if (sv && *sv)
quant->ed_height = SvIV(*sv);
sv = hv_fetch(hv, "errdiff_orig", 12, 0);
if (sv && *sv)
quant->ed_orig = SvIV(*sv);
if (quant->ed_width > 0 && quant->ed_height > 0) {
int sum = 0;
quant->ed_map = mymalloc(sizeof(int) * quant->ed_width * quant->ed_height);
sv = hv_fetch(hv, "errdiff_map", 11, 0);
if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVAV) {
AV *av = (AV*)SvRV(*sv);
size_t avi;
len = av_len(av) + 1;
if (len > (size_t)quant->ed_width * quant->ed_height)
len = (size_t)quant->ed_width * quant->ed_height;
for (avi = 0; avi < len; ++avi) {
SV **sv2 = av_fetch(av, avi, 0);
if (sv2 && *sv2) {
IV iv = SvIV(*sv2);
if (push_errors && iv < 0) {
i_push_errorf(0, "errdiff_map values must be non-negative, errdiff[%lu] is negative",
(unsigned long)avi);
return 0;
}
quant->ed_map[avi] = iv;
sum += quant->ed_map[avi];
}
}
}
if (!sum) {
/* broken map */
myfree(quant->ed_map);
quant->ed_map = 0;
quant->errdiff = ed_floyd;
if (push_errors) {
i_push_error(0, "error diffusion map must contain some non-zero values");
return 0;
}
}
}
}
sv = hv_fetch(hv, "perturb", 7, 0);
if (sv && *sv)
quant->perturb = SvIV(*sv);
return 1;
}
static void
ip_cleanup_quant_opts(pTHX_ i_quantize *quant) {
myfree(quant->mc_colors);
if (quant->ed_map)
myfree(quant->ed_map);
}
static int
ip_handle_quant_opts2(pTHX_ i_quantize *quant, HV *hv) {
int result = ip_handle_quant_opts_low(aTHX_ quant, hv, 1);
if (!result) {
ip_cleanup_quant_opts(aTHX_ quant);
}
return result;
}
static void
ip_handle_quant_opts(pTHX_ i_quantize *quant, HV *hv) {
(void)ip_handle_quant_opts_low(aTHX_ quant, hv, 0);
}
/* copies the color map from the hv into the colors member of the HV */
static void
ip_copy_colors_back(pTHX_ HV *hv, i_quantize *quant) {
SV **sv;
AV *av;
int i;
SV *work;
sv = hv_fetch(hv, "colors", 6, 0);
if (!sv || !*sv || !SvROK(*sv) || SvTYPE(SvRV(*sv)) != SVt_PVAV) {
/* nothing to do */
return;
}
av = (AV *)SvRV(*sv);
av_clear(av);
av_extend(av, quant->mc_count+1);
for (i = 0; i < quant->mc_count; ++i) {
i_color *in = quant->mc_colors+i;
Imager__Color c = ICL_new_internal(in->rgb.r, in->rgb.g, in->rgb.b, 255);
work = sv_newmortal();
sv_setref_pv(work, "Imager::Color", (void *)c);
SvREFCNT_inc(work);
av_push(av, work);
}
}
static struct value_name
poly_fill_mode_names[] =
{
{ "evenodd", i_pfm_evenodd },
{ "nonzero", i_pfm_nonzero }
};
static i_poly_fill_mode_t
S_get_poly_fill_mode(pTHX_ SV *sv) {
if (looks_like_number(sv)) {
IV work = SvIV(sv);
if (work < (IV)i_pfm_evenodd || work > (IV)i_pfm_nonzero)
work = (IV)i_pfm_evenodd;
return (i_poly_fill_mode_t)work;
}
else {
return (i_poly_fill_mode_t)lookup_name
(poly_fill_mode_names, ARRAY_COUNT(poly_fill_mode_names),
SvPV_nolen(sv), i_pfm_evenodd, 0, NULL, NULL);
}
}
static void
S_get_polygon_list(pTHX_ i_polygon_list *polys, SV *sv) {
AV *av;
int i;
i_polygon_t *s;
SvGETMAGIC(sv);
if (!SvOK(sv) || !SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVAV)
croak("polys must be an arrayref");
av = (AV*)SvRV(sv);
polys->count = av_len(av) + 1;
if (polys->count < 1)
croak("polypolygon: no polygons provided");
s = malloc_temp(aTHX_ sizeof(i_polygon_t) * polys->count);
for (i = 0; i < polys->count; ++i) {
SV **poly_sv = av_fetch(av, i, 0);
AV *poly_av;
SV **x_sv, **y_sv;
AV *x_av, *y_av;
double *x_data, *y_data;
ssize_t j;
ssize_t point_count;
if (!poly_sv)
croak("poly_polygon: nothing found for polygon %d", i);
/* needs to be another av */
SvGETMAGIC(*poly_sv);
if (!SvOK(*poly_sv) || !SvROK(*poly_sv) || SvTYPE(SvRV(*poly_sv)) != SVt_PVAV)
croak("poly_polygon: polygon %d isn't an arrayref", i);
poly_av = (AV*)SvRV(*poly_sv);
/* with two elements */
if (av_len(poly_av) != 1)
croak("poly_polygon: polygon %d should contain two arrays", i);
x_sv = av_fetch(poly_av, 0, 0);
y_sv = av_fetch(poly_av, 1, 0);
if (!x_sv)
croak("poly_polygon: polygon %d has no x elements", i);
if (!y_sv)
croak("poly_polygon: polygon %d has no y elements", i);
SvGETMAGIC(*x_sv);
SvGETMAGIC(*y_sv);
if (!SvOK(*x_sv) || !SvROK(*x_sv) || SvTYPE(SvRV(*x_sv)) != SVt_PVAV)
croak("poly_polygon: polygon %d x elements isn't an array", i);
if (!SvOK(*y_sv) || !SvROK(*y_sv) || SvTYPE(SvRV(*y_sv)) != SVt_PVAV)
croak("poly_polygon: polygon %d y elements isn't an array", i);
x_av = (AV*)SvRV(*x_sv);
y_av = (AV*)SvRV(*y_sv);
if (av_len(x_av) != av_len(y_av))
croak("poly_polygon: polygon %d x and y arrays different lengths", i+1);
point_count = av_len(x_av)+1;
x_data = malloc_temp(aTHX_ sizeof(double) * point_count * 2);
y_data = x_data + point_count;
for (j = 0; j < point_count; ++j) {
SV **x_item_sv = av_fetch(x_av, j, 0);
SV **y_item_sv = av_fetch(y_av, j, 0);
x_data[j] = x_item_sv ? SvNV(*x_item_sv) : 0;
y_data[j] = y_item_sv ? SvNV(*y_item_sv) : 0;
}
s[i].x = x_data;
s[i].y = y_data;
s[i].count = point_count;
}
polys->polygons = s;
}
/* loads the segments of a fountain fill into an array */
static i_fountain_seg *
load_fount_segs(pTHX_ AV *asegs, int *count) {
/* Each element of segs must contain:
[ start, middle, end, c0, c1, segtype, colortrans ]
start, middle, end are doubles from 0 to 1
c0, c1 are Imager::Color::Float or Imager::Color objects
segtype, colortrans are ints
*/
int i, j;
AV *aseg;
i_fountain_seg *segs;
double work[3];
int worki[2];
*count = av_len(asegs)+1;
if (*count < 1)
croak("i_fountain must have at least one segment");
segs = mymalloc(sizeof(i_fountain_seg) * *count);
for(i = 0; i < *count; i++) {
SV **sv1 = av_fetch(asegs, i, 0);
if (!sv1 || !*sv1 || !SvROK(*sv1)
|| SvTYPE(SvRV(*sv1)) != SVt_PVAV) {
myfree(segs);
croak("i_fountain: segs must be an arrayref of arrayrefs");
}
aseg = (AV *)SvRV(*sv1);
if (av_len(aseg) != 7-1) {
myfree(segs);
croak("i_fountain: a segment must have 7 members");
}
for (j = 0; j < 3; ++j) {
SV **sv2 = av_fetch(aseg, j, 0);
if (!sv2 || !*sv2) {
myfree(segs);
croak("i_fountain: XS error");
}
work[j] = SvNV(*sv2);
}
segs[i].start = work[0];
segs[i].middle = work[1];
segs[i].end = work[2];
for (j = 0; j < 2; ++j) {
SV **sv3 = av_fetch(aseg, 3+j, 0);