]> git.imager.perl.org - imager.git/blob - t/t103raw.t
was sizeof(int *)
[imager.git] / t / t103raw.t
1 print "1..6\n";
2 use Imager qw(:all);
3
4 init_log("testout/t103raw.log",1);
5
6 $green=i_color_new(0,255,0,255);
7 $blue=i_color_new(0,0,255,255);
8 $red=i_color_new(255,0,0,255);
9
10 $img=Imager::ImgRaw::new(150,150,3);
11 $cmpimg=Imager::ImgRaw::new(150,150,3);
12
13 i_box_filled($img,70,25,130,125,$green);
14 i_box_filled($img,20,25,80,125,$blue);
15 i_arc($img,75,75,30,0,361,$red);
16 i_conv($img,[0.1, 0.2, 0.4, 0.2, 0.1]);
17
18 my $timg = Imager::ImgRaw::new(20, 20, 4);
19 my $trans = i_color_new(255, 0, 0, 127);
20 i_box_filled($timg, 0, 0, 20, 20, $green);
21 i_box_filled($timg, 2, 2, 18, 18, $trans);
22
23 open(FH,">testout/t103.raw") || die "Cannot open testout/t103.raw for writing\n";
24 binmode(FH);
25 i_writeraw($img,fileno(FH)) || die "Cannot write testout/t103.raw\n";
26 close(FH);
27
28 print "ok 1\n";
29
30 open(FH,"testout/t103.raw") || die "Cannot open testout/t103.raw\n";
31 binmode(FH);
32 $cmpimg=i_readraw(fileno(FH),150,150,3,3,0) || die "Cannot read testout/t103.raw\n";
33 close(FH);
34
35 print "# raw average mean square pixel difference: ",sqrt(i_img_diff($img,$cmpimg))/150*150,"\n";
36 print "ok 2\n";
37
38 # I could have kept the raw images for these tests in binary files in
39 # testimg/, but I think keeping them as hex encoded data in here makes
40 # it simpler to add more if necessary
41 # Later we may change this to read from a scalar instead
42 save_data('testout/t103_base.raw');
43 save_data('testout/t103_3to4.raw');
44 save_data('testout/t103_line_int.raw');
45 save_data('testout/t103_img_int.raw');
46
47 # load the base image
48 open FH, "testout/t103_base.raw" 
49   or die "Cannot open testout/t103_base.raw: $!";
50 binmode FH;
51 my $baseimg = i_readraw(fileno(FH), 4, 4, 3, 3, 0)
52   or die "Cannot read base raw image";
53 close FH;
54
55 # the actual read tests
56 # each read_test() call does 2 tests:
57 #  - check if the read succeeds
58 #  - check if it matches $baseimg
59 read_test('testout/t103_3to4.raw', 4, 4, 4, 3, 0, $baseimg, 3);
60 read_test('testout/t103_line_int.raw', 4, 4, 3, 3, 1, $baseimg, 5);
61 # intrl==2 is documented in raw.c but doesn't seem to be implemented
62 #read_test('testout/t103_img_int.raw', 4, 4, 3, 3, 2, $baseimg, 7);
63
64 sub read_test {
65   my ($in, $xsize, $ysize, $data, $store, $intrl, $base, $test) = @_;
66   open FH, $in or die "Cannot open $in: $!";
67   binmode FH;
68   my $img = i_readraw(fileno(FH), $xsize, $ysize, $data, $store, $intrl);
69   if ($img) {
70     print "ok $test\n";
71     if (i_img_diff($img, $baseimg)) {
72       print "ok ",$test+1," # skip images don't match, but maybe I don't understand\n";
73     }
74     else {
75       print "ok ",$test+1,"\n";
76     }
77   }
78   else {
79     print "not ok $test # could not read image\n";
80     print "ok ",$test+1," # skip\n";
81   }
82 }
83
84 sub save_data {
85   my $outname = shift;
86   my $data = load_data();
87   open FH, "> $outname" or die "Cannot create $outname: $!";
88   binmode FH;
89   print FH $data;
90   close FH;
91 }
92
93 sub load_data {
94   my $hex = '';
95   while (<DATA>) {
96     next if /^#/;
97     last if /^EOF/;
98     chomp;
99     $hex .= $_;
100   }
101   $hex =~ tr/ //d;
102   my $result = pack("H*", $hex);
103   print unpack("H*", $result),"\n";
104   return $result;
105 }
106
107 # FIXME: may need tests for 1,2,4 channel images
108
109 __DATA__
110 # we keep some packed raw images here
111 # we decode this in the code, ignoring lines starting with #, a subfile
112 # ends with EOF, data is HEX encoded (spaces ignored)
113
114 # basic 3 channel version of the image
115 001122 011223 021324 031425
116 102132 112233 122334 132435
117 203142 213243 223344 233445
118 304152 314253 324354 334455
119 EOF
120
121 # test image for reading a 4 channel image into a 3 channel image
122 # 4 x 4 pixels
123 00112233 01122334 02132435 03142536
124 10213243 11223344 12233445 13243546
125 20314253 21324354 22334455 23344556
126 30415263 31425364 32435465 33445566
127 EOF
128
129 # test image for line based interlacing
130 # 4 x 4 pixels
131 # first line
132 00 01 02 03
133 11 12 13 14
134 22 23 24 25
135
136 # second line
137 10 11 12 13
138 21 22 23 24
139 32 33 34 35
140
141 # third line
142 20 21 22 23
143 31 32 33 34
144 42 43 44 45
145
146 # fourth line
147 30 31 32 33
148 41 42 43 44
149 52 53 54 55
150
151 EOF
152
153 # test image for image based interlacing
154 # first channel
155 00 01 02 03
156 10 11 12 13
157 20 21 22 23
158 30 31 32 33
159
160 # second channel
161 11 12 13 14
162 21 22 23 24
163 31 32 33 34
164 41 42 43 44
165
166 # third channel
167 22 23 24 25
168 32 33 34 35
169 42 43 44 45
170 52 53 54 55
171
172 EOF