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
|
#!perl -w
use strict;
use Test::More tests => 215;
use Imager qw(:all);
use Imager::Test qw(test_image_raw is_image is_color3 test_image);
-d "testout" or mkdir "testout";
Imager->open_log(log => "testout/t107bmp.log");
my @files;
my $debug_writes = 0;
my $base_diff = 0;
# if you change this make sure you generate new compressed versions
my $green=i_color_new(0,255,0,255);
my $blue=i_color_new(0,0,255,255);
my $red=i_color_new(255,0,0,255);
my $img = test_image_raw();
Imager::i_tags_add($img, 'i_xres', 0, '300', 0);
Imager::i_tags_add($img, 'i_yres', 0, undef, 300);
write_test($img, "testout/t107_24bit.bmp");
push @files, "t107_24bit.bmp";
# 'webmap' is noticably faster than the default
my $im8 = Imager::i_img_to_pal($img, { make_colors=>'webmap',
translate=>'errdiff'});
write_test($im8, "testout/t107_8bit.bmp");
push @files, "t107_8bit.bmp";
# use a fixed palette so we get reproducible results for the compressed
# version
my @pal16 = map { NC($_) }
qw(605844 966600 0148b2 00f800 bf0a33 5e009e
2ead1b 0000f8 004b01 fd0000 0e1695 000002);
my $im4 = Imager::i_img_to_pal($img, { colors=>\@pal16, make_colors=>'none' });
write_test($im4, "testout/t107_4bit.bmp");
push @files, "t107_4bit.bmp";
my $im1 = Imager::i_img_to_pal($img, { colors=>[ NC(0, 0, 0), NC(176, 160, 144) ],
make_colors=>'none', translate=>'errdiff' });
write_test($im1, "testout/t107_1bit.bmp");
push @files, "t107_1bit.bmp";
my $bi_rgb = 0;
my $bi_rle8 = 1;
my $bi_rle4 = 2;
my $bi_bitfields = 3;
read_test("testout/t107_24bit.bmp", $img,
bmp_compression=>0, bmp_bit_count => 24);
read_test("testout/t107_8bit.bmp", $im8,
bmp_compression=>0, bmp_bit_count => 8);
read_test("testout/t107_4bit.bmp", $im4,
bmp_compression=>0, bmp_bit_count => 4);
read_test("testout/t107_1bit.bmp", $im1, bmp_compression=>0,
bmp_bit_count=>1);
# the following might have slight differences
$base_diff = i_img_diff($img, $im8) * 2;
print "# base difference $base_diff\n";
read_test("testimg/comp4.bmp", $im4,
bmp_compression=>$bi_rle4, bmp_bit_count => 4);
read_test("testimg/comp8.bmp", $im8,
bmp_compression=>$bi_rle8, bmp_bit_count => 8);
my $imoo = Imager->new;
# read via OO
ok($imoo->read(file=>'testout/t107_24bit.bmp'), "read via OO")
or print "# ",$imoo->errstr,"\n";
ok($imoo->write(file=>'testout/t107_oo.bmp'), "write via OO")
or print "# ",$imoo->errstr,"\n";
push @files, "t107_oo.bmp";
# various invalid format tests
# we have so many different test images to try to detect all the possible
# failure paths in the code, adding these did detect real problems
print "# catch various types of invalid bmp files\n";
my @tests =
(
# entries in each array ref are:
# - basename of an invalid BMP file
# - error message that should be produced
# - description of what is being tested
# - possible flag to indicate testing only on 32-bit machines
[ 'badplanes.bmp', 'not a BMP file', "invalid planes value" ],
[ 'badbits.bmp', 'unknown bit count for BMP file (5)',
'should fail to read invalid bits' ],
# 1-bit/pixel BMPs
[ 'badused1.bmp', 'out of range colors used (3)',
'out of range palette size (1-bit)' ],
[ 'badcomp1.bmp', 'unknown 1-bit BMP compression (1)',
'invalid compression value (1-bit)' ],
[ 'bad1wid0.bmp', 'file size limit - image width of 0 is not positive',
'width 0 (1-bit)' ],
[ 'bad4oflow.bmp',
'file size limit - integer overflow calculating storage',
'overflow integers on 32-bit machines (1-bit)', '32bitonly' ],
[ 'short1.bmp', 'failed reading 1-bit bmp data',
'short 1-bit' ],
# 4-bit/pixel BMPs
[ 'badused4a.bmp', 'out of range colors used (272)',
'should fail to read invalid pal size (272) (4-bit)' ],
[ 'badused4b.bmp', 'out of range colors used (17)',
'should fail to read invalid pal size (17) (4-bit)' ],
[ 'badcomp4.bmp', 'unknown 4-bit BMP compression (1)',
'invalid compression value (4-bit)' ],
[ 'short4.bmp', 'failed reading 4-bit bmp data',
'short uncompressed 4-bit' ],
[ 'short4rle.bmp', 'missing data during decompression',
'short compressed 4-bit' ],
[ 'bad4wid0.bmp', 'file size limit - image width of 0 is not positive',
'width 0 (4-bit)' ],
[ 'bad4widbig.bmp', 'file size limit - image width of -2147483628 is not positive',
'width big (4-bit)' ],
[ 'bad4oflow.bmp', 'file size limit - integer overflow calculating storage',
'overflow integers on 32-bit machines (4-bit)', '32bitonly' ],
# 8-bit/pixel BMPs
[ 'bad8useda.bmp', 'out of range colors used (257)',
'should fail to read invalid pal size (8-bit)' ],
[ 'bad8comp.bmp', 'unknown 8-bit BMP compression (2)',
'invalid compression value (8-bit)' ],
[ 'short8.bmp', 'failed reading 8-bit bmp data',
'short uncompressed 8-bit' ],
[ 'short8rle.bmp', 'missing data during decompression',
'short compressed 8-bit' ],
[ 'bad8wid0.bmp', 'file size limit - image width of 0 is not positive',
'width 0 (8-bit)' ],
[ 'bad8oflow.bmp', 'file size limit - integer overflow calculating storage',
'overflow integers on 32-bit machines (8-bit)', '32bitonly' ],
# 24-bit/pixel BMPs
[ 'short24.bmp', 'failed reading image data',
'short 24-bit' ],
[ 'bad24wid0.bmp', 'file size limit - image width of 0 is not positive',
'width 0 (24-bit)' ],
[ 'bad24oflow.bmp', 'file size limit - integer overflow calculating storage',
'overflow integers on 32-bit machines (24-bit)', '32bitonly' ],
[ 'bad24comp.bmp', 'unknown 24-bit BMP compression (4)',
'bad compression (24-bit)' ],
);
use Config;
my $ptrsize = $Config{ptrsize};
for my $test (@tests) {
my ($file, $error, $comment, $bit32only) = @$test;
SKIP:
{
skip("only tested on 32-bit machines", 2)
if $bit32only && $ptrsize != 4;
ok(!$imoo->read(file=>"testimg/$file"), $comment);
print "# ", $imoo->errstr, "\n";
is($imoo->errstr, $error, "check error message");
}
}
# previously we didn't seek to the offbits position before reading
# the image data, check we handle it correctly
# in each case the first is an original image with a given number of
# bits and the second is the same file with data inserted before the
# image bits and the offset modified to suit
my @comp =
(
[ 'winrgb2.bmp', 'winrgb2off.bmp', 1 ],
[ 'winrgb4.bmp', 'winrgb4off.bmp', 4 ],
[ 'winrgb8.bmp', 'winrgb8off.bmp', 8 ],
[ 'winrgb24.bmp', 'winrgb24off.bmp', 24 ],
);
for my $comp (@comp) {
my ($base_file, $off_file, $bits) = @$comp;
my $base_im = Imager->new;
my $got_base =
ok($base_im->read(file=>"testimg/$base_file"),
"read original")
or print "# ",$base_im->errstr,"\n";
my $off_im = Imager->new;
my $got_off =
ok($off_im->read(file=>"testimg/$off_file"),
"read offset file")
or print "# ",$off_im->errstr,"\n";
SKIP:
{
skip("missed one file", 1)
unless $got_base && $got_off;
is(i_img_diff($base_im->{IMG}, $off_im->{IMG}), 0,
"compare base and offset image ($bits bits)");
}
}
{ # check file limits are checked
my $limit_file = "testout/t107_24bit.bmp";
ok(Imager->set_file_limits(reset=>1, width=>149), "set width limit 149");
my $im = Imager->new;
ok(!$im->read(file=>$limit_file),
"should fail read due to size limits");
print "# ",$im->errstr,"\n";
like($im->errstr, qr/image width/, "check message");
ok(Imager->set_file_limits(reset=>1, height=>149), "set height limit 149");
ok(!$im->read(file=>$limit_file),
"should fail read due to size limits");
print "# ",$im->errstr,"\n";
like($im->errstr, qr/image height/, "check message");
ok(Imager->set_file_limits(reset=>1, width=>150), "set width limit 150");
ok($im->read(file=>$limit_file),
"should succeed - just inside width limit");
ok(Imager->set_file_limits(reset=>1, height=>150), "set height limit 150");
ok($im->read(file=>$limit_file),
"should succeed - just inside height limit");
# 150 x 150 x 3 channel image uses 67500 bytes
ok(Imager->set_file_limits(reset=>1, bytes=>67499),
"set bytes limit 67499");
ok(!$im->read(file=>$limit_file),
"should fail - too many bytes");
print "# ",$im->errstr,"\n";
like($im->errstr, qr/storage size/, "check error message");
ok(Imager->set_file_limits(reset=>1, bytes=>67500),
"set bytes limit 67500");
ok($im->read(file=>$limit_file),
"should succeed - just inside bytes limit");
Imager->set_file_limits(reset=>1);
}
{ # various short read failure tests, each entry has:
# source filename, size, expected error
# these have been selected based on code coverage, to check each
# failure path is checked, where practical
my @tests =
(
[
"file truncated inside header",
"winrgb2.bmp",
20, "file too short to be a BMP file"
],
[
"1-bit, truncated inside palette",
"winrgb2.bmp",
56, "reading BMP palette"
],
[
"1-bit, truncated in offset region",
"winrgb2off.bmp", 64, "failed skipping to image data offset"
],
[
"1-bit, truncated in image data",
"winrgb2.bmp", 96, "failed reading 1-bit bmp data"
],
[
"4-bit, truncated inside palette",
"winrgb4.bmp",
56, "reading BMP palette"
],
[
"4-bit, truncated in offset region",
"winrgb4off.bmp", 120, "failed skipping to image data offset",
],
[
"4-bit, truncate in image data",
"winrgb4.bmp", 120, "failed reading 4-bit bmp data"
],
[
"4-bit RLE, truncate in uncompressed data",
"comp4.bmp", 0x229, "missing data during decompression"
],
[
"8-bit, truncated in palette",
"winrgb8.bmp", 1060, "reading BMP palette"
],
[
"8-bit, truncated in offset region",
"winrgb8off.bmp", 1080, "failed skipping to image data offset"
],
[
"8-bit, truncated in image data",
"winrgb8.bmp", 1080, "failed reading 8-bit bmp data"
],
[
"8-bit RLE, truncate in uncompressed data",
"comp8.bmp", 0x68C, "missing data during decompression"
],
[
"24-bit, truncate in offset region",
"winrgb24off.bmp", 56, "failed skipping to image data offset",
],
[
"24-bit, truncate in image data",
"winrgb24.bmp", 100, "failed reading image data",
],
);
my $test_index = 0;
for my $test (@tests) {
my ($desc, $srcfile, $size, $error) = @$test;
my $im = Imager->new;
open IMDATA, "< testimg/$srcfile"
or die "$test_index - $desc: Cannot open testimg/$srcfile: $!";
binmode IMDATA;
my $data;
read(IMDATA, $data, $size) == $size
or die "$test_index - $desc: Could not read $size data from $srcfile";
close IMDATA;
ok(!$im->read(data => $data, type =>'bmp'),
"$test_index - $desc: Should fail to read");
is($im->errstr, $error, "$test_index - $desc: check message");
++$test_index;
}
}
{ # various short read success tests, each entry has:
# source filename, size, expected tags
print "# allow_incomplete tests\n";
my @tests =
(
[
"1-bit",
"winrgb2.bmp", 96,
{
bmp_compression_name => 'BI_RGB',
bmp_compression => 0,
bmp_used_colors => 2,
i_lines_read => 8,
},
],
[
"4-bit",
"winrgb4.bmp", 250,
{
bmp_compression_name => 'BI_RGB',
bmp_compression => 0,
bmp_used_colors => 16,
i_lines_read => 11,
},
],
[
"4-bit RLE - uncompressed seq",
"comp4.bmp", 0x229,
{
bmp_compression_name => 'BI_RLE4',
bmp_compression => 2,
bmp_used_colors => 16,
i_lines_read => 44,
},
],
[
"4-bit RLE - start seq",
"comp4.bmp", 0x97,
{
bmp_compression_name => 'BI_RLE4',
bmp_compression => 2,
bmp_used_colors => 16,
i_lines_read => 8,
},
],
[
"8-bit",
"winrgb8.bmp", 1250,
{
bmp_compression_name => 'BI_RGB',
bmp_compression => 0,
bmp_used_colors => 256,
i_lines_read => 8,
},
],
[
"8-bit RLE - uncompressed seq",
"comp8.bmp", 0x68C,
{
bmp_compression_name => 'BI_RLE8',
bmp_compression => 1,
bmp_used_colors => 256,
i_lines_read => 27,
},
],
[
"8-bit RLE - initial seq",
"comp8.bmp", 0x487,
{
bmp_compression_name => 'BI_RLE8',
bmp_compression => 1,
bmp_used_colors => 256,
i_lines_read => 20,
},
],
[
"24-bit",
"winrgb24.bmp", 800,
{
bmp_compression_name => 'BI_RGB',
bmp_compression => 0,
bmp_used_colors => 0,
i_lines_read => 12,
},
],
);
my $test_index = 0;
for my $test (@tests) {
my ($desc, $srcfile, $size, $tags) = @$test;
my $im = Imager->new;
open IMDATA, "< testimg/$srcfile"
or die "$test_index - $desc: Cannot open testimg/$srcfile: $!";
binmode IMDATA;
my $data;
read(IMDATA, $data, $size) == $size
or die "$test_index - $desc: Could not read $size data from $srcfile";
close IMDATA;
ok($im->read(data => $data, type =>'bmp', allow_incomplete => 1),
"$test_index - $desc: Should read successfully");
# check standard tags are set
is($im->tags(name => 'i_format'), 'bmp',
"$test_index - $desc: i_format set");
is($im->tags(name => 'i_incomplete'), 1,
"$test_index - $desc: i_incomplete set");
my %check_tags;
for my $key (keys %$tags) {
$check_tags{$key} = $im->tags(name => $key);
}
is_deeply(\%check_tags, $tags, "$test_index - $desc: check tags");
++$test_index;
}
}
{ # check handling of reading images with negative height
# each entry is:
# source file, description
print "# check handling of negative height values\n";
my @tests =
(
[ "winrgb2.bmp", "1-bit, uncompressed" ],
[ "winrgb4.bmp", "4-bit, uncompressed" ],
[ "winrgb8.bmp", "8-bit, uncompressed" ],
[ "winrgb24.bmp", "24-bit, uncompressed" ],
[ "comp4.bmp", "4-bit, RLE" ],
[ "comp8.bmp", "8-bit, RLE" ],
);
my $test_index = 0;
for my $test (@tests) {
my ($file, $desc) = @$test;
open IMDATA, "< testimg/$file"
or die "$test_index - Cannot open $file: $!";
binmode IMDATA;
my $data = do { local $/; <IMDATA> };
close IMDATA;
my $im_orig = Imager->new;
$im_orig->read(data => $data)
or die "Cannot load original $file: ", $im_orig->errstr;
# now negate the height
my $orig_height = unpack("V", substr($data, 0x16, 4));
my $neg_height = 0xFFFFFFFF & ~($orig_height - 1);
substr($data, 0x16, 4) = pack("V", $neg_height);
# and read the modified image
my $im = Imager->new;
ok($im->read(data => $data),
"$test_index - $desc: read negated height image")
or print "# ", $im->errstr, "\n";
# flip orig to match what we should get
$im_orig->flip(dir => 'v');
# check it out
is_image($im, $im_orig, "$test_index - $desc: check image");
++$test_index;
}
}
{ print "# patched data read failure tests\n";
# like the "various invalid format" tests, these generate fail
# images from other images included with Imager without providing a
# full bmp source, saving on dist size and focusing on the changes needed
# to cause the failure
# each entry is: source file, patches, expected error, description
my @tests =
(
# low image data offsets
[
"winrgb2.bmp",
{ 10 => "3d 00 00 00" },
"image data offset too small (61)",
"1-bit, small image offset"
],
[
"winrgb4.bmp",
{ 10 => "75 00 00 00" },
"image data offset too small (117)",
"4-bit, small image offset"
],
[
"winrgb8.bmp",
{ 10 => "35 04 00 00" },
"image data offset too small (1077)",
"8-bit, small image offset"
],
[
"winrgb24.bmp",
{ 10 => "35 00 00 00" },
"image data offset too small (53)",
"24-bit, small image offset"
],
# compression issues
[
"comp8.bmp",
{ 0x436 => "97" },
"invalid data during decompression",
"8bit, RLE run beyond edge of image"
],
[
# caused glibc malloc or valgrind to complain
"comp8.bmp",
{ 0x436 => "94 00 00 03" },
"invalid data during decompression",
"8bit, literal run beyond edge of image"
],
[
"comp4.bmp",
{ 0x76 => "FF bb FF BB" },
"invalid data during decompression",
"4bit - RLE run beyond edge of image"
],
[
"comp4.bmp",
{ 0x76 => "94 bb 00 FF" },
"invalid data during decompression",
"4bit - literal run beyond edge of image"
],
);
my $test_index = 0;
for my $test (@tests) {
my ($filename, $patches, $error, $desc) = @$test;
my $data = load_patched_file("testimg/$filename", $patches);
my $im = Imager->new;
ok(!$im->read(data => $data, type=>'bmp'),
"$test_index - $desc:should fail to read");
is($im->errstr, $error, "$test_index - $desc:check message");
++$test_index;
}
}
{ # various write failure tests
# each entry is:
# source, limit, expected error, description
my @tests =
(
[
"winrgb2.bmp", 1,
"cannot write bmp header: limit reached",
"1-bit, writing header"
],
[
"winrgb4.bmp", 1,
"cannot write bmp header: limit reached",
"4-bit, writing header"
],
[
"winrgb8.bmp", 1,
"cannot write bmp header: limit reached",
"8-bit, writing header"
],
[
"winrgb24.bmp", 1,
"cannot write bmp header: limit reached",
"24-bit, writing header"
],
[
"winrgb2.bmp", 0x38,
"cannot write palette entry: limit reached",
"1-bit, writing palette"
],
[
"winrgb4.bmp", 0x38,
"cannot write palette entry: limit reached",
"4-bit, writing palette"
],
[
"winrgb8.bmp", 0x38,
"cannot write palette entry: limit reached",
"8-bit, writing palette"
],
[
"winrgb2.bmp", 0x40,
"writing 1 bit/pixel packed data: limit reached",
"1-bit, writing image data"
],
[
"winrgb4.bmp", 0x80,
"writing 4 bit/pixel packed data: limit reached",
"4-bit, writing image data"
],
[
"winrgb8.bmp", 0x440,
"writing 8 bit/pixel packed data: limit reached",
"8-bit, writing image data"
],
[
"winrgb24.bmp", 0x39,
"writing image data: limit reached",
"24-bit, writing image data"
],
);
print "# write failure tests\n";
my $test_index = 0;
for my $test (@tests) {
my ($file, $limit, $error, $desc) = @$test;
my $im = Imager->new;
$im->read(file => "testimg/$file")
or die "Cannot read $file: ", $im->errstr;
my $io = Imager::io_new_cb(limited_write($limit), undef, undef, undef, 1);
$io->set_buffered(0);
print "# writing with limit of $limit\n";
ok(!$im->write(type => 'bmp', io => $io),
"$test_index - $desc: write should fail");
is($im->errstr, $error, "$test_index - $desc: check error message");
++$test_index;
}
}
{
ok(grep($_ eq 'bmp', Imager->read_types), "check bmp in read types");
ok(grep($_ eq 'bmp', Imager->write_types), "check bmp in write types");
}
{
# RT #30075
# give 4/2 channel images a background color when saving to BMP
my $im = Imager->new(xsize=>16, ysize=>16, channels=>4);
$im->box(filled => 1, xmin => 8, color => '#FFE0C0');
$im->box(filled => 1, color => NC(0, 192, 192, 128),
ymin => 8, xmax => 7);
ok($im->write(file=>"testout/t107_alpha.bmp", type=>'bmp'),
"should succeed writing 4 channel image");
push @files, "t107_alpha.bmp";
my $imread = Imager->new;
ok($imread->read(file => 'testout/t107_alpha.bmp'), "read it back");
is_color3($imread->getpixel('x' => 0, 'y' => 0), 0, 0, 0,
"check transparent became black");
is_color3($imread->getpixel('x' => 8, 'y' => 0), 255, 224, 192,
"check color came through");
is_color3($imread->getpixel('x' => 0, 'y' => 15), 0, 96, 96,
"check translucent came through");
my $data;
ok($im->write(data => \$data, type => 'bmp', i_background => '#FF0000'),
"write with red background");
ok($imread->read(data => $data, type => 'bmp'),
"read it back");
is_color3($imread->getpixel('x' => 0, 'y' => 0), 255, 0, 0,
"check transparent became red");
is_color3($imread->getpixel('x' => 8, 'y' => 0), 255, 224, 192,
"check color came through");
is_color3($imread->getpixel('x' => 0, 'y' => 15), 127, 96, 96,
"check translucent came through");
}
{ # RT 41406
my $data;
my $im = test_image();
ok($im->write(data => \$data, type => 'bmp'), "write using OO");
my $size = unpack("V", substr($data, 34, 4));
is($size, 67800, "check data size");
}
{ # check close failures are handled correctly
my $im = test_image();
my $fail_close = sub {
Imager::i_push_error(0, "synthetic close failure");
return 0;
};
ok(!$im->write(type => "bmp", callback => sub { 1 },
closecb => $fail_close),
"check failing close fails");
like($im->errstr, qr/synthetic close failure/,
"check error message");
}
Imager->close_log;
unless ($ENV{IMAGER_KEEP_FILES}) {
unlink map "testout/$_", @files;
unlink "testout/t107bmp.log";
}
sub write_test {
my ($im, $filename) = @_;
local *FH;
if (open FH, "> $filename") {
binmode FH;
my $IO = Imager::io_new_fd(fileno(FH));
unless (ok(Imager::i_writebmp_wiol($im, $IO), $filename)) {
print "# ",Imager->_error_as_msg(),"\n";
}
undef $IO;
close FH;
}
else {
fail("could not open $filename: $!");
}
}
sub read_test {
my ($filename, $im, %tags) = @_;
local *FH;
print "# read_test: $filename\n";
$tags{i_format} = "bmp";
if (open FH, "< $filename") {
binmode FH;
my $IO = Imager::io_new_fd(fileno(FH));
my $im_read = Imager::i_readbmp_wiol($IO);
if ($im_read) {
my $diff = i_img_diff($im, $im_read);
if ($diff > $base_diff) {
fail("image mismatch reading $filename");
}
else {
my $tags_ok = 1;
for my $tag (keys %tags) {
if (my $index = Imager::i_tags_find($im_read, $tag, 0)) {
my ($name, $value) = Imager::i_tags_get($im_read, $index);
my $exp_value = $tags{$tag};
print "# tag $name = '$value' - expect '$exp_value'\n";
if ($exp_value =~ /\d/) {
if ($value != $tags{$tag}) {
print "# tag $tag value mismatch $tags{$tag} != $value\n";
$tags_ok = 0;
}
}
else {
if ($value ne $tags{$tag}) {
print "# tag $tag value mismatch $tags{$tag} != $value\n";
$tags_ok = 0;
}
}
}
}
ok($tags_ok, "reading $filename");
# for my $i (0 .. Imager::i_tags_count($im_read)-1) {
# my ($name, $value) = Imager::i_tags_get($im_read, $i);
# print "# tag '$name' => '$value'\n";
#}
}
}
else {
fail("could not read $filename: ".Imager->_error_as_msg());
}
undef $IO;
close FH;
}
else {
fail("could not open $filename: $!");
}
}
sub limited_write {
my ($limit) = @_;
return
sub {
my ($data) = @_;
$limit -= length $data;
if ($limit >= 0) {
print "# write of ", length $data, " bytes successful ($limit left)\n" if $debug_writes;
return 1;
}
else {
print "# write of ", length $data, " bytes failed\n";
Imager::i_push_error(0, "limit reached");
return;
}
};
}
sub load_patched_file {
my ($filename, $patches) = @_;
open IMDATA, "< $filename"
or die "Cannot open $filename: $!";
binmode IMDATA;
my $data = do { local $/; <IMDATA> };
for my $offset (keys %$patches) {
(my $hdata = $patches->{$offset}) =~ tr/ //d;
my $pdata = pack("H*", $hdata);
substr($data, $offset, length $pdata) = $pdata;
}
return $data;
}
|