blob: 10d19f34ee3161984c46dfba2fbf83739e03a85e [file] [log] [blame]
msarett3d9d7a72015-10-21 10:27:10 -07001/*
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"
9#include "SkCodec.h"
10#include "SkCodecPriv.h"
Mike Reedede7bac2017-07-23 15:30:02 -040011#include "SkMakeUnique.h"
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050012#include "SkPixmap.h"
13#include "SkPixmapPriv.h"
yujieqin916de9f2016-01-25 08:26:16 -080014#include "SkRawAdapterCodec.h"
msarett3d9d7a72015-10-21 10:27:10 -070015#include "SkSampledCodec.h"
16#include "SkWebpAdapterCodec.h"
17
18static bool is_valid_sample_size(int sampleSize) {
19 // FIXME: As Leon has mentioned elsewhere, surely there is also a maximum sampleSize?
20 return sampleSize > 0;
21}
22
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050023/**
24 * Loads the gamut as a set of three points (triangle).
25 */
Leon Scroggins III36f7e322018-08-27 11:55:46 -040026static void load_gamut(SkPoint rgb[], const skcms_Matrix3x3& xyz) {
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050027 // rx = rX / (rX + rY + rZ)
28 // ry = rY / (rX + rY + rZ)
29 // gx, gy, bx, and gy are calulcated similarly.
nagarajan.n6e17b6f2017-07-10 15:02:00 +053030 for (int rgbIdx = 0; rgbIdx < 3; rgbIdx++) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -040031 float sum = xyz.vals[rgbIdx][0] + xyz.vals[rgbIdx][1] + xyz.vals[rgbIdx][2];
32 rgb[rgbIdx].fX = xyz.vals[rgbIdx][0] / sum;
33 rgb[rgbIdx].fY = xyz.vals[rgbIdx][1] / sum;
nagarajan.n6e17b6f2017-07-10 15:02:00 +053034 }
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050035}
36
37/**
38 * Calculates the area of the triangular gamut.
39 */
40static float calculate_area(SkPoint abc[]) {
41 SkPoint a = abc[0];
42 SkPoint b = abc[1];
43 SkPoint c = abc[2];
44 return 0.5f * SkTAbs(a.fX*b.fY + b.fX*c.fY - a.fX*c.fY - c.fX*b.fY - b.fX*a.fY);
45}
46
Leon Scroggins III862c1962017-10-02 16:28:49 -040047static constexpr float kSRGB_D50_GamutArea = 0.084f;
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050048
Leon Scroggins III36f7e322018-08-27 11:55:46 -040049static bool is_wide_gamut(const skcms_ICCProfile& profile) {
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050050 // Determine if the source image has a gamut that is wider than sRGB. If so, we
51 // will use P3 as the output color space to avoid clipping the gamut.
Leon Scroggins III36f7e322018-08-27 11:55:46 -040052 if (profile.has_toXYZD50) {
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050053 SkPoint rgb[3];
Leon Scroggins III36f7e322018-08-27 11:55:46 -040054 load_gamut(rgb, profile.toXYZD50);
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050055 return calculate_area(rgb) > kSRGB_D50_GamutArea;
56 }
57
58 return false;
59}
60
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050061static inline SkImageInfo adjust_info(SkCodec* codec,
62 SkAndroidCodec::ExifOrientationBehavior orientationBehavior) {
63 auto info = codec->getInfo();
64 if (orientationBehavior == SkAndroidCodec::ExifOrientationBehavior::kIgnore
65 || !SkPixmapPriv::ShouldSwapWidthHeight(codec->getOrigin())) {
66 return info;
67 }
68 return SkPixmapPriv::SwapWidthHeight(info);
69}
70
71SkAndroidCodec::SkAndroidCodec(SkCodec* codec, ExifOrientationBehavior orientationBehavior)
72 : fInfo(adjust_info(codec, orientationBehavior))
73 , fOrientationBehavior(orientationBehavior)
msarett90c4d5f2015-12-10 13:09:24 -080074 , fCodec(codec)
msarett3d9d7a72015-10-21 10:27:10 -070075{}
76
Leon Scroggins III07418182017-08-15 12:24:02 -040077SkAndroidCodec::~SkAndroidCodec() {}
78
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050079std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
80 SkPngChunkReader* chunkReader) {
Mike Reedede7bac2017-07-23 15:30:02 -040081 auto codec = SkCodec::MakeFromStream(std::move(stream), nullptr, chunkReader);
Leon Scroggins III7397d7a2018-01-04 13:26:30 -050082 return MakeFromCodec(std::move(codec));
83}
84
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050085std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromCodec(std::unique_ptr<SkCodec> codec,
86 ExifOrientationBehavior orientationBehavior) {
msarett3d9d7a72015-10-21 10:27:10 -070087 if (nullptr == codec) {
88 return nullptr;
89 }
90
Hal Canarydb683012016-11-23 08:55:18 -070091 switch ((SkEncodedImageFormat)codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -070092 case SkEncodedImageFormat::kPNG:
93 case SkEncodedImageFormat::kICO:
Hal Canarydb683012016-11-23 08:55:18 -070094 case SkEncodedImageFormat::kJPEG:
Hal Canarydb683012016-11-23 08:55:18 -070095 case SkEncodedImageFormat::kGIF:
96 case SkEncodedImageFormat::kBMP:
97 case SkEncodedImageFormat::kWBMP:
Leon Scroggins III04be2b52017-08-17 15:13:20 -040098 case SkEncodedImageFormat::kHEIF:
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050099 return skstd::make_unique<SkSampledCodec>(codec.release(), orientationBehavior);
msarettad3a5c62016-05-06 07:21:26 -0700100#ifdef SK_HAS_WEBP_LIBRARY
Hal Canarydb683012016-11-23 08:55:18 -0700101 case SkEncodedImageFormat::kWEBP:
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500102 return skstd::make_unique<SkWebpAdapterCodec>((SkWebpCodec*) codec.release(),
103 orientationBehavior);
msarett39b2d5a2016-02-17 08:26:31 -0800104#endif
yujieqin916de9f2016-01-25 08:26:16 -0800105#ifdef SK_CODEC_DECODES_RAW
Hal Canarydb683012016-11-23 08:55:18 -0700106 case SkEncodedImageFormat::kDNG:
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500107 return skstd::make_unique<SkRawAdapterCodec>((SkRawCodec*)codec.release(),
108 orientationBehavior);
yujieqin916de9f2016-01-25 08:26:16 -0800109#endif
msarett3d9d7a72015-10-21 10:27:10 -0700110 default:
msarett3d9d7a72015-10-21 10:27:10 -0700111 return nullptr;
112 }
113}
114
Mike Reedede7bac2017-07-23 15:30:02 -0400115std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromData(sk_sp<SkData> data,
116 SkPngChunkReader* chunkReader) {
msarett3d9d7a72015-10-21 10:27:10 -0700117 if (!data) {
118 return nullptr;
119 }
120
Mike Reed847068c2017-07-26 11:35:53 -0400121 return MakeFromStream(SkMemoryStream::Make(std::move(data)), chunkReader);
msarett3d9d7a72015-10-21 10:27:10 -0700122}
123
msarett9a0e3462015-12-11 07:38:50 -0800124SkColorType SkAndroidCodec::computeOutputColorType(SkColorType requestedColorType) {
Leon Scroggins1793e7b2017-12-05 15:38:14 +0000125 bool highPrecision = fCodec->getEncodedInfo().bitsPerComponent() > 8;
msarett9a0e3462015-12-11 07:38:50 -0800126 switch (requestedColorType) {
127 case kARGB_4444_SkColorType:
msarett9a0e3462015-12-11 07:38:50 -0800128 return kN32_SkColorType;
Matt Sarett8dcc84f2016-12-14 10:23:41 -0500129 case kN32_SkColorType:
msarett9a0e3462015-12-11 07:38:50 -0800130 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 Sarett8db74f12017-06-14 13:02:05 +0000136 if (kGray_8_SkColorType == this->getInfo().colorType()) {
msarett9a0e3462015-12-11 07:38:50 -0800137 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 Sarett8dcc84f2016-12-14 10:23:41 -0500145 case kRGBA_F16_SkColorType:
146 return kRGBA_F16_SkColorType;
msarett9a0e3462015-12-11 07:38:50 -0800147 default:
148 break;
149 }
150
Leon Scroggins1793e7b2017-12-05 15:38:14 +0000151 // F16 is the Android default for high precision images.
152 return highPrecision ? kRGBA_F16_SkColorType : kN32_SkColorType;
msarett9a0e3462015-12-11 07:38:50 -0800153}
154
155SkAlphaType 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 Sarett68feef42017-04-11 09:51:32 -0400162sk_sp<SkColorSpace> SkAndroidCodec::computeOutputColorSpace(SkColorType outputColorType,
163 sk_sp<SkColorSpace> prefColorSpace) {
Matt Sarett966bb342016-12-12 16:30:13 -0500164 switch (outputColorType) {
Leon Scroggins IIIc20b5f82017-07-13 08:05:29 -0400165 case kRGBA_8888_SkColorType:
166 case kBGRA_8888_SkColorType: {
Matt Sarett68feef42017-04-11 09:51:32 -0400167 // If |prefColorSpace| is supported, choose it.
Matt Sarett9341c982017-02-28 15:36:42 -0500168 SkColorSpaceTransferFn fn;
Matt Sarett68feef42017-04-11 09:51:32 -0400169 if (prefColorSpace && prefColorSpace->isNumericalTransferFn(&fn)) {
170 return prefColorSpace;
171 }
172
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400173 const skcms_ICCProfile* encodedProfile = fCodec->getEncodedInfo().profile();
Leon Scroggins III227d4e12018-09-27 08:30:22 -0400174 if (encodedProfile) {
175 if (auto encodedSpace = SkColorSpace::Make(*encodedProfile)) {
176 // Leave the pixels in the encoded color space. Color space conversion
177 // will be handled after decode time.
178 return encodedSpace;
179 }
Matt Sarett9341c982017-02-28 15:36:42 -0500180
Leon Scroggins III227d4e12018-09-27 08:30:22 -0400181 if (is_wide_gamut(*encodedProfile)) {
182 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
183 SkColorSpace::kDCIP3_D65_Gamut);
184 }
Matt Sarett2ae3a2e2017-02-15 08:48:02 -0500185 }
186
Matt Sarettcf3f2342017-03-23 15:32:25 -0400187 return SkColorSpace::MakeSRGB();
Matt Sarett9341c982017-02-28 15:36:42 -0500188 }
Matt Sarett966bb342016-12-12 16:30:13 -0500189 case kRGBA_F16_SkColorType:
Matt Sarett68feef42017-04-11 09:51:32 -0400190 // Note that |prefColorSpace| is ignored, F16 is always linear sRGB.
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500191 return SkColorSpace::MakeSRGBLinear();
Matt Sarett3725f0a2017-03-28 14:34:20 -0400192 case kRGB_565_SkColorType:
Matt Sarett68feef42017-04-11 09:51:32 -0400193 // Note that |prefColorSpace| is ignored, 565 is always sRGB.
Matt Sarett3725f0a2017-03-28 14:34:20 -0400194 return SkColorSpace::MakeSRGB();
Matt Sarett966bb342016-12-12 16:30:13 -0500195 default:
Matt Sarett3725f0a2017-03-28 14:34:20 -0400196 // Color correction not supported for kGray.
Matt Sarett966bb342016-12-12 16:30:13 -0500197 return nullptr;
198 }
199}
200
Leon Scroggins III07a722c2018-01-16 15:01:17 -0500201static bool supports_any_down_scale(const SkCodec* codec) {
202 return codec->getEncodedFormat() == SkEncodedImageFormat::kWEBP;
203}
204
205// There are a variety of ways two SkISizes could be compared. This method
206// returns true if either dimensions of a is < that of b.
207// computeSampleSize also uses the opposite, which means that both
208// dimensions of a >= b.
209static inline bool smaller_than(const SkISize& a, const SkISize& b) {
210 return a.width() < b.width() || a.height() < b.height();
211}
212
213// Both dimensions of a > that of b.
214static inline bool strictly_bigger_than(const SkISize& a, const SkISize& b) {
215 return a.width() > b.width() && a.height() > b.height();
216}
217
218int SkAndroidCodec::computeSampleSize(SkISize* desiredSize) const {
219 SkASSERT(desiredSize);
220
221 if (!desiredSize || *desiredSize == fInfo.dimensions()) {
222 return 1;
223 }
224
225 if (smaller_than(fInfo.dimensions(), *desiredSize)) {
226 *desiredSize = fInfo.dimensions();
227 return 1;
228 }
229
230 // Handle bad input:
231 if (desiredSize->width() < 1 || desiredSize->height() < 1) {
232 *desiredSize = SkISize::Make(std::max(1, desiredSize->width()),
233 std::max(1, desiredSize->height()));
234 }
235
236 if (supports_any_down_scale(fCodec.get())) {
237 return 1;
238 }
239
240 int sampleX = fInfo.width() / desiredSize->width();
241 int sampleY = fInfo.height() / desiredSize->height();
242 int sampleSize = std::min(sampleX, sampleY);
243 auto computedSize = this->getSampledDimensions(sampleSize);
244 if (computedSize == *desiredSize) {
245 return sampleSize;
246 }
247
248 if (computedSize == fInfo.dimensions() || sampleSize == 1) {
249 // Cannot downscale
250 *desiredSize = computedSize;
251 return 1;
252 }
253
254 if (strictly_bigger_than(computedSize, *desiredSize)) {
255 // See if there is a tighter fit.
256 while (true) {
257 auto smaller = this->getSampledDimensions(sampleSize + 1);
258 if (smaller == *desiredSize) {
259 return sampleSize + 1;
260 }
261 if (smaller == computedSize || smaller_than(smaller, *desiredSize)) {
262 // Cannot get any smaller without being smaller than desired.
263 *desiredSize = computedSize;
264 return sampleSize;
265 }
266
267 sampleSize++;
268 computedSize = smaller;
269 }
270
271 SkASSERT(false);
272 }
273
274 if (!smaller_than(computedSize, *desiredSize)) {
275 // This means one of the computed dimensions is equal to desired, and
276 // the other is bigger. This is as close as we can get.
277 *desiredSize = computedSize;
278 return sampleSize;
279 }
280
281 // computedSize is too small. Make it larger.
282 while (sampleSize > 2) {
283 auto bigger = this->getSampledDimensions(sampleSize - 1);
284 if (bigger == *desiredSize || !smaller_than(bigger, *desiredSize)) {
285 *desiredSize = bigger;
286 return sampleSize - 1;
287 }
288 sampleSize--;
289 }
290
291 *desiredSize = fInfo.dimensions();
292 return 1;
293}
294
msarett3d9d7a72015-10-21 10:27:10 -0700295SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const {
296 if (!is_valid_sample_size(sampleSize)) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400297 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700298 }
299
scroggo501b7342015-11-03 07:55:11 -0800300 // Fast path for when we are not scaling.
301 if (1 == sampleSize) {
302 return fInfo.dimensions();
303 }
304
msarett3d9d7a72015-10-21 10:27:10 -0700305 return this->onGetSampledDimensions(sampleSize);
306}
307
308bool SkAndroidCodec::getSupportedSubset(SkIRect* desiredSubset) const {
309 if (!desiredSubset || !is_valid_subset(*desiredSubset, fInfo.dimensions())) {
310 return false;
311 }
312
313 return this->onGetSupportedSubset(desiredSubset);
314}
315
316SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const {
317 if (!is_valid_sample_size(sampleSize)) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400318 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700319 }
320
321 // We require that the input subset is a subset that is supported by SkAndroidCodec.
322 // We test this by calling getSupportedSubset() and verifying that no modifications
323 // are made to the subset.
324 SkIRect copySubset = subset;
325 if (!this->getSupportedSubset(&copySubset) || copySubset != subset) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400326 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700327 }
328
scroggo501b7342015-11-03 07:55:11 -0800329 // If the subset is the entire image, for consistency, use getSampledDimensions().
msarett3d9d7a72015-10-21 10:27:10 -0700330 if (fInfo.dimensions() == subset.size()) {
scroggo501b7342015-11-03 07:55:11 -0800331 return this->getSampledDimensions(sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700332 }
333
334 // This should perhaps call a virtual function, but currently both of our subclasses
335 // want the same implementation.
Hal Canaryfafe1352017-04-11 12:12:02 -0400336 return {get_scaled_dimension(subset.width(), sampleSize),
337 get_scaled_dimension(subset.height(), sampleSize)};
msarett3d9d7a72015-10-21 10:27:10 -0700338}
339
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500340static bool acceptable_result(SkCodec::Result result) {
341 switch (result) {
342 // These results mean a partial or complete image. They should be considered
343 // a success by SkPixmapPriv.
344 case SkCodec::kSuccess:
345 case SkCodec::kIncompleteInput:
346 case SkCodec::kErrorInInput:
347 return true;
348 default:
349 return false;
350 }
351}
352
353SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& requestInfo,
354 void* requestPixels, size_t requestRowBytes, const AndroidOptions* options) {
355 if (!requestPixels) {
msarett3d9d7a72015-10-21 10:27:10 -0700356 return SkCodec::kInvalidParameters;
357 }
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500358 if (requestRowBytes < requestInfo.minRowBytes()) {
msarett3d9d7a72015-10-21 10:27:10 -0700359 return SkCodec::kInvalidParameters;
360 }
361
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500362 SkImageInfo adjustedInfo = fInfo;
363 if (ExifOrientationBehavior::kRespect == fOrientationBehavior
364 && SkPixmapPriv::ShouldSwapWidthHeight(fCodec->getOrigin())) {
365 adjustedInfo = SkPixmapPriv::SwapWidthHeight(adjustedInfo);
366 }
367
msarett3d9d7a72015-10-21 10:27:10 -0700368 AndroidOptions defaultOptions;
369 if (!options) {
370 options = &defaultOptions;
371 } else if (options->fSubset) {
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500372 if (!is_valid_subset(*options->fSubset, adjustedInfo.dimensions())) {
msarett3d9d7a72015-10-21 10:27:10 -0700373 return SkCodec::kInvalidParameters;
374 }
scroggo501b7342015-11-03 07:55:11 -0800375
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500376 if (SkIRect::MakeSize(adjustedInfo.dimensions()) == *options->fSubset) {
scroggo501b7342015-11-03 07:55:11 -0800377 // The caller wants the whole thing, rather than a subset. Modify
378 // the AndroidOptions passed to onGetAndroidPixels to not specify
379 // a subset.
380 defaultOptions = *options;
381 defaultOptions.fSubset = nullptr;
382 options = &defaultOptions;
383 }
msarett3d9d7a72015-10-21 10:27:10 -0700384 }
385
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500386 if (ExifOrientationBehavior::kIgnore == fOrientationBehavior) {
387 return this->onGetAndroidPixels(requestInfo, requestPixels, requestRowBytes, *options);
388 }
389
390 SkCodec::Result result;
391 auto decode = [this, options, &result](const SkPixmap& pm) {
392 result = this->onGetAndroidPixels(pm.info(), pm.writable_addr(), pm.rowBytes(), *options);
393 return acceptable_result(result);
394 };
395
396 SkPixmap dst(requestInfo, requestPixels, requestRowBytes);
397 if (SkPixmapPriv::Orient(dst, fCodec->getOrigin(), decode)) {
398 return result;
399 }
400
401 // Orient returned false. If onGetAndroidPixels succeeded, then Orient failed internally.
402 if (acceptable_result(result)) {
403 return SkCodec::kInternalError;
404 }
405
406 return result;
msarett3d9d7a72015-10-21 10:27:10 -0700407}
408
409SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels,
410 size_t rowBytes) {
411 return this->getAndroidPixels(info, pixels, rowBytes, nullptr);
412}