This repository has been archived by the owner on Jan 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGD.pd
2295 lines (1825 loc) · 62.1 KB
/
GD.pd
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
#
# GD.pd
#
# PDLA interface to the GD c library
# ('cos looping over a piddle in perl and using the perl GD lib is too slow...)
#
# Judd Taylor, USF IMaRS
# 13 March 2003
#
use strict;
#use PDLA;
use vars qw( $VERSION );
$VERSION = "2.013006";
#####################################
# Start the General Interface Docs: #
#####################################
pp_addpm({ At => 'Top' }, <<'ENDPM');
=head1 NAME
PDLA::IO::GD - Interface to the GD image library.
=head1 SYNOPSIS
my $pdl = sequence(byte, 30, 30);
write_png($pdl, load_lut($lutfile), "test.png");
write_true_png(sequence(100, 100, 3), "test_true.png");
my $image = read_png("test.png");
my $image = read_true_png("test_true_read.png");
write_true_png($image, "test_true_read.out.png");
my $lut = read_png_lut("test.png");
$pdl = sequence(byte, 30, 30);
write_png_ex($pdl, load_lut($lutfile), "test_nocomp.png", 0);
write_png_ex($pdl, load_lut($lutfile), "test_bestcomp1.png", 9);
write_png_best($pdl, load_lut($lutfile), "test_bestcomp2.png");
$pdl = sequence(100, 100, 3);
write_true_png_ex($pdl, "test_true_nocomp.png", 0);
write_true_png_ex($pdl, "test_true_bestcomp1.png", 9);
write_true_png_best($pdl, "test_true_bestcomp2.png");
recompress_png_best("test_recomp_best.png");
=head1 DESCRIPTION
This is the "General Interface" for the PDLA::IO::GD library, and is actually several
years old at this point (read: stable). If you're feeling frisky, try the new OO
interface described below.
The general version just provides several image IO utility functions you can use with
piddle variables. It's deceptively useful, however.
=cut
ENDPM
###########################
# General Interface Code: #
###########################
# needed header files:
pp_addhdr(<<'EOH');
#include "gd.h"
#include "gdfontl.h"
#include "gdfonts.h"
#include "gdfontmb.h"
#include "gdfontg.h"
#include "gdfontt.h"
#include <stdio.h>
#define PKG "PDLA::IO::GD"
EOH
# Function to write a PNG image from a piddle variable:
pp_def( 'write_png',
Pars => 'byte img(x,y); byte lut(i,j);',
OtherPars => 'char* filename',
Doc => <<'ENDDOC',
Writes a 2-d PDLA varable out to a PNG file, using the supplied color look-up-table piddle
(hereafter referred to as a LUT).
The LUT contains a line for each value 0-255 with a corresponding R, G, and B value.
ENDDOC
Code => <<'EOC' );
gdImagePtr im;
int xsize, ysize, tmp, ind, x2, y2;
char str[255];
FILE *out;
if ($SIZE(i) != 3 || $SIZE(j) > 256)
{
croak("Wrong LUT dimensions (%"IND_FLAG", %"IND_FLAG")! (should be (3, X), where X <= 256)\n",
$SIZE(i), $SIZE(j) );
}
xsize = $SIZE(x);
ysize = $SIZE(y);
im = gdImageCreate(xsize, ysize);
/* Set up the color palette */
for(ind = 0; ind < $SIZE(j); ind++)
{
tmp = gdImageColorAllocate(im, $lut(i=>0,j=>ind), $lut(i=>1,j=>ind), $lut(i=>2,j=>ind));
if (tmp != ind)
{
croak("palette mismatch on index %d (mapped to %d)!\n", ind, tmp);
}
}
/* render the data */
for( y2 = 0; y2 < $SIZE(y); y2++ )
{
for( x2 = 0; x2 < $SIZE(x); x2++ )
{
gdImageSetPixel(im, x2, y2, $img(x=>x2,y=>y2));
}
}
/* write the image to the file */
out = fopen($COMP(filename), "wb");
gdImagePng(im, out);
fclose(out);
gdImageDestroy(im);
EOC
# Function to write a PNG image from a piddle variable, accepting a compression
# level argument:
pp_def( 'write_png_ex',
Pars => 'img(x,y); lut(i,j);',
OtherPars => 'char* filename; int level',
Doc => <<'ENDDOC',
Same as write_png(), except you can specify the compression level (0-9) as the last arguement.
ENDDOC
Code => <<'EOC' );
gdImagePtr im;
int xsize, ysize, tmp, ind, x2, y2;
char str[255];
FILE *out;
if( $COMP(level) < -1 || $COMP(level) > 9 )
{
croak("Invalid compression level %d, should be [-1,9]!\n",
$COMP(level) );
}
if ($SIZE(i) != 3 || $SIZE(j) > 256)
{
croak("Wrong LUT dimensions (%"IND_FLAG", %"IND_FLAG")! (should be (3, X), where X <= 256)\n",
$SIZE(i), $SIZE(j) );
}
xsize = $SIZE(x);
ysize = $SIZE(y);
im = gdImageCreate(xsize, ysize);
/* Set up the color palette */
for(ind = 0; ind < $SIZE(j); ind++)
{
tmp = gdImageColorAllocate(im, $lut(i=>0,j=>ind), $lut(i=>1,j=>ind), $lut(i=>2,j=>ind));
if (tmp != ind)
{
croak("palette mismatch on index %d (mapped to %d)!\n", ind, tmp);
}
}
/* render the data */
for( y2 = 0; y2 < $SIZE(y); y2++ )
{
for( x2 = 0; x2 < $SIZE(x); x2++ )
{
gdImageSetPixel(im, x2, y2, $img(x=>x2,y=>y2));
}
}
/* write the image to the file */
out = fopen($COMP(filename), "wb");
gdImagePngEx(im, out, $COMP(level));
fclose(out);
gdImageDestroy(im);
EOC
# Function to write a TRUE COLOR PNG image from a piddle variable:
pp_def( 'write_true_png',
Pars => 'img(x,y,z);',
OtherPars => 'char* filename',
Doc => <<'ENDDOC',
Writes an (x, y, z(3)) PDLA varable out to a PNG file, using a true color format.
This means a larger file on disk, but can contain more than 256 colors.
ENDDOC
Code => <<'EOC' );
gdImagePtr im;
int xsize, ysize, x2, y2;
char str[255];
FILE *out;
if ($SIZE(z) != 3)
{
croak("Wrong dimensions (%"IND_FLAG", %"IND_FLAG", %"IND_FLAG")! (should be (X,Y,3))\n",
$SIZE(x), $SIZE(y), $SIZE(z) );
}
xsize = $SIZE(x);
ysize = $SIZE(y);
im = gdImageCreateTrueColor(xsize, ysize);
/* render the data */
for( y2 = 0; y2 < ysize; y2++ )
{
for( x2 = 0; x2 < xsize; x2++ )
{
gdImageSetPixel(im, x2, y2,
gdImageColorResolve(im,
$img(x=>x2,y=>y2,z=>0),
$img(x=>x2,y=>y2,z=>1),
$img(x=>x2,y=>y2,z=>2)
)
);
}
}
/* write the image to the file */
out = fopen($COMP(filename), "wb");
gdImagePng(im, out);
fclose(out);
gdImageDestroy(im);
EOC
# Function to write a TRUE COLOR PNG image from a piddle variable,
# with the specified compression level:
pp_def( 'write_true_png_ex',
Pars => 'img(x,y,z);',
OtherPars => 'char* filename; int level',
Doc => <<'ENDDOC',
Same as write_true_png(), except you can specify the compression level (0-9) as the last arguement.
ENDDOC
Code => <<'EOC' );
gdImagePtr im;
int xsize, ysize, x2, y2;
char str[255];
FILE *out;
if( $COMP(level) < -1 || $COMP(level) > 9 )
{
croak("Invalid compression level %d, should be [-1,9]!\n",
$COMP(level) );
}
if ($SIZE(z) != 3)
{
croak("Wrong dimensions (%"IND_FLAG", %"IND_FLAG", %"IND_FLAG")! (should be (X,Y,3))\n",
$SIZE(x), $SIZE(y), $SIZE(z) );
}
xsize = $SIZE(x);
ysize = $SIZE(y);
im = gdImageCreateTrueColor(xsize, ysize);
/* render the data */
for( y2 = 0; y2 < ysize; y2++ )
{
for( x2 = 0; x2 < xsize; x2++ )
{
gdImageSetPixel(im, x2, y2,
gdImageColorResolve(im,
$img(x=>x2,y=>y2,z=>0),
$img(x=>x2,y=>y2,z=>1),
$img(x=>x2,y=>y2,z=>2)
)
);
}
}
/* write the image to the file */
out = fopen($COMP(filename), "wb");
gdImagePngEx( im, out, $COMP(level) );
fclose(out);
gdImageDestroy(im);
EOC
#
# Add some perl level alias functions to automatically use the best compression
#
pp_addpm(<<'ENDPM');
=head2 write_png_best( $img(piddle), $lut(piddle), $filename )
Like write_png(), but it assumes the best PNG compression (9).
=cut
sub write_png_best
{
my $img = shift;
my $lut = shift;
my $filename = shift;
return write_png_ex( $img, $lut, $filename, 9 );
} # End of write_png_best()...
=head2 write_true_png_best( $img(piddle), $filename )
Like write_true_png(), but it assumes the best PNG compression (9).
=cut
sub write_true_png_best
{
my $img = shift;
my $filename = shift;
return write_true_png_ex( $img, $filename, 9 );
} # End of write_true_png_best()...
ENDPM
# End of best copression aliases
pp_add_exported( '', 'write_png_best write_true_png_best' );
#
# Function to recompress PNG files with the best compression available:
# NOTE: libgd doesn't return anything, so there's nothing to check!
pp_addpm( '', <<'ENDPM' );
=head2 recompress_png_best( $filename )
Recompresses the given PNG file using the best compression (9).
=cut
ENDPM
pp_addxs( '', <<'ENDXS' );
void
recompress_png_best(char* filename)
CODE:
gdImagePtr im;
FILE* file;
file = fopen(filename, "rb");
im = gdImageCreateFromPng(file);
fclose(file);
file = fopen(filename, "wb");
gdImagePngEx( im, file, 9 );
fclose(file);
gdImageDestroy(im);
ENDXS
pp_add_exported( '', 'recompress_png_best' );
# End of recompress_png_best() XS code...
pp_addpm(<<'EOPM');
=head2 load_lut( $filename )
Loads a color look up table from an ASCII file. returns a piddle
=cut
sub load_lut
{
return xchg(byte(cat(rcols(shift))), 0, 1);
} # end of load_lut()...
=head2 read_png( $filename )
Reads a (palette) PNG image into a (new) PDLA variable.
=cut
sub read_png
{
my $filename = shift;
# Get the image dims...
my $x = _get_png_xs($filename);
my $y = _get_png_ys($filename);
#print "\$x=$x\t\$y=$y\n";
my $temp = zeroes(long, $x, $y);
_read_png($temp, $filename);
return byte($temp);
} # End of read_png()...
=head2 read_png_true( $filename )
Reads a true color PNG image into a (new) PDLA variable.
=cut
sub read_true_png
{
my $filename = shift;
# Get the image dims...
my $x = _get_png_xs($filename);
my $y = _get_png_ys($filename);
#print "\$x=$x\t\$y=$y\n";
my $temp = zeroes(long, $x, $y, 3);
_read_true_png($temp, $filename);
return byte($temp);
} # End of read_png()...
EOPM
pp_add_exported('', 'load_lut read_png read_true_png');
pp_addxs('', <<'EOXS');
int
_get_png_xs(char* filename)
CODE:
gdImagePtr im;
FILE* in;
in = fopen(filename, "rb");
im = gdImageCreateFromPng(in);
fclose(in);
RETVAL = gdImageSX(im);
gdImageDestroy(im);
OUTPUT:
RETVAL
int
_get_png_ys(char* filename)
CODE:
gdImagePtr im;
FILE* in;
in = fopen(filename, "rb");
im = gdImageCreateFromPng(in);
fclose(in);
RETVAL = gdImageSY(im);
gdImageDestroy(im);
OUTPUT:
RETVAL
EOXS
# Function to read a TRUE COLOR PNG image into a piddle variable:
pp_def( '_read_true_png',
Pars => 'int [o] img(x,y,z);',
OtherPars => 'char* filename',
Doc => undef,
Code => <<'EOC' );
gdImagePtr im;
int xsize, ysize, x2, y2, z2;
char* func = "PDLA::IO::GD::_read_png(): ";
char str[255];
FILE *in = NULL;
in = fopen($COMP(filename), "rb");
if ( in == NULL )
{
croak("%sError opening %s!\n", func, $COMP(filename));
}
im = gdImageCreateFromPng(in);
if ( im == NULL )
{
croak("%sError reading PNG data!\n", func);
}
fclose(in);
xsize = gdImageSX(im);
ysize = gdImageSY(im);
/* Check the dims... */
if ( !( ($SIZE(x)==xsize) && ($SIZE(y)==ysize) ) )
{
croak("%sDims of %s (%dx%d) and piddle (%"IND_FLAG"x%"IND_FLAG") do not match!\n",
func, $COMP(filename), xsize, ysize, $SIZE(x), $SIZE(y));
}
/* read the data */
for( y2 = 0; y2 < ysize; y2++ )
{
for( x2 = 0; x2 < xsize; x2++ )
{
int tpixel = gdImageTrueColorPixel(im, x2, y2);
$img(x=>x2,y=>y2,z=>0) = gdTrueColorGetRed(tpixel);
$img(x=>x2,y=>y2,z=>1) = gdTrueColorGetGreen(tpixel);
$img(x=>x2,y=>y2,z=>2) = gdTrueColorGetBlue(tpixel);
}
}
gdImageDestroy(im);
EOC
# Function to read PNG image into a piddle variable:
pp_def( '_read_png',
Pars => 'int [o] img(x,y);',
OtherPars => 'char* filename',
Doc => undef,
Code => <<'EOC' );
gdImagePtr im;
int xsize, ysize, x2, y2;
char* func = "PDLA::IO::GD::_read_png(): ";
char str[255];
FILE *in = NULL;
in = fopen($COMP(filename), "rb");
if ( in == NULL )
{
croak("%sError opening %s!\n", func, $COMP(filename));
}
im = gdImageCreateFromPng(in);
if ( im == NULL )
{
croak("%sError reading PNG data!\n", func);
}
fclose(in);
xsize = gdImageSX(im);
ysize = gdImageSY(im);
/* Check the dims... */
if ( !( ($SIZE(x)==xsize) && ($SIZE(y)==ysize) ) )
{
croak("%sDims of %s (%dx%d) and piddle (%"IND_FLAG"x%"IND_FLAG") do not match!\n",
func, $COMP(filename), xsize, ysize, $SIZE(x), $SIZE(y));
}
/* read the data */
for( y2 = 0; y2 < ysize; y2++ )
{
for( x2 = 0; x2 < xsize; x2++ )
{
$img(x=>x2,y=>y2) = gdImageGetPixel(im, x2, y2);
}
}
/* write the image to the file */
gdImageDestroy(im);
EOC
pp_def( '_gd_image_to_pdl_true',
Pars => 'byte [o] img(x,y,z);',
OtherPars => 'IV img_ptr',
Doc => undef,
Code => <<'ENDCODE' );
int xsize, ysize, x2, y2, z2;
gdImagePtr im = INT2PTR(gdImagePtr, $COMP(img_ptr));
char* func = "PDLA::IO::GD::_gd_image_to_pdl_true(): ";
char str[255];
xsize = gdImageSX(im);
ysize = gdImageSY(im);
/* Check the dims... */
if ( !( ($SIZE(x)==xsize) && ($SIZE(y)==ysize) ) )
{
croak("%sDims of gdImage (%dx%d) and piddle (%"IND_FLAG"x%"IND_FLAG") do not match!\n",
func, xsize, ysize, $SIZE(x), $SIZE(y));
}
/* read the data */
for( y2 = 0; y2 < ysize; y2++ )
{
for( x2 = 0; x2 < xsize; x2++ )
{
int tpixel = gdImageTrueColorPixel(im, x2, y2);
$img(x=>x2,y=>y2,z=>0) = gdTrueColorGetRed(tpixel);
$img(x=>x2,y=>y2,z=>1) = gdTrueColorGetGreen(tpixel);
$img(x=>x2,y=>y2,z=>2) = gdTrueColorGetBlue(tpixel);
}
}
ENDCODE
pp_def( '_gd_image_to_pdl',
Pars => 'byte [o] img(x,y);',
OtherPars => 'IV img_ptr',
Doc => undef,
Code => <<'ENDCODE' );
int xsize, ysize, x2, y2;
char* func = "PDLA::IO::GD::_gd_image_to_pdl(): ";
gdImagePtr im = INT2PTR(gdImagePtr, $COMP(img_ptr));
char str[255];
xsize = gdImageSX(im);
ysize = gdImageSY(im);
/* Check the dims... */
if ( !( ($SIZE(x)==xsize) && ($SIZE(y)==ysize) ) )
{
croak("%sDims of gdImage (%dx%d) and piddle (%"IND_FLAG"x%"IND_FLAG") do not match!\n",
func, xsize, ysize, $SIZE(x), $SIZE(y));
}
/* read the data */
for( y2 = 0; y2 < ysize; y2++ )
{
for( x2 = 0; x2 < xsize; x2++ )
{
$img(x=>x2,y=>y2) = gdImageGetPixel(im, x2, y2);
}
}
ENDCODE
pp_def( '_pdl_to_gd_image_true',
Pars => 'byte img(x,y,z); longlong [o] img_ptr(i)',
Doc => undef,
Code => <<'ENDCODE' );
gdImagePtr im;
int xsize, ysize, x2, y2;
char str[255];
if ($SIZE(z) != 3)
{
croak("Wrong dimensions (%"IND_FLAG", %"IND_FLAG", %"IND_FLAG")! (should be (X,Y,3))\n",
$SIZE(x), $SIZE(y), $SIZE(z) );
}
xsize = $SIZE(x);
ysize = $SIZE(y);
im = gdImageCreateTrueColor(xsize, ysize);
/* render the data */
for( y2 = 0; y2 < ysize; y2++ )
{
for( x2 = 0; x2 < xsize; x2++ )
{
gdImageSetPixel(im, x2, y2,
gdImageColorResolve(im,
$img(x=>x2,y=>y2,z=>0),
$img(x=>x2,y=>y2,z=>1),
$img(x=>x2,y=>y2,z=>2)
)
);
}
}
$img_ptr(i=>0) = (PDLA_LongLong) PTR2IV(im);
ENDCODE
pp_def( '_pdl_to_gd_image_lut',
Pars => 'byte img(x,y); byte lut(i,j); longlong [o] img_ptr(q)',
Doc => undef,
Code => <<'ENDCODE' );
gdImagePtr im;
int xsize, ysize, tmp, ind, x2, y2;
char str[255];
if ($SIZE(i) != 3 || $SIZE(j) > 256)
{
croak("Wrong LUT dimensions (%"IND_FLAG", %"IND_FLAG")! (should be (3, X), where X <= 256)\n",
$SIZE(i), $SIZE(j) );
}
xsize = $SIZE(x);
ysize = $SIZE(y);
im = gdImageCreate(xsize, ysize);
/* Set up the color palette */
for(ind = 0; ind < $SIZE(j); ind++)
{
tmp = gdImageColorAllocate(im, $lut(i=>0,j=>ind), $lut(i=>1,j=>ind), $lut(i=>2,j=>ind));
if (tmp != ind)
{
croak("palette mismatch on index %d (mapped to %d)!\n", ind, tmp);
}
}
/* render the data */
for( y2 = 0; y2 < $SIZE(y); y2++ )
{
for( x2 = 0; x2 < $SIZE(x); x2++ )
{
gdImageSetPixel(im, x2, y2, $img(x=>x2,y=>y2));
}
}
$img_ptr(q=>0) = (PDLA_LongLong) PTR2IV(im);
ENDCODE
# Function to write Read PNG LUT Table into a piddle variable:
pp_addpm(<<'EOPM');
=head2 my $lut = read_png_lut( $filename )
Reads a color LUT from an already-existing palette PNG file.
=cut
sub read_png_lut
{
my $filename = shift;
my $lut = zeroes(byte, 3, 256);
_read_png_lut($lut, $filename);
return $lut;
} # End of read_png_lut()...
EOPM
pp_add_exported('', 'read_png_lut');
pp_def( '_read_png_lut',
Pars => 'byte [o] lut(c,i);',
OtherPars => 'char* filename',
Doc => undef,
Code => <<'EOC' );
gdImagePtr im;
int ind;
char* func = "PDLA::IO::GD::_read_png_lut(): ";
char str[255];
FILE *in = NULL;
/* Check dims: */
if ( $SIZE(c) != 3 )
{
croak("%sLUT dims should be 3,256!\n", func);
}
in = fopen($COMP(filename), "rb");
if ( in == NULL )
{
croak("%sError opening %s!\n", func, $COMP(filename));
}
im = gdImageCreateFromPng(in);
if ( im == NULL )
{
croak("%sError reading PNG data!\n", func);
}
fclose(in);
/* read the data */
for( ind = 0; ind < 256; ind++ )
{
$lut(c=>0,i=>ind) = gdImageRed(im, ind);
$lut(c=>1,i=>ind) = gdImageGreen(im, ind);
$lut(c=>2,i=>ind) = gdImageBlue(im, ind);
}
gdImageDestroy(im);
EOC
pp_addxs( <<'ENDXS' );
void
_gdImageDestroy( im )
gdImagePtr im
CODE:
/* fprintf( stderr, "_gdImageDestroy(): gdImagePtr = %p (d=%d x=%x l=%ld ll=%lld)\n", im, im, im, im, im);*/
gdImageDestroy ( im );
OUTPUT:
ENDXS
####################
# NEW OO Interface #
####################
##############################################
# Autogeneration of the low level interface: #
##############################################
##################################################
# Process functions to create images from files: #
##################################################
#########################################
# Start the PDLA::IO::GD OO module code: #
#########################################
pp_addpm( { At => 'Bot' }, <<'ENDPM' );
=head1 OO INTERFACE
Object Oriented interface to the GD image library.
=head1 SYNOPSIS
# Open an existing file:
#
my $gd = PDLA::IO::GD->new( { filename => "test.png" } );
# Query the x and y sizes:
my $x = $gd->SX();
my $y = $gd->SY();
# Grab the PDLA of the data:
my $pdl = $gd->to_pdl();
# Kill this thing:
$gd->DESTROY();
# Create a new object:
#
my $im = PDLA::IO::GD->new( { x => 300, y => 300 } );
# Allocate some colors:
#
my $black = $im->ColorAllocate( 0, 0, 0 );
my $red = $im->ColorAllocate( 255, 0, 0 );
my $green = $im->ColorAllocate( 0, 255, 0 );
my $blue = $im->ColorAllocate( 0, 0, 255 );
# Draw a rectangle:
$im->Rectangle( 10, 10, 290, 290, $red );
# Add some text:
$im->String( gdFontGetLarge(), 20, 20, "Test Large Font!", $green );
# Write the output file:
$im->write_Png( "test2.png" );
=head1 DESCRIPTION
This is the Object-Oriented interface from PDLA to the GD image library.
See L<http://www.boutell.com/gd/> for more information on the GD library and how it works.
=head2 IMPLEMENTATION NOTES
Surprisingly enough, this interface has nothing to do with the other Perl->GD interface module,
aka 'GD' (as in 'use GD;'). This is done from scratch over the years.
Requires at least version 2.0.22 of the GD library, but it's only been thoroughly tested with
gd-2.0.33, so it would be best to use that. The 2.0.22 requirement has to do with a change in
GD's font handling functions, so if you don't use those, then don't worry about it.
I should also add, the statement about "thoroughly tested" above is mostly a joke. This OO
interface is very young, and it has I<barely> been tested at all, so if something
breaks, email me and I'll get it fixed ASAP (for me).
Functions that manipulate and query the image objects generally have a 'gdImage' prefix on the
function names (ex: gdImageString()). I've created aliases here for all of those member
functions so you don't have to keep typing 'gdImage' in your code, but the long version are in
there as well.
=head1 METHODS
=cut
use PDLA;
use PDLA::Slices;
use PDLA::IO::Misc;
#
# Some helper functions:
#
sub _pkg_name
{ return "PDLA::IO::GD::" . (shift) . "()"; }
# ID a file type from it's filename:
sub _id_image_file
{
my $filename = shift;
return 'png'
if( $filename =~ /\.png$/ );
return 'jpg'
if( $filename =~ /\.jpe?g$/ );
return 'wbmp'
if( $filename =~ /\.w?bmp$/ );
return 'gd'
if( $filename =~ /\.gd$/ );
return 'gd2'
if( $filename =~ /\.gd2$/ );
return 'gif'
if( $filename =~ /\.gif$/ );
return 'xbm'
if( $filename =~ /\.xbm$/ );
return undef;
} # End of _id_image_file()...
# Load a new file up (don't read it yet):
sub _img_ptr_from_file
{
my $filename = shift;
my $type = shift;
return _gdImageCreateFromPng( $filename )
if( $type eq 'png' );
return _gdImageCreateFromJpeg( $filename )
if( $type eq 'jpg' );
return _gdImageCreateFromWBMP( $filename )
if( $type eq 'wbmp' );
return _gdImageCreateFromGd( $filename )
if( $type eq 'gd' );
return _gdImageCreateFromGd2( $filename )
if( $type eq 'gd2' );
return _gdImageCreateFromGif( $filename )
if( $type eq 'gif' );
return _gdImageCreateFromXbm( $filename )
if( $type eq 'xbm' );
return undef;
} # End of _img_ptr_from_file()...
# ID a file type from it's "magic" header in the image data:
sub _id_image_data
{
my $data = shift;
my $magic = substr($data,0,4);
return 'png'
if( $magic eq "\x89PNG" );
return 'jpg'
if( $magic eq "\377\330\377\340" );
return 'jpg'
if( $magic eq "\377\330\377\341" );
return 'jpg'
if( $magic eq "\377\330\377\356" );
return 'gif'
if( $magic eq "GIF8" );
return 'gd2'
if( $magic eq "gd2\000" );
# Still need filters for WBMP and .gd!
return undef;
} # End of _id_image_data()...
# Load a new data scalar up:
sub _img_ptr_from_data
{
my $data = shift;
my $type = shift;
return _gdImageCreateFromPngPtr( $data )
if( $type eq 'png' );
return _gdImageCreateFromJpegPtr( $data )
if( $type eq 'jpg' );
return _gdImageCreateFromWBMPPtr( $data )
if( $type eq 'wbmp' );
return _gdImageCreateFromGdPtr( $data )
if( $type eq 'gd' );
return _gdImageCreateFromGd2Ptr( $data )
if( $type eq 'gd2' );
return _gdImageCreateFromGifPtr( $data )
if( $type eq 'gif' );
return undef;
} # End of _img_ptr_from_data()...
=head2 new
Creates a new PDLA::IO::GD object.
Accepts a hash describing how to create the object. Accepts a single hash ( with
curly braces ), an inline hash (the same, but without the braces) or a single
string interpreted as a filename. Thus the following are all equivalent:
PDLA::IO::GD->new( {filename => 'image.png'} );
PDLA::IO::GD->new( filename => 'image.png' );
PDLA::IO::GD->new( 'image.png' );
If the hash has: