blob: f995c2e8abe6f0fb3db4cb8d657fdd7baf12e824 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/codec/SkAndroidCodec.h"
9#include "include/codec/SkCodec.h"
10#include "include/core/SkPixmap.h"
11#include "src/codec/SkAndroidCodecAdapter.h"
12#include "src/codec/SkCodecPriv.h"
13#include "src/codec/SkSampledCodec.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/core/SkPixmapPriv.h"
msarett3d9d7a72015-10-21 10:27:10 -070015
16static bool is_valid_sample_size(int sampleSize) {
17 // FIXME: As Leon has mentioned elsewhere, surely there is also a maximum sampleSize?
18 return sampleSize > 0;
19}
20
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050021/**
22 * Loads the gamut as a set of three points (triangle).
23 */
Leon Scroggins III36f7e322018-08-27 11:55:46 -040024static void load_gamut(SkPoint rgb[], const skcms_Matrix3x3& xyz) {
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050025 // rx = rX / (rX + rY + rZ)
26 // ry = rY / (rX + rY + rZ)
27 // gx, gy, bx, and gy are calulcated similarly.
nagarajan.n6e17b6f2017-07-10 15:02:00 +053028 for (int rgbIdx = 0; rgbIdx < 3; rgbIdx++) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -040029 float sum = xyz.vals[rgbIdx][0] + xyz.vals[rgbIdx][1] + xyz.vals[rgbIdx][2];
30 rgb[rgbIdx].fX = xyz.vals[rgbIdx][0] / sum;
31 rgb[rgbIdx].fY = xyz.vals[rgbIdx][1] / sum;
nagarajan.n6e17b6f2017-07-10 15:02:00 +053032 }
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050033}
34
35/**
36 * Calculates the area of the triangular gamut.
37 */
38static float calculate_area(SkPoint abc[]) {
39 SkPoint a = abc[0];
40 SkPoint b = abc[1];
41 SkPoint c = abc[2];
42 return 0.5f * SkTAbs(a.fX*b.fY + b.fX*c.fY - a.fX*c.fY - c.fX*b.fY - b.fX*a.fY);
43}
44
Leon Scroggins III862c1962017-10-02 16:28:49 -040045static constexpr float kSRGB_D50_GamutArea = 0.084f;
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050046
Leon Scroggins III36f7e322018-08-27 11:55:46 -040047static bool is_wide_gamut(const skcms_ICCProfile& profile) {
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050048 // Determine if the source image has a gamut that is wider than sRGB. If so, we
49 // will use P3 as the output color space to avoid clipping the gamut.
Leon Scroggins III36f7e322018-08-27 11:55:46 -040050 if (profile.has_toXYZD50) {
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050051 SkPoint rgb[3];
Leon Scroggins III36f7e322018-08-27 11:55:46 -040052 load_gamut(rgb, profile.toXYZD50);
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050053 return calculate_area(rgb) > kSRGB_D50_GamutArea;
54 }
55
56 return false;
57}
58
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050059static inline SkImageInfo adjust_info(SkCodec* codec,
60 SkAndroidCodec::ExifOrientationBehavior orientationBehavior) {
61 auto info = codec->getInfo();
62 if (orientationBehavior == SkAndroidCodec::ExifOrientationBehavior::kIgnore
63 || !SkPixmapPriv::ShouldSwapWidthHeight(codec->getOrigin())) {
64 return info;
65 }
66 return SkPixmapPriv::SwapWidthHeight(info);
67}
68
69SkAndroidCodec::SkAndroidCodec(SkCodec* codec, ExifOrientationBehavior orientationBehavior)
70 : fInfo(adjust_info(codec, orientationBehavior))
71 , fOrientationBehavior(orientationBehavior)
msarett90c4d5f2015-12-10 13:09:24 -080072 , fCodec(codec)
msarett3d9d7a72015-10-21 10:27:10 -070073{}
74
Leon Scroggins III07418182017-08-15 12:24:02 -040075SkAndroidCodec::~SkAndroidCodec() {}
76
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050077std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
78 SkPngChunkReader* chunkReader) {
Mike Reedede7bac2017-07-23 15:30:02 -040079 auto codec = SkCodec::MakeFromStream(std::move(stream), nullptr, chunkReader);
Leon Scroggins III7397d7a2018-01-04 13:26:30 -050080 return MakeFromCodec(std::move(codec));
81}
82
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050083std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromCodec(std::unique_ptr<SkCodec> codec,
84 ExifOrientationBehavior orientationBehavior) {
msarett3d9d7a72015-10-21 10:27:10 -070085 if (nullptr == codec) {
86 return nullptr;
87 }
88
Hal Canarydb683012016-11-23 08:55:18 -070089 switch ((SkEncodedImageFormat)codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -070090 case SkEncodedImageFormat::kPNG:
91 case SkEncodedImageFormat::kICO:
Hal Canarydb683012016-11-23 08:55:18 -070092 case SkEncodedImageFormat::kJPEG:
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -040093#ifndef SK_HAS_WUFFS_LIBRARY
Hal Canarydb683012016-11-23 08:55:18 -070094 case SkEncodedImageFormat::kGIF:
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -040095#endif
Hal Canarydb683012016-11-23 08:55:18 -070096 case SkEncodedImageFormat::kBMP:
97 case SkEncodedImageFormat::kWBMP:
Leon Scroggins III04be2b52017-08-17 15:13:20 -040098 case SkEncodedImageFormat::kHEIF:
Vignesh Venkatasubramanianeb7f9602020-11-04 14:13:39 -080099 case SkEncodedImageFormat::kAVIF:
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500100 return std::make_unique<SkSampledCodec>(codec.release(), orientationBehavior);
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400101#ifdef SK_HAS_WUFFS_LIBRARY
102 case SkEncodedImageFormat::kGIF:
103#endif
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -0400104#ifdef SK_CODEC_DECODES_WEBP
Hal Canarydb683012016-11-23 08:55:18 -0700105 case SkEncodedImageFormat::kWEBP:
Mike Klein0af99292019-05-15 22:02:05 +0000106#endif
107#ifdef SK_CODEC_DECODES_RAW
Hal Canarydb683012016-11-23 08:55:18 -0700108 case SkEncodedImageFormat::kDNG:
Mike Klein0af99292019-05-15 22:02:05 +0000109#endif
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -0400110#if defined(SK_CODEC_DECODES_WEBP) || defined(SK_CODEC_DECODES_RAW) || defined(SK_HAS_WUFFS_LIBRARY)
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500111 return std::make_unique<SkAndroidCodecAdapter>(codec.release(), orientationBehavior);
Mike Klein0af99292019-05-15 22:02:05 +0000112#endif
Nigel Tao612a65d2018-11-09 10:10:31 +1100113
msarett3d9d7a72015-10-21 10:27:10 -0700114 default:
msarett3d9d7a72015-10-21 10:27:10 -0700115 return nullptr;
116 }
117}
118
Mike Reedede7bac2017-07-23 15:30:02 -0400119std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromData(sk_sp<SkData> data,
120 SkPngChunkReader* chunkReader) {
msarett3d9d7a72015-10-21 10:27:10 -0700121 if (!data) {
122 return nullptr;
123 }
124
Mike Reed847068c2017-07-26 11:35:53 -0400125 return MakeFromStream(SkMemoryStream::Make(std::move(data)), chunkReader);
msarett3d9d7a72015-10-21 10:27:10 -0700126}
127
msarett9a0e3462015-12-11 07:38:50 -0800128SkColorType SkAndroidCodec::computeOutputColorType(SkColorType requestedColorType) {
Leon Scroggins1793e7b2017-12-05 15:38:14 +0000129 bool highPrecision = fCodec->getEncodedInfo().bitsPerComponent() > 8;
msarett9a0e3462015-12-11 07:38:50 -0800130 switch (requestedColorType) {
131 case kARGB_4444_SkColorType:
msarett9a0e3462015-12-11 07:38:50 -0800132 return kN32_SkColorType;
Matt Sarett8dcc84f2016-12-14 10:23:41 -0500133 case kN32_SkColorType:
msarett9a0e3462015-12-11 07:38:50 -0800134 break;
135 case kAlpha_8_SkColorType:
136 // Fall through to kGray_8. Before kGray_8_SkColorType existed,
137 // we allowed clients to request kAlpha_8 when they wanted a
138 // grayscale decode.
139 case kGray_8_SkColorType:
Matt Sarett8db74f12017-06-14 13:02:05 +0000140 if (kGray_8_SkColorType == this->getInfo().colorType()) {
msarett9a0e3462015-12-11 07:38:50 -0800141 return kGray_8_SkColorType;
142 }
143 break;
144 case kRGB_565_SkColorType:
145 if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
146 return kRGB_565_SkColorType;
147 }
148 break;
Matt Sarett8dcc84f2016-12-14 10:23:41 -0500149 case kRGBA_F16_SkColorType:
150 return kRGBA_F16_SkColorType;
msarett9a0e3462015-12-11 07:38:50 -0800151 default:
152 break;
153 }
154
Leon Scroggins1793e7b2017-12-05 15:38:14 +0000155 // F16 is the Android default for high precision images.
156 return highPrecision ? kRGBA_F16_SkColorType : kN32_SkColorType;
msarett9a0e3462015-12-11 07:38:50 -0800157}
158
159SkAlphaType SkAndroidCodec::computeOutputAlphaType(bool requestedUnpremul) {
160 if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
161 return kOpaque_SkAlphaType;
162 }
163 return requestedUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
164}
165
Matt Sarett68feef42017-04-11 09:51:32 -0400166sk_sp<SkColorSpace> SkAndroidCodec::computeOutputColorSpace(SkColorType outputColorType,
167 sk_sp<SkColorSpace> prefColorSpace) {
Matt Sarett966bb342016-12-12 16:30:13 -0500168 switch (outputColorType) {
Derek Sollenberger1be431f2019-01-30 11:21:33 -0500169 case kRGBA_F16_SkColorType:
170 case kRGB_565_SkColorType:
Leon Scroggins IIIc20b5f82017-07-13 08:05:29 -0400171 case kRGBA_8888_SkColorType:
172 case kBGRA_8888_SkColorType: {
Brian Osman82ebe042019-01-04 17:03:00 -0500173 // If |prefColorSpace| is supplied, choose it.
174 if (prefColorSpace) {
Matt Sarett68feef42017-04-11 09:51:32 -0400175 return prefColorSpace;
176 }
177
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400178 const skcms_ICCProfile* encodedProfile = fCodec->getEncodedInfo().profile();
Leon Scroggins III227d4e12018-09-27 08:30:22 -0400179 if (encodedProfile) {
180 if (auto encodedSpace = SkColorSpace::Make(*encodedProfile)) {
181 // Leave the pixels in the encoded color space. Color space conversion
182 // will be handled after decode time.
183 return encodedSpace;
184 }
Matt Sarett9341c982017-02-28 15:36:42 -0500185
Leon Scroggins III227d4e12018-09-27 08:30:22 -0400186 if (is_wide_gamut(*encodedProfile)) {
Mike Kleinb147ace2020-01-16 11:11:06 -0600187 return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3);
Leon Scroggins III227d4e12018-09-27 08:30:22 -0400188 }
Matt Sarett2ae3a2e2017-02-15 08:48:02 -0500189 }
190
Matt Sarettcf3f2342017-03-23 15:32:25 -0400191 return SkColorSpace::MakeSRGB();
Matt Sarett9341c982017-02-28 15:36:42 -0500192 }
Matt Sarett966bb342016-12-12 16:30:13 -0500193 default:
Matt Sarett3725f0a2017-03-28 14:34:20 -0400194 // Color correction not supported for kGray.
Matt Sarett966bb342016-12-12 16:30:13 -0500195 return nullptr;
196 }
197}
198
Leon Scroggins III07a722c2018-01-16 15:01:17 -0500199static bool supports_any_down_scale(const SkCodec* codec) {
200 return codec->getEncodedFormat() == SkEncodedImageFormat::kWEBP;
201}
202
203// There are a variety of ways two SkISizes could be compared. This method
204// returns true if either dimensions of a is < that of b.
205// computeSampleSize also uses the opposite, which means that both
206// dimensions of a >= b.
207static inline bool smaller_than(const SkISize& a, const SkISize& b) {
208 return a.width() < b.width() || a.height() < b.height();
209}
210
211// Both dimensions of a > that of b.
212static inline bool strictly_bigger_than(const SkISize& a, const SkISize& b) {
213 return a.width() > b.width() && a.height() > b.height();
214}
215
216int SkAndroidCodec::computeSampleSize(SkISize* desiredSize) const {
217 SkASSERT(desiredSize);
218
219 if (!desiredSize || *desiredSize == fInfo.dimensions()) {
220 return 1;
221 }
222
223 if (smaller_than(fInfo.dimensions(), *desiredSize)) {
224 *desiredSize = fInfo.dimensions();
225 return 1;
226 }
227
228 // Handle bad input:
229 if (desiredSize->width() < 1 || desiredSize->height() < 1) {
230 *desiredSize = SkISize::Make(std::max(1, desiredSize->width()),
231 std::max(1, desiredSize->height()));
232 }
233
234 if (supports_any_down_scale(fCodec.get())) {
235 return 1;
236 }
237
238 int sampleX = fInfo.width() / desiredSize->width();
239 int sampleY = fInfo.height() / desiredSize->height();
240 int sampleSize = std::min(sampleX, sampleY);
241 auto computedSize = this->getSampledDimensions(sampleSize);
242 if (computedSize == *desiredSize) {
243 return sampleSize;
244 }
245
246 if (computedSize == fInfo.dimensions() || sampleSize == 1) {
247 // Cannot downscale
248 *desiredSize = computedSize;
249 return 1;
250 }
251
252 if (strictly_bigger_than(computedSize, *desiredSize)) {
253 // See if there is a tighter fit.
254 while (true) {
255 auto smaller = this->getSampledDimensions(sampleSize + 1);
256 if (smaller == *desiredSize) {
257 return sampleSize + 1;
258 }
259 if (smaller == computedSize || smaller_than(smaller, *desiredSize)) {
260 // Cannot get any smaller without being smaller than desired.
261 *desiredSize = computedSize;
262 return sampleSize;
263 }
264
265 sampleSize++;
266 computedSize = smaller;
267 }
268
269 SkASSERT(false);
270 }
271
272 if (!smaller_than(computedSize, *desiredSize)) {
273 // This means one of the computed dimensions is equal to desired, and
274 // the other is bigger. This is as close as we can get.
275 *desiredSize = computedSize;
276 return sampleSize;
277 }
278
279 // computedSize is too small. Make it larger.
280 while (sampleSize > 2) {
281 auto bigger = this->getSampledDimensions(sampleSize - 1);
282 if (bigger == *desiredSize || !smaller_than(bigger, *desiredSize)) {
283 *desiredSize = bigger;
284 return sampleSize - 1;
285 }
286 sampleSize--;
287 }
288
289 *desiredSize = fInfo.dimensions();
290 return 1;
291}
292
msarett3d9d7a72015-10-21 10:27:10 -0700293SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const {
294 if (!is_valid_sample_size(sampleSize)) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400295 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700296 }
297
scroggo501b7342015-11-03 07:55:11 -0800298 // Fast path for when we are not scaling.
299 if (1 == sampleSize) {
300 return fInfo.dimensions();
301 }
302
Leon Scroggins III95d0c8d2019-01-30 15:33:09 -0500303 auto dims = this->onGetSampledDimensions(sampleSize);
304 if (fOrientationBehavior == SkAndroidCodec::ExifOrientationBehavior::kIgnore
305 || !SkPixmapPriv::ShouldSwapWidthHeight(fCodec->getOrigin())) {
306 return dims;
307 }
308
309 return { dims.height(), dims.width() };
msarett3d9d7a72015-10-21 10:27:10 -0700310}
311
312bool SkAndroidCodec::getSupportedSubset(SkIRect* desiredSubset) const {
313 if (!desiredSubset || !is_valid_subset(*desiredSubset, fInfo.dimensions())) {
314 return false;
315 }
316
317 return this->onGetSupportedSubset(desiredSubset);
318}
319
320SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const {
321 if (!is_valid_sample_size(sampleSize)) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400322 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700323 }
324
325 // We require that the input subset is a subset that is supported by SkAndroidCodec.
326 // We test this by calling getSupportedSubset() and verifying that no modifications
327 // are made to the subset.
328 SkIRect copySubset = subset;
329 if (!this->getSupportedSubset(&copySubset) || copySubset != subset) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400330 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700331 }
332
scroggo501b7342015-11-03 07:55:11 -0800333 // If the subset is the entire image, for consistency, use getSampledDimensions().
msarett3d9d7a72015-10-21 10:27:10 -0700334 if (fInfo.dimensions() == subset.size()) {
scroggo501b7342015-11-03 07:55:11 -0800335 return this->getSampledDimensions(sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700336 }
337
338 // This should perhaps call a virtual function, but currently both of our subclasses
339 // want the same implementation.
Hal Canaryfafe1352017-04-11 12:12:02 -0400340 return {get_scaled_dimension(subset.width(), sampleSize),
341 get_scaled_dimension(subset.height(), sampleSize)};
msarett3d9d7a72015-10-21 10:27:10 -0700342}
343
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500344static bool acceptable_result(SkCodec::Result result) {
345 switch (result) {
346 // These results mean a partial or complete image. They should be considered
347 // a success by SkPixmapPriv.
348 case SkCodec::kSuccess:
349 case SkCodec::kIncompleteInput:
350 case SkCodec::kErrorInInput:
351 return true;
352 default:
353 return false;
354 }
355}
356
357SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& requestInfo,
358 void* requestPixels, size_t requestRowBytes, const AndroidOptions* options) {
359 if (!requestPixels) {
msarett3d9d7a72015-10-21 10:27:10 -0700360 return SkCodec::kInvalidParameters;
361 }
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500362 if (requestRowBytes < requestInfo.minRowBytes()) {
msarett3d9d7a72015-10-21 10:27:10 -0700363 return SkCodec::kInvalidParameters;
364 }
365
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500366 SkImageInfo adjustedInfo = fInfo;
367 if (ExifOrientationBehavior::kRespect == fOrientationBehavior
368 && SkPixmapPriv::ShouldSwapWidthHeight(fCodec->getOrigin())) {
369 adjustedInfo = SkPixmapPriv::SwapWidthHeight(adjustedInfo);
370 }
371
msarett3d9d7a72015-10-21 10:27:10 -0700372 AndroidOptions defaultOptions;
373 if (!options) {
374 options = &defaultOptions;
Leon Scrogginsfc4fdc52020-11-09 14:18:12 -0500375 } else {
376 if (options->fSubset) {
377 if (!is_valid_subset(*options->fSubset, adjustedInfo.dimensions())) {
378 return SkCodec::kInvalidParameters;
379 }
380
381 if (SkIRect::MakeSize(adjustedInfo.dimensions()) == *options->fSubset) {
382 // The caller wants the whole thing, rather than a subset. Modify
383 // the AndroidOptions passed to onGetAndroidPixels to not specify
384 // a subset.
385 defaultOptions = *options;
386 defaultOptions.fSubset = nullptr;
387 options = &defaultOptions;
388 }
msarett3d9d7a72015-10-21 10:27:10 -0700389 }
scroggo501b7342015-11-03 07:55:11 -0800390
Leon Scrogginsfc4fdc52020-11-09 14:18:12 -0500391 // To simplify frame compositing, force the client to use kIgnore and
392 // handle orientation themselves.
393 if (options->fFrameIndex != 0 && fOrientationBehavior == ExifOrientationBehavior::kRespect
394 && fCodec->getOrigin() != kDefault_SkEncodedOrigin) {
395 return SkCodec::kInvalidParameters;
scroggo501b7342015-11-03 07:55:11 -0800396 }
msarett3d9d7a72015-10-21 10:27:10 -0700397 }
398
Leon Scrogginsfc4fdc52020-11-09 14:18:12 -0500399 if (auto result = fCodec->handleFrameIndex(requestInfo, requestPixels, requestRowBytes,
400 *options, this); result != SkCodec::kSuccess) {
401 return result;
402 }
403
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500404 if (ExifOrientationBehavior::kIgnore == fOrientationBehavior) {
405 return this->onGetAndroidPixels(requestInfo, requestPixels, requestRowBytes, *options);
406 }
407
408 SkCodec::Result result;
409 auto decode = [this, options, &result](const SkPixmap& pm) {
410 result = this->onGetAndroidPixels(pm.info(), pm.writable_addr(), pm.rowBytes(), *options);
411 return acceptable_result(result);
412 };
413
414 SkPixmap dst(requestInfo, requestPixels, requestRowBytes);
415 if (SkPixmapPriv::Orient(dst, fCodec->getOrigin(), decode)) {
416 return result;
417 }
418
419 // Orient returned false. If onGetAndroidPixels succeeded, then Orient failed internally.
420 if (acceptable_result(result)) {
421 return SkCodec::kInternalError;
422 }
423
424 return result;
msarett3d9d7a72015-10-21 10:27:10 -0700425}
426
427SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels,
428 size_t rowBytes) {
429 return this->getAndroidPixels(info, pixels, rowBytes, nullptr);
430}