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