Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(907)

Side by Side Diff: tests/CodecTest.cpp

Issue 2325223002: Support RGBA/BGRA Premul/Unpremul from SkPNGImageEncoder (Closed)
Patch Set: Rebase Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/images/transform_scanline.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 1055 matching lines...) Expand 10 before | Expand all | Expand 10 after
1066 SkAutoMalloc pixelStorage(dstInfo.getSafeSize(rowBytes)); 1066 SkAutoMalloc pixelStorage(dstInfo.getSafeSize(rowBytes));
1067 SkCodec::Result result = codec->getAndroidPixels(dstInfo, pixelStorage.get() , rowBytes, &opts); 1067 SkCodec::Result result = codec->getAndroidPixels(dstInfo, pixelStorage.get() , rowBytes, &opts);
1068 REPORTER_ASSERT(r, SkCodec::kSuccess == result); 1068 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1069 } 1069 }
1070 1070
1071 DEF_TEST(Codec_ColorXform, r) { 1071 DEF_TEST(Codec_ColorXform, r) {
1072 check_color_xform(r, "mandrill_512_q075.jpg"); 1072 check_color_xform(r, "mandrill_512_q075.jpg");
1073 check_color_xform(r, "mandrill_512.png"); 1073 check_color_xform(r, "mandrill_512.png");
1074 } 1074 }
1075 1075
1076 static void check_round_trip(skiatest::Reporter* r, const SkBitmap& bm1) { 1076 static bool color_type_match(SkColorType origColorType, SkColorType codecColorTy pe) {
1077 SkColorType origColorType = bm1.colorType(); 1077 switch (origColorType) {
1078 SkAlphaType origAlphaType = bm1.alphaType(); 1078 case kRGBA_8888_SkColorType:
1079 case kBGRA_8888_SkColorType:
1080 return kRGBA_8888_SkColorType == codecColorType ||
1081 kBGRA_8888_SkColorType == codecColorType;
1082 default:
1083 return origColorType == codecColorType;
1084 }
1085 }
1086
1087 static bool alpha_type_match(SkAlphaType origAlphaType, SkAlphaType codecAlphaTy pe) {
1088 switch (origAlphaType) {
1089 case kUnpremul_SkAlphaType:
1090 case kPremul_SkAlphaType:
1091 return kUnpremul_SkAlphaType == codecAlphaType ||
1092 kPremul_SkAlphaType == codecAlphaType;
1093 default:
1094 return origAlphaType == codecAlphaType;
1095 }
1096 }
1097
1098 static void check_round_trip(skiatest::Reporter* r, SkCodec* origCodec, const Sk ImageInfo& info) {
1099 SkBitmap bm1;
1100 SkPMColor colors[256];
1101 SkAutoTUnref<SkColorTable> colorTable1(new SkColorTable(colors, 256));
1102 bm1.allocPixels(info, nullptr, colorTable1.get());
1103 int numColors;
1104 SkCodec::Result result = origCodec->getPixels(info, bm1.getPixels(), bm1.row Bytes(), nullptr,
1105 const_cast<SkPMColor*>(colorTa ble1->readColors()),
1106 &numColors);
1107 // This will fail to update colorTable1->count() but is fine for the purpose of this test.
1108 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1079 1109
1080 // Encode the image to png. 1110 // Encode the image to png.
1081 sk_sp<SkData> data = 1111 sk_sp<SkData> data =
1082 sk_sp<SkData>(SkImageEncoder::EncodeData(bm1, SkImageEncoder::kPNG_T ype, 100)); 1112 sk_sp<SkData>(SkImageEncoder::EncodeData(bm1, SkImageEncoder::kPNG_T ype, 100));
1083 1113
1084 // Prepare to decode. The codec should recognize that the PNG is 565.
1085 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data)); 1114 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data));
1086 REPORTER_ASSERT(r, origColorType == codec->getInfo().colorType()); 1115 REPORTER_ASSERT(r, color_type_match(info.colorType(), codec->getInfo().color Type()));
1087 REPORTER_ASSERT(r, origAlphaType == codec->getInfo().alphaType()); 1116 REPORTER_ASSERT(r, alpha_type_match(info.alphaType(), codec->getInfo().alpha Type()));
1088 1117
1089 SkBitmap bm2; 1118 SkBitmap bm2;
1090 bm2.allocPixels(codec->getInfo()); 1119 SkAutoTUnref<SkColorTable> colorTable2(new SkColorTable(colors, 256));
1091 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);
1092 REPORTER_ASSERT(r, SkCodec::kSuccess == result); 1123 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1093 1124
1094 SkMD5::Digest d1, d2; 1125 SkMD5::Digest d1, d2;
1095 md5(bm1, &d1); 1126 md5(bm1, &d1);
1096 md5(bm2, &d2); 1127 md5(bm2, &d2);
1097 REPORTER_ASSERT(r, d1 == d2); 1128 REPORTER_ASSERT(r, d1 == d2);
1098 } 1129 }
1099 1130
1100 DEF_TEST(Codec_PngRoundTrip, r) { 1131 DEF_TEST(Codec_PngRoundTrip, r) {
1101 // Create an arbitrary 565 bitmap.
1102 const char* path = "mandrill_512_q075.jpg"; 1132 const char* path = "mandrill_512_q075.jpg";
1103 SkAutoTDelete<SkStream> stream(resource(path)); 1133 SkAutoTDelete<SkStream> stream(resource(path));
1104 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); 1134 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
1105 SkImageInfo info565 = codec->getInfo().makeColorType(kRGB_565_SkColorType);
1106 SkBitmap bm1;
1107 bm1.allocPixels(info565);
1108 SkCodec::Result result = codec->getPixels(info565, bm1.getPixels(), bm1.rowB ytes());
1109 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1110 check_round_trip(r, bm1);
1111 1135
1112 // 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
1113 path = "grayscale.jpg"; 1144 path = "grayscale.jpg";
1114 stream.reset(resource(path)); 1145 stream.reset(resource(path));
1115 codec.reset(SkCodec::NewFromStream(stream.release())); 1146 codec.reset(SkCodec::NewFromStream(stream.release()));
1116 SkBitmap bm2; 1147 check_round_trip(r, codec.get(), codec->getInfo());
1117 bm2.allocPixels(codec->getInfo()); 1148
1118 result = codec->getPixels(codec->getInfo(), bm2.getPixels(), bm2.rowBytes()) ; 1149 path = "yellow_rose.png";
1119 REPORTER_ASSERT(r, SkCodec::kSuccess == result); 1150 stream.reset(resource(path));
1120 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 }
1121 } 1178 }
1122 1179
1123 static void test_conversion_possible(skiatest::Reporter* r, const char* path, 1180 static void test_conversion_possible(skiatest::Reporter* r, const char* path,
1124 bool testScanlineDecoder) { 1181 bool testScanlineDecoder) {
1125 SkAutoTDelete<SkStream> stream(resource(path)); 1182 SkAutoTDelete<SkStream> stream(resource(path));
1126 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); 1183 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
1127 SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType); 1184 SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType);
1128 1185
1129 SkBitmap bm; 1186 SkBitmap bm;
1130 bm.allocPixels(infoF16); 1187 bm.allocPixels(infoF16);
(...skipping 11 matching lines...) Expand all
1142 result = codec->startScanlineDecode(infoF16); 1199 result = codec->startScanlineDecode(infoF16);
1143 REPORTER_ASSERT(r, SkCodec::kSuccess == result); 1200 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1144 } 1201 }
1145 } 1202 }
1146 1203
1147 DEF_TEST(Codec_F16ConversionPossible, r) { 1204 DEF_TEST(Codec_F16ConversionPossible, r) {
1148 test_conversion_possible(r, "color_wheel.webp", false); 1205 test_conversion_possible(r, "color_wheel.webp", false);
1149 test_conversion_possible(r, "mandrill_512_q075.jpg", true); 1206 test_conversion_possible(r, "mandrill_512_q075.jpg", true);
1150 test_conversion_possible(r, "yellow_rose.png", true); 1207 test_conversion_possible(r, "yellow_rose.png", true);
1151 } 1208 }
OLDNEW
« no previous file with comments | « src/images/transform_scanline.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698