blob: 1251d8d73c72ca098a03c92feaf23ffd541fdf13 [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
Cary Clarka4083c92017-09-15 11:59:23 -040011#include "SkColorData.h"
msarettf7eb6fc2016-09-13 09:04:11 -070012#include "SkColorSpaceXform.h"
Matt Sarett909d3792016-12-22 10:52:25 -050013#include "SkColorSpaceXformPriv.h"
msarett9e43cab2015-04-29 07:38:43 -070014#include "SkColorTable.h"
Matt Sarett1a85ca52016-11-04 11:52:48 -040015#include "SkEncodedInfo.h"
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050016#include "SkEncodedOrigin.h"
msarett74114382015-03-16 11:55:18 -070017#include "SkImageInfo.h"
msarett74114382015-03-16 11:55:18 -070018#include "SkTypes.h"
19
Leon Scroggins III2cc7d132018-01-19 12:12:13 -050020#ifdef SK_PRINT_CODEC_MESSAGES
scroggoc5560be2016-02-03 09:42:42 -080021 #define SkCodecPrintf SkDebugf
22#else
23 #define SkCodecPrintf(...)
24#endif
25
msarett3d9d7a72015-10-21 10:27:10 -070026// FIXME: Consider sharing with dm, nanbench, and tools.
msarettd1ec89b2016-08-03 12:59:27 -070027static inline float get_scale_from_sample_size(int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -070028 return 1.0f / ((float) sampleSize);
29}
30
msarettd1ec89b2016-08-03 12:59:27 -070031static inline bool is_valid_subset(const SkIRect& subset, const SkISize& imageDims) {
msarett3d9d7a72015-10-21 10:27:10 -070032 return SkIRect::MakeSize(imageDims).contains(subset);
33}
34
msarett5406d6f2015-08-31 06:55:13 -070035/*
msarett10522ff2015-09-07 08:54:01 -070036 * returns a scaled dimension based on the original dimension and the sampleSize
37 * NOTE: we round down here for scaled dimension to match the behavior of SkImageDecoder
msarett3d9d7a72015-10-21 10:27:10 -070038 * FIXME: I think we should call this get_sampled_dimension().
msarett10522ff2015-09-07 08:54:01 -070039 */
msarettd1ec89b2016-08-03 12:59:27 -070040static inline int get_scaled_dimension(int srcDimension, int sampleSize) {
msarett10522ff2015-09-07 08:54:01 -070041 if (sampleSize > srcDimension) {
42 return 1;
43 }
44 return srcDimension / sampleSize;
45}
46
47/*
msarett5406d6f2015-08-31 06:55:13 -070048 * Returns the first coordinate that we will keep during a scaled decode.
49 * The output can be interpreted as an x-coordinate or a y-coordinate.
50 *
51 * This does not need to be called and is not called when sampleFactor == 1.
52 */
msarettd1ec89b2016-08-03 12:59:27 -070053static inline int get_start_coord(int sampleFactor) { return sampleFactor / 2; };
msarett5406d6f2015-08-31 06:55:13 -070054
55/*
56 * Given a coordinate in the original image, this returns the corresponding
57 * coordinate in the scaled image. This function is meaningless if
58 * IsCoordNecessary returns false.
59 * The output can be interpreted as an x-coordinate or a y-coordinate.
60 *
61 * This does not need to be called and is not called when sampleFactor == 1.
62 */
msarettd1ec89b2016-08-03 12:59:27 -070063static inline int get_dst_coord(int srcCoord, int sampleFactor) { return srcCoord / sampleFactor; };
msarett5406d6f2015-08-31 06:55:13 -070064
65/*
66 * When scaling, we will discard certain y-coordinates (rows) and
67 * x-coordinates (columns). This function returns true if we should keep the
68 * coordinate and false otherwise.
69 * The inputs may be x-coordinates or y-coordinates.
70 *
71 * This does not need to be called and is not called when sampleFactor == 1.
72 */
msarettd1ec89b2016-08-03 12:59:27 -070073static inline bool is_coord_necessary(int srcCoord, int sampleFactor, int scaledDim) {
msarett5406d6f2015-08-31 06:55:13 -070074 // Get the first coordinate that we want to keep
75 int startCoord = get_start_coord(sampleFactor);
76
77 // Return false on edge cases
78 if (srcCoord < startCoord || get_dst_coord(srcCoord, sampleFactor) >= scaledDim) {
79 return false;
80 }
81
82 // Every sampleFactor rows are necessary
83 return ((srcCoord - startCoord) % sampleFactor) == 0;
84}
85
Leon Scroggins III07418182017-08-15 12:24:02 -040086static inline bool valid_alpha(SkAlphaType dstAlpha, bool srcIsOpaque) {
scroggoc5560be2016-02-03 09:42:42 -080087 if (kUnknown_SkAlphaType == dstAlpha) {
88 return false;
89 }
90
Leon Scroggins III07418182017-08-15 12:24:02 -040091 if (srcIsOpaque) {
92 if (kOpaque_SkAlphaType != dstAlpha) {
scroggoc5560be2016-02-03 09:42:42 -080093 SkCodecPrintf("Warning: an opaque image should be decoded as opaque "
94 "- it is being decoded as non-opaque, which will draw slower\n");
msarett4ab9d5f2015-08-06 15:34:42 -070095 }
Leon Scroggins III07418182017-08-15 12:24:02 -040096 return true;
msarett4ab9d5f2015-08-06 15:34:42 -070097 }
Leon Scroggins III07418182017-08-15 12:24:02 -040098
99 return dstAlpha != kOpaque_SkAlphaType;
msarett4ab9d5f2015-08-06 15:34:42 -0700100}
101
msarett74114382015-03-16 11:55:18 -0700102/*
halcanary96fcdcc2015-08-27 07:41:13 -0700103 * If there is a color table, get a pointer to the colors, otherwise return nullptr
msarett99f567e2015-08-05 12:58:26 -0700104 */
msarettd1ec89b2016-08-03 12:59:27 -0700105static inline const SkPMColor* get_color_ptr(SkColorTable* colorTable) {
halcanary96fcdcc2015-08-27 07:41:13 -0700106 return nullptr != colorTable ? colorTable->readColors() : nullptr;
msarett99f567e2015-08-05 12:58:26 -0700107}
108
109/*
msarette6dd0042015-10-09 11:07:34 -0700110 * Given that the encoded image uses a color table, return the fill value
111 */
msarettcf7b8772016-09-22 12:37:04 -0700112static inline uint64_t get_color_table_fill_value(SkColorType dstColorType, SkAlphaType alphaType,
Matt Sarett19aff5d2017-04-03 16:01:10 -0400113 const SkPMColor* colorPtr, uint8_t fillIndex, SkColorSpaceXform* colorXform, bool isRGBA) {
msarette6dd0042015-10-09 11:07:34 -0700114 SkASSERT(nullptr != colorPtr);
msarettcf7b8772016-09-22 12:37:04 -0700115 switch (dstColorType) {
msarett34e0ec42016-04-22 16:27:24 -0700116 case kRGBA_8888_SkColorType:
117 case kBGRA_8888_SkColorType:
msarette6dd0042015-10-09 11:07:34 -0700118 return colorPtr[fillIndex];
119 case kRGB_565_SkColorType:
120 return SkPixel32ToPixel16(colorPtr[fillIndex]);
msarettf7eb6fc2016-09-13 09:04:11 -0700121 case kRGBA_F16_SkColorType: {
122 SkASSERT(colorXform);
123 uint64_t dstColor;
124 uint32_t srcColor = colorPtr[fillIndex];
Matt Sarett19aff5d2017-04-03 16:01:10 -0400125 SkColorSpaceXform::ColorFormat srcFormat =
126 isRGBA ? SkColorSpaceXform::kRGBA_8888_ColorFormat
127 : SkColorSpaceXform::kBGRA_8888_ColorFormat;
msarett31d097e82016-10-11 12:15:03 -0700128 SkAssertResult(colorXform->apply(select_xform_format(dstColorType), &dstColor,
Matt Sarett19aff5d2017-04-03 16:01:10 -0400129 srcFormat, &srcColor, 1, alphaType));
msarettf7eb6fc2016-09-13 09:04:11 -0700130 return dstColor;
131 }
msarette6dd0042015-10-09 11:07:34 -0700132 default:
133 SkASSERT(false);
134 return 0;
135 }
136}
137
138/*
msarett74114382015-03-16 11:55:18 -0700139 * Compute row bytes for an image using pixels per byte
msarett74114382015-03-16 11:55:18 -0700140 */
msarettd1ec89b2016-08-03 12:59:27 -0700141static inline size_t compute_row_bytes_ppb(int width, uint32_t pixelsPerByte) {
msarett74114382015-03-16 11:55:18 -0700142 return (width + pixelsPerByte - 1) / pixelsPerByte;
143}
144
145/*
msarett74114382015-03-16 11:55:18 -0700146 * Compute row bytes for an image using bytes per pixel
msarett74114382015-03-16 11:55:18 -0700147 */
msarettd1ec89b2016-08-03 12:59:27 -0700148static inline size_t compute_row_bytes_bpp(int width, uint32_t bytesPerPixel) {
msarett74114382015-03-16 11:55:18 -0700149 return width * bytesPerPixel;
150}
151
152/*
msarett74114382015-03-16 11:55:18 -0700153 * Compute row bytes for an image
msarett74114382015-03-16 11:55:18 -0700154 */
msarettd1ec89b2016-08-03 12:59:27 -0700155static inline size_t compute_row_bytes(int width, uint32_t bitsPerPixel) {
msarett74114382015-03-16 11:55:18 -0700156 if (bitsPerPixel < 16) {
157 SkASSERT(0 == 8 % bitsPerPixel);
158 const uint32_t pixelsPerByte = 8 / bitsPerPixel;
159 return compute_row_bytes_ppb(width, pixelsPerByte);
160 } else {
161 SkASSERT(0 == bitsPerPixel % 8);
162 const uint32_t bytesPerPixel = bitsPerPixel / 8;
163 return compute_row_bytes_bpp(width, bytesPerPixel);
164 }
165}
166
167/*
msarett74114382015-03-16 11:55:18 -0700168 * Get a byte from a buffer
169 * This method is unsafe, the caller is responsible for performing a check
msarett74114382015-03-16 11:55:18 -0700170 */
msarettd1ec89b2016-08-03 12:59:27 -0700171static inline uint8_t get_byte(uint8_t* buffer, uint32_t i) {
msarett74114382015-03-16 11:55:18 -0700172 return buffer[i];
173}
174
175/*
msarett74114382015-03-16 11:55:18 -0700176 * Get a short from a buffer
177 * This method is unsafe, the caller is responsible for performing a check
msarett74114382015-03-16 11:55:18 -0700178 */
msarettd1ec89b2016-08-03 12:59:27 -0700179static inline uint16_t get_short(uint8_t* buffer, uint32_t i) {
msarett74114382015-03-16 11:55:18 -0700180 uint16_t result;
181 memcpy(&result, &(buffer[i]), 2);
182#ifdef SK_CPU_BENDIAN
183 return SkEndianSwap16(result);
184#else
185 return result;
186#endif
187}
188
189/*
msarett74114382015-03-16 11:55:18 -0700190 * Get an int from a buffer
191 * This method is unsafe, the caller is responsible for performing a check
msarett74114382015-03-16 11:55:18 -0700192 */
msarettd1ec89b2016-08-03 12:59:27 -0700193static inline uint32_t get_int(uint8_t* buffer, uint32_t i) {
msarett74114382015-03-16 11:55:18 -0700194 uint32_t result;
195 memcpy(&result, &(buffer[i]), 4);
196#ifdef SK_CPU_BENDIAN
197 return SkEndianSwap32(result);
198#else
199 return result;
200#endif
201}
202
msarett0e6274f2016-03-21 08:04:40 -0700203/*
204 * @param data Buffer to read bytes from
205 * @param isLittleEndian Output parameter
206 * Indicates if the data is little endian
207 * Is unaffected on false returns
208 */
msarettd1ec89b2016-08-03 12:59:27 -0700209static inline bool is_valid_endian_marker(const uint8_t* data, bool* isLittleEndian) {
msarett0e6274f2016-03-21 08:04:40 -0700210 // II indicates Intel (little endian) and MM indicates motorola (big endian).
211 if (('I' != data[0] || 'I' != data[1]) && ('M' != data[0] || 'M' != data[1])) {
212 return false;
213 }
214
215 *isLittleEndian = ('I' == data[0]);
216 return true;
217}
218
msarettd1ec89b2016-08-03 12:59:27 -0700219static inline uint16_t get_endian_short(const uint8_t* data, bool littleEndian) {
msarett0e6274f2016-03-21 08:04:40 -0700220 if (littleEndian) {
221 return (data[1] << 8) | (data[0]);
222 }
223
224 return (data[0] << 8) | (data[1]);
225}
226
msarettd1ec89b2016-08-03 12:59:27 -0700227static inline SkPMColor premultiply_argb_as_rgba(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
msarett34e0ec42016-04-22 16:27:24 -0700228 if (a != 255) {
229 r = SkMulDiv255Round(r, a);
230 g = SkMulDiv255Round(g, a);
231 b = SkMulDiv255Round(b, a);
232 }
233
234 return SkPackARGB_as_RGBA(a, r, g, b);
235}
236
msarettd1ec89b2016-08-03 12:59:27 -0700237static inline SkPMColor premultiply_argb_as_bgra(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
msarett34e0ec42016-04-22 16:27:24 -0700238 if (a != 255) {
239 r = SkMulDiv255Round(r, a);
240 g = SkMulDiv255Round(g, a);
241 b = SkMulDiv255Round(b, a);
242 }
243
244 return SkPackARGB_as_BGRA(a, r, g, b);
245}
246
msarettd1ec89b2016-08-03 12:59:27 -0700247static inline bool is_rgba(SkColorType colorType) {
msarett34e0ec42016-04-22 16:27:24 -0700248#ifdef SK_PMCOLOR_IS_RGBA
249 return (kBGRA_8888_SkColorType != colorType);
250#else
251 return (kRGBA_8888_SkColorType == colorType);
252#endif
253}
254
255// Method for coverting to a 32 bit pixel.
256typedef uint32_t (*PackColorProc)(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
257
msarettd1ec89b2016-08-03 12:59:27 -0700258static inline PackColorProc choose_pack_color_proc(bool isPremul, SkColorType colorType) {
msarett34e0ec42016-04-22 16:27:24 -0700259 bool isRGBA = is_rgba(colorType);
260 if (isPremul) {
261 if (isRGBA) {
262 return &premultiply_argb_as_rgba;
263 } else {
264 return &premultiply_argb_as_bgra;
265 }
266 } else {
267 if (isRGBA) {
268 return &SkPackARGB_as_RGBA;
269 } else {
270 return &SkPackARGB_as_BGRA;
271 }
272 }
273}
274
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400275static inline bool needs_premul(SkAlphaType dstAT, SkEncodedInfo::Alpha encodedAlpha) {
276 return kPremul_SkAlphaType == dstAT && SkEncodedInfo::kUnpremul_Alpha == encodedAlpha;
msarettdcd5e652016-08-22 08:48:40 -0700277}
278
Leon Scroggins III07418182017-08-15 12:24:02 -0400279static inline bool needs_color_xform(const SkImageInfo& dstInfo, const SkColorSpace* srcCS,
Matt Sarettcf3f2342017-03-23 15:32:25 -0400280 bool needsColorCorrectPremul) {
Matt Sarette522f4c2017-02-22 13:02:31 -0500281 // We never perform a color xform in legacy mode.
282 if (!dstInfo.colorSpace()) {
283 return false;
284 }
285
msarettdcd5e652016-08-22 08:48:40 -0700286 // F16 is by definition a linear space, so we always must perform a color xform.
287 bool isF16 = kRGBA_F16_SkColorType == dstInfo.colorType();
288
289 // Need a color xform when dst space does not match the src.
Leon Scroggins III07418182017-08-15 12:24:02 -0400290 bool srcDstNotEqual = !SkColorSpace::Equals(srcCS, dstInfo.colorSpace());
msarettdcd5e652016-08-22 08:48:40 -0700291
Matt Sarette522f4c2017-02-22 13:02:31 -0500292 return needsColorCorrectPremul || isF16 || srcDstNotEqual;
msarettd1ec89b2016-08-03 12:59:27 -0700293}
294
msarettc0444612016-09-16 11:45:58 -0700295static inline SkAlphaType select_xform_alpha(SkAlphaType dstAlphaType, SkAlphaType srcAlphaType) {
msarette99883f2016-09-08 06:05:35 -0700296 return (kOpaque_SkAlphaType == srcAlphaType) ? kOpaque_SkAlphaType : dstAlphaType;
297}
298
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500299bool is_orientation_marker(const uint8_t* data, size_t data_length, SkEncodedOrigin* orientation);
300
msarett74114382015-03-16 11:55:18 -0700301#endif // SkCodecPriv_DEFINED