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

Side by Side Diff: tests/CodecTest.cpp

Issue 2325223002: Support RGBA/BGRA Premul/Unpremul from SkPNGImageEncoder (Closed)
Patch Set: 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
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 1056 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 bm1.allocPixels(info);
1102 SkCodec::Result result = origCodec->getPixels(info, bm1.getPixels(), bm1.row Bytes());
1103 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1080 1104
1081 // Encode the image to png. 1105 // Encode the image to png.
1082 sk_sp<SkData> data = 1106 sk_sp<SkData> data =
1083 sk_sp<SkData>(SkImageEncoder::EncodeData(bm1, SkImageEncoder::kPNG_T ype, 100)); 1107 sk_sp<SkData>(SkImageEncoder::EncodeData(bm1, SkImageEncoder::kPNG_T ype, 100));
1084 1108
1085 // Prepare to decode. The codec should recognize that the PNG is 565.
1086 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data.get())); 1109 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(data.get()));
1087 REPORTER_ASSERT(r, origColorType == codec->getInfo().colorType()); 1110 REPORTER_ASSERT(r, color_type_match(info.colorType(), codec->getInfo().color Type()));
1088 REPORTER_ASSERT(r, origAlphaType == codec->getInfo().alphaType()); 1111 REPORTER_ASSERT(r, alpha_type_match(info.alphaType(), codec->getInfo().alpha Type()));
1089 1112
1090 SkBitmap bm2; 1113 SkBitmap bm2;
1091 bm2.allocPixels(codec->getInfo()); 1114 bm2.allocPixels(info);
1092 SkCodec::Result result = codec->getPixels(codec->getInfo(), bm2.getPixels(), bm2.rowBytes()); 1115 result = codec->getPixels(info, bm2.getPixels(), bm2.rowBytes());
1093 REPORTER_ASSERT(r, SkCodec::kSuccess == result); 1116 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1094 1117
1095 SkMD5::Digest d1, d2; 1118 SkMD5::Digest d1, d2;
1096 md5(bm1, &d1); 1119 md5(bm1, &d1);
1097 md5(bm2, &d2); 1120 md5(bm2, &d2);
1098 REPORTER_ASSERT(r, d1 == d2); 1121 REPORTER_ASSERT(r, d1 == d2);
1099 } 1122 }
1100 1123
1101 DEF_TEST(Codec_PngRoundTrip, r) { 1124 DEF_TEST(Codec_PngRoundTrip, r) {
1102 // Create an arbitrary 565 bitmap.
1103 const char* path = "mandrill_512_q075.jpg"; 1125 const char* path = "mandrill_512_q075.jpg";
1104 SkAutoTDelete<SkStream> stream(resource(path)); 1126 SkAutoTDelete<SkStream> stream(resource(path));
1105 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); 1127 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
1128
1129 // Test 565
1106 SkImageInfo info565 = codec->getInfo().makeColorType(kRGB_565_SkColorType); 1130 SkImageInfo info565 = codec->getInfo().makeColorType(kRGB_565_SkColorType);
1107 SkBitmap bm1; 1131 check_round_trip(r, codec.get(), info565);
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 1132
1113 // Create an arbitrary gray bitmap. 1133 // Test RGBA opaque
1134 SkImageInfo infoRGBA = codec->getInfo().makeColorType(kRGBA_8888_SkColorType );
1135 check_round_trip(r, codec.get(), infoRGBA);
1136
1137 // Test BGRA opaque
1138 SkImageInfo infoBGRA = codec->getInfo().makeColorType(kBGRA_8888_SkColorType );
1139 check_round_trip(r, codec.get(), infoBGRA);
1140
1141 // Test Gray
1114 path = "grayscale.jpg"; 1142 path = "grayscale.jpg";
1115 stream.reset(resource(path)); 1143 stream.reset(resource(path));
1116 codec.reset(SkCodec::NewFromStream(stream.release())); 1144 codec.reset(SkCodec::NewFromStream(stream.release()));
1117 SkBitmap bm2; 1145 check_round_trip(r, codec.get(), codec->getInfo());
1118 bm2.allocPixels(codec->getInfo()); 1146
1119 result = codec->getPixels(codec->getInfo(), bm2.getPixels(), bm2.rowBytes()) ; 1147 path = "arrow.png";
1120 REPORTER_ASSERT(r, SkCodec::kSuccess == result); 1148 stream.reset(resource(path));
1121 check_round_trip(r, bm2); 1149 codec.reset(SkCodec::NewFromStream(stream.release()));
1150
1151 // Test RGBA/BGRA with alpha
1152 SkImageInfo infoRGBAPremul = codec->getInfo().makeColorType(kRGBA_8888_SkCol orType)
1153 .makeAlphaType(kPremul_SkAlphaT ype)
1154 .makeColorSpace(nullptr);
1155 check_round_trip(r, codec.get(), infoRGBAPremul);
1156 SkImageInfo infoBGRAPremul = codec->getInfo().makeColorType(kBGRA_8888_SkCol orType)
1157 .makeAlphaType(kPremul_SkAlphaT ype)
1158 .makeColorSpace(nullptr);
1159 check_round_trip(r, codec.get(), infoBGRAPremul);
1160 SkImageInfo infoRGBAUnpremul = codec->getInfo().makeColorType(kRGBA_8888_SkC olorType)
1161 .makeAlphaType(kUnpremul_SkAl phaType)
1162 .makeColorSpace(nullptr);
1163 check_round_trip(r, codec.get(), infoRGBAUnpremul);
1164 SkImageInfo infoBGRAUnpremul = codec->getInfo().makeColorType(kBGRA_8888_SkC olorType)
1165 .makeAlphaType(kUnpremul_SkAl phaType)
1166 .makeColorSpace(nullptr);
1167 check_round_trip(r, codec.get(), infoBGRAUnpremul);
1122 } 1168 }
1123 1169
1124 static void test_conversion_possible(skiatest::Reporter* r, const char* path, 1170 static void test_conversion_possible(skiatest::Reporter* r, const char* path,
1125 bool testScanlineDecoder) { 1171 bool testScanlineDecoder) {
1126 SkAutoTDelete<SkStream> stream(resource(path)); 1172 SkAutoTDelete<SkStream> stream(resource(path));
1127 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); 1173 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
1128 SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType); 1174 SkImageInfo infoF16 = codec->getInfo().makeColorType(kRGBA_F16_SkColorType);
1129 1175
1130 SkBitmap bm; 1176 SkBitmap bm;
1131 bm.allocPixels(infoF16); 1177 bm.allocPixels(infoF16);
(...skipping 11 matching lines...) Expand all
1143 result = codec->startScanlineDecode(infoF16); 1189 result = codec->startScanlineDecode(infoF16);
1144 REPORTER_ASSERT(r, SkCodec::kSuccess == result); 1190 REPORTER_ASSERT(r, SkCodec::kSuccess == result);
1145 } 1191 }
1146 } 1192 }
1147 1193
1148 DEF_TEST(Codec_F16ConversionPossible, r) { 1194 DEF_TEST(Codec_F16ConversionPossible, r) {
1149 test_conversion_possible(r, "color_wheel.webp", false); 1195 test_conversion_possible(r, "color_wheel.webp", false);
1150 test_conversion_possible(r, "mandrill_512_q075.jpg", true); 1196 test_conversion_possible(r, "mandrill_512_q075.jpg", true);
1151 test_conversion_possible(r, "yellow_rose.png", true); 1197 test_conversion_possible(r, "yellow_rose.png", true);
1152 } 1198 }
OLDNEW
« src/images/SkPNGImageEncoder.cpp ('K') | « src/images/transform_scanline.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698