Chromium Code Reviews| 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <cmath> | 6 #include <cmath> |
| 7 | 7 |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "third_party/libpng/png.h" | 10 #include "third_party/libpng/png.h" |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 abs(static_cast<int>(SkColorGetA(a) - SkColorGetA(b))) < 2; | 242 abs(static_cast<int>(SkColorGetA(a) - SkColorGetA(b))) < 2; |
| 243 } | 243 } |
| 244 | 244 |
| 245 // Returns true if the RGB components are "close." | 245 // Returns true if the RGB components are "close." |
| 246 bool NonAlphaColorsClose(uint32_t a, uint32_t b) { | 246 bool NonAlphaColorsClose(uint32_t a, uint32_t b) { |
| 247 return abs(static_cast<int>(SkColorGetB(a) - SkColorGetB(b))) < 2 && | 247 return abs(static_cast<int>(SkColorGetB(a) - SkColorGetB(b))) < 2 && |
| 248 abs(static_cast<int>(SkColorGetG(a) - SkColorGetG(b))) < 2 && | 248 abs(static_cast<int>(SkColorGetG(a) - SkColorGetG(b))) < 2 && |
| 249 abs(static_cast<int>(SkColorGetR(a) - SkColorGetR(b))) < 2; | 249 abs(static_cast<int>(SkColorGetR(a) - SkColorGetR(b))) < 2; |
| 250 } | 250 } |
| 251 | 251 |
| 252 void MakeTestSkBitmap(int w, int h, SkBitmap* bmp) { | 252 // Returns true if the BGRA 4-bit SkColor is "close" to the Gray 8-bit SkColor. |
|
Peter Kasting
2014/02/04 01:37:15
Nit: Don't you mean 32-bit (or 4-byte)?
| |
| 253 bool BGRAGrayColorsClose(uint32_t a, uint8_t b) { | |
| 254 return abs(static_cast<int>(SkColorGetB(a) - b)) < 2 && | |
| 255 abs(static_cast<int>(SkColorGetG(a) - b)) < 2 && | |
| 256 abs(static_cast<int>(SkColorGetR(a) - b)) < 2 && | |
| 257 abs(static_cast<int>(SkColorGetA(a) - 255)) < 2; | |
| 258 } | |
| 259 | |
| 260 void MakeTestBGRASkBitmap(int w, int h, SkBitmap* bmp) { | |
| 253 bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); | 261 bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 254 bmp->allocPixels(); | 262 bmp->allocPixels(); |
| 255 | 263 |
| 256 uint32_t* src_data = bmp->getAddr32(0, 0); | 264 uint32_t* src_data = bmp->getAddr32(0, 0); |
| 257 for (int i = 0; i < w * h; i++) { | 265 for (int i = 0; i < w * h; i++) { |
|
Peter Kasting
2014/02/04 01:37:15
Nit: {} unnecessary
| |
| 258 src_data[i] = SkPreMultiplyARGB(i % 255, i % 250, i % 245, i % 240); | 266 src_data[i] = SkPreMultiplyARGB(i % 255, i % 250, i % 245, i % 240); |
| 259 } | 267 } |
| 260 } | 268 } |
| 261 | 269 |
| 270 void MakeTestA8SkBitmap(int w, int h, SkBitmap* bmp) { | |
| 271 bmp->setConfig(SkBitmap::kA8_Config, w, h); | |
| 272 bmp->allocPixels(); | |
| 273 | |
| 274 uint8_t* src_data = bmp->getAddr8(0, 0); | |
| 275 for (int i = 0; i < w * h; i++) { | |
|
Peter Kasting
2014/02/04 01:37:15
Nit: {} unnecessary
| |
| 276 src_data[i] = i % 255; | |
| 277 } | |
| 278 } | |
| 279 | |
| 262 TEST(PNGCodec, EncodeDecodeRGB) { | 280 TEST(PNGCodec, EncodeDecodeRGB) { |
| 263 const int w = 20, h = 20; | 281 const int w = 20, h = 20; |
| 264 | 282 |
| 265 // create an image with known values | 283 // create an image with known values |
| 266 std::vector<unsigned char> original; | 284 std::vector<unsigned char> original; |
| 267 MakeRGBImage(w, h, &original); | 285 MakeRGBImage(w, h, &original); |
| 268 | 286 |
| 269 // encode | 287 // encode |
| 270 std::vector<unsigned char> encoded; | 288 std::vector<unsigned char> encoded; |
| 271 ASSERT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_RGB, | 289 ASSERT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_RGB, |
| (...skipping 738 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1010 uint32_t decoded_pixel = decoded_bitmap.getAddr32(0, y)[x]; | 1028 uint32_t decoded_pixel = decoded_bitmap.getAddr32(0, y)[x]; |
| 1011 EXPECT_TRUE(ColorsClose(original_pixel, decoded_pixel)); | 1029 EXPECT_TRUE(ColorsClose(original_pixel, decoded_pixel)); |
| 1012 } | 1030 } |
| 1013 } | 1031 } |
| 1014 } | 1032 } |
| 1015 | 1033 |
| 1016 TEST(PNGCodec, EncodeBGRASkBitmap) { | 1034 TEST(PNGCodec, EncodeBGRASkBitmap) { |
| 1017 const int w = 20, h = 20; | 1035 const int w = 20, h = 20; |
| 1018 | 1036 |
| 1019 SkBitmap original_bitmap; | 1037 SkBitmap original_bitmap; |
| 1020 MakeTestSkBitmap(w, h, &original_bitmap); | 1038 MakeTestBGRASkBitmap(w, h, &original_bitmap); |
| 1021 | 1039 |
| 1022 // Encode the bitmap. | 1040 // Encode the bitmap. |
| 1023 std::vector<unsigned char> encoded; | 1041 std::vector<unsigned char> encoded; |
| 1024 PNGCodec::EncodeBGRASkBitmap(original_bitmap, false, &encoded); | 1042 PNGCodec::EncodeBGRASkBitmap(original_bitmap, false, &encoded); |
| 1025 | 1043 |
| 1026 // Decode the encoded string. | 1044 // Decode the encoded string. |
| 1027 SkBitmap decoded_bitmap; | 1045 SkBitmap decoded_bitmap; |
| 1028 EXPECT_TRUE(PNGCodec::Decode(&encoded.front(), encoded.size(), | 1046 EXPECT_TRUE(PNGCodec::Decode(&encoded.front(), encoded.size(), |
| 1029 &decoded_bitmap)); | 1047 &decoded_bitmap)); |
| 1030 | 1048 |
| 1031 // Compare the original bitmap and the output bitmap. We use ColorsClose | 1049 // Compare the original bitmap and the output bitmap. We use ColorsClose |
| 1032 // as SkBitmaps are considered to be pre-multiplied, the unpremultiplication | 1050 // as SkBitmaps are considered to be pre-multiplied, the unpremultiplication |
| 1033 // (in Encode) and repremultiplication (in Decode) can be lossy. | 1051 // (in Encode) and repremultiplication (in Decode) can be lossy. |
| 1034 for (int x = 0; x < w; x++) { | 1052 for (int x = 0; x < w; x++) { |
| 1035 for (int y = 0; y < h; y++) { | 1053 for (int y = 0; y < h; y++) { |
| 1036 uint32_t original_pixel = original_bitmap.getAddr32(0, y)[x]; | 1054 uint32_t original_pixel = original_bitmap.getAddr32(0, y)[x]; |
| 1037 uint32_t decoded_pixel = decoded_bitmap.getAddr32(0, y)[x]; | 1055 uint32_t decoded_pixel = decoded_bitmap.getAddr32(0, y)[x]; |
| 1038 EXPECT_TRUE(ColorsClose(original_pixel, decoded_pixel)); | 1056 EXPECT_TRUE(ColorsClose(original_pixel, decoded_pixel)); |
| 1039 } | 1057 } |
| 1040 } | 1058 } |
| 1041 } | 1059 } |
| 1042 | 1060 |
| 1061 TEST(PNGCodec, EncodeA8SkBitmap) { | |
| 1062 const int w = 20, h = 20; | |
| 1063 | |
| 1064 SkBitmap original_bitmap; | |
| 1065 MakeTestA8SkBitmap(w, h, &original_bitmap); | |
| 1066 | |
| 1067 // Encode the bitmap. | |
| 1068 std::vector<unsigned char> encoded; | |
| 1069 EXPECT_TRUE(PNGCodec::EncodeA8SkBitmap(original_bitmap, &encoded)); | |
| 1070 | |
| 1071 // Decode the encoded string. | |
| 1072 SkBitmap decoded_bitmap; | |
| 1073 EXPECT_TRUE(PNGCodec::Decode(&encoded.front(), encoded.size(), | |
| 1074 &decoded_bitmap)); | |
| 1075 | |
| 1076 for (int x = 0; x < w; x++) { | |
| 1077 for (int y = 0; y < h; y++) { | |
| 1078 uint8_t original_pixel = *original_bitmap.getAddr8(x, y); | |
| 1079 uint32_t decoded_pixel = *decoded_bitmap.getAddr32(x, y); | |
| 1080 EXPECT_TRUE(BGRAGrayColorsClose(decoded_pixel, original_pixel)); | |
|
Peter Kasting
2014/02/04 01:37:15
How come we can't just check for direct equality?
| |
| 1081 } | |
| 1082 } | |
| 1083 } | |
| 1084 | |
| 1043 TEST(PNGCodec, EncodeBGRASkBitmapDiscardTransparency) { | 1085 TEST(PNGCodec, EncodeBGRASkBitmapDiscardTransparency) { |
| 1044 const int w = 20, h = 20; | 1086 const int w = 20, h = 20; |
| 1045 | 1087 |
| 1046 SkBitmap original_bitmap; | 1088 SkBitmap original_bitmap; |
| 1047 MakeTestSkBitmap(w, h, &original_bitmap); | 1089 MakeTestBGRASkBitmap(w, h, &original_bitmap); |
| 1048 | 1090 |
| 1049 // Encode the bitmap. | 1091 // Encode the bitmap. |
| 1050 std::vector<unsigned char> encoded; | 1092 std::vector<unsigned char> encoded; |
| 1051 PNGCodec::EncodeBGRASkBitmap(original_bitmap, true, &encoded); | 1093 PNGCodec::EncodeBGRASkBitmap(original_bitmap, true, &encoded); |
| 1052 | 1094 |
| 1053 // Decode the encoded string. | 1095 // Decode the encoded string. |
| 1054 SkBitmap decoded_bitmap; | 1096 SkBitmap decoded_bitmap; |
| 1055 EXPECT_TRUE(PNGCodec::Decode(&encoded.front(), encoded.size(), | 1097 EXPECT_TRUE(PNGCodec::Decode(&encoded.front(), encoded.size(), |
| 1056 &decoded_bitmap)); | 1098 &decoded_bitmap)); |
| 1057 | 1099 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1114 kExpected3 + arraysize(kExpected3)), | 1156 kExpected3 + arraysize(kExpected3)), |
| 1115 encoded.end()); | 1157 encoded.end()); |
| 1116 } | 1158 } |
| 1117 | 1159 |
| 1118 TEST(PNGCodec, EncodeDecodeWithVaryingCompressionLevels) { | 1160 TEST(PNGCodec, EncodeDecodeWithVaryingCompressionLevels) { |
| 1119 const int w = 20, h = 20; | 1161 const int w = 20, h = 20; |
| 1120 | 1162 |
| 1121 // create an image with known values, a must be opaque because it will be | 1163 // create an image with known values, a must be opaque because it will be |
| 1122 // lost during encoding | 1164 // lost during encoding |
| 1123 SkBitmap original_bitmap; | 1165 SkBitmap original_bitmap; |
| 1124 MakeTestSkBitmap(w, h, &original_bitmap); | 1166 MakeTestBGRASkBitmap(w, h, &original_bitmap); |
| 1125 | 1167 |
| 1126 // encode | 1168 // encode |
| 1127 std::vector<unsigned char> encoded_normal; | 1169 std::vector<unsigned char> encoded_normal; |
| 1128 EXPECT_TRUE( | 1170 EXPECT_TRUE( |
| 1129 PNGCodec::EncodeBGRASkBitmap(original_bitmap, false, &encoded_normal)); | 1171 PNGCodec::EncodeBGRASkBitmap(original_bitmap, false, &encoded_normal)); |
| 1130 | 1172 |
| 1131 std::vector<unsigned char> encoded_fast; | 1173 std::vector<unsigned char> encoded_fast; |
| 1132 EXPECT_TRUE( | 1174 EXPECT_TRUE( |
| 1133 PNGCodec::FastEncodeBGRASkBitmap(original_bitmap, false, &encoded_fast)); | 1175 PNGCodec::FastEncodeBGRASkBitmap(original_bitmap, false, &encoded_fast)); |
| 1134 | 1176 |
| 1135 // Make sure the different compression settings actually do something; the | 1177 // Make sure the different compression settings actually do something; the |
| 1136 // sizes should be different. | 1178 // sizes should be different. |
| 1137 EXPECT_NE(encoded_normal.size(), encoded_fast.size()); | 1179 EXPECT_NE(encoded_normal.size(), encoded_fast.size()); |
| 1138 | 1180 |
| 1139 // decode, they should be identical to the original. | 1181 // decode, they should be identical to the original. |
| 1140 SkBitmap decoded; | 1182 SkBitmap decoded; |
| 1141 EXPECT_TRUE( | 1183 EXPECT_TRUE( |
| 1142 PNGCodec::Decode(&encoded_normal[0], encoded_normal.size(), &decoded)); | 1184 PNGCodec::Decode(&encoded_normal[0], encoded_normal.size(), &decoded)); |
| 1143 EXPECT_TRUE(BitmapsAreEqual(decoded, original_bitmap)); | 1185 EXPECT_TRUE(BitmapsAreEqual(decoded, original_bitmap)); |
| 1144 | 1186 |
| 1145 EXPECT_TRUE( | 1187 EXPECT_TRUE( |
| 1146 PNGCodec::Decode(&encoded_fast[0], encoded_fast.size(), &decoded)); | 1188 PNGCodec::Decode(&encoded_fast[0], encoded_fast.size(), &decoded)); |
| 1147 EXPECT_TRUE(BitmapsAreEqual(decoded, original_bitmap)); | 1189 EXPECT_TRUE(BitmapsAreEqual(decoded, original_bitmap)); |
| 1148 } | 1190 } |
| 1149 | 1191 |
| 1150 | 1192 |
| 1151 } // namespace gfx | 1193 } // namespace gfx |
| OLD | NEW |