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 |
279 TEST(PNGCodec, EncodeDecodeRGBA) { | 307 TEST(PNGCodec, EncodeDecodeRGBA) { |
280 const int w = 20, h = 20; | 308 const int w = 20, h = 20; |
281 | 309 |
282 // create an image with known values, a must be opaque because it will be | 310 // create an image with known values, a must be opaque because it will be |
283 // lost during encoding | 311 // lost during encoding |
284 std::vector<unsigned char> original; | 312 std::vector<unsigned char> original; |
285 MakeRGBAImage(w, h, true, &original); | 313 MakeRGBAImage(w, h, true, &original); |
286 | 314 |
287 // encode | 315 // encode |
288 std::vector<unsigned char> encoded; | 316 std::vector<unsigned char> encoded; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 unsigned char* rgba_pixel = &decoded[(y * w + x) * 4]; | 400 unsigned char* rgba_pixel = &decoded[(y * w + x) * 4]; |
373 | 401 |
374 EXPECT_EQ(palette_color.red, rgba_pixel[0]); | 402 EXPECT_EQ(palette_color.red, rgba_pixel[0]); |
375 EXPECT_EQ(palette_color.green, rgba_pixel[1]); | 403 EXPECT_EQ(palette_color.green, rgba_pixel[1]); |
376 EXPECT_EQ(palette_color.blue, rgba_pixel[2]); | 404 EXPECT_EQ(palette_color.blue, rgba_pixel[2]); |
377 EXPECT_EQ(alpha, rgba_pixel[3]); | 405 EXPECT_EQ(alpha, rgba_pixel[3]); |
378 } | 406 } |
379 } | 407 } |
380 } | 408 } |
381 | 409 |
| 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 |
382 TEST(PNGCodec, DecodeInterlacedPalette) { | 453 TEST(PNGCodec, DecodeInterlacedPalette) { |
383 const int w = 20, h = 20; | 454 const int w = 20, h = 20; |
384 | 455 |
385 // create an image with known values | 456 // create an image with known values |
386 std::vector<unsigned char> original; | 457 std::vector<unsigned char> original; |
387 std::vector<png_color> original_palette; | 458 std::vector<png_color> original_palette; |
388 std::vector<unsigned char> original_trans_chunk; | 459 std::vector<unsigned char> original_trans_chunk; |
389 MakePaletteImage(w, h, &original, &original_palette, &original_trans_chunk); | 460 MakePaletteImage(w, h, &original, &original_palette, &original_trans_chunk); |
390 | 461 |
391 // encode | 462 // encode |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 MakeGrayscaleImage(w, h, &original); | 503 MakeGrayscaleImage(w, h, &original); |
433 | 504 |
434 // encode | 505 // encode |
435 std::vector<unsigned char> encoded; | 506 std::vector<unsigned char> encoded; |
436 ASSERT_TRUE(EncodeImage(original, w, h, COLOR_TYPE_GRAY, &encoded)); | 507 ASSERT_TRUE(EncodeImage(original, w, h, COLOR_TYPE_GRAY, &encoded)); |
437 | 508 |
438 // decode | 509 // decode |
439 std::vector<unsigned char> decoded; | 510 std::vector<unsigned char> decoded; |
440 int outw, outh; | 511 int outw, outh; |
441 ASSERT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), | 512 ASSERT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), |
442 PNGCodec::FORMAT_RGBA, &decoded, &outw, &outh)); | 513 PNGCodec::FORMAT_RGB, &decoded, |
| 514 &outw, &outh)); |
443 ASSERT_EQ(w, outw); | 515 ASSERT_EQ(w, outw); |
444 ASSERT_EQ(h, outh); | 516 ASSERT_EQ(h, outh); |
445 ASSERT_EQ(decoded.size(), original.size() * 4); | 517 ASSERT_EQ(decoded.size(), original.size() * 3); |
446 | 518 |
447 // Images must be equal | 519 // Images must be equal |
448 for (int y = 0; y < h; ++y) { | 520 for (int y = 0; y < h; ++y) { |
449 for (int x = 0; x < w; ++x) { | 521 for (int x = 0; x < w; ++x) { |
450 unsigned char gray_pixel = original[(y * w + x)]; | 522 unsigned char gray_pixel = original[(y * w + x)]; |
451 unsigned char* rgba_pixel = &decoded[(y * w + x) * 4]; | 523 unsigned char* rgba_pixel = &decoded[(y * w + x) * 3]; |
452 EXPECT_EQ(rgba_pixel[0], gray_pixel); | 524 EXPECT_EQ(rgba_pixel[0], gray_pixel); |
453 EXPECT_EQ(rgba_pixel[1], gray_pixel); | 525 EXPECT_EQ(rgba_pixel[1], gray_pixel); |
454 EXPECT_EQ(rgba_pixel[2], gray_pixel); | 526 EXPECT_EQ(rgba_pixel[2], gray_pixel); |
455 EXPECT_EQ(rgba_pixel[3], 0xff); | |
456 } | 527 } |
457 } | 528 } |
458 } | 529 } |
459 | 530 |
460 TEST(PNGCodec, DecodeGrayscaleWithAlpha) { | 531 TEST(PNGCodec, DecodeGrayscaleWithAlpha) { |
461 const int w = 20, h = 20; | 532 const int w = 20, h = 20; |
462 | 533 |
463 // create an image with known values | 534 // create an image with known values |
464 std::vector<unsigned char> original; | 535 std::vector<unsigned char> original; |
465 MakeGrayscaleAlphaImage(w, h, &original); | 536 MakeGrayscaleAlphaImage(w, h, &original); |
(...skipping 21 matching lines...) Expand all Loading... |
487 unsigned char* gray_pixel = &original[(y * w + x) * 2]; | 558 unsigned char* gray_pixel = &original[(y * w + x) * 2]; |
488 unsigned char* rgba_pixel = &decoded[(y * w + x) * 4]; | 559 unsigned char* rgba_pixel = &decoded[(y * w + x) * 4]; |
489 EXPECT_EQ(rgba_pixel[0], gray_pixel[0]); | 560 EXPECT_EQ(rgba_pixel[0], gray_pixel[0]); |
490 EXPECT_EQ(rgba_pixel[1], gray_pixel[0]); | 561 EXPECT_EQ(rgba_pixel[1], gray_pixel[0]); |
491 EXPECT_EQ(rgba_pixel[2], gray_pixel[0]); | 562 EXPECT_EQ(rgba_pixel[2], gray_pixel[0]); |
492 EXPECT_EQ(rgba_pixel[3], gray_pixel[1]); | 563 EXPECT_EQ(rgba_pixel[3], gray_pixel[1]); |
493 } | 564 } |
494 } | 565 } |
495 } | 566 } |
496 | 567 |
| 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 |
497 TEST(PNGCodec, DecodeInterlacedGrayscale) { | 604 TEST(PNGCodec, DecodeInterlacedGrayscale) { |
498 const int w = 20, h = 20; | 605 const int w = 20, h = 20; |
499 | 606 |
500 // create an image with known values | 607 // create an image with known values |
501 std::vector<unsigned char> original; | 608 std::vector<unsigned char> original; |
502 MakeGrayscaleImage(w, h, &original); | 609 MakeGrayscaleImage(w, h, &original); |
503 | 610 |
504 // encode | 611 // encode |
505 std::vector<unsigned char> encoded; | 612 std::vector<unsigned char> encoded; |
506 ASSERT_TRUE(EncodeImage(original, | 613 ASSERT_TRUE(EncodeImage(original, |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
563 unsigned char* gray_pixel = &original[(y * w + x) * 2]; | 670 unsigned char* gray_pixel = &original[(y * w + x) * 2]; |
564 unsigned char* rgba_pixel = &decoded[(y * w + x) * 4]; | 671 unsigned char* rgba_pixel = &decoded[(y * w + x) * 4]; |
565 EXPECT_EQ(rgba_pixel[0], gray_pixel[0]); | 672 EXPECT_EQ(rgba_pixel[0], gray_pixel[0]); |
566 EXPECT_EQ(rgba_pixel[1], gray_pixel[0]); | 673 EXPECT_EQ(rgba_pixel[1], gray_pixel[0]); |
567 EXPECT_EQ(rgba_pixel[2], gray_pixel[0]); | 674 EXPECT_EQ(rgba_pixel[2], gray_pixel[0]); |
568 EXPECT_EQ(rgba_pixel[3], gray_pixel[1]); | 675 EXPECT_EQ(rgba_pixel[3], gray_pixel[1]); |
569 } | 676 } |
570 } | 677 } |
571 } | 678 } |
572 | 679 |
| 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 |
573 TEST(PNGCodec, DecodeInterlacedRGBA) { | 709 TEST(PNGCodec, DecodeInterlacedRGBA) { |
574 const int w = 20, h = 20; | 710 const int w = 20, h = 20; |
575 | 711 |
576 // create an image with known values | 712 // create an image with known values |
577 std::vector<unsigned char> original; | 713 std::vector<unsigned char> original; |
578 MakeRGBAImage(w, h, false, &original); | 714 MakeRGBAImage(w, h, false, &original); |
579 | 715 |
580 // encode | 716 // encode |
581 std::vector<unsigned char> encoded; | 717 std::vector<unsigned char> encoded; |
582 ASSERT_TRUE(EncodeImage(original, | 718 ASSERT_TRUE(EncodeImage(original, |
583 w, h, | 719 w, h, |
584 COLOR_TYPE_RGBA, | 720 COLOR_TYPE_RGBA, |
585 &encoded, | 721 &encoded, |
586 PNG_INTERLACE_ADAM7)); | 722 PNG_INTERLACE_ADAM7)); |
587 | 723 |
588 // decode, it should have the same size as the original | 724 // decode, it should have the same size as the original |
589 std::vector<unsigned char> decoded; | 725 std::vector<unsigned char> decoded; |
590 int outw, outh; | 726 int outw, outh; |
591 ASSERT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), | 727 ASSERT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(), |
592 PNGCodec::FORMAT_RGBA, &decoded, | 728 PNGCodec::FORMAT_RGBA, &decoded, |
593 &outw, &outh)); | 729 &outw, &outh)); |
594 ASSERT_EQ(w, outw); | 730 ASSERT_EQ(w, outw); |
595 ASSERT_EQ(h, outh); | 731 ASSERT_EQ(h, outh); |
596 ASSERT_EQ(original.size(), decoded.size()); | 732 ASSERT_EQ(original.size(), decoded.size()); |
597 | 733 |
598 // Images must be equal | 734 // Images must be equal |
599 ASSERT_EQ(original, decoded); | 735 ASSERT_EQ(original, decoded); |
600 } | 736 } |
601 | 737 |
| 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 |
602 TEST(PNGCodec, DecodeInterlacedBGR) { | 775 TEST(PNGCodec, DecodeInterlacedBGR) { |
603 const int w = 20, h = 20; | 776 const int w = 20, h = 20; |
604 | 777 |
605 // create an image with known values | 778 // create an image with known values |
606 std::vector<unsigned char> original; | 779 std::vector<unsigned char> original; |
607 MakeRGBImage(w, h, &original); | 780 MakeRGBImage(w, h, &original); |
608 | 781 |
609 // encode | 782 // encode |
610 std::vector<unsigned char> encoded; | 783 std::vector<unsigned char> encoded; |
611 ASSERT_TRUE(EncodeImage(original, | 784 ASSERT_TRUE(EncodeImage(original, |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
732 } | 905 } |
733 } | 906 } |
734 } | 907 } |
735 | 908 |
736 // Test that corrupted data decompression causes failures. | 909 // Test that corrupted data decompression causes failures. |
737 TEST(PNGCodec, DecodeCorrupted) { | 910 TEST(PNGCodec, DecodeCorrupted) { |
738 int w = 20, h = 20; | 911 int w = 20, h = 20; |
739 | 912 |
740 // Make some random data (an uncompressed image). | 913 // Make some random data (an uncompressed image). |
741 std::vector<unsigned char> original; | 914 std::vector<unsigned char> original; |
742 MakeRGBAImage(w, h, false, &original); | 915 MakeRGBImage(w, h, &original); |
743 | 916 |
744 // It should fail when given non-PNG compressed data. | 917 // It should fail when given non-JPEG compressed data. |
745 std::vector<unsigned char> output; | 918 std::vector<unsigned char> output; |
746 int outw, outh; | 919 int outw, outh; |
747 EXPECT_FALSE(PNGCodec::Decode(&original[0], original.size(), | 920 EXPECT_FALSE(PNGCodec::Decode(&original[0], original.size(), |
748 PNGCodec::FORMAT_RGBA, &output, &outw, &outh)); | 921 PNGCodec::FORMAT_RGB, &output, |
| 922 &outw, &outh)); |
749 | 923 |
750 // Make some compressed data. | 924 // Make some compressed data. |
751 std::vector<unsigned char> compressed; | 925 std::vector<unsigned char> compressed; |
752 ASSERT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_RGBA, Size(w, h), | 926 ASSERT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_RGB, |
753 w * 4, false, std::vector<PNGCodec::Comment>(), | 927 Size(w, h), w * 3, false, |
| 928 std::vector<PNGCodec::Comment>(), |
754 &compressed)); | 929 &compressed)); |
755 | 930 |
756 // Try decompressing a truncated version. | 931 // Try decompressing a truncated version. |
757 EXPECT_FALSE(PNGCodec::Decode(&compressed[0], compressed.size() / 2, | 932 EXPECT_FALSE(PNGCodec::Decode(&compressed[0], compressed.size() / 2, |
758 PNGCodec::FORMAT_RGBA, &output, &outw, &outh)); | 933 PNGCodec::FORMAT_RGB, &output, |
| 934 &outw, &outh)); |
759 | 935 |
760 // Corrupt it and try decompressing that. | 936 // Corrupt it and try decompressing that. |
761 for (int i = 10; i < 30; i++) | 937 for (int i = 10; i < 30; i++) |
762 compressed[i] = i; | 938 compressed[i] = i; |
763 EXPECT_FALSE(PNGCodec::Decode(&compressed[0], compressed.size(), | 939 EXPECT_FALSE(PNGCodec::Decode(&compressed[0], compressed.size(), |
764 PNGCodec::FORMAT_RGBA, &output, &outw, &outh)); | 940 PNGCodec::FORMAT_RGB, &output, |
| 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); |
765 } | 989 } |
766 | 990 |
767 TEST(PNGCodec, EncodeBGRASkBitmapStridePadded) { | 991 TEST(PNGCodec, EncodeBGRASkBitmapStridePadded) { |
768 const int kWidth = 20; | 992 const int kWidth = 20; |
769 const int kHeight = 20; | 993 const int kHeight = 20; |
770 const int kPaddedWidth = 32; | 994 const int kPaddedWidth = 32; |
771 const int kBytesPerPixel = 4; | 995 const int kBytesPerPixel = 4; |
772 const int kPaddedSize = kPaddedWidth * kHeight; | 996 const int kPaddedSize = kPaddedWidth * kHeight; |
773 const int kRowBytes = kPaddedWidth * kBytesPerPixel; | 997 const int kRowBytes = kPaddedWidth * kBytesPerPixel; |
774 | 998 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
894 << SkColorGetG(unpremultiplied_decoded) << ", " | 1118 << SkColorGetG(unpremultiplied_decoded) << ", " |
895 << SkColorGetB(unpremultiplied_decoded) << ")"; | 1119 << SkColorGetB(unpremultiplied_decoded) << ")"; |
896 } | 1120 } |
897 } | 1121 } |
898 } | 1122 } |
899 | 1123 |
900 TEST(PNGCodec, EncodeWithComment) { | 1124 TEST(PNGCodec, EncodeWithComment) { |
901 const int w = 10, h = 10; | 1125 const int w = 10, h = 10; |
902 | 1126 |
903 std::vector<unsigned char> original; | 1127 std::vector<unsigned char> original; |
904 MakeRGBAImage(w, h, true, &original); | 1128 MakeRGBImage(w, h, &original); |
905 | 1129 |
906 std::vector<unsigned char> encoded; | 1130 std::vector<unsigned char> encoded; |
907 std::vector<PNGCodec::Comment> comments; | 1131 std::vector<PNGCodec::Comment> comments; |
908 comments.push_back(PNGCodec::Comment("key", "text")); | 1132 comments.push_back(PNGCodec::Comment("key", "text")); |
909 comments.push_back(PNGCodec::Comment("test", "something")); | 1133 comments.push_back(PNGCodec::Comment("test", "something")); |
910 comments.push_back(PNGCodec::Comment("have some", "spaces in both")); | 1134 comments.push_back(PNGCodec::Comment("have some", "spaces in both")); |
911 EXPECT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_RGBA, Size(w, h), | 1135 EXPECT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_RGB, |
912 w * 4, false, comments, &encoded)); | 1136 Size(w, h), w * 3, false, comments, &encoded)); |
913 | 1137 |
914 // Each chunk is of the form length (4 bytes), chunk type (tEXt), data, | 1138 // Each chunk is of the form length (4 bytes), chunk type (tEXt), data, |
915 // checksum (4 bytes). Make sure we find all of them in the encoded | 1139 // checksum (4 bytes). Make sure we find all of them in the encoded |
916 // results. | 1140 // results. |
917 const unsigned char kExpected1[] = | 1141 const unsigned char kExpected1[] = |
918 "\x00\x00\x00\x08tEXtkey\x00text\x9e\xe7\x66\x51"; | 1142 "\x00\x00\x00\x08tEXtkey\x00text\x9e\xe7\x66\x51"; |
919 const unsigned char kExpected2[] = | 1143 const unsigned char kExpected2[] = |
920 "\x00\x00\x00\x0etEXttest\x00something\x29\xba\xef\xac"; | 1144 "\x00\x00\x00\x0etEXttest\x00something\x29\xba\xef\xac"; |
921 const unsigned char kExpected3[] = | 1145 const unsigned char kExpected3[] = |
922 "\x00\x00\x00\x18tEXthave some\x00spaces in both\x8d\x69\x34\x2d"; | 1146 "\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... |
959 PNGCodec::Decode(&encoded_normal[0], encoded_normal.size(), &decoded)); | 1183 PNGCodec::Decode(&encoded_normal[0], encoded_normal.size(), &decoded)); |
960 EXPECT_TRUE(BitmapsAreEqual(decoded, original_bitmap)); | 1184 EXPECT_TRUE(BitmapsAreEqual(decoded, original_bitmap)); |
961 | 1185 |
962 EXPECT_TRUE( | 1186 EXPECT_TRUE( |
963 PNGCodec::Decode(&encoded_fast[0], encoded_fast.size(), &decoded)); | 1187 PNGCodec::Decode(&encoded_fast[0], encoded_fast.size(), &decoded)); |
964 EXPECT_TRUE(BitmapsAreEqual(decoded, original_bitmap)); | 1188 EXPECT_TRUE(BitmapsAreEqual(decoded, original_bitmap)); |
965 } | 1189 } |
966 | 1190 |
967 | 1191 |
968 } // namespace gfx | 1192 } // namespace gfx |
OLD | NEW |