blob: b93def8790222d777e514aad8448e8265dbb2351 [file] [log] [blame]
msarett74114382015-03-16 11:55:18 -07001/*
2 * Copyright 2015 The Android Open Source Project
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#ifndef SkCodecPriv_DEFINED
9#define SkCodecPriv_DEFINED
10
msarette6dd0042015-10-09 11:07:34 -070011#include "SkColorPriv.h"
msarettf7eb6fc2016-09-13 09:04:11 -070012#include "SkColorSpaceXform.h"
msarett9e43cab2015-04-29 07:38:43 -070013#include "SkColorTable.h"
msarett74114382015-03-16 11:55:18 -070014#include "SkImageInfo.h"
msarett74114382015-03-16 11:55:18 -070015#include "SkTypes.h"
16
scroggoc5560be2016-02-03 09:42:42 -080017#ifdef SK_PRINT_CODEC_MESSAGES
18 #define SkCodecPrintf SkDebugf
19#else
20 #define SkCodecPrintf(...)
21#endif
22
msarett3d9d7a72015-10-21 10:27:10 -070023// FIXME: Consider sharing with dm, nanbench, and tools.
msarettd1ec89b2016-08-03 12:59:27 -070024static inline float get_scale_from_sample_size(int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -070025 return 1.0f / ((float) sampleSize);
26}
27
msarettd1ec89b2016-08-03 12:59:27 -070028static inline bool is_valid_subset(const SkIRect& subset, const SkISize& imageDims) {
msarett3d9d7a72015-10-21 10:27:10 -070029 return SkIRect::MakeSize(imageDims).contains(subset);
30}
31
msarett5406d6f2015-08-31 06:55:13 -070032/*
msarett10522ff2015-09-07 08:54:01 -070033 * returns a scaled dimension based on the original dimension and the sampleSize
34 * NOTE: we round down here for scaled dimension to match the behavior of SkImageDecoder
msarett3d9d7a72015-10-21 10:27:10 -070035 * FIXME: I think we should call this get_sampled_dimension().
msarett10522ff2015-09-07 08:54:01 -070036 */
msarettd1ec89b2016-08-03 12:59:27 -070037static inline int get_scaled_dimension(int srcDimension, int sampleSize) {
msarett10522ff2015-09-07 08:54:01 -070038 if (sampleSize > srcDimension) {
39 return 1;
40 }
41 return srcDimension / sampleSize;
42}
43
44/*
msarett5406d6f2015-08-31 06:55:13 -070045 * Returns the first coordinate that we will keep during a scaled decode.
46 * The output can be interpreted as an x-coordinate or a y-coordinate.
47 *
48 * This does not need to be called and is not called when sampleFactor == 1.
49 */
msarettd1ec89b2016-08-03 12:59:27 -070050static inline int get_start_coord(int sampleFactor) { return sampleFactor / 2; };
msarett5406d6f2015-08-31 06:55:13 -070051
52/*
53 * Given a coordinate in the original image, this returns the corresponding
54 * coordinate in the scaled image. This function is meaningless if
55 * IsCoordNecessary returns false.
56 * The output can be interpreted as an x-coordinate or a y-coordinate.
57 *
58 * This does not need to be called and is not called when sampleFactor == 1.
59 */
msarettd1ec89b2016-08-03 12:59:27 -070060static inline int get_dst_coord(int srcCoord, int sampleFactor) { return srcCoord / sampleFactor; };
msarett5406d6f2015-08-31 06:55:13 -070061
62/*
63 * When scaling, we will discard certain y-coordinates (rows) and
64 * x-coordinates (columns). This function returns true if we should keep the
65 * coordinate and false otherwise.
66 * The inputs may be x-coordinates or y-coordinates.
67 *
68 * This does not need to be called and is not called when sampleFactor == 1.
69 */
msarettd1ec89b2016-08-03 12:59:27 -070070static inline bool is_coord_necessary(int srcCoord, int sampleFactor, int scaledDim) {
msarett5406d6f2015-08-31 06:55:13 -070071 // Get the first coordinate that we want to keep
72 int startCoord = get_start_coord(sampleFactor);
73
74 // Return false on edge cases
75 if (srcCoord < startCoord || get_dst_coord(srcCoord, sampleFactor) >= scaledDim) {
76 return false;
77 }
78
79 // Every sampleFactor rows are necessary
80 return ((srcCoord - startCoord) % sampleFactor) == 0;
81}
82
msarettd1ec89b2016-08-03 12:59:27 -070083static inline bool valid_alpha(SkAlphaType dstAlpha, SkAlphaType srcAlpha) {
scroggoc5560be2016-02-03 09:42:42 -080084 if (kUnknown_SkAlphaType == dstAlpha) {
85 return false;
86 }
87
msarett4ab9d5f2015-08-06 15:34:42 -070088 if (srcAlpha != dstAlpha) {
89 if (kOpaque_SkAlphaType == srcAlpha) {
scroggoc5560be2016-02-03 09:42:42 -080090 // If the source is opaque, we can support any.
91 SkCodecPrintf("Warning: an opaque image should be decoded as opaque "
92 "- it is being decoded as non-opaque, which will draw slower\n");
93 return true;
msarett4ab9d5f2015-08-06 15:34:42 -070094 }
95
96 // The source is not opaque
97 switch (dstAlpha) {
98 case kPremul_SkAlphaType:
99 case kUnpremul_SkAlphaType:
100 // The source is not opaque, so either of these is okay
101 break;
102 default:
103 // We cannot decode a non-opaque image to opaque (or unknown)
104 return false;
105 }
106 }
107 return true;
108}
109
msarett74114382015-03-16 11:55:18 -0700110/*
msarett2ecc35f2016-09-08 11:55:16 -0700111 * Original version of conversion_possible that does not account for color spaces.
112 * Used by codecs that have not been updated to support color spaces.
113 *
scroggocc2feb12015-08-14 08:32:46 -0700114 * Most of our codecs support the same conversions:
scroggoc5560be2016-02-03 09:42:42 -0800115 * - opaque to any alpha type
116 * - 565 only if opaque
scroggocc2feb12015-08-14 08:32:46 -0700117 * - premul to unpremul and vice versa
msarett2ecc35f2016-09-08 11:55:16 -0700118 * - always support RGBA, BGRA
scroggocc2feb12015-08-14 08:32:46 -0700119 * - otherwise match the src color type
120 */
msarett2ecc35f2016-09-08 11:55:16 -0700121static inline bool conversion_possible_ignore_color_space(const SkImageInfo& dst,
122 const SkImageInfo& src) {
scroggocc2feb12015-08-14 08:32:46 -0700123 // Ensure the alpha type is valid
124 if (!valid_alpha(dst.alphaType(), src.alphaType())) {
125 return false;
126 }
127
128 // Check for supported color types
129 switch (dst.colorType()) {
msarett34e0ec42016-04-22 16:27:24 -0700130 case kRGBA_8888_SkColorType:
131 case kBGRA_8888_SkColorType:
scroggocc2feb12015-08-14 08:32:46 -0700132 return true;
133 case kRGB_565_SkColorType:
scroggoba584892016-05-20 13:56:13 -0700134 return kOpaque_SkAlphaType == src.alphaType();
scroggocc2feb12015-08-14 08:32:46 -0700135 default:
136 return dst.colorType() == src.colorType();
137 }
138}
139
140/*
halcanary96fcdcc2015-08-27 07:41:13 -0700141 * If there is a color table, get a pointer to the colors, otherwise return nullptr
msarett99f567e2015-08-05 12:58:26 -0700142 */
msarettd1ec89b2016-08-03 12:59:27 -0700143static inline const SkPMColor* get_color_ptr(SkColorTable* colorTable) {
halcanary96fcdcc2015-08-27 07:41:13 -0700144 return nullptr != colorTable ? colorTable->readColors() : nullptr;
msarett99f567e2015-08-05 12:58:26 -0700145}
146
msarettc0444612016-09-16 11:45:58 -0700147static inline SkColorSpaceXform::ColorFormat select_xform_format(SkColorType colorType) {
148 switch (colorType) {
149 case kRGBA_8888_SkColorType:
150 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
151 case kBGRA_8888_SkColorType:
152 return SkColorSpaceXform::kBGRA_8888_ColorFormat;
153 case kRGBA_F16_SkColorType:
154 return SkColorSpaceXform::kRGBA_F16_ColorFormat;
155 default:
156 SkASSERT(false);
157 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
158 }
159}
160
msarett99f567e2015-08-05 12:58:26 -0700161/*
msarette6dd0042015-10-09 11:07:34 -0700162 * Given that the encoded image uses a color table, return the fill value
163 */
msarettcf7b8772016-09-22 12:37:04 -0700164static inline uint64_t get_color_table_fill_value(SkColorType dstColorType, SkAlphaType alphaType,
msarettf7eb6fc2016-09-13 09:04:11 -0700165 const SkPMColor* colorPtr, uint8_t fillIndex, SkColorSpaceXform* colorXform) {
msarette6dd0042015-10-09 11:07:34 -0700166 SkASSERT(nullptr != colorPtr);
msarettcf7b8772016-09-22 12:37:04 -0700167 switch (dstColorType) {
msarett34e0ec42016-04-22 16:27:24 -0700168 case kRGBA_8888_SkColorType:
169 case kBGRA_8888_SkColorType:
msarette6dd0042015-10-09 11:07:34 -0700170 return colorPtr[fillIndex];
171 case kRGB_565_SkColorType:
172 return SkPixel32ToPixel16(colorPtr[fillIndex]);
173 case kIndex_8_SkColorType:
174 return fillIndex;
msarettf7eb6fc2016-09-13 09:04:11 -0700175 case kRGBA_F16_SkColorType: {
176 SkASSERT(colorXform);
177 uint64_t dstColor;
178 uint32_t srcColor = colorPtr[fillIndex];
msarettcf7b8772016-09-22 12:37:04 -0700179 colorXform->apply(&dstColor, &srcColor, 1, select_xform_format(dstColorType),
180 SkColorSpaceXform::kRGBA_8888_ColorFormat, alphaType);
msarettf7eb6fc2016-09-13 09:04:11 -0700181 return dstColor;
182 }
msarette6dd0042015-10-09 11:07:34 -0700183 default:
184 SkASSERT(false);
185 return 0;
186 }
187}
188
189/*
msarett74114382015-03-16 11:55:18 -0700190 *
msarett9e43cab2015-04-29 07:38:43 -0700191 * Copy the codec color table back to the client when kIndex8 color type is requested
msarett9e43cab2015-04-29 07:38:43 -0700192 */
msarettd1ec89b2016-08-03 12:59:27 -0700193static inline void copy_color_table(const SkImageInfo& dstInfo, SkColorTable* colorTable,
msarett9e43cab2015-04-29 07:38:43 -0700194 SkPMColor* inputColorPtr, int* inputColorCount) {
195 if (kIndex_8_SkColorType == dstInfo.colorType()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700196 SkASSERT(nullptr != inputColorPtr);
197 SkASSERT(nullptr != inputColorCount);
198 SkASSERT(nullptr != colorTable);
msarett10522ff2015-09-07 08:54:01 -0700199 memcpy(inputColorPtr, colorTable->readColors(), *inputColorCount * sizeof(SkPMColor));
msarett9e43cab2015-04-29 07:38:43 -0700200 }
201}
202
203/*
msarett74114382015-03-16 11:55:18 -0700204 * Compute row bytes for an image using pixels per byte
msarett74114382015-03-16 11:55:18 -0700205 */
msarettd1ec89b2016-08-03 12:59:27 -0700206static inline size_t compute_row_bytes_ppb(int width, uint32_t pixelsPerByte) {
msarett74114382015-03-16 11:55:18 -0700207 return (width + pixelsPerByte - 1) / pixelsPerByte;
208}
209
210/*
msarett74114382015-03-16 11:55:18 -0700211 * Compute row bytes for an image using bytes per pixel
msarett74114382015-03-16 11:55:18 -0700212 */
msarettd1ec89b2016-08-03 12:59:27 -0700213static inline size_t compute_row_bytes_bpp(int width, uint32_t bytesPerPixel) {
msarett74114382015-03-16 11:55:18 -0700214 return width * bytesPerPixel;
215}
216
217/*
msarett74114382015-03-16 11:55:18 -0700218 * Compute row bytes for an image
msarett74114382015-03-16 11:55:18 -0700219 */
msarettd1ec89b2016-08-03 12:59:27 -0700220static inline size_t compute_row_bytes(int width, uint32_t bitsPerPixel) {
msarett74114382015-03-16 11:55:18 -0700221 if (bitsPerPixel < 16) {
222 SkASSERT(0 == 8 % bitsPerPixel);
223 const uint32_t pixelsPerByte = 8 / bitsPerPixel;
224 return compute_row_bytes_ppb(width, pixelsPerByte);
225 } else {
226 SkASSERT(0 == bitsPerPixel % 8);
227 const uint32_t bytesPerPixel = bitsPerPixel / 8;
228 return compute_row_bytes_bpp(width, bytesPerPixel);
229 }
230}
231
232/*
msarett74114382015-03-16 11:55:18 -0700233 * Get a byte from a buffer
234 * This method is unsafe, the caller is responsible for performing a check
msarett74114382015-03-16 11:55:18 -0700235 */
msarettd1ec89b2016-08-03 12:59:27 -0700236static inline uint8_t get_byte(uint8_t* buffer, uint32_t i) {
msarett74114382015-03-16 11:55:18 -0700237 return buffer[i];
238}
239
240/*
msarett74114382015-03-16 11:55:18 -0700241 * Get a short from a buffer
242 * This method is unsafe, the caller is responsible for performing a check
msarett74114382015-03-16 11:55:18 -0700243 */
msarettd1ec89b2016-08-03 12:59:27 -0700244static inline uint16_t get_short(uint8_t* buffer, uint32_t i) {
msarett74114382015-03-16 11:55:18 -0700245 uint16_t result;
246 memcpy(&result, &(buffer[i]), 2);
247#ifdef SK_CPU_BENDIAN
248 return SkEndianSwap16(result);
249#else
250 return result;
251#endif
252}
253
254/*
msarett74114382015-03-16 11:55:18 -0700255 * Get an int from a buffer
256 * This method is unsafe, the caller is responsible for performing a check
msarett74114382015-03-16 11:55:18 -0700257 */
msarettd1ec89b2016-08-03 12:59:27 -0700258static inline uint32_t get_int(uint8_t* buffer, uint32_t i) {
msarett74114382015-03-16 11:55:18 -0700259 uint32_t result;
260 memcpy(&result, &(buffer[i]), 4);
261#ifdef SK_CPU_BENDIAN
262 return SkEndianSwap32(result);
263#else
264 return result;
265#endif
266}
267
msarett0e6274f2016-03-21 08:04:40 -0700268/*
269 * @param data Buffer to read bytes from
270 * @param isLittleEndian Output parameter
271 * Indicates if the data is little endian
272 * Is unaffected on false returns
273 */
msarettd1ec89b2016-08-03 12:59:27 -0700274static inline bool is_valid_endian_marker(const uint8_t* data, bool* isLittleEndian) {
msarett0e6274f2016-03-21 08:04:40 -0700275 // II indicates Intel (little endian) and MM indicates motorola (big endian).
276 if (('I' != data[0] || 'I' != data[1]) && ('M' != data[0] || 'M' != data[1])) {
277 return false;
278 }
279
280 *isLittleEndian = ('I' == data[0]);
281 return true;
282}
283
msarettd1ec89b2016-08-03 12:59:27 -0700284static inline uint16_t get_endian_short(const uint8_t* data, bool littleEndian) {
msarett0e6274f2016-03-21 08:04:40 -0700285 if (littleEndian) {
286 return (data[1] << 8) | (data[0]);
287 }
288
289 return (data[0] << 8) | (data[1]);
290}
291
msarettd1ec89b2016-08-03 12:59:27 -0700292static inline SkPMColor premultiply_argb_as_rgba(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
msarett34e0ec42016-04-22 16:27:24 -0700293 if (a != 255) {
294 r = SkMulDiv255Round(r, a);
295 g = SkMulDiv255Round(g, a);
296 b = SkMulDiv255Round(b, a);
297 }
298
299 return SkPackARGB_as_RGBA(a, r, g, b);
300}
301
msarettd1ec89b2016-08-03 12:59:27 -0700302static inline SkPMColor premultiply_argb_as_bgra(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
msarett34e0ec42016-04-22 16:27:24 -0700303 if (a != 255) {
304 r = SkMulDiv255Round(r, a);
305 g = SkMulDiv255Round(g, a);
306 b = SkMulDiv255Round(b, a);
307 }
308
309 return SkPackARGB_as_BGRA(a, r, g, b);
310}
311
msarettd1ec89b2016-08-03 12:59:27 -0700312static inline bool is_rgba(SkColorType colorType) {
msarett34e0ec42016-04-22 16:27:24 -0700313#ifdef SK_PMCOLOR_IS_RGBA
314 return (kBGRA_8888_SkColorType != colorType);
315#else
316 return (kRGBA_8888_SkColorType == colorType);
317#endif
318}
319
320// Method for coverting to a 32 bit pixel.
321typedef uint32_t (*PackColorProc)(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
322
msarettd1ec89b2016-08-03 12:59:27 -0700323static inline PackColorProc choose_pack_color_proc(bool isPremul, SkColorType colorType) {
msarett34e0ec42016-04-22 16:27:24 -0700324 bool isRGBA = is_rgba(colorType);
325 if (isPremul) {
326 if (isRGBA) {
327 return &premultiply_argb_as_rgba;
328 } else {
329 return &premultiply_argb_as_bgra;
330 }
331 } else {
332 if (isRGBA) {
333 return &SkPackARGB_as_RGBA;
334 } else {
335 return &SkPackARGB_as_BGRA;
336 }
337 }
338}
339
msarettdcd5e652016-08-22 08:48:40 -0700340static inline bool needs_premul(const SkImageInfo& dstInfo, const SkImageInfo& srcInfo) {
341 return kPremul_SkAlphaType == dstInfo.alphaType() &&
342 kUnpremul_SkAlphaType == srcInfo.alphaType();
343}
344
msarettd1ec89b2016-08-03 12:59:27 -0700345static inline bool needs_color_xform(const SkImageInfo& dstInfo, const SkImageInfo& srcInfo) {
msarettdcd5e652016-08-22 08:48:40 -0700346 // Color xform is necessary in order to correctly perform premultiply in linear space.
347 bool needsPremul = needs_premul(dstInfo, srcInfo);
348
349 // F16 is by definition a linear space, so we always must perform a color xform.
350 bool isF16 = kRGBA_F16_SkColorType == dstInfo.colorType();
351
352 // Need a color xform when dst space does not match the src.
353 bool srcDstNotEqual = !SkColorSpace::Equals(srcInfo.colorSpace(), dstInfo.colorSpace());
354
355 // We never perform a color xform in legacy mode.
356 bool isLegacy = nullptr == dstInfo.colorSpace();
357
358 return !isLegacy && (needsPremul || isF16 || srcDstNotEqual);
msarettd1ec89b2016-08-03 12:59:27 -0700359}
360
msarettc0444612016-09-16 11:45:58 -0700361static inline SkAlphaType select_xform_alpha(SkAlphaType dstAlphaType, SkAlphaType srcAlphaType) {
msarette99883f2016-09-08 06:05:35 -0700362 return (kOpaque_SkAlphaType == srcAlphaType) ? kOpaque_SkAlphaType : dstAlphaType;
363}
364
msarett2ecc35f2016-09-08 11:55:16 -0700365/*
366 * Alpha Type Conversions
367 * - kOpaque to kOpaque, kUnpremul, kPremul is valid
368 * - kUnpremul to kUnpremul, kPremul is valid
369 *
370 * Color Type Conversions
371 * - Always support kRGBA_8888, kBGRA_8888
372 * - Support kRGBA_F16 when there is a linear dst color space
373 * - Support kIndex8 if it matches the src
374 * - Support k565 if kOpaque and color correction is not required
375 * - Support k565 if it matches the src, kOpaque, and color correction is not required
376 */
377static inline bool conversion_possible(const SkImageInfo& dst, const SkImageInfo& src) {
378 // Ensure the alpha type is valid.
379 if (!valid_alpha(dst.alphaType(), src.alphaType())) {
380 return false;
381 }
382
383 // Check for supported color types.
384 switch (dst.colorType()) {
385 case kRGBA_8888_SkColorType:
386 case kBGRA_8888_SkColorType:
387 return true;
388 case kRGBA_F16_SkColorType:
389 return dst.colorSpace() && dst.colorSpace()->gammaIsLinear();
390 case kIndex_8_SkColorType:
391 return kIndex_8_SkColorType == src.colorType();
392 case kRGB_565_SkColorType:
393 return kOpaque_SkAlphaType == src.alphaType() && !needs_color_xform(dst, src);
394 case kGray_8_SkColorType:
395 return kGray_8_SkColorType == src.colorType() &&
396 kOpaque_SkAlphaType == src.alphaType() && !needs_color_xform(dst, src);
397 default:
398 return false;
399 }
400}
401
msarett74114382015-03-16 11:55:18 -0700402#endif // SkCodecPriv_DEFINED