msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
| 8 | #include "SkAndroidCodec.h" |
Nigel Tao | 612a65d | 2018-11-09 10:10:31 +1100 | [diff] [blame] | 9 | #include "SkAndroidCodecAdapter.h" |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 10 | #include "SkCodec.h" |
| 11 | #include "SkCodecPriv.h" |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 12 | #include "SkMakeUnique.h" |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 13 | #include "SkPixmap.h" |
| 14 | #include "SkPixmapPriv.h" |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 15 | #include "SkSampledCodec.h" |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 16 | |
| 17 | static bool is_valid_sample_size(int sampleSize) { |
| 18 | // FIXME: As Leon has mentioned elsewhere, surely there is also a maximum sampleSize? |
| 19 | return sampleSize > 0; |
| 20 | } |
| 21 | |
Matt Sarett | 2ae3a2e | 2017-02-15 08:48:02 -0500 | [diff] [blame] | 22 | /** |
| 23 | * Loads the gamut as a set of three points (triangle). |
| 24 | */ |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 25 | static void load_gamut(SkPoint rgb[], const skcms_Matrix3x3& xyz) { |
Matt Sarett | 2ae3a2e | 2017-02-15 08:48:02 -0500 | [diff] [blame] | 26 | // rx = rX / (rX + rY + rZ) |
| 27 | // ry = rY / (rX + rY + rZ) |
| 28 | // gx, gy, bx, and gy are calulcated similarly. |
nagarajan.n | 6e17b6f | 2017-07-10 15:02:00 +0530 | [diff] [blame] | 29 | for (int rgbIdx = 0; rgbIdx < 3; rgbIdx++) { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 30 | float sum = xyz.vals[rgbIdx][0] + xyz.vals[rgbIdx][1] + xyz.vals[rgbIdx][2]; |
| 31 | rgb[rgbIdx].fX = xyz.vals[rgbIdx][0] / sum; |
| 32 | rgb[rgbIdx].fY = xyz.vals[rgbIdx][1] / sum; |
nagarajan.n | 6e17b6f | 2017-07-10 15:02:00 +0530 | [diff] [blame] | 33 | } |
Matt Sarett | 2ae3a2e | 2017-02-15 08:48:02 -0500 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Calculates the area of the triangular gamut. |
| 38 | */ |
| 39 | static float calculate_area(SkPoint abc[]) { |
| 40 | SkPoint a = abc[0]; |
| 41 | SkPoint b = abc[1]; |
| 42 | SkPoint c = abc[2]; |
| 43 | return 0.5f * SkTAbs(a.fX*b.fY + b.fX*c.fY - a.fX*c.fY - c.fX*b.fY - b.fX*a.fY); |
| 44 | } |
| 45 | |
Leon Scroggins III | 862c196 | 2017-10-02 16:28:49 -0400 | [diff] [blame] | 46 | static constexpr float kSRGB_D50_GamutArea = 0.084f; |
Matt Sarett | 2ae3a2e | 2017-02-15 08:48:02 -0500 | [diff] [blame] | 47 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 48 | static bool is_wide_gamut(const skcms_ICCProfile& profile) { |
Matt Sarett | 2ae3a2e | 2017-02-15 08:48:02 -0500 | [diff] [blame] | 49 | // Determine if the source image has a gamut that is wider than sRGB. If so, we |
| 50 | // will use P3 as the output color space to avoid clipping the gamut. |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 51 | if (profile.has_toXYZD50) { |
Matt Sarett | 2ae3a2e | 2017-02-15 08:48:02 -0500 | [diff] [blame] | 52 | SkPoint rgb[3]; |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 53 | load_gamut(rgb, profile.toXYZD50); |
Matt Sarett | 2ae3a2e | 2017-02-15 08:48:02 -0500 | [diff] [blame] | 54 | return calculate_area(rgb) > kSRGB_D50_GamutArea; |
| 55 | } |
| 56 | |
| 57 | return false; |
| 58 | } |
| 59 | |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 60 | static inline SkImageInfo adjust_info(SkCodec* codec, |
| 61 | SkAndroidCodec::ExifOrientationBehavior orientationBehavior) { |
| 62 | auto info = codec->getInfo(); |
| 63 | if (orientationBehavior == SkAndroidCodec::ExifOrientationBehavior::kIgnore |
| 64 | || !SkPixmapPriv::ShouldSwapWidthHeight(codec->getOrigin())) { |
| 65 | return info; |
| 66 | } |
| 67 | return SkPixmapPriv::SwapWidthHeight(info); |
| 68 | } |
| 69 | |
| 70 | SkAndroidCodec::SkAndroidCodec(SkCodec* codec, ExifOrientationBehavior orientationBehavior) |
| 71 | : fInfo(adjust_info(codec, orientationBehavior)) |
| 72 | , fOrientationBehavior(orientationBehavior) |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 73 | , fCodec(codec) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 74 | {} |
| 75 | |
Leon Scroggins III | 0741818 | 2017-08-15 12:24:02 -0400 | [diff] [blame] | 76 | SkAndroidCodec::~SkAndroidCodec() {} |
| 77 | |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 78 | std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromStream(std::unique_ptr<SkStream> stream, |
| 79 | SkPngChunkReader* chunkReader) { |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 80 | auto codec = SkCodec::MakeFromStream(std::move(stream), nullptr, chunkReader); |
Leon Scroggins III | 7397d7a | 2018-01-04 13:26:30 -0500 | [diff] [blame] | 81 | return MakeFromCodec(std::move(codec)); |
| 82 | } |
| 83 | |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 84 | std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromCodec(std::unique_ptr<SkCodec> codec, |
| 85 | ExifOrientationBehavior orientationBehavior) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 86 | if (nullptr == codec) { |
| 87 | return nullptr; |
| 88 | } |
| 89 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 90 | switch ((SkEncodedImageFormat)codec->getEncodedFormat()) { |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 91 | case SkEncodedImageFormat::kPNG: |
| 92 | case SkEncodedImageFormat::kICO: |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 93 | case SkEncodedImageFormat::kJPEG: |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 94 | case SkEncodedImageFormat::kGIF: |
| 95 | case SkEncodedImageFormat::kBMP: |
| 96 | case SkEncodedImageFormat::kWBMP: |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 97 | case SkEncodedImageFormat::kHEIF: |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 98 | return skstd::make_unique<SkSampledCodec>(codec.release(), orientationBehavior); |
Nigel Tao | 612a65d | 2018-11-09 10:10:31 +1100 | [diff] [blame] | 99 | |
msarett | ad3a5c6 | 2016-05-06 07:21:26 -0700 | [diff] [blame] | 100 | #ifdef SK_HAS_WEBP_LIBRARY |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 101 | case SkEncodedImageFormat::kWEBP: |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 102 | #endif |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 103 | #ifdef SK_CODEC_DECODES_RAW |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 104 | case SkEncodedImageFormat::kDNG: |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 105 | #endif |
Nigel Tao | 612a65d | 2018-11-09 10:10:31 +1100 | [diff] [blame] | 106 | #if defined(SK_HAS_WEBP_LIBRARY) || defined(SK_CODEC_DECODES_RAW) |
| 107 | return skstd::make_unique<SkAndroidCodecAdapter>(codec.release(), orientationBehavior); |
| 108 | #endif |
| 109 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 110 | default: |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 111 | return nullptr; |
| 112 | } |
| 113 | } |
| 114 | |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 115 | std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromData(sk_sp<SkData> data, |
| 116 | SkPngChunkReader* chunkReader) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 117 | if (!data) { |
| 118 | return nullptr; |
| 119 | } |
| 120 | |
Mike Reed | 847068c | 2017-07-26 11:35:53 -0400 | [diff] [blame] | 121 | return MakeFromStream(SkMemoryStream::Make(std::move(data)), chunkReader); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 122 | } |
| 123 | |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 124 | SkColorType SkAndroidCodec::computeOutputColorType(SkColorType requestedColorType) { |
Leon Scroggins | 1793e7b | 2017-12-05 15:38:14 +0000 | [diff] [blame] | 125 | bool highPrecision = fCodec->getEncodedInfo().bitsPerComponent() > 8; |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 126 | switch (requestedColorType) { |
| 127 | case kARGB_4444_SkColorType: |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 128 | return kN32_SkColorType; |
Matt Sarett | 8dcc84f | 2016-12-14 10:23:41 -0500 | [diff] [blame] | 129 | case kN32_SkColorType: |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 130 | break; |
| 131 | case kAlpha_8_SkColorType: |
| 132 | // Fall through to kGray_8. Before kGray_8_SkColorType existed, |
| 133 | // we allowed clients to request kAlpha_8 when they wanted a |
| 134 | // grayscale decode. |
| 135 | case kGray_8_SkColorType: |
Matt Sarett | 8db74f1 | 2017-06-14 13:02:05 +0000 | [diff] [blame] | 136 | if (kGray_8_SkColorType == this->getInfo().colorType()) { |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 137 | return kGray_8_SkColorType; |
| 138 | } |
| 139 | break; |
| 140 | case kRGB_565_SkColorType: |
| 141 | if (kOpaque_SkAlphaType == this->getInfo().alphaType()) { |
| 142 | return kRGB_565_SkColorType; |
| 143 | } |
| 144 | break; |
Matt Sarett | 8dcc84f | 2016-12-14 10:23:41 -0500 | [diff] [blame] | 145 | case kRGBA_F16_SkColorType: |
| 146 | return kRGBA_F16_SkColorType; |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 147 | default: |
| 148 | break; |
| 149 | } |
| 150 | |
Leon Scroggins | 1793e7b | 2017-12-05 15:38:14 +0000 | [diff] [blame] | 151 | // F16 is the Android default for high precision images. |
| 152 | return highPrecision ? kRGBA_F16_SkColorType : kN32_SkColorType; |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | SkAlphaType SkAndroidCodec::computeOutputAlphaType(bool requestedUnpremul) { |
| 156 | if (kOpaque_SkAlphaType == this->getInfo().alphaType()) { |
| 157 | return kOpaque_SkAlphaType; |
| 158 | } |
| 159 | return requestedUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType; |
| 160 | } |
| 161 | |
Matt Sarett | 68feef4 | 2017-04-11 09:51:32 -0400 | [diff] [blame] | 162 | sk_sp<SkColorSpace> SkAndroidCodec::computeOutputColorSpace(SkColorType outputColorType, |
| 163 | sk_sp<SkColorSpace> prefColorSpace) { |
Matt Sarett | 966bb34 | 2016-12-12 16:30:13 -0500 | [diff] [blame] | 164 | switch (outputColorType) { |
Derek Sollenberger | 1be431f | 2019-01-30 11:21:33 -0500 | [diff] [blame] | 165 | #ifdef SK_SUPPORT_LEGACY_ANDROID_CODEC_COLORSPACE |
| 166 | case kRGBA_F16_SkColorType: |
| 167 | // Note that |prefColorSpace| is ignored, F16 is always linear sRGB. |
| 168 | return SkColorSpace::MakeSRGBLinear(); |
| 169 | case kRGB_565_SkColorType: |
| 170 | // Note that |prefColorSpace| is ignored, 565 is always sRGB. |
| 171 | return SkColorSpace::MakeSRGB(); |
| 172 | #else |
| 173 | case kRGBA_F16_SkColorType: |
| 174 | case kRGB_565_SkColorType: |
| 175 | #endif |
Leon Scroggins III | c20b5f8 | 2017-07-13 08:05:29 -0400 | [diff] [blame] | 176 | case kRGBA_8888_SkColorType: |
| 177 | case kBGRA_8888_SkColorType: { |
Brian Osman | 82ebe04 | 2019-01-04 17:03:00 -0500 | [diff] [blame] | 178 | // If |prefColorSpace| is supplied, choose it. |
| 179 | if (prefColorSpace) { |
Matt Sarett | 68feef4 | 2017-04-11 09:51:32 -0400 | [diff] [blame] | 180 | return prefColorSpace; |
| 181 | } |
| 182 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 183 | const skcms_ICCProfile* encodedProfile = fCodec->getEncodedInfo().profile(); |
Leon Scroggins III | 227d4e1 | 2018-09-27 08:30:22 -0400 | [diff] [blame] | 184 | if (encodedProfile) { |
| 185 | if (auto encodedSpace = SkColorSpace::Make(*encodedProfile)) { |
| 186 | // Leave the pixels in the encoded color space. Color space conversion |
| 187 | // will be handled after decode time. |
| 188 | return encodedSpace; |
| 189 | } |
Matt Sarett | 9341c98 | 2017-02-28 15:36:42 -0500 | [diff] [blame] | 190 | |
Leon Scroggins III | 227d4e1 | 2018-09-27 08:30:22 -0400 | [diff] [blame] | 191 | if (is_wide_gamut(*encodedProfile)) { |
Brian Osman | 82ebe04 | 2019-01-04 17:03:00 -0500 | [diff] [blame] | 192 | return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDCIP3); |
Leon Scroggins III | 227d4e1 | 2018-09-27 08:30:22 -0400 | [diff] [blame] | 193 | } |
Matt Sarett | 2ae3a2e | 2017-02-15 08:48:02 -0500 | [diff] [blame] | 194 | } |
| 195 | |
Matt Sarett | cf3f234 | 2017-03-23 15:32:25 -0400 | [diff] [blame] | 196 | return SkColorSpace::MakeSRGB(); |
Matt Sarett | 9341c98 | 2017-02-28 15:36:42 -0500 | [diff] [blame] | 197 | } |
Matt Sarett | 966bb34 | 2016-12-12 16:30:13 -0500 | [diff] [blame] | 198 | default: |
Matt Sarett | 3725f0a | 2017-03-28 14:34:20 -0400 | [diff] [blame] | 199 | // Color correction not supported for kGray. |
Matt Sarett | 966bb34 | 2016-12-12 16:30:13 -0500 | [diff] [blame] | 200 | return nullptr; |
| 201 | } |
| 202 | } |
| 203 | |
Leon Scroggins III | 07a722c | 2018-01-16 15:01:17 -0500 | [diff] [blame] | 204 | static bool supports_any_down_scale(const SkCodec* codec) { |
| 205 | return codec->getEncodedFormat() == SkEncodedImageFormat::kWEBP; |
| 206 | } |
| 207 | |
| 208 | // There are a variety of ways two SkISizes could be compared. This method |
| 209 | // returns true if either dimensions of a is < that of b. |
| 210 | // computeSampleSize also uses the opposite, which means that both |
| 211 | // dimensions of a >= b. |
| 212 | static inline bool smaller_than(const SkISize& a, const SkISize& b) { |
| 213 | return a.width() < b.width() || a.height() < b.height(); |
| 214 | } |
| 215 | |
| 216 | // Both dimensions of a > that of b. |
| 217 | static inline bool strictly_bigger_than(const SkISize& a, const SkISize& b) { |
| 218 | return a.width() > b.width() && a.height() > b.height(); |
| 219 | } |
| 220 | |
| 221 | int SkAndroidCodec::computeSampleSize(SkISize* desiredSize) const { |
| 222 | SkASSERT(desiredSize); |
| 223 | |
| 224 | if (!desiredSize || *desiredSize == fInfo.dimensions()) { |
| 225 | return 1; |
| 226 | } |
| 227 | |
| 228 | if (smaller_than(fInfo.dimensions(), *desiredSize)) { |
| 229 | *desiredSize = fInfo.dimensions(); |
| 230 | return 1; |
| 231 | } |
| 232 | |
| 233 | // Handle bad input: |
| 234 | if (desiredSize->width() < 1 || desiredSize->height() < 1) { |
| 235 | *desiredSize = SkISize::Make(std::max(1, desiredSize->width()), |
| 236 | std::max(1, desiredSize->height())); |
| 237 | } |
| 238 | |
| 239 | if (supports_any_down_scale(fCodec.get())) { |
| 240 | return 1; |
| 241 | } |
| 242 | |
| 243 | int sampleX = fInfo.width() / desiredSize->width(); |
| 244 | int sampleY = fInfo.height() / desiredSize->height(); |
| 245 | int sampleSize = std::min(sampleX, sampleY); |
| 246 | auto computedSize = this->getSampledDimensions(sampleSize); |
| 247 | if (computedSize == *desiredSize) { |
| 248 | return sampleSize; |
| 249 | } |
| 250 | |
| 251 | if (computedSize == fInfo.dimensions() || sampleSize == 1) { |
| 252 | // Cannot downscale |
| 253 | *desiredSize = computedSize; |
| 254 | return 1; |
| 255 | } |
| 256 | |
| 257 | if (strictly_bigger_than(computedSize, *desiredSize)) { |
| 258 | // See if there is a tighter fit. |
| 259 | while (true) { |
| 260 | auto smaller = this->getSampledDimensions(sampleSize + 1); |
| 261 | if (smaller == *desiredSize) { |
| 262 | return sampleSize + 1; |
| 263 | } |
| 264 | if (smaller == computedSize || smaller_than(smaller, *desiredSize)) { |
| 265 | // Cannot get any smaller without being smaller than desired. |
| 266 | *desiredSize = computedSize; |
| 267 | return sampleSize; |
| 268 | } |
| 269 | |
| 270 | sampleSize++; |
| 271 | computedSize = smaller; |
| 272 | } |
| 273 | |
| 274 | SkASSERT(false); |
| 275 | } |
| 276 | |
| 277 | if (!smaller_than(computedSize, *desiredSize)) { |
| 278 | // This means one of the computed dimensions is equal to desired, and |
| 279 | // the other is bigger. This is as close as we can get. |
| 280 | *desiredSize = computedSize; |
| 281 | return sampleSize; |
| 282 | } |
| 283 | |
| 284 | // computedSize is too small. Make it larger. |
| 285 | while (sampleSize > 2) { |
| 286 | auto bigger = this->getSampledDimensions(sampleSize - 1); |
| 287 | if (bigger == *desiredSize || !smaller_than(bigger, *desiredSize)) { |
| 288 | *desiredSize = bigger; |
| 289 | return sampleSize - 1; |
| 290 | } |
| 291 | sampleSize--; |
| 292 | } |
| 293 | |
| 294 | *desiredSize = fInfo.dimensions(); |
| 295 | return 1; |
| 296 | } |
| 297 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 298 | SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const { |
| 299 | if (!is_valid_sample_size(sampleSize)) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 300 | return {0, 0}; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 301 | } |
| 302 | |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 303 | // Fast path for when we are not scaling. |
| 304 | if (1 == sampleSize) { |
| 305 | return fInfo.dimensions(); |
| 306 | } |
| 307 | |
Leon Scroggins III | 95d0c8d | 2019-01-30 15:33:09 -0500 | [diff] [blame] | 308 | auto dims = this->onGetSampledDimensions(sampleSize); |
| 309 | if (fOrientationBehavior == SkAndroidCodec::ExifOrientationBehavior::kIgnore |
| 310 | || !SkPixmapPriv::ShouldSwapWidthHeight(fCodec->getOrigin())) { |
| 311 | return dims; |
| 312 | } |
| 313 | |
| 314 | return { dims.height(), dims.width() }; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | bool SkAndroidCodec::getSupportedSubset(SkIRect* desiredSubset) const { |
| 318 | if (!desiredSubset || !is_valid_subset(*desiredSubset, fInfo.dimensions())) { |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | return this->onGetSupportedSubset(desiredSubset); |
| 323 | } |
| 324 | |
| 325 | SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const { |
| 326 | if (!is_valid_sample_size(sampleSize)) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 327 | return {0, 0}; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | // We require that the input subset is a subset that is supported by SkAndroidCodec. |
| 331 | // We test this by calling getSupportedSubset() and verifying that no modifications |
| 332 | // are made to the subset. |
| 333 | SkIRect copySubset = subset; |
| 334 | if (!this->getSupportedSubset(©Subset) || copySubset != subset) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 335 | return {0, 0}; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 336 | } |
| 337 | |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 338 | // If the subset is the entire image, for consistency, use getSampledDimensions(). |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 339 | if (fInfo.dimensions() == subset.size()) { |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 340 | return this->getSampledDimensions(sampleSize); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | // This should perhaps call a virtual function, but currently both of our subclasses |
| 344 | // want the same implementation. |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 345 | return {get_scaled_dimension(subset.width(), sampleSize), |
| 346 | get_scaled_dimension(subset.height(), sampleSize)}; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 349 | static bool acceptable_result(SkCodec::Result result) { |
| 350 | switch (result) { |
| 351 | // These results mean a partial or complete image. They should be considered |
| 352 | // a success by SkPixmapPriv. |
| 353 | case SkCodec::kSuccess: |
| 354 | case SkCodec::kIncompleteInput: |
| 355 | case SkCodec::kErrorInInput: |
| 356 | return true; |
| 357 | default: |
| 358 | return false; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& requestInfo, |
| 363 | void* requestPixels, size_t requestRowBytes, const AndroidOptions* options) { |
| 364 | if (!requestPixels) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 365 | return SkCodec::kInvalidParameters; |
| 366 | } |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 367 | if (requestRowBytes < requestInfo.minRowBytes()) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 368 | return SkCodec::kInvalidParameters; |
| 369 | } |
| 370 | |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 371 | SkImageInfo adjustedInfo = fInfo; |
| 372 | if (ExifOrientationBehavior::kRespect == fOrientationBehavior |
| 373 | && SkPixmapPriv::ShouldSwapWidthHeight(fCodec->getOrigin())) { |
| 374 | adjustedInfo = SkPixmapPriv::SwapWidthHeight(adjustedInfo); |
| 375 | } |
| 376 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 377 | AndroidOptions defaultOptions; |
| 378 | if (!options) { |
| 379 | options = &defaultOptions; |
| 380 | } else if (options->fSubset) { |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 381 | if (!is_valid_subset(*options->fSubset, adjustedInfo.dimensions())) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 382 | return SkCodec::kInvalidParameters; |
| 383 | } |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 384 | |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 385 | if (SkIRect::MakeSize(adjustedInfo.dimensions()) == *options->fSubset) { |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 386 | // The caller wants the whole thing, rather than a subset. Modify |
| 387 | // the AndroidOptions passed to onGetAndroidPixels to not specify |
| 388 | // a subset. |
| 389 | defaultOptions = *options; |
| 390 | defaultOptions.fSubset = nullptr; |
| 391 | options = &defaultOptions; |
| 392 | } |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 395 | if (ExifOrientationBehavior::kIgnore == fOrientationBehavior) { |
| 396 | return this->onGetAndroidPixels(requestInfo, requestPixels, requestRowBytes, *options); |
| 397 | } |
| 398 | |
| 399 | SkCodec::Result result; |
| 400 | auto decode = [this, options, &result](const SkPixmap& pm) { |
| 401 | result = this->onGetAndroidPixels(pm.info(), pm.writable_addr(), pm.rowBytes(), *options); |
| 402 | return acceptable_result(result); |
| 403 | }; |
| 404 | |
| 405 | SkPixmap dst(requestInfo, requestPixels, requestRowBytes); |
| 406 | if (SkPixmapPriv::Orient(dst, fCodec->getOrigin(), decode)) { |
| 407 | return result; |
| 408 | } |
| 409 | |
| 410 | // Orient returned false. If onGetAndroidPixels succeeded, then Orient failed internally. |
| 411 | if (acceptable_result(result)) { |
| 412 | return SkCodec::kInternalError; |
| 413 | } |
| 414 | |
| 415 | return result; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels, |
| 419 | size_t rowBytes) { |
| 420 | return this->getAndroidPixels(info, pixels, rowBytes, nullptr); |
| 421 | } |