| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <math.h> | |
| 6 | |
| 7 #include "base/gfx/png_encoder.h" | |
| 8 #include "base/gfx/png_decoder.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 #include "third_party/skia/include/core/SkBitmap.h" | |
| 11 | |
| 12 static void MakeRGBImage(int w, int h, std::vector<unsigned char>* dat) { | |
| 13 dat->resize(w * h * 3); | |
| 14 for (int y = 0; y < h; y++) { | |
| 15 for (int x = 0; x < w; x++) { | |
| 16 unsigned char* org_px = &(*dat)[(y * w + x) * 3]; | |
| 17 org_px[0] = x * 3; // r | |
| 18 org_px[1] = x * 3 + 1; // g | |
| 19 org_px[2] = x * 3 + 2; // b | |
| 20 } | |
| 21 } | |
| 22 } | |
| 23 | |
| 24 // Set use_transparency to write data into the alpha channel, otherwise it will | |
| 25 // be filled with 0xff. With the alpha channel stripped, this should yield the | |
| 26 // same image as MakeRGBImage above, so the code below can make reference | |
| 27 // images for conversion testing. | |
| 28 static void MakeRGBAImage(int w, int h, bool use_transparency, | |
| 29 std::vector<unsigned char>* dat) { | |
| 30 dat->resize(w * h * 4); | |
| 31 for (int y = 0; y < h; y++) { | |
| 32 for (int x = 0; x < w; x++) { | |
| 33 unsigned char* org_px = &(*dat)[(y * w + x) * 4]; | |
| 34 org_px[0] = x * 3; // r | |
| 35 org_px[1] = x * 3 + 1; // g | |
| 36 org_px[2] = x * 3 + 2; // b | |
| 37 if (use_transparency) | |
| 38 org_px[3] = x*3 + 3; // a | |
| 39 else | |
| 40 org_px[3] = 0xFF; // a (opaque) | |
| 41 } | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 // Returns true if each channel of the given two colors are "close." This is | |
| 46 // used for comparing colors where rounding errors may cause off-by-one. | |
| 47 bool ColorsClose(uint32_t a, uint32_t b) { | |
| 48 return abs(static_cast<int>(SkColorGetB(a) - SkColorGetB(b))) < 2 && | |
| 49 abs(static_cast<int>(SkColorGetG(a) - SkColorGetG(b))) < 2 && | |
| 50 abs(static_cast<int>(SkColorGetR(a) - SkColorGetR(b))) < 2 && | |
| 51 abs(static_cast<int>(SkColorGetA(a) - SkColorGetA(b))) < 2; | |
| 52 } | |
| 53 | |
| 54 void MakeTestSkBitmap(int w, int h, SkBitmap* bmp) { | |
| 55 bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); | |
| 56 bmp->allocPixels(); | |
| 57 | |
| 58 uint32_t* src_data = bmp->getAddr32(0, 0); | |
| 59 for (int i = 0; i < w * h; i++) { | |
| 60 src_data[i] = SkPreMultiplyARGB(i % 255, i % 250, i % 245, i % 240); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 TEST(PNGCodec, EncodeDecodeRGB) { | |
| 65 const int w = 20, h = 20; | |
| 66 | |
| 67 // create an image with known values | |
| 68 std::vector<unsigned char> original; | |
| 69 MakeRGBImage(w, h, &original); | |
| 70 | |
| 71 // encode | |
| 72 std::vector<unsigned char> encoded; | |
| 73 EXPECT_TRUE(PNGEncoder::Encode(&original[0], PNGEncoder::FORMAT_RGB, w, h, | |
| 74 w * 3, false, &encoded)); | |
| 75 | |
| 76 // decode, it should have the same size as the original | |
| 77 std::vector<unsigned char> decoded; | |
| 78 int outw, outh; | |
| 79 EXPECT_TRUE(PNGDecoder::Decode(&encoded[0], encoded.size(), | |
| 80 PNGDecoder::FORMAT_RGB, &decoded, | |
| 81 &outw, &outh)); | |
| 82 ASSERT_EQ(w, outw); | |
| 83 ASSERT_EQ(h, outh); | |
| 84 ASSERT_EQ(original.size(), decoded.size()); | |
| 85 | |
| 86 // Images must be equal | |
| 87 ASSERT_TRUE(original == decoded); | |
| 88 } | |
| 89 | |
| 90 TEST(PNGCodec, EncodeDecodeRGBA) { | |
| 91 const int w = 20, h = 20; | |
| 92 | |
| 93 // create an image with known values, a must be opaque because it will be | |
| 94 // lost during encoding | |
| 95 std::vector<unsigned char> original; | |
| 96 MakeRGBAImage(w, h, true, &original); | |
| 97 | |
| 98 // encode | |
| 99 std::vector<unsigned char> encoded; | |
| 100 EXPECT_TRUE(PNGEncoder::Encode(&original[0], PNGEncoder::FORMAT_RGBA, w, h, | |
| 101 w * 4, false, &encoded)); | |
| 102 | |
| 103 // decode, it should have the same size as the original | |
| 104 std::vector<unsigned char> decoded; | |
| 105 int outw, outh; | |
| 106 EXPECT_TRUE(PNGDecoder::Decode(&encoded[0], encoded.size(), | |
| 107 PNGDecoder::FORMAT_RGBA, &decoded, | |
| 108 &outw, &outh)); | |
| 109 ASSERT_EQ(w, outw); | |
| 110 ASSERT_EQ(h, outh); | |
| 111 ASSERT_EQ(original.size(), decoded.size()); | |
| 112 | |
| 113 // Images must be exactly equal | |
| 114 ASSERT_TRUE(original == decoded); | |
| 115 } | |
| 116 | |
| 117 // Test that corrupted data decompression causes failures. | |
| 118 TEST(PNGCodec, DecodeCorrupted) { | |
| 119 int w = 20, h = 20; | |
| 120 | |
| 121 // Make some random data (an uncompressed image). | |
| 122 std::vector<unsigned char> original; | |
| 123 MakeRGBImage(w, h, &original); | |
| 124 | |
| 125 // It should fail when given non-JPEG compressed data. | |
| 126 std::vector<unsigned char> output; | |
| 127 int outw, outh; | |
| 128 EXPECT_FALSE(PNGDecoder::Decode(&original[0], original.size(), | |
| 129 PNGDecoder::FORMAT_RGB, &output, | |
| 130 &outw, &outh)); | |
| 131 | |
| 132 // Make some compressed data. | |
| 133 std::vector<unsigned char> compressed; | |
| 134 EXPECT_TRUE(PNGEncoder::Encode(&original[0], PNGEncoder::FORMAT_RGB, w, h, | |
| 135 w * 3, false, &compressed)); | |
| 136 | |
| 137 // Try decompressing a truncated version. | |
| 138 EXPECT_FALSE(PNGDecoder::Decode(&compressed[0], compressed.size() / 2, | |
| 139 PNGDecoder::FORMAT_RGB, &output, | |
| 140 &outw, &outh)); | |
| 141 | |
| 142 // Corrupt it and try decompressing that. | |
| 143 for (int i = 10; i < 30; i++) | |
| 144 compressed[i] = i; | |
| 145 EXPECT_FALSE(PNGDecoder::Decode(&compressed[0], compressed.size(), | |
| 146 PNGDecoder::FORMAT_RGB, &output, | |
| 147 &outw, &outh)); | |
| 148 } | |
| 149 | |
| 150 TEST(PNGCodec, EncodeDecodeBGRA) { | |
| 151 const int w = 20, h = 20; | |
| 152 | |
| 153 // Create an image with known values, alpha must be opaque because it will be | |
| 154 // lost during encoding. | |
| 155 std::vector<unsigned char> original; | |
| 156 MakeRGBAImage(w, h, true, &original); | |
| 157 | |
| 158 // Encode. | |
| 159 std::vector<unsigned char> encoded; | |
| 160 EXPECT_TRUE(PNGEncoder::Encode(&original[0], PNGEncoder::FORMAT_BGRA, w, h, | |
| 161 w * 4, false, &encoded)); | |
| 162 | |
| 163 // Decode, it should have the same size as the original. | |
| 164 std::vector<unsigned char> decoded; | |
| 165 int outw, outh; | |
| 166 EXPECT_TRUE(PNGDecoder::Decode(&encoded[0], encoded.size(), | |
| 167 PNGDecoder::FORMAT_BGRA, &decoded, | |
| 168 &outw, &outh)); | |
| 169 ASSERT_EQ(w, outw); | |
| 170 ASSERT_EQ(h, outh); | |
| 171 ASSERT_EQ(original.size(), decoded.size()); | |
| 172 | |
| 173 // Images must be exactly equal. | |
| 174 ASSERT_TRUE(original == decoded); | |
| 175 } | |
| 176 | |
| 177 TEST(PNGCodec, StripAddAlpha) { | |
| 178 const int w = 20, h = 20; | |
| 179 | |
| 180 // These should be the same except one has a 0xff alpha channel. | |
| 181 std::vector<unsigned char> original_rgb; | |
| 182 MakeRGBImage(w, h, &original_rgb); | |
| 183 std::vector<unsigned char> original_rgba; | |
| 184 MakeRGBAImage(w, h, false, &original_rgba); | |
| 185 | |
| 186 // Encode RGBA data as RGB. | |
| 187 std::vector<unsigned char> encoded; | |
| 188 EXPECT_TRUE(PNGEncoder::Encode(&original_rgba[0], | |
| 189 PNGEncoder::FORMAT_RGBA, | |
| 190 w, h, | |
| 191 w * 4, true, &encoded)); | |
| 192 | |
| 193 // Decode the RGB to RGBA. | |
| 194 std::vector<unsigned char> decoded; | |
| 195 int outw, outh; | |
| 196 EXPECT_TRUE(PNGDecoder::Decode(&encoded[0], encoded.size(), | |
| 197 PNGDecoder::FORMAT_RGBA, &decoded, | |
| 198 &outw, &outh)); | |
| 199 | |
| 200 // Decoded and reference should be the same (opaque alpha). | |
| 201 ASSERT_EQ(w, outw); | |
| 202 ASSERT_EQ(h, outh); | |
| 203 ASSERT_EQ(original_rgba.size(), decoded.size()); | |
| 204 ASSERT_TRUE(original_rgba == decoded); | |
| 205 | |
| 206 // Encode RGBA to RGBA. | |
| 207 EXPECT_TRUE(PNGEncoder::Encode(&original_rgba[0], | |
| 208 PNGEncoder::FORMAT_RGBA, | |
| 209 w, h, | |
| 210 w * 4, false, &encoded)); | |
| 211 | |
| 212 // Decode the RGBA to RGB. | |
| 213 EXPECT_TRUE(PNGDecoder::Decode(&encoded[0], encoded.size(), | |
| 214 PNGDecoder::FORMAT_RGB, &decoded, | |
| 215 &outw, &outh)); | |
| 216 | |
| 217 // It should be the same as our non-alpha-channel reference. | |
| 218 ASSERT_EQ(w, outw); | |
| 219 ASSERT_EQ(h, outh); | |
| 220 ASSERT_EQ(original_rgb.size(), decoded.size()); | |
| 221 ASSERT_TRUE(original_rgb == decoded); | |
| 222 } | |
| 223 | |
| 224 TEST(PNGCodec, EncodeBGRASkBitmap) { | |
| 225 const int w = 20, h = 20; | |
| 226 | |
| 227 SkBitmap original_bitmap; | |
| 228 MakeTestSkBitmap(w, h, &original_bitmap); | |
| 229 | |
| 230 // Encode the bitmap. | |
| 231 std::vector<unsigned char> encoded; | |
| 232 PNGEncoder::EncodeBGRASkBitmap(original_bitmap, false, &encoded); | |
| 233 | |
| 234 // Decode the encoded string. | |
| 235 SkBitmap decoded_bitmap; | |
| 236 EXPECT_TRUE(PNGDecoder::Decode(&encoded, &decoded_bitmap)); | |
| 237 | |
| 238 // Compare the original bitmap and the output bitmap. We use ColorsClose | |
| 239 // as SkBitmaps are considered to be pre-multiplied, the unpremultiplication | |
| 240 // (in Encode) and repremultiplication (in Decode) can be lossy. | |
| 241 for (int x = 0; x < w; x++) { | |
| 242 for (int y = 0; y < h; y++) { | |
| 243 uint32_t original_pixel = original_bitmap.getAddr32(0, y)[x]; | |
| 244 uint32_t decoded_pixel = decoded_bitmap.getAddr32(0, y)[x]; | |
| 245 EXPECT_TRUE(ColorsClose(original_pixel, decoded_pixel)); | |
| 246 } | |
| 247 } | |
| 248 } | |
| 249 | |
| OLD | NEW |