Leon Scroggins III | 07a722c | 2018-01-16 15:01:17 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Ben Wagner | 1ebeefe | 2018-03-02 16:59:53 -0500 | [diff] [blame] | 8 | #include "Resources.h" |
Leon Scroggins III | 07a722c | 2018-01-16 15:01:17 -0500 | [diff] [blame] | 9 | #include "SkAndroidCodec.h" |
Ben Wagner | 1ebeefe | 2018-03-02 16:59:53 -0500 | [diff] [blame] | 10 | #include "SkBitmap.h" |
Leon Scroggins III | 07a722c | 2018-01-16 15:01:17 -0500 | [diff] [blame] | 11 | #include "SkCodec.h" |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 12 | #include "SkCodecImageGenerator.h" |
Ben Wagner | 1ebeefe | 2018-03-02 16:59:53 -0500 | [diff] [blame] | 13 | #include "SkColor.h" |
| 14 | #include "SkData.h" |
Leon Scroggins III | 07a722c | 2018-01-16 15:01:17 -0500 | [diff] [blame] | 15 | #include "SkEncodedImageFormat.h" |
Ben Wagner | 1ebeefe | 2018-03-02 16:59:53 -0500 | [diff] [blame] | 16 | #include "SkImageGenerator.h" |
| 17 | #include "SkImageInfo.h" |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 18 | #include "SkMatrix44.h" |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 19 | #include "SkPixmapPriv.h" |
Ben Wagner | 1ebeefe | 2018-03-02 16:59:53 -0500 | [diff] [blame] | 20 | #include "SkRefCnt.h" |
| 21 | #include "SkSize.h" |
| 22 | #include "SkString.h" |
| 23 | #include "SkTypes.h" |
Leon Scroggins III | 07a722c | 2018-01-16 15:01:17 -0500 | [diff] [blame] | 24 | #include "Test.h" |
| 25 | |
Ben Wagner | 1ebeefe | 2018-03-02 16:59:53 -0500 | [diff] [blame] | 26 | #include <algorithm> |
| 27 | #include <memory> |
| 28 | |
Leon Scroggins III | 07a722c | 2018-01-16 15:01:17 -0500 | [diff] [blame] | 29 | static SkISize times(const SkISize& size, float factor) { |
| 30 | return { (int) (size.width() * factor), (int) (size.height() * factor) }; |
| 31 | } |
| 32 | |
| 33 | static SkISize plus(const SkISize& size, int term) { |
| 34 | return { size.width() + term, size.height() + term }; |
| 35 | } |
| 36 | |
| 37 | static bool invalid(const SkISize& size) { |
| 38 | return size.width() < 1 || size.height() < 1; |
| 39 | } |
| 40 | |
| 41 | DEF_TEST(AndroidCodec_computeSampleSize, r) { |
| 42 | if (GetResourcePath().isEmpty()) { |
| 43 | return; |
| 44 | } |
| 45 | for (const char* file : { "images/color_wheel.webp", |
| 46 | "images/ship.png", |
| 47 | "images/dog.jpg", |
| 48 | "images/color_wheel.gif", |
| 49 | "images/rle.bmp", |
| 50 | "images/google_chrome.ico", |
| 51 | "images/mandrill.wbmp", |
| 52 | #ifdef SK_CODEC_DECODES_RAW |
| 53 | "images/sample_1mp.dng", |
| 54 | #endif |
| 55 | }) { |
| 56 | auto data = GetResourceAsData(file); |
| 57 | if (!data) { |
| 58 | ERRORF(r, "Could not get %s", file); |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | auto codec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(std::move(data))); |
| 63 | if (!codec) { |
| 64 | ERRORF(r, "Could not create codec for %s", file); |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | const auto dims = codec->getInfo().dimensions(); |
| 69 | const SkISize downscales[] = { |
| 70 | plus(dims, -1), |
| 71 | times(dims, .15f), |
| 72 | times(dims, .6f), |
| 73 | { (int32_t) (dims.width() * .25f), (int32_t) (dims.height() * .75f ) }, |
| 74 | { 1, 1 }, |
| 75 | { 1, 2 }, |
| 76 | { 2, 1 }, |
| 77 | { 0, -1 }, |
| 78 | { dims.width(), dims.height() - 1 }, |
| 79 | }; |
| 80 | for (SkISize size : downscales) { |
| 81 | const auto requested = size; |
| 82 | const int computedSampleSize = codec->computeSampleSize(&size); |
| 83 | REPORTER_ASSERT(r, size.width() >= 1 && size.height() >= 1); |
| 84 | if (codec->getEncodedFormat() == SkEncodedImageFormat::kWEBP) { |
| 85 | // WebP supports arbitrary down-scaling. |
| 86 | REPORTER_ASSERT(r, size == requested || invalid(requested)); |
| 87 | } else if (computedSampleSize == 1) { |
| 88 | REPORTER_ASSERT(r, size == dims); |
| 89 | } else { |
| 90 | REPORTER_ASSERT(r, computedSampleSize > 1); |
| 91 | if (size.width() >= dims.width() || size.height() >= dims.height()) { |
| 92 | ERRORF(r, "File %s's computed sample size (%i) is bigger than" |
| 93 | " original? original: %i x %i\tsampled: %i x %i", |
| 94 | file, computedSampleSize, dims.width(), dims.height(), |
| 95 | size.width(), size.height()); |
| 96 | } |
| 97 | REPORTER_ASSERT(r, size.width() >= requested.width() && |
| 98 | size.height() >= requested.height()); |
| 99 | REPORTER_ASSERT(r, size.width() < dims.width() && |
| 100 | size.height() < dims.height()); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | const SkISize upscales[] = { |
| 105 | dims, plus(dims, 5), times(dims, 2), |
| 106 | }; |
| 107 | for (SkISize size : upscales) { |
| 108 | const int computedSampleSize = codec->computeSampleSize(&size); |
| 109 | REPORTER_ASSERT(r, computedSampleSize == 1); |
| 110 | REPORTER_ASSERT(r, dims == size); |
| 111 | } |
| 112 | |
| 113 | // This mimics how Android's ImageDecoder uses SkAndroidCodec. A client |
| 114 | // can choose their dimensions based on calling getSampledDimensions, |
| 115 | // but the ImageDecoder API takes an arbitrary size. It then uses |
| 116 | // computeSampleSize to determine the best dimensions and sampleSize. |
| 117 | // It should return the same dimensions. the sampleSize may be different |
| 118 | // due to integer division. |
| 119 | for (int sampleSize : { 1, 2, 3, 4, 8, 16, 32 }) { |
| 120 | const SkISize sampledDims = codec->getSampledDimensions(sampleSize); |
| 121 | SkISize size = sampledDims; |
| 122 | const int computedSampleSize = codec->computeSampleSize(&size); |
| 123 | if (sampledDims != size) { |
| 124 | ERRORF(r, "File '%s'->getSampledDimensions(%i) yields computed" |
| 125 | " sample size of %i\n\tsampledDimensions: %i x %i\t" |
| 126 | "computed dimensions: %i x %i", |
| 127 | file, sampleSize, computedSampleSize, |
| 128 | sampledDims.width(), sampledDims.height(), |
| 129 | size.width(), size.height()); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 134 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 135 | DEF_TEST(AndroidCodec_wide, r) { |
| 136 | if (GetResourcePath().isEmpty()) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | const char* path = "images/wide-gamut.png"; |
| 141 | auto data = GetResourceAsData(path); |
| 142 | if (!data) { |
| 143 | ERRORF(r, "Missing file %s", path); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | auto codec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(std::move(data))); |
| 148 | if (!codec) { |
| 149 | ERRORF(r, "Failed to create codec from %s", path); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | auto info = codec->getInfo(); |
| 154 | auto cs = codec->computeOutputColorSpace(info.colorType(), nullptr); |
| 155 | if (!cs) { |
| 156 | ERRORF(r, "%s should have a color space", path); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | auto expected = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma, |
| 161 | SkColorSpace::kDCIP3_D65_Gamut); |
| 162 | REPORTER_ASSERT(r, SkColorSpace::Equals(cs.get(), expected.get())); |
| 163 | } |
| 164 | |
| 165 | DEF_TEST(AndroidCodec_P3, r) { |
| 166 | if (GetResourcePath().isEmpty()) { |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | const char* path = "images/purple-displayprofile.png"; |
| 171 | auto data = GetResourceAsData(path); |
| 172 | if (!data) { |
| 173 | ERRORF(r, "Missing file %s", path); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | auto codec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(std::move(data))); |
| 178 | if (!codec) { |
| 179 | ERRORF(r, "Failed to create codec from %s", path); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | auto info = codec->getInfo(); |
| 184 | auto cs = codec->computeOutputColorSpace(info.colorType(), nullptr); |
| 185 | if (!cs) { |
| 186 | ERRORF(r, "%s should have a color space", path); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | REPORTER_ASSERT(r, !cs->isSRGB()); |
| 191 | REPORTER_ASSERT(r, cs->gammaCloseToSRGB()); |
| 192 | |
| 193 | const SkMatrix44* matrix = cs->toXYZD50(); |
| 194 | SkMatrix44 expected(SkMatrix44::kUninitialized_Constructor); |
| 195 | static constexpr float kExpected[] = { |
| 196 | 0.426254272f, 0.369018555f, 0.168914795f, |
| 197 | 0.226013184f, 0.685974121f, 0.0880126953f, |
| 198 | 0.0116729736f, 0.0950927734f, 0.71812439f, |
| 199 | }; |
| 200 | |
| 201 | expected.set3x3RowMajorf(kExpected); |
| 202 | REPORTER_ASSERT(r, *matrix == expected); |
| 203 | } |
| 204 | |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 205 | DEF_TEST(AndroidCodec_orientation, r) { |
| 206 | if (GetResourcePath().isEmpty()) { |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | for (const char* ext : { "jpg", "webp" }) |
| 211 | for (char i = '1'; i <= '8'; ++i) { |
| 212 | SkString path = SkStringPrintf("images/orientation/%c.%s", i, ext); |
| 213 | auto data = GetResourceAsData(path.c_str()); |
| 214 | auto gen = SkCodecImageGenerator::MakeFromEncodedCodec(data); |
| 215 | if (!gen) { |
| 216 | ERRORF(r, "failed to decode %s", path.c_str()); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | // Dimensions after adjusting for the origin. |
| 221 | const SkISize expectedDims = { 100, 80 }; |
| 222 | |
| 223 | // SkCodecImageGenerator automatically adjusts for the origin. |
| 224 | REPORTER_ASSERT(r, gen->getInfo().dimensions() == expectedDims); |
| 225 | |
| 226 | auto androidCodec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(data)); |
| 227 | if (!androidCodec) { |
| 228 | ERRORF(r, "failed to decode %s", path.c_str()); |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | // SkAndroidCodec does not adjust for the origin by default. Dimensions may be reversed. |
| 233 | if (SkPixmapPriv::ShouldSwapWidthHeight(androidCodec->codec()->getOrigin())) { |
| 234 | auto swappedDims = SkPixmapPriv::SwapWidthHeight(androidCodec->getInfo()).dimensions(); |
| 235 | REPORTER_ASSERT(r, expectedDims == swappedDims); |
| 236 | } else { |
| 237 | REPORTER_ASSERT(r, expectedDims == androidCodec->getInfo().dimensions()); |
| 238 | } |
| 239 | |
| 240 | // Passing kRespect adjusts for the origin. |
| 241 | androidCodec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(std::move(data)), |
| 242 | SkAndroidCodec::ExifOrientationBehavior::kRespect); |
| 243 | auto info = androidCodec->getInfo(); |
| 244 | REPORTER_ASSERT(r, info.dimensions() == expectedDims); |
| 245 | |
| 246 | SkBitmap fromGenerator; |
| 247 | fromGenerator.allocPixels(info); |
| 248 | REPORTER_ASSERT(r, gen->getPixels(info, fromGenerator.getPixels(), |
| 249 | fromGenerator.rowBytes())); |
| 250 | |
| 251 | SkBitmap fromAndroidCodec; |
| 252 | fromAndroidCodec.allocPixels(info); |
| 253 | auto result = androidCodec->getPixels(info, fromAndroidCodec.getPixels(), |
| 254 | fromAndroidCodec.rowBytes()); |
| 255 | REPORTER_ASSERT(r, result == SkCodec::kSuccess); |
| 256 | |
| 257 | for (int i = 0; i < info.width(); ++i) |
| 258 | for (int j = 0; j < info.height(); ++j) { |
| 259 | SkColor c1 = *fromGenerator .getAddr32(i, j); |
| 260 | SkColor c2 = *fromAndroidCodec.getAddr32(i, j); |
| 261 | if (c1 != c2) { |
| 262 | ERRORF(r, "Bitmaps for %s do not match starting at position %i, %i\n" |
| 263 | "\tfromGenerator: %x\tfromAndroidCodec: %x", path.c_str(), i, j, |
| 264 | c1, c2); |
| 265 | return; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |