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 "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
is_valid_sample_size(int sampleSize)15 static bool is_valid_sample_size(int sampleSize) {
16 // FIXME: As Leon has mentioned elsewhere, surely there is also a maximum sampleSize?
17 return sampleSize > 0;
18 }
19
20 /**
21 * Loads the gamut as a set of three points (triangle).
22 */
load_gamut(SkPoint rgb[],const skcms_Matrix3x3 & xyz)23 static void load_gamut(SkPoint rgb[], const skcms_Matrix3x3& xyz) {
24 // rx = rX / (rX + rY + rZ)
25 // ry = rY / (rX + rY + rZ)
26 // gx, gy, bx, and gy are calulcated similarly.
27 for (int rgbIdx = 0; rgbIdx < 3; rgbIdx++) {
28 float sum = xyz.vals[rgbIdx][0] + xyz.vals[rgbIdx][1] + xyz.vals[rgbIdx][2];
29 rgb[rgbIdx].fX = xyz.vals[rgbIdx][0] / sum;
30 rgb[rgbIdx].fY = xyz.vals[rgbIdx][1] / sum;
31 }
32 }
33
34 /**
35 * Calculates the area of the triangular gamut.
36 */
calculate_area(SkPoint abc[])37 static float calculate_area(SkPoint abc[]) {
38 SkPoint a = abc[0];
39 SkPoint b = abc[1];
40 SkPoint c = abc[2];
41 return 0.5f * SkTAbs(a.fX*b.fY + b.fX*c.fY - a.fX*c.fY - c.fX*b.fY - b.fX*a.fY);
42 }
43
44 static constexpr float kSRGB_D50_GamutArea = 0.084f;
45
is_wide_gamut(const skcms_ICCProfile & profile)46 static bool is_wide_gamut(const skcms_ICCProfile& profile) {
47 // Determine if the source image has a gamut that is wider than sRGB. If so, we
48 // will use P3 as the output color space to avoid clipping the gamut.
49 if (profile.has_toXYZD50) {
50 SkPoint rgb[3];
51 load_gamut(rgb, profile.toXYZD50);
52 return calculate_area(rgb) > kSRGB_D50_GamutArea;
53 }
54
55 return false;
56 }
57
SkAndroidCodec(SkCodec * codec)58 SkAndroidCodec::SkAndroidCodec(SkCodec* codec)
59 : fInfo(codec->getInfo())
60 , fCodec(codec)
61 {}
62
~SkAndroidCodec()63 SkAndroidCodec::~SkAndroidCodec() {}
64
MakeFromStream(std::unique_ptr<SkStream> stream,SkPngChunkReader * chunkReader)65 std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
66 SkPngChunkReader* chunkReader) {
67 auto codec = SkCodec::MakeFromStream(std::move(stream), nullptr, chunkReader);
68 return MakeFromCodec(std::move(codec));
69 }
70
MakeFromCodec(std::unique_ptr<SkCodec> codec)71 std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromCodec(std::unique_ptr<SkCodec> codec) {
72 if (nullptr == codec) {
73 return nullptr;
74 }
75
76 switch ((SkEncodedImageFormat)codec->getEncodedFormat()) {
77 case SkEncodedImageFormat::kPNG:
78 case SkEncodedImageFormat::kICO:
79 case SkEncodedImageFormat::kJPEG:
80 #ifndef SK_HAS_WUFFS_LIBRARY
81 case SkEncodedImageFormat::kGIF:
82 #endif
83 case SkEncodedImageFormat::kBMP:
84 case SkEncodedImageFormat::kWBMP:
85 case SkEncodedImageFormat::kHEIF:
86 case SkEncodedImageFormat::kAVIF:
87 return std::make_unique<SkSampledCodec>(codec.release());
88 #ifdef SK_HAS_WUFFS_LIBRARY
89 case SkEncodedImageFormat::kGIF:
90 #endif
91 #ifdef SK_CODEC_DECODES_WEBP
92 case SkEncodedImageFormat::kWEBP:
93 #endif
94 #ifdef SK_CODEC_DECODES_RAW
95 case SkEncodedImageFormat::kDNG:
96 #endif
97 #if defined(SK_CODEC_DECODES_WEBP) || defined(SK_CODEC_DECODES_RAW) || defined(SK_HAS_WUFFS_LIBRARY)
98 return std::make_unique<SkAndroidCodecAdapter>(codec.release());
99 #endif
100
101 default:
102 return nullptr;
103 }
104 }
105
MakeFromData(sk_sp<SkData> data,SkPngChunkReader * chunkReader)106 std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromData(sk_sp<SkData> data,
107 SkPngChunkReader* chunkReader) {
108 if (!data) {
109 return nullptr;
110 }
111
112 return MakeFromStream(SkMemoryStream::Make(std::move(data)), chunkReader);
113 }
114
computeOutputColorType(SkColorType requestedColorType)115 SkColorType SkAndroidCodec::computeOutputColorType(SkColorType requestedColorType) {
116 bool highPrecision = fCodec->getEncodedInfo().bitsPerComponent() > 8;
117 uint8_t colorDepth = fCodec->getEncodedInfo().getColorDepth();
118 switch (requestedColorType) {
119 case kARGB_4444_SkColorType:
120 return kN32_SkColorType;
121 case kN32_SkColorType:
122 break;
123 case kAlpha_8_SkColorType:
124 // Fall through to kGray_8. Before kGray_8_SkColorType existed,
125 // we allowed clients to request kAlpha_8 when they wanted a
126 // grayscale decode.
127 case kGray_8_SkColorType:
128 if (kGray_8_SkColorType == this->getInfo().colorType()) {
129 return kGray_8_SkColorType;
130 }
131 break;
132 case kRGB_565_SkColorType:
133 if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
134 return kRGB_565_SkColorType;
135 }
136 break;
137 case kRGBA_1010102_SkColorType:
138 if (colorDepth == 10) {
139 return kRGBA_1010102_SkColorType;
140 }
141 break;
142 case kRGBA_F16_SkColorType:
143 return kRGBA_F16_SkColorType;
144 default:
145 break;
146 }
147
148 // F16 is the Android default for high precision images.
149 return highPrecision ? kRGBA_F16_SkColorType :
150 (colorDepth == 10 ? kRGBA_1010102_SkColorType : kN32_SkColorType);
151 }
152
computeOutputAlphaType(bool requestedUnpremul)153 SkAlphaType SkAndroidCodec::computeOutputAlphaType(bool requestedUnpremul) {
154 if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
155 return kOpaque_SkAlphaType;
156 }
157 return requestedUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
158 }
159
computeOutputColorSpace(SkColorType outputColorType,sk_sp<SkColorSpace> prefColorSpace)160 sk_sp<SkColorSpace> SkAndroidCodec::computeOutputColorSpace(SkColorType outputColorType,
161 sk_sp<SkColorSpace> prefColorSpace) {
162 switch (outputColorType) {
163 case kRGBA_F16_SkColorType:
164 case kRGB_565_SkColorType:
165 case kRGBA_8888_SkColorType:
166 case kBGRA_8888_SkColorType:
167 case kRGBA_1010102_SkColorType: {
168 // If |prefColorSpace| is supplied, choose it.
169 if (prefColorSpace) {
170 return prefColorSpace;
171 }
172
173 const skcms_ICCProfile* encodedProfile = fCodec->getEncodedInfo().profile();
174 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 }
180
181 if (is_wide_gamut(*encodedProfile)) {
182 return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3);
183 }
184 }
185
186 return SkColorSpace::MakeSRGB();
187 }
188 default:
189 // Color correction not supported for kGray.
190 return nullptr;
191 }
192 }
193
supports_any_down_scale(const SkCodec * codec)194 static 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.
smaller_than(const SkISize & a,const SkISize & b)202 static 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.
strictly_bigger_than(const SkISize & a,const SkISize & b)207 static inline bool strictly_bigger_than(const SkISize& a, const SkISize& b) {
208 return a.width() > b.width() && a.height() > b.height();
209 }
210
computeSampleSize(SkISize * desiredSize) const211 int SkAndroidCodec::computeSampleSize(SkISize* desiredSize) const {
212 SkASSERT(desiredSize);
213
214 const auto origDims = fCodec->dimensions();
215 if (!desiredSize || *desiredSize == origDims) {
216 return 1;
217 }
218
219 if (smaller_than(origDims, *desiredSize)) {
220 *desiredSize = origDims;
221 return 1;
222 }
223
224 // Handle bad input:
225 if (desiredSize->width() < 1 || desiredSize->height() < 1) {
226 *desiredSize = SkISize::Make(std::max(1, desiredSize->width()),
227 std::max(1, desiredSize->height()));
228 }
229
230 if (supports_any_down_scale(fCodec.get())) {
231 return 1;
232 }
233
234 int sampleX = origDims.width() / desiredSize->width();
235 int sampleY = origDims.height() / desiredSize->height();
236 int sampleSize = std::min(sampleX, sampleY);
237 auto computedSize = this->getSampledDimensions(sampleSize);
238 if (computedSize == *desiredSize) {
239 return sampleSize;
240 }
241
242 if (computedSize == origDims || sampleSize == 1) {
243 // Cannot downscale
244 *desiredSize = computedSize;
245 return 1;
246 }
247
248 if (strictly_bigger_than(computedSize, *desiredSize)) {
249 // See if there is a tighter fit.
250 while (true) {
251 auto smaller = this->getSampledDimensions(sampleSize + 1);
252 if (smaller == *desiredSize) {
253 return sampleSize + 1;
254 }
255 if (smaller == computedSize || smaller_than(smaller, *desiredSize)) {
256 // Cannot get any smaller without being smaller than desired.
257 *desiredSize = computedSize;
258 return sampleSize;
259 }
260
261 sampleSize++;
262 computedSize = smaller;
263 }
264
265 SkASSERT(false);
266 }
267
268 if (!smaller_than(computedSize, *desiredSize)) {
269 // This means one of the computed dimensions is equal to desired, and
270 // the other is bigger. This is as close as we can get.
271 *desiredSize = computedSize;
272 return sampleSize;
273 }
274
275 // computedSize is too small. Make it larger.
276 while (sampleSize > 2) {
277 auto bigger = this->getSampledDimensions(sampleSize - 1);
278 if (bigger == *desiredSize || !smaller_than(bigger, *desiredSize)) {
279 *desiredSize = bigger;
280 return sampleSize - 1;
281 }
282 sampleSize--;
283 }
284
285 *desiredSize = origDims;
286 return 1;
287 }
288
getSampledDimensions(int sampleSize) const289 SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const {
290 if (!is_valid_sample_size(sampleSize)) {
291 return {0, 0};
292 }
293
294 // Fast path for when we are not scaling.
295 if (1 == sampleSize) {
296 return fCodec->dimensions();
297 }
298
299 return this->onGetSampledDimensions(sampleSize);
300 }
301
getSupportedSubset(SkIRect * desiredSubset) const302 bool SkAndroidCodec::getSupportedSubset(SkIRect* desiredSubset) const {
303 if (!desiredSubset || !is_valid_subset(*desiredSubset, fCodec->dimensions())) {
304 return false;
305 }
306
307 return this->onGetSupportedSubset(desiredSubset);
308 }
309
getSampledSubsetDimensions(int sampleSize,const SkIRect & subset) const310 SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const {
311 if (!is_valid_sample_size(sampleSize)) {
312 return {0, 0};
313 }
314
315 // We require that the input subset is a subset that is supported by SkAndroidCodec.
316 // We test this by calling getSupportedSubset() and verifying that no modifications
317 // are made to the subset.
318 SkIRect copySubset = subset;
319 if (!this->getSupportedSubset(©Subset) || copySubset != subset) {
320 return {0, 0};
321 }
322
323 // If the subset is the entire image, for consistency, use getSampledDimensions().
324 if (fCodec->dimensions() == subset.size()) {
325 return this->getSampledDimensions(sampleSize);
326 }
327
328 // This should perhaps call a virtual function, but currently both of our subclasses
329 // want the same implementation.
330 return {get_scaled_dimension(subset.width(), sampleSize),
331 get_scaled_dimension(subset.height(), sampleSize)};
332 }
333
getAndroidPixels(const SkImageInfo & requestInfo,void * requestPixels,size_t requestRowBytes,const AndroidOptions * options)334 SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& requestInfo,
335 void* requestPixels, size_t requestRowBytes, const AndroidOptions* options) {
336 if (!requestPixels) {
337 return SkCodec::kInvalidParameters;
338 }
339 if (requestRowBytes < requestInfo.minRowBytes()) {
340 return SkCodec::kInvalidParameters;
341 }
342
343 AndroidOptions defaultOptions;
344 if (!options) {
345 options = &defaultOptions;
346 } else {
347 if (options->fSubset) {
348 if (!is_valid_subset(*options->fSubset, fCodec->dimensions())) {
349 return SkCodec::kInvalidParameters;
350 }
351
352 if (SkIRect::MakeSize(fCodec->dimensions()) == *options->fSubset) {
353 // The caller wants the whole thing, rather than a subset. Modify
354 // the AndroidOptions passed to onGetAndroidPixels to not specify
355 // a subset.
356 defaultOptions = *options;
357 defaultOptions.fSubset = nullptr;
358 options = &defaultOptions;
359 }
360 }
361 }
362
363 if (auto result = fCodec->handleFrameIndex(requestInfo, requestPixels, requestRowBytes,
364 *options, this); result != SkCodec::kSuccess) {
365 return result;
366 }
367
368 return this->onGetAndroidPixels(requestInfo, requestPixels, requestRowBytes, *options);
369 }
370
getAndroidPixels(const SkImageInfo & info,void * pixels,size_t rowBytes)371 SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels,
372 size_t rowBytes) {
373 return this->getAndroidPixels(info, pixels, rowBytes, nullptr);
374 }
375