]> git.imager.perl.org - imager.git/blame - t/t103raw.t
- i_readraw_wiol() now checks for image creation failure
[imager.git] / t / t103raw.t
CommitLineData
faa9b3e7 1#!perl -w
50dc291e 2print "1..16\n";
9267f8f3 3use Imager qw(:all);
faa9b3e7 4use strict;
9267f8f3
TC
5init_log("testout/t103raw.log",1);
6
faa9b3e7
TC
7my $green=i_color_new(0,255,0,255);
8my $blue=i_color_new(0,0,255,255);
9my $red=i_color_new(255,0,0,255);
9267f8f3 10
faa9b3e7
TC
11my $img=Imager::ImgRaw::new(150,150,3);
12my $cmpimg=Imager::ImgRaw::new(150,150,3);
9267f8f3
TC
13
14i_box_filled($img,70,25,130,125,$green);
15i_box_filled($img,20,25,80,125,$blue);
16i_arc($img,75,75,30,0,361,$red);
17i_conv($img,[0.1, 0.2, 0.4, 0.2, 0.1]);
18
19my $timg = Imager::ImgRaw::new(20, 20, 4);
20my $trans = i_color_new(255, 0, 0, 127);
21i_box_filled($timg, 0, 0, 20, 20, $green);
22i_box_filled($timg, 2, 2, 18, 18, $trans);
23
24open(FH,">testout/t103.raw") || die "Cannot open testout/t103.raw for writing\n";
25binmode(FH);
faa9b3e7 26my $IO = Imager::io_new_fd( fileno(FH) );
895dbd34 27i_writeraw_wiol($img, $IO) || die "Cannot write testout/t103.raw\n";
9267f8f3
TC
28close(FH);
29
30print "ok 1\n";
31
32open(FH,"testout/t103.raw") || die "Cannot open testout/t103.raw\n";
33binmode(FH);
895dbd34
AMH
34$IO = Imager::io_new_fd( fileno(FH) );
35$cmpimg = i_readraw_wiol($IO, 150, 150, 3, 3, 0) || die "Cannot read testout/t103.raw\n";
9267f8f3
TC
36close(FH);
37
38print "# raw average mean square pixel difference: ",sqrt(i_img_diff($img,$cmpimg))/150*150,"\n";
39print "ok 2\n";
40
41# I could have kept the raw images for these tests in binary files in
42# testimg/, but I think keeping them as hex encoded data in here makes
43# it simpler to add more if necessary
44# Later we may change this to read from a scalar instead
45save_data('testout/t103_base.raw');
46save_data('testout/t103_3to4.raw');
47save_data('testout/t103_line_int.raw');
48save_data('testout/t103_img_int.raw');
49
50# load the base image
51open FH, "testout/t103_base.raw"
52 or die "Cannot open testout/t103_base.raw: $!";
53binmode FH;
895dbd34
AMH
54$IO = Imager::io_new_fd( fileno(FH) );
55
56my $baseimg = i_readraw_wiol( $IO, 4, 4, 3, 3, 0)
9267f8f3
TC
57 or die "Cannot read base raw image";
58close FH;
59
60# the actual read tests
61# each read_test() call does 2 tests:
62# - check if the read succeeds
63# - check if it matches $baseimg
64read_test('testout/t103_3to4.raw', 4, 4, 4, 3, 0, $baseimg, 3);
65read_test('testout/t103_line_int.raw', 4, 4, 3, 3, 1, $baseimg, 5);
66# intrl==2 is documented in raw.c but doesn't seem to be implemented
67#read_test('testout/t103_img_int.raw', 4, 4, 3, 3, 2, $baseimg, 7);
68
faa9b3e7
TC
69# paletted images
70my $palim = Imager::i_img_pal_new(20, 20, 3, 256)
71 or print "not ";
72print "ok 7\n";
73my $redindex = Imager::i_addcolors($palim, $red);
74my $blueindex = Imager::i_addcolors($palim, $blue);
75for my $y (0..9) {
76 Imager::i_ppal($palim, 0, $y, ($redindex) x 20);
77}
78for my $y (10..19) {
79 Imager::i_ppal($palim, 0, $y, ($blueindex) x 20);
80}
81open FH, "> testout/t103_pal.raw"
82 or die "Cannot create testout/t103_pal.raw: $!";
83binmode FH;
84$IO = Imager::io_new_fd(fileno(FH));
85i_writeraw_wiol($palim, $IO) or print "not ";
86print "ok 8\n";
87close FH;
88
89open FH, "testout/t103_pal.raw"
90 or die "Cannot open testout/t103_pal.raw: $!";
91binmode FH;
92my $data = do { local $/; <FH> };
93$data eq "\x0" x 200 . "\x1" x 200
94 or print "not ";
95print "ok 9\n";
96
97# 16-bit image
98# we don't have 16-bit reads yet
99my $img16 = Imager::i_img_16_new(150, 150, 3)
100 or print "not ";
101print "ok 10\n";
102i_box_filled($img16,70,25,130,125,$green);
103i_box_filled($img16,20,25,80,125,$blue);
104i_arc($img16,75,75,30,0,361,$red);
105i_conv($img16,[0.1, 0.2, 0.4, 0.2, 0.1]);
106
107open FH, "> testout/t103_16.raw"
108 or die "Cannot create testout/t103_16.raw: $!";
109binmode FH;
110$IO = Imager::io_new_fd(fileno(FH));
111i_writeraw_wiol($img16, $IO) or print "not ";
112print "ok 11\n";
113close FH;
114
115# try a simple virtual image
116my $maskimg = Imager::i_img_masked_new($img, undef, 0, 0, 150, 150)
117 or print "not ";
118print "ok 12\n";
119
120open FH, "> testout/t103_virt.raw"
121 or die "Cannot create testout/t103_virt.raw: $!";
122binmode FH;
123$IO = Imager::io_new_fd(fileno(FH));
124i_writeraw_wiol($maskimg, $IO) or print "not ";
125print "ok 13\n";
126close FH;
127
128open FH, "testout/t103_virt.raw"
129 or die "Cannot open testout/t103_virt.raw: $!";
130binmode FH;
131$IO = Imager::io_new_fd(fileno(FH));
132my $cmpimgmask = i_readraw_wiol($IO, 150, 150, 3, 3, 0)
133 or print "not ";
134print "ok 14\n";
135my $diff = i_img_diff($maskimg, $cmpimgmask);
136print "# difference for virtual image $diff\n";
137$diff and print "not ";
138print "ok 15\n";
139
50dc291e
TC
140# check that i_format is set correctly
141my $index = Imager::i_tags_find($cmpimgmask, 'i_format', 0);
142
143if ($index) {
144 my $value = Imager::i_tags_get($cmpimgmask, $index);
145 print $value eq 'raw' ? "ok 16\n" : "not ok 16 - bad value for i_format tag\n";
146}
147else {
148 print "not ok 16 - no i_format tag set\n";
149}
150
9267f8f3
TC
151sub read_test {
152 my ($in, $xsize, $ysize, $data, $store, $intrl, $base, $test) = @_;
153 open FH, $in or die "Cannot open $in: $!";
154 binmode FH;
895dbd34
AMH
155 my $IO = Imager::io_new_fd( fileno(FH) );
156
157 my $img = i_readraw_wiol($IO, $xsize, $ysize, $data, $store, $intrl);
9267f8f3
TC
158 if ($img) {
159 print "ok $test\n";
160 if (i_img_diff($img, $baseimg)) {
161 print "ok ",$test+1," # skip images don't match, but maybe I don't understand\n";
162 }
163 else {
164 print "ok ",$test+1,"\n";
165 }
166 }
167 else {
168 print "not ok $test # could not read image\n";
169 print "ok ",$test+1," # skip\n";
170 }
171}
172
173sub save_data {
174 my $outname = shift;
175 my $data = load_data();
176 open FH, "> $outname" or die "Cannot create $outname: $!";
177 binmode FH;
178 print FH $data;
179 close FH;
180}
181
182sub load_data {
183 my $hex = '';
184 while (<DATA>) {
185 next if /^#/;
186 last if /^EOF/;
187 chomp;
188 $hex .= $_;
189 }
190 $hex =~ tr/ //d;
191 my $result = pack("H*", $hex);
faa9b3e7 192 #print unpack("H*", $result),"\n";
9267f8f3
TC
193 return $result;
194}
195
9267f8f3
TC
196# FIXME: may need tests for 1,2,4 channel images
197
198__DATA__
199# we keep some packed raw images here
200# we decode this in the code, ignoring lines starting with #, a subfile
201# ends with EOF, data is HEX encoded (spaces ignored)
202
203# basic 3 channel version of the image
204001122 011223 021324 031425
205102132 112233 122334 132435
206203142 213243 223344 233445
207304152 314253 324354 334455
208EOF
209
210# test image for reading a 4 channel image into a 3 channel image
211# 4 x 4 pixels
21200112233 01122334 02132435 03142536
21310213243 11223344 12233445 13243546
21420314253 21324354 22334455 23344556
21530415263 31425364 32435465 33445566
216EOF
217
218# test image for line based interlacing
219# 4 x 4 pixels
220# first line
22100 01 02 03
22211 12 13 14
22322 23 24 25
224
225# second line
22610 11 12 13
22721 22 23 24
22832 33 34 35
229
230# third line
23120 21 22 23
23231 32 33 34
23342 43 44 45
234
235# fourth line
23630 31 32 33
23741 42 43 44
23852 53 54 55
239
240EOF
241
242# test image for image based interlacing
243# first channel
24400 01 02 03
24510 11 12 13
24620 21 22 23
24730 31 32 33
248
249# second channel
25011 12 13 14
25121 22 23 24
25231 32 33 34
25341 42 43 44
254
255# third channel
25622 23 24 25
25732 33 34 35
25842 43 44 45
25952 53 54 55
260
261EOF