• 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 "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     switch (requestedColorType) {
118         case kARGB_4444_SkColorType:
119             return kN32_SkColorType;
120         case kN32_SkColorType:
121             break;
122         case kAlpha_8_SkColorType:
123             // Fall through to kGray_8.  Before kGray_8_SkColorType existed,
124             // we allowed clients to request kAlpha_8 when they wanted a
125             // grayscale decode.
126         case kGray_8_SkColorType:
127             if (kGray_8_SkColorType == this->getInfo().colorType()) {
128                 return kGray_8_SkColorType;
129             }
130             break;
131         case kRGB_565_SkColorType:
132             if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
133                 return kRGB_565_SkColorType;
134             }
135             break;
136         case kRGBA_F16_SkColorType:
137             return kRGBA_F16_SkColorType;
138         default:
139             break;
140     }
141 
142     // F16 is the Android default for high precision images.
143     return highPrecision ? kRGBA_F16_SkColorType : kN32_SkColorType;
144 }
145 
computeOutputAlphaType(bool requestedUnpremul)146 SkAlphaType SkAndroidCodec::computeOutputAlphaType(bool requestedUnpremul) {
147     if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
148         return kOpaque_SkAlphaType;
149     }
150     return requestedUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
151 }
152 
computeOutputColorSpace(SkColorType outputColorType,sk_sp<SkColorSpace> prefColorSpace)153 sk_sp<SkColorSpace> SkAndroidCodec::computeOutputColorSpace(SkColorType outputColorType,
154                                                             sk_sp<SkColorSpace> prefColorSpace) {
155     switch (outputColorType) {
156         case kRGBA_F16_SkColorType:
157         case kRGB_565_SkColorType:
158         case kRGBA_8888_SkColorType:
159         case kBGRA_8888_SkColorType: {
160             // If |prefColorSpace| is supplied, choose it.
161             if (prefColorSpace) {
162                 return prefColorSpace;
163             }
164 
165             const skcms_ICCProfile* encodedProfile = fCodec->getEncodedInfo().profile();
166             if (encodedProfile) {
167                 if (auto encodedSpace = SkColorSpace::Make(*encodedProfile)) {
168                     // Leave the pixels in the encoded color space.  Color space conversion
169                     // will be handled after decode time.
170                     return encodedSpace;
171                 }
172 
173                 if (is_wide_gamut(*encodedProfile)) {
174                     return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3);
175                 }
176             }
177 
178             return SkColorSpace::MakeSRGB();
179         }
180         default:
181             // Color correction not supported for kGray.
182             return nullptr;
183     }
184 }
185 
supports_any_down_scale(const SkCodec * codec)186 static bool supports_any_down_scale(const SkCodec* codec) {
187     return codec->getEncodedFormat() == SkEncodedImageFormat::kWEBP;
188 }
189 
190 // There are a variety of ways two SkISizes could be compared. This method
191 // returns true if either dimensions of a is < that of b.
192 // computeSampleSize also uses the opposite, which means that both
193 // dimensions of a >= b.
smaller_than(const SkISize & a,const SkISize & b)194 static inline bool smaller_than(const SkISize& a, const SkISize& b) {
195     return a.width() < b.width() || a.height() < b.height();
196 }
197 
198 // Both dimensions of a > that of b.
strictly_bigger_than(const SkISize & a,const SkISize & b)199 static inline bool strictly_bigger_than(const SkISize& a, const SkISize& b) {
200     return a.width() > b.width() && a.height() > b.height();
201 }
202 
computeSampleSize(SkISize * desiredSize) const203 int SkAndroidCodec::computeSampleSize(SkISize* desiredSize) const {
204     SkASSERT(desiredSize);
205 
206     const auto origDims = fCodec->dimensions();
207     if (!desiredSize || *desiredSize == origDims) {
208         return 1;
209     }
210 
211     if (smaller_than(origDims, *desiredSize)) {
212         *desiredSize = origDims;
213         return 1;
214     }
215 
216     // Handle bad input:
217     if (desiredSize->width() < 1 || desiredSize->height() < 1) {
218         *desiredSize = SkISize::Make(std::max(1, desiredSize->width()),
219                                      std::max(1, desiredSize->height()));
220     }
221 
222     if (supports_any_down_scale(fCodec.get())) {
223         return 1;
224     }
225 
226     int sampleX = origDims.width()  / desiredSize->width();
227     int sampleY = origDims.height() / desiredSize->height();
228     int sampleSize = std::min(sampleX, sampleY);
229     auto computedSize = this->getSampledDimensions(sampleSize);
230     if (computedSize == *desiredSize) {
231         return sampleSize;
232     }
233 
234     if (computedSize == origDims || sampleSize == 1) {
235         // Cannot downscale
236         *desiredSize = computedSize;
237         return 1;
238     }
239 
240     if (strictly_bigger_than(computedSize, *desiredSize)) {
241         // See if there is a tighter fit.
242         while (true) {
243             auto smaller = this->getSampledDimensions(sampleSize + 1);
244             if (smaller == *desiredSize) {
245                 return sampleSize + 1;
246             }
247             if (smaller == computedSize || smaller_than(smaller, *desiredSize)) {
248                 // Cannot get any smaller without being smaller than desired.
249                 *desiredSize = computedSize;
250                 return sampleSize;
251             }
252 
253             sampleSize++;
254             computedSize = smaller;
255         }
256 
257         SkASSERT(false);
258     }
259 
260     if (!smaller_than(computedSize, *desiredSize)) {
261         // This means one of the computed dimensions is equal to desired, and
262         // the other is bigger. This is as close as we can get.
263         *desiredSize = computedSize;
264         return sampleSize;
265     }
266 
267     // computedSize is too small. Make it larger.
268     while (sampleSize > 2) {
269         auto bigger = this->getSampledDimensions(sampleSize - 1);
270         if (bigger == *desiredSize || !smaller_than(bigger, *desiredSize)) {
271             *desiredSize = bigger;
272             return sampleSize - 1;
273         }
274         sampleSize--;
275     }
276 
277     *desiredSize = origDims;
278     return 1;
279 }
280 
getSampledDimensions(int sampleSize) const281 SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const {
282     if (!is_valid_sample_size(sampleSize)) {
283         return {0, 0};
284     }
285 
286     // Fast path for when we are not scaling.
287     if (1 == sampleSize) {
288         return fCodec->dimensions();
289     }
290 
291     return this->onGetSampledDimensions(sampleSize);
292 }
293 
getSupportedSubset(SkIRect * desiredSubset) const294 bool SkAndroidCodec::getSupportedSubset(SkIRect* desiredSubset) const {
295     if (!desiredSubset || !is_valid_subset(*desiredSubset, fCodec->dimensions())) {
296         return false;
297     }
298 
299     return this->onGetSupportedSubset(desiredSubset);
300 }
301 
getSampledSubsetDimensions(int sampleSize,const SkIRect & subset) const302 SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const {
303     if (!is_valid_sample_size(sampleSize)) {
304         return {0, 0};
305     }
306 
307     // We require that the input subset is a subset that is supported by SkAndroidCodec.
308     // We test this by calling getSupportedSubset() and verifying that no modifications
309     // are made to the subset.
310     SkIRect copySubset = subset;
311     if (!this->getSupportedSubset(&copySubset) || copySubset != subset) {
312         return {0, 0};
313     }
314 
315     // If the subset is the entire image, for consistency, use getSampledDimensions().
316     if (fCodec->dimensions() == subset.size()) {
317         return this->getSampledDimensions(sampleSize);
318     }
319 
320     // This should perhaps call a virtual function, but currently both of our subclasses
321     // want the same implementation.
322     return {get_scaled_dimension(subset.width(), sampleSize),
323             get_scaled_dimension(subset.height(), sampleSize)};
324 }
325 
getAndroidPixels(const SkImageInfo & requestInfo,void * requestPixels,size_t requestRowBytes,const AndroidOptions * options)326 SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& requestInfo,
327         void* requestPixels, size_t requestRowBytes, const AndroidOptions* options) {
328     if (!requestPixels) {
329         return SkCodec::kInvalidParameters;
330     }
331     if (requestRowBytes < requestInfo.minRowBytes()) {
332         return SkCodec::kInvalidParameters;
333     }
334 
335     AndroidOptions defaultOptions;
336     if (!options) {
337         options = &defaultOptions;
338     } else {
339         if (options->fSubset) {
340             if (!is_valid_subset(*options->fSubset, fCodec->dimensions())) {
341                 return SkCodec::kInvalidParameters;
342             }
343 
344             if (SkIRect::MakeSize(fCodec->dimensions()) == *options->fSubset) {
345                 // The caller wants the whole thing, rather than a subset. Modify
346                 // the AndroidOptions passed to onGetAndroidPixels to not specify
347                 // a subset.
348                 defaultOptions = *options;
349                 defaultOptions.fSubset = nullptr;
350                 options = &defaultOptions;
351             }
352         }
353     }
354 
355     if (auto result = fCodec->handleFrameIndex(requestInfo, requestPixels, requestRowBytes,
356             *options, this); result != SkCodec::kSuccess) {
357         return result;
358     }
359 
360     return this->onGetAndroidPixels(requestInfo, requestPixels, requestRowBytes, *options);
361 }
362 
getAndroidPixels(const SkImageInfo & info,void * pixels,size_t rowBytes)363 SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels,
364         size_t rowBytes) {
365     return this->getAndroidPixels(info, pixels, rowBytes, nullptr);
366 }
367