blob: 8f3146ed1696bbd68538b18b741f89a513500f3e [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
8#include "SkAndroidCodec.h"
9#include "SkCodec.h"
10#include "SkCodecPriv.h"
yujieqin916de9f2016-01-25 08:26:16 -080011#include "SkRawAdapterCodec.h"
msarett3d9d7a72015-10-21 10:27:10 -070012#include "SkSampledCodec.h"
13#include "SkWebpAdapterCodec.h"
14
15static 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
Mike Reed58050132017-07-12 22:10:29 -040020#ifdef SK_SUPPORT_LEGACY_INDEX_8_COLORTYPE
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050021/**
22 * Loads the gamut as a set of three points (triangle).
23 */
24static void load_gamut(SkPoint rgb[], const SkMatrix44& xyz) {
25 // rx = rX / (rX + rY + rZ)
26 // ry = rY / (rX + rY + rZ)
27 // gx, gy, bx, and gy are calulcated similarly.
28 float rSum = xyz.get(0, 0) + xyz.get(1, 0) + xyz.get(2, 0);
29 float gSum = xyz.get(0, 1) + xyz.get(1, 1) + xyz.get(2, 1);
30 float bSum = xyz.get(0, 2) + xyz.get(1, 2) + xyz.get(2, 2);
31 rgb[0].fX = xyz.get(0, 0) / rSum;
32 rgb[0].fY = xyz.get(1, 0) / rSum;
33 rgb[1].fX = xyz.get(0, 1) / gSum;
34 rgb[1].fY = xyz.get(1, 1) / gSum;
35 rgb[2].fX = xyz.get(0, 2) / bSum;
36 rgb[2].fY = xyz.get(1, 2) / bSum;
37}
38
39/**
40 * Calculates the area of the triangular gamut.
41 */
42static float calculate_area(SkPoint abc[]) {
43 SkPoint a = abc[0];
44 SkPoint b = abc[1];
45 SkPoint c = abc[2];
46 return 0.5f * SkTAbs(a.fX*b.fY + b.fX*c.fY - a.fX*c.fY - c.fX*b.fY - b.fX*a.fY);
47}
48
49static const float kSRGB_D50_GamutArea = 0.084f;
50
51static bool is_wide_gamut(const SkColorSpace* colorSpace) {
52 // Determine if the source image has a gamut that is wider than sRGB. If so, we
53 // will use P3 as the output color space to avoid clipping the gamut.
54 const SkMatrix44* toXYZD50 = as_CSB(colorSpace)->toXYZD50();
55 if (toXYZD50) {
56 SkPoint rgb[3];
57 load_gamut(rgb, *toXYZD50);
58 return calculate_area(rgb) > kSRGB_D50_GamutArea;
59 }
60
61 return false;
62}
Mike Reed58050132017-07-12 22:10:29 -040063#endif
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050064
msarett90c4d5f2015-12-10 13:09:24 -080065SkAndroidCodec::SkAndroidCodec(SkCodec* codec)
66 : fInfo(codec->getInfo())
67 , fCodec(codec)
msarett3d9d7a72015-10-21 10:27:10 -070068{}
69
msarett7d5105c2015-12-02 07:02:41 -080070SkAndroidCodec* SkAndroidCodec::NewFromStream(SkStream* stream, SkPngChunkReader* chunkReader) {
Ben Wagner145dbcd2016-11-03 14:40:50 -040071 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream, chunkReader));
msarett3d9d7a72015-10-21 10:27:10 -070072 if (nullptr == codec) {
73 return nullptr;
74 }
75
Hal Canarydb683012016-11-23 08:55:18 -070076 switch ((SkEncodedImageFormat)codec->getEncodedFormat()) {
msarettad3a5c62016-05-06 07:21:26 -070077#ifdef SK_HAS_PNG_LIBRARY
Hal Canarydb683012016-11-23 08:55:18 -070078 case SkEncodedImageFormat::kPNG:
79 case SkEncodedImageFormat::kICO:
msarett39b2d5a2016-02-17 08:26:31 -080080#endif
msarettad3a5c62016-05-06 07:21:26 -070081#ifdef SK_HAS_JPEG_LIBRARY
Hal Canarydb683012016-11-23 08:55:18 -070082 case SkEncodedImageFormat::kJPEG:
msarett39b2d5a2016-02-17 08:26:31 -080083#endif
Hal Canarydb683012016-11-23 08:55:18 -070084 case SkEncodedImageFormat::kGIF:
85 case SkEncodedImageFormat::kBMP:
86 case SkEncodedImageFormat::kWBMP:
mtklein18300a32016-03-16 13:53:35 -070087 return new SkSampledCodec(codec.release());
msarettad3a5c62016-05-06 07:21:26 -070088#ifdef SK_HAS_WEBP_LIBRARY
Hal Canarydb683012016-11-23 08:55:18 -070089 case SkEncodedImageFormat::kWEBP:
mtklein18300a32016-03-16 13:53:35 -070090 return new SkWebpAdapterCodec((SkWebpCodec*) codec.release());
msarett39b2d5a2016-02-17 08:26:31 -080091#endif
yujieqin916de9f2016-01-25 08:26:16 -080092#ifdef SK_CODEC_DECODES_RAW
Hal Canarydb683012016-11-23 08:55:18 -070093 case SkEncodedImageFormat::kDNG:
mtklein18300a32016-03-16 13:53:35 -070094 return new SkRawAdapterCodec((SkRawCodec*)codec.release());
yujieqin916de9f2016-01-25 08:26:16 -080095#endif
msarett3d9d7a72015-10-21 10:27:10 -070096 default:
msarett3d9d7a72015-10-21 10:27:10 -070097 return nullptr;
98 }
99}
100
reed42943c82016-09-12 12:01:44 -0700101SkAndroidCodec* SkAndroidCodec::NewFromData(sk_sp<SkData> data, SkPngChunkReader* chunkReader) {
msarett3d9d7a72015-10-21 10:27:10 -0700102 if (!data) {
103 return nullptr;
104 }
105
msarett7d5105c2015-12-02 07:02:41 -0800106 return NewFromStream(new SkMemoryStream(data), chunkReader);
msarett3d9d7a72015-10-21 10:27:10 -0700107}
108
msarett9a0e3462015-12-11 07:38:50 -0800109SkColorType SkAndroidCodec::computeOutputColorType(SkColorType requestedColorType) {
Matt Sarett8dcc84f2016-12-14 10:23:41 -0500110 bool highPrecision = fCodec->getEncodedInfo().bitsPerComponent() > 8;
msarett9a0e3462015-12-11 07:38:50 -0800111 switch (requestedColorType) {
112 case kARGB_4444_SkColorType:
msarett9a0e3462015-12-11 07:38:50 -0800113 return kN32_SkColorType;
Matt Sarett8dcc84f2016-12-14 10:23:41 -0500114 case kN32_SkColorType:
Mike Reed58050132017-07-12 22:10:29 -0400115#ifdef SK_SUPPORT_LEGACY_INDEX_8_COLORTYPE
msarett9a0e3462015-12-11 07:38:50 -0800116 case kIndex_8_SkColorType:
Mike Reed58050132017-07-12 22:10:29 -0400117#endif
msarett9a0e3462015-12-11 07:38:50 -0800118 break;
119 case kAlpha_8_SkColorType:
120 // Fall through to kGray_8. Before kGray_8_SkColorType existed,
121 // we allowed clients to request kAlpha_8 when they wanted a
122 // grayscale decode.
123 case kGray_8_SkColorType:
Matt Sarett8db74f12017-06-14 13:02:05 +0000124 if (kGray_8_SkColorType == this->getInfo().colorType()) {
msarett9a0e3462015-12-11 07:38:50 -0800125 return kGray_8_SkColorType;
126 }
127 break;
128 case kRGB_565_SkColorType:
129 if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
130 return kRGB_565_SkColorType;
131 }
132 break;
Matt Sarett8dcc84f2016-12-14 10:23:41 -0500133 case kRGBA_F16_SkColorType:
134 return kRGBA_F16_SkColorType;
msarett9a0e3462015-12-11 07:38:50 -0800135 default:
136 break;
137 }
138
Matt Sarett8db74f12017-06-14 13:02:05 +0000139 // F16 is the Android default for high precision images.
140 return highPrecision ? kRGBA_F16_SkColorType : kN32_SkColorType;
msarett9a0e3462015-12-11 07:38:50 -0800141}
142
143SkAlphaType SkAndroidCodec::computeOutputAlphaType(bool requestedUnpremul) {
144 if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
145 return kOpaque_SkAlphaType;
146 }
147 return requestedUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
148}
149
Matt Sarett68feef42017-04-11 09:51:32 -0400150sk_sp<SkColorSpace> SkAndroidCodec::computeOutputColorSpace(SkColorType outputColorType,
151 sk_sp<SkColorSpace> prefColorSpace) {
Matt Sarett966bb342016-12-12 16:30:13 -0500152 switch (outputColorType) {
153 case kRGBA_8888_SkColorType:
154 case kBGRA_8888_SkColorType:
Mike Reed58050132017-07-12 22:10:29 -0400155#ifdef SK_SUPPORT_LEGACY_INDEX_8_COLORTYPE
Matt Sarett9341c982017-02-28 15:36:42 -0500156 case kIndex_8_SkColorType: {
Matt Sarett68feef42017-04-11 09:51:32 -0400157 // If |prefColorSpace| is supported, choose it.
Matt Sarett9341c982017-02-28 15:36:42 -0500158 SkColorSpaceTransferFn fn;
Matt Sarett68feef42017-04-11 09:51:32 -0400159 if (prefColorSpace && prefColorSpace->isNumericalTransferFn(&fn)) {
160 return prefColorSpace;
161 }
162
163 SkColorSpace* encodedSpace = fCodec->getInfo().colorSpace();
Matt Sarett9341c982017-02-28 15:36:42 -0500164 if (encodedSpace->isNumericalTransferFn(&fn)) {
165 // Leave the pixels in the encoded color space. Color space conversion
166 // will be handled after decode time.
Matt Sarettcf3f2342017-03-23 15:32:25 -0400167 return sk_ref_sp(encodedSpace);
Matt Sarett9341c982017-02-28 15:36:42 -0500168 }
169
170 if (is_wide_gamut(encodedSpace)) {
Matt Sarett2ae3a2e2017-02-15 08:48:02 -0500171 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
Matt Sarettcf3f2342017-03-23 15:32:25 -0400172 SkColorSpace::kDCIP3_D65_Gamut);
Matt Sarett2ae3a2e2017-02-15 08:48:02 -0500173 }
174
Matt Sarettcf3f2342017-03-23 15:32:25 -0400175 return SkColorSpace::MakeSRGB();
Matt Sarett9341c982017-02-28 15:36:42 -0500176 }
Mike Reed58050132017-07-12 22:10:29 -0400177#endif
Matt Sarett966bb342016-12-12 16:30:13 -0500178 case kRGBA_F16_SkColorType:
Matt Sarett68feef42017-04-11 09:51:32 -0400179 // Note that |prefColorSpace| is ignored, F16 is always linear sRGB.
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500180 return SkColorSpace::MakeSRGBLinear();
Matt Sarett3725f0a2017-03-28 14:34:20 -0400181 case kRGB_565_SkColorType:
Matt Sarett68feef42017-04-11 09:51:32 -0400182 // Note that |prefColorSpace| is ignored, 565 is always sRGB.
Matt Sarett3725f0a2017-03-28 14:34:20 -0400183 return SkColorSpace::MakeSRGB();
Matt Sarett966bb342016-12-12 16:30:13 -0500184 default:
Matt Sarett3725f0a2017-03-28 14:34:20 -0400185 // Color correction not supported for kGray.
Matt Sarett966bb342016-12-12 16:30:13 -0500186 return nullptr;
187 }
188}
189
msarett3d9d7a72015-10-21 10:27:10 -0700190SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const {
191 if (!is_valid_sample_size(sampleSize)) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400192 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700193 }
194
scroggo501b7342015-11-03 07:55:11 -0800195 // Fast path for when we are not scaling.
196 if (1 == sampleSize) {
197 return fInfo.dimensions();
198 }
199
msarett3d9d7a72015-10-21 10:27:10 -0700200 return this->onGetSampledDimensions(sampleSize);
201}
202
203bool SkAndroidCodec::getSupportedSubset(SkIRect* desiredSubset) const {
204 if (!desiredSubset || !is_valid_subset(*desiredSubset, fInfo.dimensions())) {
205 return false;
206 }
207
208 return this->onGetSupportedSubset(desiredSubset);
209}
210
211SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const {
212 if (!is_valid_sample_size(sampleSize)) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400213 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700214 }
215
216 // We require that the input subset is a subset that is supported by SkAndroidCodec.
217 // We test this by calling getSupportedSubset() and verifying that no modifications
218 // are made to the subset.
219 SkIRect copySubset = subset;
220 if (!this->getSupportedSubset(&copySubset) || copySubset != subset) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400221 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700222 }
223
scroggo501b7342015-11-03 07:55:11 -0800224 // If the subset is the entire image, for consistency, use getSampledDimensions().
msarett3d9d7a72015-10-21 10:27:10 -0700225 if (fInfo.dimensions() == subset.size()) {
scroggo501b7342015-11-03 07:55:11 -0800226 return this->getSampledDimensions(sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700227 }
228
229 // This should perhaps call a virtual function, but currently both of our subclasses
230 // want the same implementation.
Hal Canaryfafe1352017-04-11 12:12:02 -0400231 return {get_scaled_dimension(subset.width(), sampleSize),
232 get_scaled_dimension(subset.height(), sampleSize)};
msarett3d9d7a72015-10-21 10:27:10 -0700233}
234
235SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels,
scroggoe95a0682015-11-04 04:31:12 -0800236 size_t rowBytes, const AndroidOptions* options) {
msarett3d9d7a72015-10-21 10:27:10 -0700237 if (!pixels) {
238 return SkCodec::kInvalidParameters;
239 }
240 if (rowBytes < info.minRowBytes()) {
241 return SkCodec::kInvalidParameters;
242 }
243
244 AndroidOptions defaultOptions;
245 if (!options) {
246 options = &defaultOptions;
247 } else if (options->fSubset) {
248 if (!is_valid_subset(*options->fSubset, fInfo.dimensions())) {
249 return SkCodec::kInvalidParameters;
250 }
scroggo501b7342015-11-03 07:55:11 -0800251
252 if (SkIRect::MakeSize(fInfo.dimensions()) == *options->fSubset) {
253 // The caller wants the whole thing, rather than a subset. Modify
254 // the AndroidOptions passed to onGetAndroidPixels to not specify
255 // a subset.
256 defaultOptions = *options;
257 defaultOptions.fSubset = nullptr;
258 options = &defaultOptions;
259 }
msarett3d9d7a72015-10-21 10:27:10 -0700260 }
261
262 return this->onGetAndroidPixels(info, pixels, rowBytes, *options);
263}
264
265SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels,
266 size_t rowBytes) {
267 return this->getAndroidPixels(info, pixels, rowBytes, nullptr);
268}