forked from dk/Prima
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.c
1585 lines (1446 loc) · 45.6 KB
/
Image.c
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
/*-
* Copyright (c) 1997-2002 The Protein Laboratory, University of Copenhagen
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id$
*/
#include "img.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <math.h>
#include "apricot.h"
#include "Image.h"
#include "img_conv.h"
#include <Image.inc>
#include "Clipboard.h"
#ifdef PerlIO
typedef PerlIO *FileStream;
#else
#define PERLIO_IS_STDIO 1
typedef FILE *FileStream;
#define PerlIO_fileno(f) fileno(f)
#endif
#ifdef __cplusplus
extern "C" {
#endif
#undef my
#define inherited CDrawable->
#define my ((( PImage) self)-> self)
#define var (( PImage) self)
static Bool Image_set_extended_data( Handle self, HV * profile);
static void Image_reset_notifications( Handle self);
void
Image_init( Handle self, HV * profile)
{
dPROFILE;
inherited init( self, profile);
var-> eventMask1 =
( query_method( self, "on_headerready", 0) ? IMG_EVENTS_HEADER_READY : 0) |
( query_method( self, "on_dataready", 0) ? IMG_EVENTS_DATA_READY : 0);
Image_reset_notifications( self);
var->w = pget_i( width);
var->h = pget_i( height);
var->conversion = pget_i( conversion);
opt_assign( optHScaling, pget_B( hScaling));
opt_assign( optVScaling, pget_B( vScaling));
if ( !itype_supported( var-> type = pget_i( type)))
if ( !itype_importable( var-> type, &var-> type, nil, nil)) {
warn( "Image::init: cannot set type %08x", var-> type);
var-> type = imBW;
}
var->lineSize = (( var->w * ( var->type & imBPP) + 31) / 32) * 4;
var->dataSize = ( var->lineSize) * var->h;
if ( var-> dataSize > 0) {
var->data = allocb( var->dataSize);
memset( var-> data, 0, var-> dataSize);
if ( var-> data == nil) {
my-> make_empty( self);
croak("Image::init: cannot allocate %d bytes", var-> dataSize);
}
} else
var-> data = nil;
var->palette = allocn( RGBColor, 256);
if ( var-> palette == nil) {
free( var-> data);
var-> data = nil;
croak("Image::init: cannot allocate %d bytes", 768);
}
if ( !Image_set_extended_data( self, profile))
my-> set_data( self, pget_sv( data));
opt_assign( optPreserveType, pget_B( preserveType));
var->palSize = (1 << (var->type & imBPP)) & 0x1ff;
if (!( var->type & imGrayScale) &&
pexist( palette)) { /* palette might be killed by set_extended_data() */
int ps = apc_img_read_palette( var->palette, pget_sv( palette), true);
if ( ps) var-> palSize = ps;
}
{
Point set;
prima_read_point( pget_sv( resolution), (int*)&set, 2, "Array panic on 'resolution'");
my-> set_resolution( self, set);
}
if ( var->type & imGrayScale) switch ( var->type & imBPP)
{
case imbpp1:
memcpy( var->palette, stdmono_palette, sizeof( stdmono_palette));
break;
case imbpp4:
memcpy( var->palette, std16gray_palette, sizeof( std16gray_palette));
break;
case imbpp8:
memcpy( var->palette, std256gray_palette, sizeof( std256gray_palette));
break;
}
apc_image_create( self);
my->update_change( self);
CORE_INIT_TRANSIENT(Image);
}
void
Image_handle_event( Handle self, PEvent event)
{
inherited handle_event ( self, event);
if ( var-> stage > csNormal) return;
switch ( event-> cmd) {
case cmImageHeaderReady:
my-> notify( self, "<s", "HeaderReady");
break;
case cmImageDataReady:
my-> update_change( self);
my-> notify( self, "<siiii", "DataReady",
event-> gen. R. left,
event-> gen. R. bottom,
event-> gen. R. right - event-> gen. R. left + 1,
event-> gen. R. top - event-> gen. R. bottom + 1);
break;
}
}
void
Image_reset( Handle self, int new_type, RGBColor * palette, int palSize)
{
Bool want_palette;
RGBColor new_palette[256];
Byte * new_data = nil;
int new_pal_size = 0, new_line_size, new_data_size, want_only_palette_colors = 0;
if ( var->stage > csFrozen) return;
want_palette = (!( new_type & imGrayScale)) && ( new_type != imRGB) && (palSize > 0);
if ( want_palette) {
new_pal_size = palSize;
if ( new_pal_size == 0) want_palette = false;
if ( new_pal_size > ( 1 << ( new_type & imBPP)))
new_pal_size = 1 << ( new_type & imBPP);
if ( new_pal_size > 256)
new_pal_size = 256;
if ( palette != nil)
memcpy( new_palette, palette, new_pal_size * 3);
else
want_only_palette_colors = 1;
}
if ( !want_palette && (
((var->type == (imbpp8|imGrayScale)) && (new_type == imbpp8)) ||
((var->type == (imbpp4|imGrayScale)) && (new_type == imbpp4)) ||
((var->type == (imbpp1|imGrayScale)) && (new_type == imbpp1))
)) {
var->type = new_type;
return;
}
if ( var-> type == new_type && (
((new_type != imbpp8 && new_type != imbpp4 && new_type != imbpp1) || !want_palette)
)) return;
new_line_size = (( var-> w * ( new_type & imBPP) + 31) / 32) * 4;
new_data_size = new_line_size * var-> h;
if ( new_data_size > 0) {
if ( !( new_data = allocb( new_data_size))) {
my-> make_empty( self);
croak("Image::reset: cannot allocate %d bytes", new_data_size);
}
memset( new_data, 0, new_data_size);
if ( new_pal_size != 1)
ic_type_convert( self, new_data, new_palette, new_type,
&new_pal_size, want_only_palette_colors);
}
if ( new_pal_size > 0) {
var-> palSize = new_pal_size;
memcpy( var-> palette, new_palette, new_pal_size * 3);
}
free( var-> data);
var-> type = new_type;
var-> data = new_data;
var-> lineSize = new_line_size;
var-> dataSize = new_data_size;
my-> update_change( self);
}
void
Image_stretch( Handle self, int width, int height)
{
Byte * newData = nil;
int lineSize;
if ( var->stage > csFrozen) return;
if ( width > 65535) width = 65535;
if ( height > 65535) height = 65535;
if ( width < -65535) width = -65535;
if ( height < -65535) height = -65535;
if (( width == var->w) && ( height == var->h)) return;
if ( width == 0 || height == 0)
{
my->create_empty( self, 0, 0, var->type);
return;
}
lineSize = (( abs( width) * ( var->type & imBPP) + 31) / 32) * 4;
newData = allocb( lineSize * abs( height));
if ( newData == nil)
croak("Image::stretch: cannot allocate %d bytes", lineSize * abs( height));
memset( newData, 0, lineSize * abs( height));
if ( var-> data)
ic_stretch( var-> type, var-> data, var-> w, var-> h,
newData, width, height,
is_opt( optHScaling), is_opt( optVScaling));
free( var->data);
var->data = newData;
var->lineSize = lineSize;
var->dataSize = lineSize * abs( height);
var->w = abs( width);
var->h = abs( height);
my->update_change( self);
}
static void
Image_reset_sv( Handle self, int new_type, SV * palette, Bool triplets)
{
int colors;
RGBColor pal_buf[256], *pal_ptr;
if ( !palette || palette == nilSV) {
pal_ptr = nil;
colors = 0;
} else if ( SvROK( palette) && ( SvTYPE( SvRV( palette)) == SVt_PVAV)) {
colors = apc_img_read_palette( pal_ptr = pal_buf, palette, triplets);
} else {
pal_ptr = nil;
colors = SvIV( palette);
}
my-> reset( self, new_type, pal_ptr, colors);
}
void
Image_set( Handle self, HV * profile)
{
dPROFILE;
if ( pexist( conversion))
{
my-> set_conversion( self, pget_i( conversion));
pdelete( conversion);
}
if ( pexist( hScaling))
{
my->set_hScaling( self, pget_B( hScaling));
pdelete( hScaling);
}
if ( pexist( vScaling))
{
my->set_vScaling( self, pget_B( vScaling));
pdelete( vScaling);
}
if ( Image_set_extended_data( self, profile))
pdelete( data);
if ( pexist( type))
{
int newType = pget_i( type);
if ( !itype_supported( newType))
warn("Invalid image type requested (%08x) in Image::set_type", newType);
else
if ( !opt_InPaint) {
SV * palette;
Bool triplets;
if ( pexist( palette)) {
palette = pget_sv(palette);
triplets = true;
} else if ( pexist( colormap)) {
palette = pget_sv(colormap);
triplets = false;
} else {
palette = nilSV;
triplets = false;
}
Image_reset_sv( self, newType, palette, triplets);
}
pdelete( colormap);
pdelete( palette);
pdelete( type);
}
if ( pexist( resolution))
{
Point set;
prima_read_point( pget_sv( resolution), (int*)&set, 2, "Array panic on 'resolution'");
my-> set_resolution( self, set);
pdelete( resolution);
}
inherited set ( self, profile);
}
void
Image_done( Handle self)
{
apc_image_destroy( self);
my->make_empty( self);
inherited done( self);
}
void
Image_make_empty( Handle self)
{
free( var->data);
free( var->palette);
var->w = 0;
var->h = 0;
var->type = 0;
var->palSize = 0;
var->lineSize = 0;
var->dataSize = 0;
var->data = nil;
var->palette = nil;
my->update_change( self);
}
Bool
Image_hScaling( Handle self, Bool set, Bool scaling)
{
if ( !set)
return is_opt( optHScaling);
opt_assign( optHScaling, scaling);
return false;
}
Bool
Image_vScaling( Handle self, Bool set, Bool scaling)
{
if ( !set)
return is_opt( optVScaling);
opt_assign( optVScaling, scaling);
return false;
}
Point
Image_resolution( Handle self, Bool set, Point resolution)
{
if ( !set)
return var-> resolution;
if ( resolution. x <= 0 || resolution. y <= 0)
resolution = apc_gp_get_resolution( application);
var-> resolution = resolution;
return resolution;
}
Point
Image_size( Handle self, Bool set, Point size)
{
if ( !set)
return inherited size( self, set, size);
CImage( self)-> stretch( self, size.x, size.y);
return size;
}
SV *
Image_get_handle( Handle self)
{
char buf[ 256];
snprintf( buf, 256, "0x%08lx", apc_image_get_handle( self));
return newSVpv( buf, 0);
}
Color
Image_get_nearest_color( Handle self, Color color)
{
Byte pal;
RGBColor rgb, *pcolor;
if ( is_opt( optInDrawInfo) || is_opt( optInDraw))
return inherited get_nearest_color( self, color);
switch ( var-> type & imCategory) {
case imColor:
if (( var-> type & imBPP) > 8)
return color;
rgb. b = color & 0xFF;
rgb. g = (color >> 8) & 0xFF;
rgb. r = (color >> 16) & 0xFF;
break;
case imGrayScale:
rgb. r = rgb. g = rgb. b = (
(color & 0xFF) +
((color >> 8) & 0xFF) +
((color >> 16) & 0xFF)
) / 3;
break;
default:
return clInvalid; /* what else? */
}
pal = cm_nearest_color( rgb, var-> palSize, var-> palette);
pcolor = var->palette + pal;
return ARGB( pcolor-> r, pcolor-> g, pcolor-> b);
}
SV *
Image_data( Handle self, Bool set, SV * svdata)
{
void *data;
STRLEN dataSize;
if ( var->stage > csFrozen) return nilSV;
if ( !set)
return newSVpvn(( char *) var-> data, var-> dataSize);
data = SvPV( svdata, dataSize);
if ( is_opt( optInDraw) || dataSize <= 0) return nilSV;
memcpy( var->data, data, dataSize > var->dataSize ? var->dataSize : dataSize);
my-> update_change( self);
return nilSV;
}
/*
Routine sets image data almost as Image::set_data, but taking into
account 'lineSize', 'type', and 'reverse' fields. To be called from bunch routines,
line ::init or ::set. Returns true if relevant fields were found and
data extracted and set, and false if user data should be set throught ::set_data.
Image itself may undergo conversion during the routine; in that case 'palette'
property may be used also. All these fields, if used, or meant to be used but
erroneously set, will be deleted regardless of routine success.
*/
Bool
Image_set_extended_data( Handle self, HV * profile)
{
dPROFILE;
void *data, *proc;
STRLEN dataSize;
int lineSize = 0, newType = var-> type, fixType, oldType = -1;
Bool pexistType, pexistLine, pexistReverse, supp, reverse = false;
if ( !pexist( data)) {
if ( pexist( lineSize)) {
warn( "Image: lineSize supplied without data property.");
pdelete( lineSize);
}
return false;
}
data = SvPV( pget_sv( data), dataSize);
/* parameters check */
pexistType = pexist( type) && ( newType = pget_i( type)) != var-> type;
pexistLine = pexist( lineSize) && ( lineSize = pget_i( lineSize)) != var-> lineSize;
pexistReverse = pexist( reverse) && ( reverse = pget_B( reverse));
pdelete( lineSize);
pdelete( type);
pdelete( reverse);
if ( !pexistLine && !pexistType && !pexistReverse) return false;
if ( is_opt( optInDraw) || dataSize <= 0)
goto GOOD_RETURN;
/* determine line size, if any */
if ( pexistLine) {
if ( lineSize <= 0) {
warn( "Image::set_data: invalid lineSize:%d passed", lineSize);
goto GOOD_RETURN;
}
if ( !pexistType) { /* plain repadding */
ibc_repad(( Byte*) data, var-> data, lineSize, var-> lineSize, dataSize, var-> dataSize, 1, 1, nil, reverse);
my-> update_change( self);
goto GOOD_RETURN;
}
}
/* pre-fetch auto conversion, if set in same clause */
if ( pexist( preserveType))
opt_assign( optPreserveType, pget_B( preserveType));
if ( is_opt( optPreserveType))
oldType = var-> type;
/* getting closest type */
if (( supp = itype_supported( newType))) {
fixType = newType;
proc = nil;
} else if ( !itype_importable( newType, &fixType, &proc, nil)) {
warn( "Image::set_data: invalid image type %08x", newType);
goto GOOD_RETURN;
}
/* fixing image and maybe palette - for known type it's same code as in ::set, */
/* but here's no sense calling it, just doing what we need. */
if ( fixType != var-> type || pexist( palette) || pexist( colormap)) {
SV * palette;
Bool triplets;
if ( pexist( palette)) {
palette = pget_sv( palette);
triplets = true;
} else if ( pexist( colormap)) {
palette = pget_sv( colormap);
triplets = false;
} else {
palette = nilSV;
triplets = false;
}
Image_reset_sv( self, fixType, palette, triplets);
pdelete( palette);
pdelete( colormap);
}
/* copying user data */
if ( supp && lineSize == 0 && !reverse)
/* same code as in ::set_data */
memcpy( var->data, data, dataSize > var->dataSize ? var->dataSize : dataSize);
else {
/* if no explicit lineSize set, assuming x4 padding */
if ( lineSize == 0)
lineSize = (( var-> w * ( newType & imBPP) + 31) / 32) * 4;
/* copying using repadding routine */
ibc_repad(( Byte*) data, var-> data, lineSize, var-> lineSize, dataSize, var-> dataSize,
( newType & imBPP) / 8, ( var-> type & imBPP) / 8, proc, reverse
);
}
my-> update_change( self);
/* if want to keep original type, restoring */
if ( is_opt( optPreserveType))
my-> set_type( self, oldType);
GOOD_RETURN:
pdelete(data);
return true;
}
static size_t
img_perlio_read( void * f, size_t bufsize, void * buffer)
{
#ifdef PerlIO
return PerlIO_read(( FileStream) f, buffer, bufsize);
#else
return fread( buffer, 1, bufsize, ( FileStream) f);
#endif
}
static size_t
img_perlio_write( void * f, size_t bufsize, void * buffer)
{
#ifdef PerlIO
return PerlIO_write( ( FileStream) f, buffer, bufsize);
#else
return fwrite( buffer, 1, bufsize, ( FileStream) f);
#endif
}
static int
img_perlio_seek( void * f, long offset, int whence)
{
#ifdef PerlIO
return PerlIO_seek( ( FileStream) f, offset, whence);
#else
return fseek( ( FileStream) f, offset, whence);
#endif
}
static long
img_perlio_tell( void * f)
{
#ifdef PerlIO
return PerlIO_tell( ( FileStream) f);
#else
return ftell( ( FileStream) f);
#endif
}
static int
img_perlio_flush( void * f)
{
#ifdef PerlIO
return PerlIO_flush( ( FileStream) f);
#else
return fflush( ( FileStream) f);
#endif
}
static int
img_perlio_error( void * f)
{
#ifdef PerlIO
return PerlIO_error( ( FileStream) f);
#else
return ferror( ( FileStream) f);
#endif
}
XS( Image_load_FROMPERL)
{
dXSARGS;
Handle self;
SV * sv;
HV *profile;
char *fn;
PList ret;
Bool err = false;
FileStream f = NULL;
ImgIORequest ioreq, *pioreq;
char error[256];
if (( items < 2) || (( items % 2) != 0))
croak("Invalid usage of Prima::Image::load");
self = gimme_the_mate( ST( 0));
sv = ST(1);
if ( SvROK(sv) && SvTYPE( SvRV( sv)) == SVt_PVGV)
f = IoIFP(sv_2io(ST(1)));
if ( f != NULL) {
pioreq = &ioreq;
ioreq. handle = f;
ioreq. read = img_perlio_read;
ioreq. write = img_perlio_write;
ioreq. seek = img_perlio_seek;
ioreq. tell = img_perlio_tell;
ioreq. flush = img_perlio_flush;
ioreq. error = img_perlio_error;
fn = NULL;
} else {
fn = ( char *) SvPV_nolen( ST( 1));
pioreq = NULL;
}
profile = parse_hv( ax, sp, items, mark, 2, "Image::load");
if ( !pexist( className))
pset_c( className, self ? my-> className : ( char*) SvPV_nolen( ST( 0)));
pset_i( eventMask, self ? var-> eventMask2 : 0);
ret = apc_img_load( self, fn, pioreq, profile, error);
sv_free(( SV *) profile);
SPAGAIN;
SP -= items;
if ( ret) {
int i;
for ( i = 0; i < ret-> count; i++) {
PAnyObject o = ( PAnyObject) ret-> items[i];
if ( o && o-> mate && o-> mate != nilSV) {
XPUSHs( sv_mortalcopy( o-> mate));
if (( Handle) o != self)
--SvREFCNT( SvRV( o-> mate));
} else {
XPUSHs( &PL_sv_undef);
err = true;
}
}
plist_destroy( ret);
} else {
XPUSHs( &PL_sv_undef);
err = true;
}
/* This code breaks exception propagation chain
since it uses $@ for its own needs */
if ( err)
sv_setpv( GvSV( PL_errgv), error);
else
sv_setsv( GvSV( PL_errgv), nilSV);
PUTBACK;
return;
}
int
Image_lineSize( Handle self, Bool set, int dummy)
{
if ( set)
croak("Image::lineSize: attempt to write read-only property");
return var-> lineSize;
}
PList
Image_load_REDEFINED( SV * who, char *filename, HV * profile)
{
return nil;
}
PList
Image_load( SV * who, char *filename, HV * profile)
{
PList ret;
Handle self = gimme_the_mate( who);
char error[ 256];
if ( !pexist( className))
pset_c( className, self ? my-> className : ( char*) SvPV_nolen( who));
ret = apc_img_load( self, filename, NULL, profile, error);
return ret;
}
XS( Image_save_FROMPERL)
{
dXSARGS;
Handle self;
HV *profile;
char *fn;
int ret;
char error[256];
FileStream f = NULL;
SV * sv;
ImgIORequest ioreq, *pioreq;
if (( items < 2) || (( items % 2) != 0))
croak("Invalid usage of Prima::Image::save");
self = gimme_the_mate( ST( 0));
sv = ST(1);
if ( SvROK(sv) && SvTYPE( SvRV( sv)) == SVt_PVGV)
f = IoIFP(sv_2io(ST(1)));
if ( f != NULL) {
pioreq = &ioreq;
ioreq. handle = f;
ioreq. read = img_perlio_read;
ioreq. write = img_perlio_write;
ioreq. seek = img_perlio_seek;
ioreq. tell = img_perlio_tell;
ioreq. flush = img_perlio_flush;
ioreq. error = img_perlio_error;
fn = NULL;
} else {
fn = ( char *) SvPV_nolen( ST( 1));
pioreq = NULL;
}
profile = parse_hv( ax, sp, items, mark, 2, "Image::save");
ret = apc_img_save( self, fn, pioreq, profile, error);
sv_free(( SV *) profile);
SPAGAIN;
SP -= items;
XPUSHs( sv_2mortal( newSViv(( ret > 0) ? ret : -ret)));
/* This code breaks exception propagation chain
since it uses $@ for its own needs */
if ( ret <= 0)
sv_setpv( GvSV( PL_errgv), error);
else
sv_setsv( GvSV( PL_errgv), nilSV);
PUTBACK;
return;
}
int
Image_save_REDEFINED( SV * who, char *filename, HV * profile)
{
return 0;
}
int
Image_save( SV * who, char *filename, HV * profile)
{
Handle self = gimme_the_mate( who);
char error[ 256];
if ( !pexist( className))
pset_c( className, self ? my-> className : ( char*) SvPV_nolen( who));
return apc_img_save( self, filename, NULL, profile, error);
}
int
Image_type( Handle self, Bool set, int type)
{
HV * profile;
if ( !set)
return var->type;
profile = newHV();
pset_i( type, type);
my-> set( self, profile);
sv_free(( SV *) profile);
return nilHandle;
}
int
Image_get_bpp( Handle self)
{
return var->type & imBPP;
}
Bool
Image_begin_paint( Handle self)
{
Bool ok;
if ( !inherited begin_paint( self))
return false;
if ( !( ok = apc_image_begin_paint( self))) {
inherited end_paint( self);
perl_error();
}
return ok;
}
Bool
Image_begin_paint_info( Handle self)
{
Bool ok;
if ( is_opt( optInDraw)) return true;
if ( !inherited begin_paint_info( self))
return false;
if ( !( ok = apc_image_begin_paint_info( self))) {
inherited end_paint_info( self);
perl_error();
}
return ok;
}
void
Image_end_paint( Handle self)
{
int oldType = var->type;
if ( !is_opt( optInDraw)) return;
apc_image_end_paint( self);
inherited end_paint( self);
if ( is_opt( optPreserveType) && var->type != oldType) {
my->reset( self, oldType, nil, 0);
} else {
switch( var->type)
{
case imbpp1:
if ( var-> palSize == 2 && memcmp( var->palette, stdmono_palette, sizeof( stdmono_palette)) == 0)
var->type |= imGrayScale;
break;
case imbpp4:
if ( var-> palSize == 16 && memcmp( var->palette, std16gray_palette, sizeof( std16gray_palette)) == 0)
var->type |= imGrayScale;
break;
case imbpp8:
if ( var-> palSize == 256 && memcmp( var->palette, std256gray_palette, sizeof( std256gray_palette)) == 0)
var->type |= imGrayScale;
break;
}
my->update_change( self);
}
}
void
Image_end_paint_info( Handle self)
{
if ( !is_opt( optInDrawInfo)) return;
apc_image_end_paint_info( self);
inherited end_paint_info( self);
}
void
Image_update_change( Handle self)
{
if ( var-> stage <= csNormal) apc_image_update_change( self);
var->statsCache = 0;
}
double
Image_stats( Handle self, Bool set, int index, double value)
{
if ( index < 0 || index > isMaxIndex) return 0;
if ( set) {
var-> stats[ index] = value;
var-> statsCache |= 1 << index;
return 0;
} else {
#define gather_stats(TYP) if ( var->data) { \
TYP *src = (TYP*)var->data, *stop, *s; \
maxv = minv = *src; \
for ( y = 0; y < var->h; y++) { \
s = src; stop = s + var->w; \
while (s != stop) { \
v = (double)*s; \
sum += v; \
sum2 += v*v; \
if ( minv > v) minv = v; \
if ( maxv < v) maxv = v; \
s++; \
} \
src = (TYP*)(((Byte *)src) + var->lineSize); \
} \
}
int y;
double sum = 0.0, sum2 = 0.0, minv = 0.0, maxv = 0.0, v;
if ( var->statsCache & ( 1 << index)) return var->stats[ index];
/* calculate image stats */
switch ( var->type) {
case imByte: gather_stats(uint8_t);break;
case imShort: gather_stats(int16_t); break;
case imLong: gather_stats(int32_t); break;
case imFloat: gather_stats(float); break;
case imDouble: gather_stats(double); break;
default: return 0;
}
if ( var->w * var->h > 0)
{
var->stats[ isSum] = sum;
var->stats[ isSum2] = sum2;
sum /= var->w * var->h;
sum2 /= var->w * var->h;
sum2 = sum2 - sum*sum;
var->stats[ isMean] = sum;
var->stats[ isVariance] = sum2;
var->stats[ isStdDev] = sqrt(sum2);
var->stats[ isRangeLo] = minv;
var->stats[ isRangeHi] = maxv;
} else {
for ( y = 0; y <= isMaxIndex; y++) var->stats[ y] = 0;
}
var->statsCache = (1 << (isMaxIndex + 1)) - 1;
}
return var->stats[ index];
}
void
Image_resample( Handle self, double srcLo, double srcHi, double dstLo, double dstHi)
{
#define RSPARMS self, var->data, var->type, srcLo, srcHi, dstLo, dstHi
switch ( var->type)
{
case imByte: rs_Byte_Byte ( RSPARMS); break;
case imShort: rs_Short_Short ( RSPARMS); break;
case imLong: rs_Long_Long ( RSPARMS); break;
case imFloat: rs_float_float ( RSPARMS); break;
case imDouble: rs_double_double ( RSPARMS); break;
default: return;
}
my->update_change( self);
}
SV *
Image_palette( Handle self, Bool set, SV * palette)
{
if ( var->stage > csFrozen) return nilSV;
if ( set) {
int ps;
if ( var->type & imGrayScale) return nilSV;
if ( !var->palette) return nilSV;
ps = apc_img_read_palette( var->palette, palette, true);
if ( ps)
var-> palSize = ps;
else
warn("Invalid array reference passed to Image::palette");
my-> update_change( self);
} else {
int i;
AV * av = newAV();
int colors = ( 1 << ( var->type & imBPP)) & 0x1ff;
Byte * pal = ( Byte*) var->palette;
if (( var->type & imGrayScale) && (( var->type & imBPP) > imbpp8)) colors = 256;
if ( var-> palSize < colors) colors = var-> palSize;
for ( i = 0; i < colors*3; i++) av_push( av, newSViv( pal[ i]));
return newRV_noinc(( SV *) av);
}
return nilSV;
}
int
Image_conversion( Handle self, Bool set, int conversion)
{
if ( !set)
return var-> conversion;
return var-> conversion = conversion;
}
void
Image_create_empty( Handle self, int width, int height, int type)
{
free( var->data);
var->w = width;
var->h = height;
var->type = type;
var->lineSize = (( var->w * ( var->type & imBPP) + 31) / 32) * 4;
var->dataSize = var->lineSize * var->h;
var->palSize = (1 << (var->type & imBPP)) & 0x1ff;
var->statsCache = 0;