1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkAndroidCodec.h"
#include "SkCodec.h"
#include "SkCodecPriv.h"
#include "SkRawAdapterCodec.h"
#include "SkSampledCodec.h"
#include "SkWebpAdapterCodec.h"
static bool is_valid_sample_size(int sampleSize) {
// FIXME: As Leon has mentioned elsewhere, surely there is also a maximum sampleSize?
return sampleSize > 0;
}
/**
* Loads the gamut as a set of three points (triangle).
*/
static void load_gamut(SkPoint rgb[], const SkMatrix44& xyz) {
// rx = rX / (rX + rY + rZ)
// ry = rY / (rX + rY + rZ)
// gx, gy, bx, and gy are calulcated similarly.
float rSum = xyz.get(0, 0) + xyz.get(1, 0) + xyz.get(2, 0);
float gSum = xyz.get(0, 1) + xyz.get(1, 1) + xyz.get(2, 1);
float bSum = xyz.get(0, 2) + xyz.get(1, 2) + xyz.get(2, 2);
rgb[0].fX = xyz.get(0, 0) / rSum;
rgb[0].fY = xyz.get(1, 0) / rSum;
rgb[1].fX = xyz.get(0, 1) / gSum;
rgb[1].fY = xyz.get(1, 1) / gSum;
rgb[2].fX = xyz.get(0, 2) / bSum;
rgb[2].fY = xyz.get(1, 2) / bSum;
}
/**
* Calculates the area of the triangular gamut.
*/
static float calculate_area(SkPoint abc[]) {
SkPoint a = abc[0];
SkPoint b = abc[1];
SkPoint c = abc[2];
return 0.5f * SkTAbs(a.fX*b.fY + b.fX*c.fY - a.fX*c.fY - c.fX*b.fY - b.fX*a.fY);
}
static const float kSRGB_D50_GamutArea = 0.084f;
static bool is_wide_gamut(const SkColorSpace* colorSpace) {
// Determine if the source image has a gamut that is wider than sRGB. If so, we
// will use P3 as the output color space to avoid clipping the gamut.
const SkMatrix44* toXYZD50 = as_CSB(colorSpace)->toXYZD50();
if (toXYZD50) {
SkPoint rgb[3];
load_gamut(rgb, *toXYZD50);
return calculate_area(rgb) > kSRGB_D50_GamutArea;
}
return false;
}
SkAndroidCodec::SkAndroidCodec(SkCodec* codec)
: fInfo(codec->getInfo())
, fCodec(codec)
{}
SkAndroidCodec* SkAndroidCodec::NewFromStream(SkStream* stream, SkPngChunkReader* chunkReader) {
std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream, chunkReader));
if (nullptr == codec) {
return nullptr;
}
switch ((SkEncodedImageFormat)codec->getEncodedFormat()) {
#ifdef SK_HAS_PNG_LIBRARY
case SkEncodedImageFormat::kPNG:
case SkEncodedImageFormat::kICO:
#endif
#ifdef SK_HAS_JPEG_LIBRARY
case SkEncodedImageFormat::kJPEG:
#endif
case SkEncodedImageFormat::kGIF:
case SkEncodedImageFormat::kBMP:
case SkEncodedImageFormat::kWBMP:
return new SkSampledCodec(codec.release());
#ifdef SK_HAS_WEBP_LIBRARY
case SkEncodedImageFormat::kWEBP:
return new SkWebpAdapterCodec((SkWebpCodec*) codec.release());
#endif
#ifdef SK_CODEC_DECODES_RAW
case SkEncodedImageFormat::kDNG:
return new SkRawAdapterCodec((SkRawCodec*)codec.release());
#endif
default:
return nullptr;
}
}
SkAndroidCodec* SkAndroidCodec::NewFromData(sk_sp<SkData> data, SkPngChunkReader* chunkReader) {
if (!data) {
return nullptr;
}
return NewFromStream(new SkMemoryStream(data), chunkReader);
}
SkColorType SkAndroidCodec::computeOutputColorType(SkColorType requestedColorType) {
// The legacy GIF and WBMP decoders always decode to kIndex_8_SkColorType.
// We will maintain this behavior when we can.
const SkColorType suggestedColorType = this->getInfo().colorType();
switch ((SkEncodedImageFormat) this->getEncodedFormat()) {
case SkEncodedImageFormat::kGIF:
if (suggestedColorType == kIndex_8_SkColorType) {
return kIndex_8_SkColorType;
}
break;
case SkEncodedImageFormat::kWBMP:
return kIndex_8_SkColorType;
default:
break;
}
bool highPrecision = fCodec->getEncodedInfo().bitsPerComponent() > 8;
switch (requestedColorType) {
case kARGB_4444_SkColorType:
return kN32_SkColorType;
case kN32_SkColorType:
// F16 is the Android default for high precision images.
return highPrecision ? kRGBA_F16_SkColorType : kN32_SkColorType;
case kIndex_8_SkColorType:
if (kIndex_8_SkColorType == suggestedColorType) {
return kIndex_8_SkColorType;
}
break;
case kAlpha_8_SkColorType:
// Fall through to kGray_8. Before kGray_8_SkColorType existed,
// we allowed clients to request kAlpha_8 when they wanted a
// grayscale decode.
case kGray_8_SkColorType:
if (kGray_8_SkColorType == suggestedColorType) {
return kGray_8_SkColorType;
}
break;
case kRGB_565_SkColorType:
if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
return kRGB_565_SkColorType;
}
break;
case kRGBA_F16_SkColorType:
return kRGBA_F16_SkColorType;
default:
break;
}
// Android has limited support for kGray_8 (using kAlpha_8). We will not
// use kGray_8 for Android unless they specifically ask for it.
if (kGray_8_SkColorType == suggestedColorType) {
return kN32_SkColorType;
}
// |suggestedColorType| may be kN32_SkColorType or kIndex_8_SkColorType.
return highPrecision ? kRGBA_F16_SkColorType : suggestedColorType;
}
SkAlphaType SkAndroidCodec::computeOutputAlphaType(bool requestedUnpremul) {
if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
return kOpaque_SkAlphaType;
}
return requestedUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
}
sk_sp<SkColorSpace> SkAndroidCodec::computeOutputColorSpace(SkColorType outputColorType,
sk_sp<SkColorSpace> prefColorSpace) {
switch (outputColorType) {
case kRGBA_8888_SkColorType:
case kBGRA_8888_SkColorType:
case kIndex_8_SkColorType: {
// If |prefColorSpace| is supported, choose it.
SkColorSpaceTransferFn fn;
if (prefColorSpace && prefColorSpace->isNumericalTransferFn(&fn)) {
return prefColorSpace;
}
SkColorSpace* encodedSpace = fCodec->getInfo().colorSpace();
if (encodedSpace->isNumericalTransferFn(&fn)) {
// Leave the pixels in the encoded color space. Color space conversion
// will be handled after decode time.
return sk_ref_sp(encodedSpace);
}
if (is_wide_gamut(encodedSpace)) {
return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
SkColorSpace::kDCIP3_D65_Gamut);
}
return SkColorSpace::MakeSRGB();
}
case kRGBA_F16_SkColorType:
// Note that |prefColorSpace| is ignored, F16 is always linear sRGB.
return SkColorSpace::MakeSRGBLinear();
case kRGB_565_SkColorType:
// Note that |prefColorSpace| is ignored, 565 is always sRGB.
return SkColorSpace::MakeSRGB();
default:
// Color correction not supported for kGray.
return nullptr;
}
}
SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const {
if (!is_valid_sample_size(sampleSize)) {
return {0, 0};
}
// Fast path for when we are not scaling.
if (1 == sampleSize) {
return fInfo.dimensions();
}
return this->onGetSampledDimensions(sampleSize);
}
bool SkAndroidCodec::getSupportedSubset(SkIRect* desiredSubset) const {
if (!desiredSubset || !is_valid_subset(*desiredSubset, fInfo.dimensions())) {
return false;
}
return this->onGetSupportedSubset(desiredSubset);
}
SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const {
if (!is_valid_sample_size(sampleSize)) {
return {0, 0};
}
// We require that the input subset is a subset that is supported by SkAndroidCodec.
// We test this by calling getSupportedSubset() and verifying that no modifications
// are made to the subset.
SkIRect copySubset = subset;
if (!this->getSupportedSubset(©Subset) || copySubset != subset) {
return {0, 0};
}
// If the subset is the entire image, for consistency, use getSampledDimensions().
if (fInfo.dimensions() == subset.size()) {
return this->getSampledDimensions(sampleSize);
}
// This should perhaps call a virtual function, but currently both of our subclasses
// want the same implementation.
return {get_scaled_dimension(subset.width(), sampleSize),
get_scaled_dimension(subset.height(), sampleSize)};
}
SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels,
size_t rowBytes, const AndroidOptions* options) {
if (!pixels) {
return SkCodec::kInvalidParameters;
}
if (rowBytes < info.minRowBytes()) {
return SkCodec::kInvalidParameters;
}
AndroidOptions defaultOptions;
if (!options) {
options = &defaultOptions;
} else if (options->fSubset) {
if (!is_valid_subset(*options->fSubset, fInfo.dimensions())) {
return SkCodec::kInvalidParameters;
}
if (SkIRect::MakeSize(fInfo.dimensions()) == *options->fSubset) {
// The caller wants the whole thing, rather than a subset. Modify
// the AndroidOptions passed to onGetAndroidPixels to not specify
// a subset.
defaultOptions = *options;
defaultOptions.fSubset = nullptr;
options = &defaultOptions;
}
}
return this->onGetAndroidPixels(info, pixels, rowBytes, *options);
}
SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels,
size_t rowBytes) {
return this->getAndroidPixels(info, pixels, rowBytes, nullptr);
}
|