| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <cmath> | 9 #include <cmath> |
| 10 | 10 |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 } | 269 } |
| 270 | 270 |
| 271 void MakeTestA8SkBitmap(int w, int h, SkBitmap* bmp) { | 271 void MakeTestA8SkBitmap(int w, int h, SkBitmap* bmp) { |
| 272 bmp->allocPixels(SkImageInfo::MakeA8(w, h)); | 272 bmp->allocPixels(SkImageInfo::MakeA8(w, h)); |
| 273 | 273 |
| 274 uint8_t* src_data = bmp->getAddr8(0, 0); | 274 uint8_t* src_data = bmp->getAddr8(0, 0); |
| 275 for (int i = 0; i < w * h; i++) | 275 for (int i = 0; i < w * h; i++) |
| 276 src_data[i] = i % 255; | 276 src_data[i] = i % 255; |
| 277 } | 277 } |
| 278 | 278 |
| 279 TEST(PNGCodec, EncodeDecodeRGB) { | |
| 280 const int w = 20, h = 20; | |
| 281 | |
| 282 // create an image with known values | |
| 283 std::vector<unsigned char> original; | |
| 284 MakeRGBImage(w, h, &original); | |
| 285 | |
| 286 // encode | |
| 287 std::vector<unsigned char> encoded; | |
| 288 ASSERT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_RGB, | |
| 289 Size(w, h), w * 3, false, | |
| 290 std::vector<PNGCodec::Comment>(), | |
| 291 &encoded)); | |
| 292 | |
| 293 // decode, it should have the same size as the original | |
| 294 std::vector<unsigned char> decoded; | |
| 295 int outw, outh; | |
| 296 ASSERT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), | |
| 297 PNGCodec::FORMAT_RGB, &decoded, | |
| 298 &outw, &outh)); | |
| 299 ASSERT_EQ(w, outw); | |
| 300 ASSERT_EQ(h, outh); | |
| 301 ASSERT_EQ(original.size(), decoded.size()); | |
| 302 | |
| 303 // Images must be equal | |
| 304 ASSERT_TRUE(original == decoded); | |
| 305 } | |
| 306 | |
| 307 TEST(PNGCodec, EncodeDecodeRGBA) { | 279 TEST(PNGCodec, EncodeDecodeRGBA) { |
| 308 const int w = 20, h = 20; | 280 const int w = 20, h = 20; |
| 309 | 281 |
| 310 // create an image with known values, a must be opaque because it will be | 282 // create an image with known values, a must be opaque because it will be |
| 311 // lost during encoding | 283 // lost during encoding |
| 312 std::vector<unsigned char> original; | 284 std::vector<unsigned char> original; |
| 313 MakeRGBAImage(w, h, true, &original); | 285 MakeRGBAImage(w, h, true, &original); |
| 314 | 286 |
| 315 // encode | 287 // encode |
| 316 std::vector<unsigned char> encoded; | 288 std::vector<unsigned char> encoded; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 unsigned char* rgba_pixel = &decoded[(y * w + x) * 4]; | 372 unsigned char* rgba_pixel = &decoded[(y * w + x) * 4]; |
| 401 | 373 |
| 402 EXPECT_EQ(palette_color.red, rgba_pixel[0]); | 374 EXPECT_EQ(palette_color.red, rgba_pixel[0]); |
| 403 EXPECT_EQ(palette_color.green, rgba_pixel[1]); | 375 EXPECT_EQ(palette_color.green, rgba_pixel[1]); |
| 404 EXPECT_EQ(palette_color.blue, rgba_pixel[2]); | 376 EXPECT_EQ(palette_color.blue, rgba_pixel[2]); |
| 405 EXPECT_EQ(alpha, rgba_pixel[3]); | 377 EXPECT_EQ(alpha, rgba_pixel[3]); |
| 406 } | 378 } |
| 407 } | 379 } |
| 408 } | 380 } |
| 409 | 381 |
| 410 TEST(PNGCodec, DecodePaletteDiscardAlpha) { | |
| 411 const int w = 20, h = 20; | |
| 412 | |
| 413 // create an image with known values | |
| 414 std::vector<unsigned char> original; | |
| 415 std::vector<png_color> original_palette; | |
| 416 std::vector<unsigned char> original_trans_chunk; | |
| 417 MakePaletteImage(w, h, &original, &original_palette, &original_trans_chunk); | |
| 418 | |
| 419 // encode | |
| 420 std::vector<unsigned char> encoded; | |
| 421 ASSERT_TRUE(EncodeImage(original, | |
| 422 w, h, | |
| 423 COLOR_TYPE_PALETTE, | |
| 424 &encoded, | |
| 425 PNG_INTERLACE_NONE, | |
| 426 &original_palette, | |
| 427 &original_trans_chunk)); | |
| 428 | |
| 429 // decode | |
| 430 std::vector<unsigned char> decoded; | |
| 431 int outw, outh; | |
| 432 ASSERT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), | |
| 433 PNGCodec::FORMAT_RGB, &decoded, | |
| 434 &outw, &outh)); | |
| 435 ASSERT_EQ(w, outw); | |
| 436 ASSERT_EQ(h, outh); | |
| 437 ASSERT_EQ(decoded.size(), w * h * 3U); | |
| 438 | |
| 439 // Images must be equal | |
| 440 for (int y = 0; y < h; ++y) { | |
| 441 for (int x = 0; x < w; ++x) { | |
| 442 unsigned char palette_pixel = original[y * w + x]; | |
| 443 png_color& palette_color = original_palette[palette_pixel]; | |
| 444 unsigned char* rgba_pixel = &decoded[(y * w + x) * 3]; | |
| 445 | |
| 446 EXPECT_EQ(palette_color.red, rgba_pixel[0]); | |
| 447 EXPECT_EQ(palette_color.green, rgba_pixel[1]); | |
| 448 EXPECT_EQ(palette_color.blue, rgba_pixel[2]); | |
| 449 } | |
| 450 } | |
| 451 } | |
| 452 | |
| 453 TEST(PNGCodec, DecodeInterlacedPalette) { | 382 TEST(PNGCodec, DecodeInterlacedPalette) { |
| 454 const int w = 20, h = 20; | 383 const int w = 20, h = 20; |
| 455 | 384 |
| 456 // create an image with known values | 385 // create an image with known values |
| 457 std::vector<unsigned char> original; | 386 std::vector<unsigned char> original; |
| 458 std::vector<png_color> original_palette; | 387 std::vector<png_color> original_palette; |
| 459 std::vector<unsigned char> original_trans_chunk; | 388 std::vector<unsigned char> original_trans_chunk; |
| 460 MakePaletteImage(w, h, &original, &original_palette, &original_trans_chunk); | 389 MakePaletteImage(w, h, &original, &original_palette, &original_trans_chunk); |
| 461 | 390 |
| 462 // encode | 391 // encode |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 MakeGrayscaleImage(w, h, &original); | 432 MakeGrayscaleImage(w, h, &original); |
| 504 | 433 |
| 505 // encode | 434 // encode |
| 506 std::vector<unsigned char> encoded; | 435 std::vector<unsigned char> encoded; |
| 507 ASSERT_TRUE(EncodeImage(original, w, h, COLOR_TYPE_GRAY, &encoded)); | 436 ASSERT_TRUE(EncodeImage(original, w, h, COLOR_TYPE_GRAY, &encoded)); |
| 508 | 437 |
| 509 // decode | 438 // decode |
| 510 std::vector<unsigned char> decoded; | 439 std::vector<unsigned char> decoded; |
| 511 int outw, outh; | 440 int outw, outh; |
| 512 ASSERT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), | 441 ASSERT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), |
| 513 PNGCodec::FORMAT_RGB, &decoded, | 442 PNGCodec::FORMAT_RGBA, &decoded, &outw, &outh)); |
| 514 &outw, &outh)); | |
| 515 ASSERT_EQ(w, outw); | 443 ASSERT_EQ(w, outw); |
| 516 ASSERT_EQ(h, outh); | 444 ASSERT_EQ(h, outh); |
| 517 ASSERT_EQ(decoded.size(), original.size() * 3); | 445 ASSERT_EQ(decoded.size(), original.size() * 4); |
| 518 | 446 |
| 519 // Images must be equal | 447 // Images must be equal |
| 520 for (int y = 0; y < h; ++y) { | 448 for (int y = 0; y < h; ++y) { |
| 521 for (int x = 0; x < w; ++x) { | 449 for (int x = 0; x < w; ++x) { |
| 522 unsigned char gray_pixel = original[(y * w + x)]; | 450 unsigned char gray_pixel = original[(y * w + x)]; |
| 523 unsigned char* rgba_pixel = &decoded[(y * w + x) * 3]; | 451 unsigned char* rgba_pixel = &decoded[(y * w + x) * 4]; |
| 524 EXPECT_EQ(rgba_pixel[0], gray_pixel); | 452 EXPECT_EQ(rgba_pixel[0], gray_pixel); |
| 525 EXPECT_EQ(rgba_pixel[1], gray_pixel); | 453 EXPECT_EQ(rgba_pixel[1], gray_pixel); |
| 526 EXPECT_EQ(rgba_pixel[2], gray_pixel); | 454 EXPECT_EQ(rgba_pixel[2], gray_pixel); |
| 455 EXPECT_EQ(rgba_pixel[3], 0xff); |
| 527 } | 456 } |
| 528 } | 457 } |
| 529 } | 458 } |
| 530 | 459 |
| 531 TEST(PNGCodec, DecodeGrayscaleWithAlpha) { | 460 TEST(PNGCodec, DecodeGrayscaleWithAlpha) { |
| 532 const int w = 20, h = 20; | 461 const int w = 20, h = 20; |
| 533 | 462 |
| 534 // create an image with known values | 463 // create an image with known values |
| 535 std::vector<unsigned char> original; | 464 std::vector<unsigned char> original; |
| 536 MakeGrayscaleAlphaImage(w, h, &original); | 465 MakeGrayscaleAlphaImage(w, h, &original); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 558 unsigned char* gray_pixel = &original[(y * w + x) * 2]; | 487 unsigned char* gray_pixel = &original[(y * w + x) * 2]; |
| 559 unsigned char* rgba_pixel = &decoded[(y * w + x) * 4]; | 488 unsigned char* rgba_pixel = &decoded[(y * w + x) * 4]; |
| 560 EXPECT_EQ(rgba_pixel[0], gray_pixel[0]); | 489 EXPECT_EQ(rgba_pixel[0], gray_pixel[0]); |
| 561 EXPECT_EQ(rgba_pixel[1], gray_pixel[0]); | 490 EXPECT_EQ(rgba_pixel[1], gray_pixel[0]); |
| 562 EXPECT_EQ(rgba_pixel[2], gray_pixel[0]); | 491 EXPECT_EQ(rgba_pixel[2], gray_pixel[0]); |
| 563 EXPECT_EQ(rgba_pixel[3], gray_pixel[1]); | 492 EXPECT_EQ(rgba_pixel[3], gray_pixel[1]); |
| 564 } | 493 } |
| 565 } | 494 } |
| 566 } | 495 } |
| 567 | 496 |
| 568 TEST(PNGCodec, DecodeGrayscaleWithAlphaDiscardAlpha) { | |
| 569 const int w = 20, h = 20; | |
| 570 | |
| 571 // create an image with known values | |
| 572 std::vector<unsigned char> original; | |
| 573 MakeGrayscaleAlphaImage(w, h, &original); | |
| 574 | |
| 575 // encode | |
| 576 std::vector<unsigned char> encoded; | |
| 577 ASSERT_TRUE(EncodeImage(original, | |
| 578 w, h, | |
| 579 COLOR_TYPE_GRAY_ALPHA, | |
| 580 &encoded)); | |
| 581 | |
| 582 // decode | |
| 583 std::vector<unsigned char> decoded; | |
| 584 int outw, outh; | |
| 585 ASSERT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), | |
| 586 PNGCodec::FORMAT_RGB, &decoded, | |
| 587 &outw, &outh)); | |
| 588 ASSERT_EQ(w, outw); | |
| 589 ASSERT_EQ(h, outh); | |
| 590 ASSERT_EQ(decoded.size(), w * h * 3U); | |
| 591 | |
| 592 // Images must be equal | |
| 593 for (int y = 0; y < h; ++y) { | |
| 594 for (int x = 0; x < w; ++x) { | |
| 595 unsigned char* gray_pixel = &original[(y * w + x) * 2]; | |
| 596 unsigned char* rgba_pixel = &decoded[(y * w + x) * 3]; | |
| 597 EXPECT_EQ(rgba_pixel[0], gray_pixel[0]); | |
| 598 EXPECT_EQ(rgba_pixel[1], gray_pixel[0]); | |
| 599 EXPECT_EQ(rgba_pixel[2], gray_pixel[0]); | |
| 600 } | |
| 601 } | |
| 602 } | |
| 603 | |
| 604 TEST(PNGCodec, DecodeInterlacedGrayscale) { | 497 TEST(PNGCodec, DecodeInterlacedGrayscale) { |
| 605 const int w = 20, h = 20; | 498 const int w = 20, h = 20; |
| 606 | 499 |
| 607 // create an image with known values | 500 // create an image with known values |
| 608 std::vector<unsigned char> original; | 501 std::vector<unsigned char> original; |
| 609 MakeGrayscaleImage(w, h, &original); | 502 MakeGrayscaleImage(w, h, &original); |
| 610 | 503 |
| 611 // encode | 504 // encode |
| 612 std::vector<unsigned char> encoded; | 505 std::vector<unsigned char> encoded; |
| 613 ASSERT_TRUE(EncodeImage(original, | 506 ASSERT_TRUE(EncodeImage(original, |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 670 unsigned char* gray_pixel = &original[(y * w + x) * 2]; | 563 unsigned char* gray_pixel = &original[(y * w + x) * 2]; |
| 671 unsigned char* rgba_pixel = &decoded[(y * w + x) * 4]; | 564 unsigned char* rgba_pixel = &decoded[(y * w + x) * 4]; |
| 672 EXPECT_EQ(rgba_pixel[0], gray_pixel[0]); | 565 EXPECT_EQ(rgba_pixel[0], gray_pixel[0]); |
| 673 EXPECT_EQ(rgba_pixel[1], gray_pixel[0]); | 566 EXPECT_EQ(rgba_pixel[1], gray_pixel[0]); |
| 674 EXPECT_EQ(rgba_pixel[2], gray_pixel[0]); | 567 EXPECT_EQ(rgba_pixel[2], gray_pixel[0]); |
| 675 EXPECT_EQ(rgba_pixel[3], gray_pixel[1]); | 568 EXPECT_EQ(rgba_pixel[3], gray_pixel[1]); |
| 676 } | 569 } |
| 677 } | 570 } |
| 678 } | 571 } |
| 679 | 572 |
| 680 TEST(PNGCodec, DecodeInterlacedRGB) { | |
| 681 const int w = 20, h = 20; | |
| 682 | |
| 683 // create an image with known values | |
| 684 std::vector<unsigned char> original; | |
| 685 MakeRGBImage(w, h, &original); | |
| 686 | |
| 687 // encode | |
| 688 std::vector<unsigned char> encoded; | |
| 689 ASSERT_TRUE(EncodeImage(original, | |
| 690 w, h, | |
| 691 COLOR_TYPE_RGB, | |
| 692 &encoded, | |
| 693 PNG_INTERLACE_ADAM7)); | |
| 694 | |
| 695 // decode, it should have the same size as the original | |
| 696 std::vector<unsigned char> decoded; | |
| 697 int outw, outh; | |
| 698 ASSERT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), | |
| 699 PNGCodec::FORMAT_RGB, &decoded, | |
| 700 &outw, &outh)); | |
| 701 ASSERT_EQ(w, outw); | |
| 702 ASSERT_EQ(h, outh); | |
| 703 ASSERT_EQ(original.size(), decoded.size()); | |
| 704 | |
| 705 // Images must be equal | |
| 706 ASSERT_EQ(original, decoded); | |
| 707 } | |
| 708 | |
| 709 TEST(PNGCodec, DecodeInterlacedRGBA) { | 573 TEST(PNGCodec, DecodeInterlacedRGBA) { |
| 710 const int w = 20, h = 20; | 574 const int w = 20, h = 20; |
| 711 | 575 |
| 712 // create an image with known values | 576 // create an image with known values |
| 713 std::vector<unsigned char> original; | 577 std::vector<unsigned char> original; |
| 714 MakeRGBAImage(w, h, false, &original); | 578 MakeRGBAImage(w, h, false, &original); |
| 715 | 579 |
| 716 // encode | 580 // encode |
| 717 std::vector<unsigned char> encoded; | 581 std::vector<unsigned char> encoded; |
| 718 ASSERT_TRUE(EncodeImage(original, | 582 ASSERT_TRUE(EncodeImage(original, |
| 719 w, h, | 583 w, h, |
| 720 COLOR_TYPE_RGBA, | 584 COLOR_TYPE_RGBA, |
| 721 &encoded, | 585 &encoded, |
| 722 PNG_INTERLACE_ADAM7)); | 586 PNG_INTERLACE_ADAM7)); |
| 723 | 587 |
| 724 // decode, it should have the same size as the original | 588 // decode, it should have the same size as the original |
| 725 std::vector<unsigned char> decoded; | 589 std::vector<unsigned char> decoded; |
| 726 int outw, outh; | 590 int outw, outh; |
| 727 ASSERT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), | 591 ASSERT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), |
| 728 PNGCodec::FORMAT_RGBA, &decoded, | 592 PNGCodec::FORMAT_RGBA, &decoded, |
| 729 &outw, &outh)); | 593 &outw, &outh)); |
| 730 ASSERT_EQ(w, outw); | 594 ASSERT_EQ(w, outw); |
| 731 ASSERT_EQ(h, outh); | 595 ASSERT_EQ(h, outh); |
| 732 ASSERT_EQ(original.size(), decoded.size()); | 596 ASSERT_EQ(original.size(), decoded.size()); |
| 733 | 597 |
| 734 // Images must be equal | 598 // Images must be equal |
| 735 ASSERT_EQ(original, decoded); | 599 ASSERT_EQ(original, decoded); |
| 736 } | 600 } |
| 737 | 601 |
| 738 TEST(PNGCodec, DecodeInterlacedRGBADiscardAlpha) { | |
| 739 const int w = 20, h = 20; | |
| 740 | |
| 741 // create an image with known values | |
| 742 std::vector<unsigned char> original; | |
| 743 MakeRGBAImage(w, h, false, &original); | |
| 744 | |
| 745 // encode | |
| 746 std::vector<unsigned char> encoded; | |
| 747 ASSERT_TRUE(EncodeImage(original, | |
| 748 w, h, | |
| 749 COLOR_TYPE_RGBA, | |
| 750 &encoded, | |
| 751 PNG_INTERLACE_ADAM7)); | |
| 752 | |
| 753 // decode | |
| 754 std::vector<unsigned char> decoded; | |
| 755 int outw, outh; | |
| 756 ASSERT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), | |
| 757 PNGCodec::FORMAT_RGB, &decoded, | |
| 758 &outw, &outh)); | |
| 759 ASSERT_EQ(w, outw); | |
| 760 ASSERT_EQ(h, outh); | |
| 761 ASSERT_EQ(decoded.size(), w * h * 3U); | |
| 762 | |
| 763 // Images must be equal | |
| 764 for (int x = 0; x < w; x++) { | |
| 765 for (int y = 0; y < h; y++) { | |
| 766 unsigned char* orig_px = &original[(y * w + x) * 4]; | |
| 767 unsigned char* dec_px = &decoded[(y * w + x) * 3]; | |
| 768 EXPECT_EQ(dec_px[0], orig_px[0]); | |
| 769 EXPECT_EQ(dec_px[1], orig_px[1]); | |
| 770 EXPECT_EQ(dec_px[2], orig_px[2]); | |
| 771 } | |
| 772 } | |
| 773 } | |
| 774 | |
| 775 TEST(PNGCodec, DecodeInterlacedBGR) { | 602 TEST(PNGCodec, DecodeInterlacedBGR) { |
| 776 const int w = 20, h = 20; | 603 const int w = 20, h = 20; |
| 777 | 604 |
| 778 // create an image with known values | 605 // create an image with known values |
| 779 std::vector<unsigned char> original; | 606 std::vector<unsigned char> original; |
| 780 MakeRGBImage(w, h, &original); | 607 MakeRGBImage(w, h, &original); |
| 781 | 608 |
| 782 // encode | 609 // encode |
| 783 std::vector<unsigned char> encoded; | 610 std::vector<unsigned char> encoded; |
| 784 ASSERT_TRUE(EncodeImage(original, | 611 ASSERT_TRUE(EncodeImage(original, |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 905 } | 732 } |
| 906 } | 733 } |
| 907 } | 734 } |
| 908 | 735 |
| 909 // Test that corrupted data decompression causes failures. | 736 // Test that corrupted data decompression causes failures. |
| 910 TEST(PNGCodec, DecodeCorrupted) { | 737 TEST(PNGCodec, DecodeCorrupted) { |
| 911 int w = 20, h = 20; | 738 int w = 20, h = 20; |
| 912 | 739 |
| 913 // Make some random data (an uncompressed image). | 740 // Make some random data (an uncompressed image). |
| 914 std::vector<unsigned char> original; | 741 std::vector<unsigned char> original; |
| 915 MakeRGBImage(w, h, &original); | 742 MakeRGBAImage(w, h, false, &original); |
| 916 | 743 |
| 917 // It should fail when given non-JPEG compressed data. | 744 // It should fail when given non-PNG compressed data. |
| 918 std::vector<unsigned char> output; | 745 std::vector<unsigned char> output; |
| 919 int outw, outh; | 746 int outw, outh; |
| 920 EXPECT_FALSE(PNGCodec::Decode(&original[0], original.size(), | 747 EXPECT_FALSE(PNGCodec::Decode(&original[0], original.size(), |
| 921 PNGCodec::FORMAT_RGB, &output, | 748 PNGCodec::FORMAT_RGBA, &output, &outw, &outh)); |
| 922 &outw, &outh)); | |
| 923 | 749 |
| 924 // Make some compressed data. | 750 // Make some compressed data. |
| 925 std::vector<unsigned char> compressed; | 751 std::vector<unsigned char> compressed; |
| 926 ASSERT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_RGB, | 752 ASSERT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_RGBA, Size(w, h), |
| 927 Size(w, h), w * 3, false, | 753 w * 4, false, std::vector<PNGCodec::Comment>(), |
| 928 std::vector<PNGCodec::Comment>(), | |
| 929 &compressed)); | 754 &compressed)); |
| 930 | 755 |
| 931 // Try decompressing a truncated version. | 756 // Try decompressing a truncated version. |
| 932 EXPECT_FALSE(PNGCodec::Decode(&compressed[0], compressed.size() / 2, | 757 EXPECT_FALSE(PNGCodec::Decode(&compressed[0], compressed.size() / 2, |
| 933 PNGCodec::FORMAT_RGB, &output, | 758 PNGCodec::FORMAT_RGBA, &output, &outw, &outh)); |
| 934 &outw, &outh)); | |
| 935 | 759 |
| 936 // Corrupt it and try decompressing that. | 760 // Corrupt it and try decompressing that. |
| 937 for (int i = 10; i < 30; i++) | 761 for (int i = 10; i < 30; i++) |
| 938 compressed[i] = i; | 762 compressed[i] = i; |
| 939 EXPECT_FALSE(PNGCodec::Decode(&compressed[0], compressed.size(), | 763 EXPECT_FALSE(PNGCodec::Decode(&compressed[0], compressed.size(), |
| 940 PNGCodec::FORMAT_RGB, &output, | 764 PNGCodec::FORMAT_RGBA, &output, &outw, &outh)); |
| 941 &outw, &outh)); | |
| 942 } | |
| 943 | |
| 944 TEST(PNGCodec, StripAddAlpha) { | |
| 945 const int w = 20, h = 20; | |
| 946 | |
| 947 // These should be the same except one has a 0xff alpha channel. | |
| 948 std::vector<unsigned char> original_rgb; | |
| 949 MakeRGBImage(w, h, &original_rgb); | |
| 950 std::vector<unsigned char> original_rgba; | |
| 951 MakeRGBAImage(w, h, false, &original_rgba); | |
| 952 | |
| 953 // Encode RGBA data as RGB. | |
| 954 std::vector<unsigned char> encoded; | |
| 955 EXPECT_TRUE(PNGCodec::Encode(&original_rgba[0], PNGCodec::FORMAT_RGBA, | |
| 956 Size(w, h), w * 4, true, | |
| 957 std::vector<PNGCodec::Comment>(), | |
| 958 &encoded)); | |
| 959 | |
| 960 // Decode the RGB to RGBA. | |
| 961 std::vector<unsigned char> decoded; | |
| 962 int outw, outh; | |
| 963 EXPECT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), | |
| 964 PNGCodec::FORMAT_RGBA, &decoded, | |
| 965 &outw, &outh)); | |
| 966 | |
| 967 // Decoded and reference should be the same (opaque alpha). | |
| 968 ASSERT_EQ(w, outw); | |
| 969 ASSERT_EQ(h, outh); | |
| 970 ASSERT_EQ(original_rgba.size(), decoded.size()); | |
| 971 ASSERT_EQ(original_rgba, decoded); | |
| 972 | |
| 973 // Encode RGBA to RGBA. | |
| 974 EXPECT_TRUE(PNGCodec::Encode(&original_rgba[0], PNGCodec::FORMAT_RGBA, | |
| 975 Size(w, h), w * 4, false, | |
| 976 std::vector<PNGCodec::Comment>(), | |
| 977 &encoded)); | |
| 978 | |
| 979 // Decode the RGBA to RGB. | |
| 980 EXPECT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), | |
| 981 PNGCodec::FORMAT_RGB, &decoded, | |
| 982 &outw, &outh)); | |
| 983 | |
| 984 // It should be the same as our non-alpha-channel reference. | |
| 985 ASSERT_EQ(w, outw); | |
| 986 ASSERT_EQ(h, outh); | |
| 987 ASSERT_EQ(original_rgb.size(), decoded.size()); | |
| 988 ASSERT_EQ(original_rgb, decoded); | |
| 989 } | 765 } |
| 990 | 766 |
| 991 TEST(PNGCodec, EncodeBGRASkBitmapStridePadded) { | 767 TEST(PNGCodec, EncodeBGRASkBitmapStridePadded) { |
| 992 const int kWidth = 20; | 768 const int kWidth = 20; |
| 993 const int kHeight = 20; | 769 const int kHeight = 20; |
| 994 const int kPaddedWidth = 32; | 770 const int kPaddedWidth = 32; |
| 995 const int kBytesPerPixel = 4; | 771 const int kBytesPerPixel = 4; |
| 996 const int kPaddedSize = kPaddedWidth * kHeight; | 772 const int kPaddedSize = kPaddedWidth * kHeight; |
| 997 const int kRowBytes = kPaddedWidth * kBytesPerPixel; | 773 const int kRowBytes = kPaddedWidth * kBytesPerPixel; |
| 998 | 774 |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1118 << SkColorGetG(unpremultiplied_decoded) << ", " | 894 << SkColorGetG(unpremultiplied_decoded) << ", " |
| 1119 << SkColorGetB(unpremultiplied_decoded) << ")"; | 895 << SkColorGetB(unpremultiplied_decoded) << ")"; |
| 1120 } | 896 } |
| 1121 } | 897 } |
| 1122 } | 898 } |
| 1123 | 899 |
| 1124 TEST(PNGCodec, EncodeWithComment) { | 900 TEST(PNGCodec, EncodeWithComment) { |
| 1125 const int w = 10, h = 10; | 901 const int w = 10, h = 10; |
| 1126 | 902 |
| 1127 std::vector<unsigned char> original; | 903 std::vector<unsigned char> original; |
| 1128 MakeRGBImage(w, h, &original); | 904 MakeRGBAImage(w, h, true, &original); |
| 1129 | 905 |
| 1130 std::vector<unsigned char> encoded; | 906 std::vector<unsigned char> encoded; |
| 1131 std::vector<PNGCodec::Comment> comments; | 907 std::vector<PNGCodec::Comment> comments; |
| 1132 comments.push_back(PNGCodec::Comment("key", "text")); | 908 comments.push_back(PNGCodec::Comment("key", "text")); |
| 1133 comments.push_back(PNGCodec::Comment("test", "something")); | 909 comments.push_back(PNGCodec::Comment("test", "something")); |
| 1134 comments.push_back(PNGCodec::Comment("have some", "spaces in both")); | 910 comments.push_back(PNGCodec::Comment("have some", "spaces in both")); |
| 1135 EXPECT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_RGB, | 911 EXPECT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_RGBA, Size(w, h), |
| 1136 Size(w, h), w * 3, false, comments, &encoded)); | 912 w * 4, false, comments, &encoded)); |
| 1137 | 913 |
| 1138 // Each chunk is of the form length (4 bytes), chunk type (tEXt), data, | 914 // Each chunk is of the form length (4 bytes), chunk type (tEXt), data, |
| 1139 // checksum (4 bytes). Make sure we find all of them in the encoded | 915 // checksum (4 bytes). Make sure we find all of them in the encoded |
| 1140 // results. | 916 // results. |
| 1141 const unsigned char kExpected1[] = | 917 const unsigned char kExpected1[] = |
| 1142 "\x00\x00\x00\x08tEXtkey\x00text\x9e\xe7\x66\x51"; | 918 "\x00\x00\x00\x08tEXtkey\x00text\x9e\xe7\x66\x51"; |
| 1143 const unsigned char kExpected2[] = | 919 const unsigned char kExpected2[] = |
| 1144 "\x00\x00\x00\x0etEXttest\x00something\x29\xba\xef\xac"; | 920 "\x00\x00\x00\x0etEXttest\x00something\x29\xba\xef\xac"; |
| 1145 const unsigned char kExpected3[] = | 921 const unsigned char kExpected3[] = |
| 1146 "\x00\x00\x00\x18tEXthave some\x00spaces in both\x8d\x69\x34\x2d"; | 922 "\x00\x00\x00\x18tEXthave some\x00spaces in both\x8d\x69\x34\x2d"; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1183 PNGCodec::Decode(&encoded_normal[0], encoded_normal.size(), &decoded)); | 959 PNGCodec::Decode(&encoded_normal[0], encoded_normal.size(), &decoded)); |
| 1184 EXPECT_TRUE(BitmapsAreEqual(decoded, original_bitmap)); | 960 EXPECT_TRUE(BitmapsAreEqual(decoded, original_bitmap)); |
| 1185 | 961 |
| 1186 EXPECT_TRUE( | 962 EXPECT_TRUE( |
| 1187 PNGCodec::Decode(&encoded_fast[0], encoded_fast.size(), &decoded)); | 963 PNGCodec::Decode(&encoded_fast[0], encoded_fast.size(), &decoded)); |
| 1188 EXPECT_TRUE(BitmapsAreEqual(decoded, original_bitmap)); | 964 EXPECT_TRUE(BitmapsAreEqual(decoded, original_bitmap)); |
| 1189 } | 965 } |
| 1190 | 966 |
| 1191 | 967 |
| 1192 } // namespace gfx | 968 } // namespace gfx |
| OLD | NEW |