Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "Resources.h" | 8 #include "Resources.h" |
| 9 #include "SkAndroidCodec.h" | 9 #include "SkAndroidCodec.h" |
| 10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
| (...skipping 1056 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1067 SkAutoMalloc pixelStorage(dstInfo.getSafeSize(rowBytes)); | 1067 SkAutoMalloc pixelStorage(dstInfo.getSafeSize(rowBytes)); |
| 1068 SkCodec::Result result = codec->getAndroidPixels(dstInfo, pixelStorage.get() , rowBytes, &opts); | 1068 SkCodec::Result result = codec->getAndroidPixels(dstInfo, pixelStorage.get() , rowBytes, &opts); |
| 1069 REPORTER_ASSERT(r, SkCodec::kSuccess == result); | 1069 REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 1070 } | 1070 } |
| 1071 | 1071 |
| 1072 DEF_TEST(Codec_ColorXform, r) { | 1072 DEF_TEST(Codec_ColorXform, r) { |
| 1073 check_color_xform(r, "mandrill_512_q075.jpg"); | 1073 check_color_xform(r, "mandrill_512_q075.jpg"); |
| 1074 check_color_xform(r, "mandrill_512.png"); | 1074 check_color_xform(r, "mandrill_512.png"); |
| 1075 } | 1075 } |
| 1076 | 1076 |
| 1077 static void check_round_trip(skiatest::Reporter* r, const SkBitmap& bm1) { | 1077 static bool color_type_match(SkColorType origColorType, SkColorType codecColorTy pe) { |
| 1078 SkColorType origColorType = bm1.colorType(); | 1078 switch (origColorType) { |
| 1079 SkAlphaType origAlphaType = bm1.alphaType(); | 1079 case kRGBA_8888_SkColorType: |
| 1080 case kBGRA_8888_SkColorType: | |
| 1081 return kRGBA_8888_SkColorType == codecColorType || | |
| 1082 kBGRA_8888_SkColorType == codecColorType; | |
| 1083 default: | |
| 1084 return origColorType == codecColorType; | |
| 1085 } | |
| 1086 } | |
| 1087 | |
| 1088 static bool alpha_type_match(SkAlphaType origAlphaType, SkAlphaType codecAlphaTy pe) { | |
| 1089 switch (origAlphaType) { | |
| 1090 case kUnpremul_SkAlphaType: | |
| 1091 case kPremul_SkAlphaType: | |
| 1092 return kUnpremul_SkAlphaType == codecAlphaType || | |
| 1093 kPremul_SkAlphaType == codecAlphaType; | |
| 1094 default: | |
| 1095 return origAlphaType == codecAlphaType; | |
| 1096 } | |
| 1097 } | |
| 1098 | |
| 1099 static void check_round_trip(skiatest::Reporter* r, SkCodec* origCodec, const Sk ImageInfo& info) { | |
| 1100 SkBitmap bm1; | |
| 1101 SkPMColor colors[256]; | |
| 1102 SkAutoTUnref<SkColorTable> colorTable1(new SkColorTable(colors, 256)); | |
| 1103 bm1.allocPixels(info, nullptr, colorTable1.get()); | |
| 1104 int numColors; | |
| 1105 SkCodec::Result result = origCodec->getPixels(info, bm1.getPixels(), bm1.row Bytes(), nullptr, | |
| 1106 const_cast<SkPMColor*>(colorTa ble1->readColors()), | |
| 1107 &numColors); | |
|
scroggo
2016/09/12 19:54:31
numColors gets updated, but colorTable1's count do
msarett
2016/09/12 20:27:57
Adding a comment. I think the real fix is dangero
| |
| 1108 REPORTER_ASSERT(r, SkCodec::kSuccess == result); | |
| 1080 | 1109 |
| 1081 // Encode the image to png. | 1110 // Encode the image to png. |
| 1082 sk_sp<SkData> data = | 1111 sk_sp<SkData> data = |
| 1083 sk_sp<SkData>(SkImageEncoder::EncodeData(bm1, SkImageEncoder::kPNG_T ype, 100)); | 1112 sk_sp<SkData>(SkImageEncoder::EncodeData(bm1, SkImageEncoder::kPNG_T ype, 100)); |
| 1084 | 1113 |
| 1085 // Prepare to decode. The codec should recognize that the PNG is 565. | |
| 1086 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data.get())); | 1114 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data.get())); |
| 1087 REPORTER_ASSERT(r, origColorType == codec->getInfo().colorType()); | 1115 REPORTER_ASSERT(r, color_type_match(info.colorType(), codec->getInfo().color Type())); |
| 1088 REPORTER_ASSERT(r, origAlphaType == codec->getInfo().alphaType()); | 1116 REPORTER_ASSERT(r, alpha_type_match(info.alphaType(), codec->getInfo().alpha Type())); |
| 1089 | 1117 |
| 1090 SkBitmap bm2; | 1118 SkBitmap bm2; |
| 1091 bm2.allocPixels(codec->getInfo()); | 1119 SkAutoTUnref<SkColorTable> colorTable2(new SkColorTable(colors, 256)); |
| 1092 SkCodec::Result result = codec->getPixels(codec->getInfo(), bm2.getPixels(), bm2.rowBytes()); | 1120 bm2.allocPixels(info, nullptr, colorTable2.get()); |
| 1121 result = codec->getPixels(info, bm2.getPixels(), bm2.rowBytes(), nullptr, | |
| 1122 const_cast<SkPMColor*>(colorTable2->readColors()), &numColors); | |
| 1093 REPORTER_ASSERT(r, SkCodec::kSuccess == result); | 1123 REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 1094 | 1124 |
| 1095 SkMD5::Digest d1, d2; | 1125 SkMD5::Digest d1, d2; |
| 1096 md5(bm1, &d1); | 1126 md5(bm1, &d1); |
| 1097 md5(bm2, &d2); | 1127 md5(bm2, &d2); |
| 1098 REPORTER_ASSERT(r, d1 == d2); | 1128 REPORTER_ASSERT(r, d1 == d2); |
| 1099 } | 1129 } |
| 1100 | 1130 |
| 1101 DEF_TEST(Codec_PngRoundTrip, r) { | 1131 DEF_TEST(Codec_PngRoundTrip, r) { |
| 1102 // Create an arbitrary 565 bitmap. | |
| 1103 const char* path = "mandrill_512_q075.jpg"; | 1132 const char* path = "mandrill_512_q075.jpg"; |
| 1104 SkAutoTDelete<SkStream> stream(resource(path)); | 1133 SkAutoTDelete<SkStream> stream(resource(path)); |
| 1105 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); | 1134 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); |
| 1106 SkImageInfo info565 = codec->getInfo().makeColorType(kRGB_565_SkColorType); | |
| 1107 SkBitmap bm1; | |
| 1108 bm1.allocPixels(info565); | |
| 1109 SkCodec::Result result = codec->getPixels(info565, bm1.getPixels(), bm1.rowB ytes()); | |
| 1110 REPORTER_ASSERT(r, SkCodec::kSuccess == result); | |
| 1111 check_round_trip(r, bm1); | |
| 1112 | 1135 |
| 1113 // Create an arbitrary gray bitmap. | 1136 SkColorType colorTypesOpaque[] = { |
| 1137 kRGB_565_SkColorType, kRGBA_8888_SkColorType, kBGRA_8888_SkColorType | |
| 1138 }; | |
| 1139 for (SkColorType colorType : colorTypesOpaque) { | |
| 1140 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType); | |
| 1141 check_round_trip(r, codec.get(), newInfo); | |
| 1142 } | |
| 1143 | |
| 1114 path = "grayscale.jpg"; | 1144 path = "grayscale.jpg"; |
| 1115 stream.reset(resource(path)); | 1145 stream.reset(resource(path)); |
| 1116 codec.reset(SkCodec::NewFromStream(stream.release())); | 1146 codec.reset(SkCodec::NewFromStream(stream.release())); |
| 1117 SkBitmap bm2; | 1147 check_round_trip(r, codec.get(), codec->getInfo()); |
| 1118 bm2.allocPixels(codec->getInfo()); | 1148 |
| 1119 result = codec->getPixels(codec->getInfo(), bm2.getPixels(), bm2.rowBytes()) ; | 1149 path = "yellow_rose.png"; |
| 1120 REPORTER_ASSERT(r, SkCodec::kSuccess == result); | 1150 stream.reset(resource(path)); |
| 1121 check_round_trip(r, bm2); | 1151 codec.reset(SkCodec::NewFromStream(stream.release())); |
| 1152 | |
| 1153 SkColorType colorTypesWithAlpha[] = { | |
| 1154 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType | |
| 1155 }; | |
| 1156 SkAlphaType alphaTypes[] = { | |
| 1157 kUnpremul_SkAlphaType, kPremul_SkAlphaType | |
| 1158 }; | |
| 1159 for (SkColorType colorType : colorTypesWithAlpha) { | |
| 1160 for (SkAlphaType alphaType : alphaTypes) { | |
| 1161 // Set color space to nullptr because color correct premultiplies do not round trip. | |
| 1162 SkImageInfo newInfo = codec->getInfo().makeColorType(colorType) | |
| 1163 .makeAlphaType(alphaType) | |
| 1164 .makeColorSpace(nullptr); | |
| 1165 check_round_trip(r, codec.get(), newInfo); | |
| 1166 } | |
| 1167 } | |
| 1168 | |
| 1169 path = "index8.png"; | |
| 1170 stream.reset(resource(path)); | |
| 1171 codec.reset(SkCodec::NewFromStream(stream.release())); | |
| 1172 | |
| 1173 for (SkAlphaType alphaType : alphaTypes) { | |
| 1174 SkImageInfo newInfo = codec->getInfo().makeAlphaType(alphaType) | |
| 1175 .makeColorSpace(nullptr); | |
| 1176 check_round_trip(r, codec.get(), newInfo); | |
| 1177 } | |
| 1122 } | 1178 } |
| 1123 | 1179 |
| 1124 static void test_conversion_possible(skiatest::Reporter* r, const char* path, | 1180 static void test_conversion_possible(skiatest::Reporter* r, const char* path, |
| 1125 bool testScanlineDecoder) { | 1181 bool testScanlineDecoder) { |
| 1126 SkAutoTDelete<SkStream> stream(resource(path)); | 1182 SkAutoTDelete<SkStream> stream(resource(path)); |
| 1127 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); | 1183 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); |
| 1128 SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType); | 1184 SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType); |
| 1129 | 1185 |
| 1130 SkBitmap bm; | 1186 SkBitmap bm; |
| 1131 bm.allocPixels(infoF16); | 1187 bm.allocPixels(infoF16); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 1143 result = codec->startScanlineDecode(infoF16); | 1199 result = codec->startScanlineDecode(infoF16); |
| 1144 REPORTER_ASSERT(r, SkCodec::kSuccess == result); | 1200 REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 1145 } | 1201 } |
| 1146 } | 1202 } |
| 1147 | 1203 |
| 1148 DEF_TEST(Codec_F16ConversionPossible, r) { | 1204 DEF_TEST(Codec_F16ConversionPossible, r) { |
| 1149 test_conversion_possible(r, "color_wheel.webp", false); | 1205 test_conversion_possible(r, "color_wheel.webp", false); |
| 1150 test_conversion_possible(r, "mandrill_512_q075.jpg", true); | 1206 test_conversion_possible(r, "mandrill_512_q075.jpg", true); |
| 1151 test_conversion_possible(r, "yellow_rose.png", true); | 1207 test_conversion_possible(r, "yellow_rose.png", true); |
| 1152 } | 1208 } |
| OLD | NEW |