• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkAndroidCodecAdapter.h"
10 #include "SkCodec.h"
11 #include "SkCodecPriv.h"
12 #include "SkMakeUnique.h"
13 #include "SkPixmap.h"
14 #include "SkPixmapPriv.h"
15 #include "SkSampledCodec.h"
16 
is_valid_sample_size(int sampleSize)17 static 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 
22 /**
23  *  Loads the gamut as a set of three points (triangle).
24  */
load_gamut(SkPoint rgb[],const skcms_Matrix3x3 & xyz)25 static void load_gamut(SkPoint rgb[], const skcms_Matrix3x3& xyz) {
26     // rx = rX / (rX + rY + rZ)
27     // ry = rY / (rX + rY + rZ)
28     // gx, gy, bx, and gy are calulcated similarly.
29     for (int rgbIdx = 0; rgbIdx < 3; rgbIdx++) {
30         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;
33     }
34 }
35 
36 /**
37  *  Calculates the area of the triangular gamut.
38  */
calculate_area(SkPoint abc[])39 static 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 
46 static constexpr float kSRGB_D50_GamutArea = 0.084f;
47 
is_wide_gamut(const skcms_ICCProfile & profile)48 static bool is_wide_gamut(const skcms_ICCProfile& profile) {
49     // 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.
51     if (profile.has_toXYZD50) {
52         SkPoint rgb[3];
53         load_gamut(rgb, profile.toXYZD50);
54         return calculate_area(rgb) > kSRGB_D50_GamutArea;
55     }
56 
57     return false;
58 }
59 
adjust_info(SkCodec * codec,SkAndroidCodec::ExifOrientationBehavior orientationBehavior)60 static 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 
SkAndroidCodec(SkCodec * codec,ExifOrientationBehavior orientationBehavior)70 SkAndroidCodec::SkAndroidCodec(SkCodec* codec, ExifOrientationBehavior orientationBehavior)
71     : fInfo(adjust_info(codec, orientationBehavior))
72     , fOrientationBehavior(orientationBehavior)
73     , fCodec(codec)
74 {}
75 
~SkAndroidCodec()76 SkAndroidCodec::~SkAndroidCodec() {}
77 
MakeFromStream(std::unique_ptr<SkStream> stream,SkPngChunkReader * chunkReader)78 std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
79                                                                SkPngChunkReader* chunkReader) {
80     auto codec = SkCodec::MakeFromStream(std::move(stream), nullptr, chunkReader);
81     return MakeFromCodec(std::move(codec));
82 }
83 
MakeFromCodec(std::unique_ptr<SkCodec> codec,ExifOrientationBehavior orientationBehavior)84 std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromCodec(std::unique_ptr<SkCodec> codec,
85         ExifOrientationBehavior orientationBehavior) {
86     if (nullptr == codec) {
87         return nullptr;
88     }
89 
90     switch ((SkEncodedImageFormat)codec->getEncodedFormat()) {
91         case SkEncodedImageFormat::kPNG:
92         case SkEncodedImageFormat::kICO:
93         case SkEncodedImageFormat::kJPEG:
94         case SkEncodedImageFormat::kGIF:
95         case SkEncodedImageFormat::kBMP:
96         case SkEncodedImageFormat::kWBMP:
97         case SkEncodedImageFormat::kHEIF:
98             return skstd::make_unique<SkSampledCodec>(codec.release(), orientationBehavior);
99 
100 #ifdef SK_HAS_WEBP_LIBRARY
101         case SkEncodedImageFormat::kWEBP:
102 #endif
103 #ifdef SK_CODEC_DECODES_RAW
104         case SkEncodedImageFormat::kDNG:
105 #endif
106 #if defined(SK_HAS_WEBP_LIBRARY) || defined(SK_CODEC_DECODES_RAW)
107             return skstd::make_unique<SkAndroidCodecAdapter>(codec.release(), orientationBehavior);
108 #endif
109 
110         default:
111             return nullptr;
112     }
113 }
114 
MakeFromData(sk_sp<SkData> data,SkPngChunkReader * chunkReader)115 std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromData(sk_sp<SkData> data,
116                                                              SkPngChunkReader* chunkReader) {
117     if (!data) {
118         return nullptr;
119     }
120 
121     return MakeFromStream(SkMemoryStream::Make(std::move(data)), chunkReader);
122 }
123 
computeOutputColorType(SkColorType requestedColorType)124 SkColorType SkAndroidCodec::computeOutputColorType(SkColorType requestedColorType) {
125     bool highPrecision = fCodec->getEncodedInfo().bitsPerComponent() > 8;
126     switch (requestedColorType) {
127         case kARGB_4444_SkColorType:
128             return kN32_SkColorType;
129         case kN32_SkColorType:
130             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:
136             if (kGray_8_SkColorType == this->getInfo().colorType()) {
137                 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;
145         case kRGBA_F16_SkColorType:
146             return kRGBA_F16_SkColorType;
147         default:
148             break;
149     }
150 
151     // F16 is the Android default for high precision images.
152     return highPrecision ? kRGBA_F16_SkColorType : kN32_SkColorType;
153 }
154 
computeOutputAlphaType(bool requestedUnpremul)155 SkAlphaType 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 
computeOutputColorSpace(SkColorType outputColorType,sk_sp<SkColorSpace> prefColorSpace)162 sk_sp<SkColorSpace> SkAndroidCodec::computeOutputColorSpace(SkColorType outputColorType,
163                                                             sk_sp<SkColorSpace> prefColorSpace) {
164     switch (outputColorType) {
165         case kRGBA_F16_SkColorType:
166         case kRGB_565_SkColorType:
167         case kRGBA_8888_SkColorType:
168         case kBGRA_8888_SkColorType: {
169             // If |prefColorSpace| is supplied, choose it.
170             if (prefColorSpace) {
171                 return prefColorSpace;
172             }
173 
174             const skcms_ICCProfile* encodedProfile = fCodec->getEncodedInfo().profile();
175             if (encodedProfile) {
176                 if (auto encodedSpace = SkColorSpace::Make(*encodedProfile)) {
177                     // Leave the pixels in the encoded color space.  Color space conversion
178                     // will be handled after decode time.
179                     return encodedSpace;
180                 }
181 
182                 if (is_wide_gamut(*encodedProfile)) {
183                     return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDCIP3);
184                 }
185             }
186 
187             return SkColorSpace::MakeSRGB();
188         }
189         default:
190             // Color correction not supported for kGray.
191             return nullptr;
192     }
193 }
194 
supports_any_down_scale(const SkCodec * codec)195 static bool supports_any_down_scale(const SkCodec* codec) {
196     return codec->getEncodedFormat() == SkEncodedImageFormat::kWEBP;
197 }
198 
199 // There are a variety of ways two SkISizes could be compared. This method
200 // returns true if either dimensions of a is < that of b.
201 // computeSampleSize also uses the opposite, which means that both
202 // dimensions of a >= b.
smaller_than(const SkISize & a,const SkISize & b)203 static inline bool smaller_than(const SkISize& a, const SkISize& b) {
204     return a.width() < b.width() || a.height() < b.height();
205 }
206 
207 // Both dimensions of a > that of b.
strictly_bigger_than(const SkISize & a,const SkISize & b)208 static inline bool strictly_bigger_than(const SkISize& a, const SkISize& b) {
209     return a.width() > b.width() && a.height() > b.height();
210 }
211 
computeSampleSize(SkISize * desiredSize) const212 int SkAndroidCodec::computeSampleSize(SkISize* desiredSize) const {
213     SkASSERT(desiredSize);
214 
215     if (!desiredSize || *desiredSize == fInfo.dimensions()) {
216         return 1;
217     }
218 
219     if (smaller_than(fInfo.dimensions(), *desiredSize)) {
220         *desiredSize = fInfo.dimensions();
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 = fInfo.width()  / desiredSize->width();
235     int sampleY = fInfo.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 == fInfo.dimensions() || 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 = fInfo.dimensions();
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 fInfo.dimensions();
297     }
298 
299     auto dims = this->onGetSampledDimensions(sampleSize);
300     if (fOrientationBehavior == SkAndroidCodec::ExifOrientationBehavior::kIgnore
301             || !SkPixmapPriv::ShouldSwapWidthHeight(fCodec->getOrigin())) {
302         return dims;
303     }
304 
305     return { dims.height(), dims.width() };
306 }
307 
getSupportedSubset(SkIRect * desiredSubset) const308 bool 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 
getSampledSubsetDimensions(int sampleSize,const SkIRect & subset) const316 SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const {
317     if (!is_valid_sample_size(sampleSize)) {
318         return {0, 0};
319     }
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) {
326         return {0, 0};
327     }
328 
329     // If the subset is the entire image, for consistency, use getSampledDimensions().
330     if (fInfo.dimensions() == subset.size()) {
331         return this->getSampledDimensions(sampleSize);
332     }
333 
334     // This should perhaps call a virtual function, but currently both of our subclasses
335     // want the same implementation.
336     return {get_scaled_dimension(subset.width(), sampleSize),
337             get_scaled_dimension(subset.height(), sampleSize)};
338 }
339 
acceptable_result(SkCodec::Result result)340 static 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 
getAndroidPixels(const SkImageInfo & requestInfo,void * requestPixels,size_t requestRowBytes,const AndroidOptions * options)353 SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& requestInfo,
354         void* requestPixels, size_t requestRowBytes, const AndroidOptions* options) {
355     if (!requestPixels) {
356         return SkCodec::kInvalidParameters;
357     }
358     if (requestRowBytes < requestInfo.minRowBytes()) {
359         return SkCodec::kInvalidParameters;
360     }
361 
362     SkImageInfo adjustedInfo = fInfo;
363     if (ExifOrientationBehavior::kRespect == fOrientationBehavior
364             && SkPixmapPriv::ShouldSwapWidthHeight(fCodec->getOrigin())) {
365         adjustedInfo = SkPixmapPriv::SwapWidthHeight(adjustedInfo);
366     }
367 
368     AndroidOptions defaultOptions;
369     if (!options) {
370         options = &defaultOptions;
371     } else if (options->fSubset) {
372         if (!is_valid_subset(*options->fSubset, adjustedInfo.dimensions())) {
373             return SkCodec::kInvalidParameters;
374         }
375 
376         if (SkIRect::MakeSize(adjustedInfo.dimensions()) == *options->fSubset) {
377             // 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         }
384     }
385 
386     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;
407 }
408 
getAndroidPixels(const SkImageInfo & info,void * pixels,size_t rowBytes)409 SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels,
410         size_t rowBytes) {
411     return this->getAndroidPixels(info, pixels, rowBytes, nullptr);
412 }
413