blob: f988dfd1fd2b2a3a849da77e9af7506f632b33d3 [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"
14#include "src/core/SkMakeUnique.h"
15#include "src/core/SkPixmapPriv.h"
msarett3d9d7a72015-10-21 10:27:10 -070016
17static 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 Sarett2ae3a2e2017-02-15 08:48:02 -050022/**
23 * Loads the gamut as a set of three points (triangle).
24 */
Leon Scroggins III36f7e322018-08-27 11:55:46 -040025static void load_gamut(SkPoint rgb[], const skcms_Matrix3x3& xyz) {
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050026 // rx = rX / (rX + rY + rZ)
27 // ry = rY / (rX + rY + rZ)
28 // gx, gy, bx, and gy are calulcated similarly.
nagarajan.n6e17b6f2017-07-10 15:02:00 +053029 for (int rgbIdx = 0; rgbIdx < 3; rgbIdx++) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -040030 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.n6e17b6f2017-07-10 15:02:00 +053033 }
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050034}
35
36/**
37 * Calculates the area of the triangular gamut.
38 */
39static 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 III862c1962017-10-02 16:28:49 -040046static constexpr float kSRGB_D50_GamutArea = 0.084f;
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050047
Leon Scroggins III36f7e322018-08-27 11:55:46 -040048static bool is_wide_gamut(const skcms_ICCProfile& profile) {
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050049 // 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 III36f7e322018-08-27 11:55:46 -040051 if (profile.has_toXYZD50) {
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050052 SkPoint rgb[3];
Leon Scroggins III36f7e322018-08-27 11:55:46 -040053 load_gamut(rgb, profile.toXYZD50);
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050054 return calculate_area(rgb) > kSRGB_D50_GamutArea;
55 }
56
57 return false;
58}
59
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050060static 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
70SkAndroidCodec::SkAndroidCodec(SkCodec* codec, ExifOrientationBehavior orientationBehavior)
71 : fInfo(adjust_info(codec, orientationBehavior))
72 , fOrientationBehavior(orientationBehavior)
msarett90c4d5f2015-12-10 13:09:24 -080073 , fCodec(codec)
msarett3d9d7a72015-10-21 10:27:10 -070074{}
75
Leon Scroggins III07418182017-08-15 12:24:02 -040076SkAndroidCodec::~SkAndroidCodec() {}
77
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050078std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
79 SkPngChunkReader* chunkReader) {
Mike Reedede7bac2017-07-23 15:30:02 -040080 auto codec = SkCodec::MakeFromStream(std::move(stream), nullptr, chunkReader);
Leon Scroggins III7397d7a2018-01-04 13:26:30 -050081 return MakeFromCodec(std::move(codec));
82}
83
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050084std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromCodec(std::unique_ptr<SkCodec> codec,
85 ExifOrientationBehavior orientationBehavior) {
msarett3d9d7a72015-10-21 10:27:10 -070086 if (nullptr == codec) {
87 return nullptr;
88 }
89
Hal Canarydb683012016-11-23 08:55:18 -070090 switch ((SkEncodedImageFormat)codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -070091 case SkEncodedImageFormat::kPNG:
92 case SkEncodedImageFormat::kICO:
Hal Canarydb683012016-11-23 08:55:18 -070093 case SkEncodedImageFormat::kJPEG:
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -040094#ifndef SK_HAS_WUFFS_LIBRARY
Hal Canarydb683012016-11-23 08:55:18 -070095 case SkEncodedImageFormat::kGIF:
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -040096#endif
Hal Canarydb683012016-11-23 08:55:18 -070097 case SkEncodedImageFormat::kBMP:
98 case SkEncodedImageFormat::kWBMP:
Leon Scroggins III04be2b52017-08-17 15:13:20 -040099 case SkEncodedImageFormat::kHEIF:
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500100 return skstd::make_unique<SkSampledCodec>(codec.release(), orientationBehavior);
Mike Klein8ae07c32019-05-15 12:00:42 -0500101
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -0400102#ifdef SK_HAS_WUFFS_LIBRARY
103 case SkEncodedImageFormat::kGIF:
104#endif
Hal Canarydb683012016-11-23 08:55:18 -0700105 case SkEncodedImageFormat::kWEBP:
Hal Canarydb683012016-11-23 08:55:18 -0700106 case SkEncodedImageFormat::kDNG:
Nigel Tao612a65d2018-11-09 10:10:31 +1100107 return skstd::make_unique<SkAndroidCodecAdapter>(codec.release(), orientationBehavior);
Nigel Tao612a65d2018-11-09 10:10:31 +1100108
msarett3d9d7a72015-10-21 10:27:10 -0700109 default:
msarett3d9d7a72015-10-21 10:27:10 -0700110 return nullptr;
111 }
112}
113
Mike Reedede7bac2017-07-23 15:30:02 -0400114std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromData(sk_sp<SkData> data,
115 SkPngChunkReader* chunkReader) {
msarett3d9d7a72015-10-21 10:27:10 -0700116 if (!data) {
117 return nullptr;
118 }
119
Mike Reed847068c2017-07-26 11:35:53 -0400120 return MakeFromStream(SkMemoryStream::Make(std::move(data)), chunkReader);
msarett3d9d7a72015-10-21 10:27:10 -0700121}
122
msarett9a0e3462015-12-11 07:38:50 -0800123SkColorType SkAndroidCodec::computeOutputColorType(SkColorType requestedColorType) {
Leon Scroggins1793e7b2017-12-05 15:38:14 +0000124 bool highPrecision = fCodec->getEncodedInfo().bitsPerComponent() > 8;
msarett9a0e3462015-12-11 07:38:50 -0800125 switch (requestedColorType) {
126 case kARGB_4444_SkColorType:
msarett9a0e3462015-12-11 07:38:50 -0800127 return kN32_SkColorType;
Matt Sarett8dcc84f2016-12-14 10:23:41 -0500128 case kN32_SkColorType:
msarett9a0e3462015-12-11 07:38:50 -0800129 break;
130 case kAlpha_8_SkColorType:
131 // Fall through to kGray_8. Before kGray_8_SkColorType existed,
132 // we allowed clients to request kAlpha_8 when they wanted a
133 // grayscale decode.
134 case kGray_8_SkColorType:
Matt Sarett8db74f12017-06-14 13:02:05 +0000135 if (kGray_8_SkColorType == this->getInfo().colorType()) {
msarett9a0e3462015-12-11 07:38:50 -0800136 return kGray_8_SkColorType;
137 }
138 break;
139 case kRGB_565_SkColorType:
140 if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
141 return kRGB_565_SkColorType;
142 }
143 break;
Matt Sarett8dcc84f2016-12-14 10:23:41 -0500144 case kRGBA_F16_SkColorType:
145 return kRGBA_F16_SkColorType;
msarett9a0e3462015-12-11 07:38:50 -0800146 default:
147 break;
148 }
149
Leon Scroggins1793e7b2017-12-05 15:38:14 +0000150 // F16 is the Android default for high precision images.
151 return highPrecision ? kRGBA_F16_SkColorType : kN32_SkColorType;
msarett9a0e3462015-12-11 07:38:50 -0800152}
153
154SkAlphaType SkAndroidCodec::computeOutputAlphaType(bool requestedUnpremul) {
155 if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
156 return kOpaque_SkAlphaType;
157 }
158 return requestedUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
159}
160
Matt Sarett68feef42017-04-11 09:51:32 -0400161sk_sp<SkColorSpace> SkAndroidCodec::computeOutputColorSpace(SkColorType outputColorType,
162 sk_sp<SkColorSpace> prefColorSpace) {
Matt Sarett966bb342016-12-12 16:30:13 -0500163 switch (outputColorType) {
Derek Sollenberger1be431f2019-01-30 11:21:33 -0500164 case kRGBA_F16_SkColorType:
165 case kRGB_565_SkColorType:
Leon Scroggins IIIc20b5f82017-07-13 08:05:29 -0400166 case kRGBA_8888_SkColorType:
167 case kBGRA_8888_SkColorType: {
Brian Osman82ebe042019-01-04 17:03:00 -0500168 // If |prefColorSpace| is supplied, choose it.
169 if (prefColorSpace) {
Matt Sarett68feef42017-04-11 09:51:32 -0400170 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)) {
Brian Osman82ebe042019-01-04 17:03:00 -0500182 return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDCIP3);
Leon Scroggins III227d4e12018-09-27 08:30:22 -0400183 }
Matt Sarett2ae3a2e2017-02-15 08:48:02 -0500184 }
185
Matt Sarettcf3f2342017-03-23 15:32:25 -0400186 return SkColorSpace::MakeSRGB();
Matt Sarett9341c982017-02-28 15:36:42 -0500187 }
Matt Sarett966bb342016-12-12 16:30:13 -0500188 default:
Matt Sarett3725f0a2017-03-28 14:34:20 -0400189 // Color correction not supported for kGray.
Matt Sarett966bb342016-12-12 16:30:13 -0500190 return nullptr;
191 }
192}
193
Leon Scroggins III07a722c2018-01-16 15:01:17 -0500194static bool supports_any_down_scale(const SkCodec* codec) {
195 return codec->getEncodedFormat() == SkEncodedImageFormat::kWEBP;
196}
197
198// There are a variety of ways two SkISizes could be compared. This method
199// returns true if either dimensions of a is < that of b.
200// computeSampleSize also uses the opposite, which means that both
201// dimensions of a >= b.
202static inline bool smaller_than(const SkISize& a, const SkISize& b) {
203 return a.width() < b.width() || a.height() < b.height();
204}
205
206// Both dimensions of a > that of b.
207static inline bool strictly_bigger_than(const SkISize& a, const SkISize& b) {
208 return a.width() > b.width() && a.height() > b.height();
209}
210
211int SkAndroidCodec::computeSampleSize(SkISize* desiredSize) const {
212 SkASSERT(desiredSize);
213
214 if (!desiredSize || *desiredSize == fInfo.dimensions()) {
215 return 1;
216 }
217
218 if (smaller_than(fInfo.dimensions(), *desiredSize)) {
219 *desiredSize = fInfo.dimensions();
220 return 1;
221 }
222
223 // Handle bad input:
224 if (desiredSize->width() < 1 || desiredSize->height() < 1) {
225 *desiredSize = SkISize::Make(std::max(1, desiredSize->width()),
226 std::max(1, desiredSize->height()));
227 }
228
229 if (supports_any_down_scale(fCodec.get())) {
230 return 1;
231 }
232
233 int sampleX = fInfo.width() / desiredSize->width();
234 int sampleY = fInfo.height() / desiredSize->height();
235 int sampleSize = std::min(sampleX, sampleY);
236 auto computedSize = this->getSampledDimensions(sampleSize);
237 if (computedSize == *desiredSize) {
238 return sampleSize;
239 }
240
241 if (computedSize == fInfo.dimensions() || sampleSize == 1) {
242 // Cannot downscale
243 *desiredSize = computedSize;
244 return 1;
245 }
246
247 if (strictly_bigger_than(computedSize, *desiredSize)) {
248 // See if there is a tighter fit.
249 while (true) {
250 auto smaller = this->getSampledDimensions(sampleSize + 1);
251 if (smaller == *desiredSize) {
252 return sampleSize + 1;
253 }
254 if (smaller == computedSize || smaller_than(smaller, *desiredSize)) {
255 // Cannot get any smaller without being smaller than desired.
256 *desiredSize = computedSize;
257 return sampleSize;
258 }
259
260 sampleSize++;
261 computedSize = smaller;
262 }
263
264 SkASSERT(false);
265 }
266
267 if (!smaller_than(computedSize, *desiredSize)) {
268 // This means one of the computed dimensions is equal to desired, and
269 // the other is bigger. This is as close as we can get.
270 *desiredSize = computedSize;
271 return sampleSize;
272 }
273
274 // computedSize is too small. Make it larger.
275 while (sampleSize > 2) {
276 auto bigger = this->getSampledDimensions(sampleSize - 1);
277 if (bigger == *desiredSize || !smaller_than(bigger, *desiredSize)) {
278 *desiredSize = bigger;
279 return sampleSize - 1;
280 }
281 sampleSize--;
282 }
283
284 *desiredSize = fInfo.dimensions();
285 return 1;
286}
287
msarett3d9d7a72015-10-21 10:27:10 -0700288SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const {
289 if (!is_valid_sample_size(sampleSize)) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400290 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700291 }
292
scroggo501b7342015-11-03 07:55:11 -0800293 // Fast path for when we are not scaling.
294 if (1 == sampleSize) {
295 return fInfo.dimensions();
296 }
297
Leon Scroggins III95d0c8d2019-01-30 15:33:09 -0500298 auto dims = this->onGetSampledDimensions(sampleSize);
299 if (fOrientationBehavior == SkAndroidCodec::ExifOrientationBehavior::kIgnore
300 || !SkPixmapPriv::ShouldSwapWidthHeight(fCodec->getOrigin())) {
301 return dims;
302 }
303
304 return { dims.height(), dims.width() };
msarett3d9d7a72015-10-21 10:27:10 -0700305}
306
307bool SkAndroidCodec::getSupportedSubset(SkIRect* desiredSubset) const {
308 if (!desiredSubset || !is_valid_subset(*desiredSubset, fInfo.dimensions())) {
309 return false;
310 }
311
312 return this->onGetSupportedSubset(desiredSubset);
313}
314
315SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const {
316 if (!is_valid_sample_size(sampleSize)) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400317 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700318 }
319
320 // We require that the input subset is a subset that is supported by SkAndroidCodec.
321 // We test this by calling getSupportedSubset() and verifying that no modifications
322 // are made to the subset.
323 SkIRect copySubset = subset;
324 if (!this->getSupportedSubset(&copySubset) || copySubset != subset) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400325 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700326 }
327
scroggo501b7342015-11-03 07:55:11 -0800328 // If the subset is the entire image, for consistency, use getSampledDimensions().
msarett3d9d7a72015-10-21 10:27:10 -0700329 if (fInfo.dimensions() == subset.size()) {
scroggo501b7342015-11-03 07:55:11 -0800330 return this->getSampledDimensions(sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700331 }
332
333 // This should perhaps call a virtual function, but currently both of our subclasses
334 // want the same implementation.
Hal Canaryfafe1352017-04-11 12:12:02 -0400335 return {get_scaled_dimension(subset.width(), sampleSize),
336 get_scaled_dimension(subset.height(), sampleSize)};
msarett3d9d7a72015-10-21 10:27:10 -0700337}
338
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500339static bool acceptable_result(SkCodec::Result result) {
340 switch (result) {
341 // These results mean a partial or complete image. They should be considered
342 // a success by SkPixmapPriv.
343 case SkCodec::kSuccess:
344 case SkCodec::kIncompleteInput:
345 case SkCodec::kErrorInInput:
346 return true;
347 default:
348 return false;
349 }
350}
351
352SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& requestInfo,
353 void* requestPixels, size_t requestRowBytes, const AndroidOptions* options) {
354 if (!requestPixels) {
msarett3d9d7a72015-10-21 10:27:10 -0700355 return SkCodec::kInvalidParameters;
356 }
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500357 if (requestRowBytes < requestInfo.minRowBytes()) {
msarett3d9d7a72015-10-21 10:27:10 -0700358 return SkCodec::kInvalidParameters;
359 }
360
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500361 SkImageInfo adjustedInfo = fInfo;
362 if (ExifOrientationBehavior::kRespect == fOrientationBehavior
363 && SkPixmapPriv::ShouldSwapWidthHeight(fCodec->getOrigin())) {
364 adjustedInfo = SkPixmapPriv::SwapWidthHeight(adjustedInfo);
365 }
366
msarett3d9d7a72015-10-21 10:27:10 -0700367 AndroidOptions defaultOptions;
368 if (!options) {
369 options = &defaultOptions;
370 } else if (options->fSubset) {
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500371 if (!is_valid_subset(*options->fSubset, adjustedInfo.dimensions())) {
msarett3d9d7a72015-10-21 10:27:10 -0700372 return SkCodec::kInvalidParameters;
373 }
scroggo501b7342015-11-03 07:55:11 -0800374
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500375 if (SkIRect::MakeSize(adjustedInfo.dimensions()) == *options->fSubset) {
scroggo501b7342015-11-03 07:55:11 -0800376 // The caller wants the whole thing, rather than a subset. Modify
377 // the AndroidOptions passed to onGetAndroidPixels to not specify
378 // a subset.
379 defaultOptions = *options;
380 defaultOptions.fSubset = nullptr;
381 options = &defaultOptions;
382 }
msarett3d9d7a72015-10-21 10:27:10 -0700383 }
384
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500385 if (ExifOrientationBehavior::kIgnore == fOrientationBehavior) {
386 return this->onGetAndroidPixels(requestInfo, requestPixels, requestRowBytes, *options);
387 }
388
389 SkCodec::Result result;
390 auto decode = [this, options, &result](const SkPixmap& pm) {
391 result = this->onGetAndroidPixels(pm.info(), pm.writable_addr(), pm.rowBytes(), *options);
392 return acceptable_result(result);
393 };
394
395 SkPixmap dst(requestInfo, requestPixels, requestRowBytes);
396 if (SkPixmapPriv::Orient(dst, fCodec->getOrigin(), decode)) {
397 return result;
398 }
399
400 // Orient returned false. If onGetAndroidPixels succeeded, then Orient failed internally.
401 if (acceptable_result(result)) {
402 return SkCodec::kInternalError;
403 }
404
405 return result;
msarett3d9d7a72015-10-21 10:27:10 -0700406}
407
408SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels,
409 size_t rowBytes) {
410 return this->getAndroidPixels(info, pixels, rowBytes, nullptr);
411}