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