blob: 10bd064ac4fc29d9729c38c2f509bcf8021306ee [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"
msarett9e43cab2015-04-29 07:38:43 -070012#include "SkColorTable.h"
Matt Sarett1a85ca52016-11-04 11:52:48 -040013#include "SkEncodedInfo.h"
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050014#include "SkEncodedOrigin.h"
msarett74114382015-03-16 11:55:18 -070015#include "SkImageInfo.h"
msarett74114382015-03-16 11:55:18 -070016#include "SkTypes.h"
17
Leon Scroggins III2cc7d132018-01-19 12:12:13 -050018#ifdef SK_PRINT_CODEC_MESSAGES
scroggoc5560be2016-02-03 09:42:42 -080019 #define SkCodecPrintf SkDebugf
20#else
21 #define SkCodecPrintf(...)
22#endif
23
msarett3d9d7a72015-10-21 10:27:10 -070024// FIXME: Consider sharing with dm, nanbench, and tools.
msarettd1ec89b2016-08-03 12:59:27 -070025static inline float get_scale_from_sample_size(int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -070026 return 1.0f / ((float) sampleSize);
27}
28
msarettd1ec89b2016-08-03 12:59:27 -070029static inline bool is_valid_subset(const SkIRect& subset, const SkISize& imageDims) {
msarett3d9d7a72015-10-21 10:27:10 -070030 return SkIRect::MakeSize(imageDims).contains(subset);
31}
32
msarett5406d6f2015-08-31 06:55:13 -070033/*
msarett10522ff2015-09-07 08:54:01 -070034 * returns a scaled dimension based on the original dimension and the sampleSize
35 * NOTE: we round down here for scaled dimension to match the behavior of SkImageDecoder
msarett3d9d7a72015-10-21 10:27:10 -070036 * FIXME: I think we should call this get_sampled_dimension().
msarett10522ff2015-09-07 08:54:01 -070037 */
msarettd1ec89b2016-08-03 12:59:27 -070038static inline int get_scaled_dimension(int srcDimension, int sampleSize) {
msarett10522ff2015-09-07 08:54:01 -070039 if (sampleSize > srcDimension) {
40 return 1;
41 }
42 return srcDimension / sampleSize;
43}
44
45/*
msarett5406d6f2015-08-31 06:55:13 -070046 * Returns the first coordinate that we will keep during a scaled decode.
47 * The output can be interpreted as an x-coordinate or a y-coordinate.
48 *
49 * This does not need to be called and is not called when sampleFactor == 1.
50 */
msarettd1ec89b2016-08-03 12:59:27 -070051static inline int get_start_coord(int sampleFactor) { return sampleFactor / 2; };
msarett5406d6f2015-08-31 06:55:13 -070052
53/*
54 * Given a coordinate in the original image, this returns the corresponding
55 * coordinate in the scaled image. This function is meaningless if
56 * IsCoordNecessary returns false.
57 * The output can be interpreted as an x-coordinate or a y-coordinate.
58 *
59 * This does not need to be called and is not called when sampleFactor == 1.
60 */
msarettd1ec89b2016-08-03 12:59:27 -070061static inline int get_dst_coord(int srcCoord, int sampleFactor) { return srcCoord / sampleFactor; };
msarett5406d6f2015-08-31 06:55:13 -070062
63/*
64 * When scaling, we will discard certain y-coordinates (rows) and
65 * x-coordinates (columns). This function returns true if we should keep the
66 * coordinate and false otherwise.
67 * The inputs may be x-coordinates or y-coordinates.
68 *
69 * This does not need to be called and is not called when sampleFactor == 1.
70 */
msarettd1ec89b2016-08-03 12:59:27 -070071static inline bool is_coord_necessary(int srcCoord, int sampleFactor, int scaledDim) {
msarett5406d6f2015-08-31 06:55:13 -070072 // Get the first coordinate that we want to keep
73 int startCoord = get_start_coord(sampleFactor);
74
75 // Return false on edge cases
76 if (srcCoord < startCoord || get_dst_coord(srcCoord, sampleFactor) >= scaledDim) {
77 return false;
78 }
79
80 // Every sampleFactor rows are necessary
81 return ((srcCoord - startCoord) % sampleFactor) == 0;
82}
83
Leon Scroggins III07418182017-08-15 12:24:02 -040084static inline bool valid_alpha(SkAlphaType dstAlpha, bool srcIsOpaque) {
scroggoc5560be2016-02-03 09:42:42 -080085 if (kUnknown_SkAlphaType == dstAlpha) {
86 return false;
87 }
88
Leon Scroggins III07418182017-08-15 12:24:02 -040089 if (srcIsOpaque) {
90 if (kOpaque_SkAlphaType != dstAlpha) {
scroggoc5560be2016-02-03 09:42:42 -080091 SkCodecPrintf("Warning: an opaque image should be decoded as opaque "
92 "- it is being decoded as non-opaque, which will draw slower\n");
msarett4ab9d5f2015-08-06 15:34:42 -070093 }
Leon Scroggins III07418182017-08-15 12:24:02 -040094 return true;
msarett4ab9d5f2015-08-06 15:34:42 -070095 }
Leon Scroggins III07418182017-08-15 12:24:02 -040096
97 return dstAlpha != kOpaque_SkAlphaType;
msarett4ab9d5f2015-08-06 15:34:42 -070098}
99
msarett74114382015-03-16 11:55:18 -0700100/*
halcanary96fcdcc2015-08-27 07:41:13 -0700101 * If there is a color table, get a pointer to the colors, otherwise return nullptr
msarett99f567e2015-08-05 12:58:26 -0700102 */
msarettd1ec89b2016-08-03 12:59:27 -0700103static inline const SkPMColor* get_color_ptr(SkColorTable* colorTable) {
halcanary96fcdcc2015-08-27 07:41:13 -0700104 return nullptr != colorTable ? colorTable->readColors() : nullptr;
msarett99f567e2015-08-05 12:58:26 -0700105}
106
107/*
msarett74114382015-03-16 11:55:18 -0700108 * Compute row bytes for an image using pixels per byte
msarett74114382015-03-16 11:55:18 -0700109 */
msarettd1ec89b2016-08-03 12:59:27 -0700110static inline size_t compute_row_bytes_ppb(int width, uint32_t pixelsPerByte) {
msarett74114382015-03-16 11:55:18 -0700111 return (width + pixelsPerByte - 1) / pixelsPerByte;
112}
113
114/*
msarett74114382015-03-16 11:55:18 -0700115 * Compute row bytes for an image using bytes per pixel
msarett74114382015-03-16 11:55:18 -0700116 */
msarettd1ec89b2016-08-03 12:59:27 -0700117static inline size_t compute_row_bytes_bpp(int width, uint32_t bytesPerPixel) {
msarett74114382015-03-16 11:55:18 -0700118 return width * bytesPerPixel;
119}
120
121/*
msarett74114382015-03-16 11:55:18 -0700122 * Compute row bytes for an image
msarett74114382015-03-16 11:55:18 -0700123 */
msarettd1ec89b2016-08-03 12:59:27 -0700124static inline size_t compute_row_bytes(int width, uint32_t bitsPerPixel) {
msarett74114382015-03-16 11:55:18 -0700125 if (bitsPerPixel < 16) {
126 SkASSERT(0 == 8 % bitsPerPixel);
127 const uint32_t pixelsPerByte = 8 / bitsPerPixel;
128 return compute_row_bytes_ppb(width, pixelsPerByte);
129 } else {
130 SkASSERT(0 == bitsPerPixel % 8);
131 const uint32_t bytesPerPixel = bitsPerPixel / 8;
132 return compute_row_bytes_bpp(width, bytesPerPixel);
133 }
134}
135
136/*
msarett74114382015-03-16 11:55:18 -0700137 * Get a byte from a buffer
138 * This method is unsafe, the caller is responsible for performing a check
msarett74114382015-03-16 11:55:18 -0700139 */
msarettd1ec89b2016-08-03 12:59:27 -0700140static inline uint8_t get_byte(uint8_t* buffer, uint32_t i) {
msarett74114382015-03-16 11:55:18 -0700141 return buffer[i];
142}
143
144/*
msarett74114382015-03-16 11:55:18 -0700145 * Get a short from a buffer
146 * This method is unsafe, the caller is responsible for performing a check
msarett74114382015-03-16 11:55:18 -0700147 */
msarettd1ec89b2016-08-03 12:59:27 -0700148static inline uint16_t get_short(uint8_t* buffer, uint32_t i) {
msarett74114382015-03-16 11:55:18 -0700149 uint16_t result;
150 memcpy(&result, &(buffer[i]), 2);
151#ifdef SK_CPU_BENDIAN
152 return SkEndianSwap16(result);
153#else
154 return result;
155#endif
156}
157
158/*
msarett74114382015-03-16 11:55:18 -0700159 * Get an int from a buffer
160 * This method is unsafe, the caller is responsible for performing a check
msarett74114382015-03-16 11:55:18 -0700161 */
msarettd1ec89b2016-08-03 12:59:27 -0700162static inline uint32_t get_int(uint8_t* buffer, uint32_t i) {
msarett74114382015-03-16 11:55:18 -0700163 uint32_t result;
164 memcpy(&result, &(buffer[i]), 4);
165#ifdef SK_CPU_BENDIAN
166 return SkEndianSwap32(result);
167#else
168 return result;
169#endif
170}
171
msarett0e6274f2016-03-21 08:04:40 -0700172/*
173 * @param data Buffer to read bytes from
174 * @param isLittleEndian Output parameter
175 * Indicates if the data is little endian
176 * Is unaffected on false returns
177 */
msarettd1ec89b2016-08-03 12:59:27 -0700178static inline bool is_valid_endian_marker(const uint8_t* data, bool* isLittleEndian) {
msarett0e6274f2016-03-21 08:04:40 -0700179 // II indicates Intel (little endian) and MM indicates motorola (big endian).
180 if (('I' != data[0] || 'I' != data[1]) && ('M' != data[0] || 'M' != data[1])) {
181 return false;
182 }
183
184 *isLittleEndian = ('I' == data[0]);
185 return true;
186}
187
msarettd1ec89b2016-08-03 12:59:27 -0700188static inline uint16_t get_endian_short(const uint8_t* data, bool littleEndian) {
msarett0e6274f2016-03-21 08:04:40 -0700189 if (littleEndian) {
190 return (data[1] << 8) | (data[0]);
191 }
192
193 return (data[0] << 8) | (data[1]);
194}
195
msarettd1ec89b2016-08-03 12:59:27 -0700196static inline SkPMColor premultiply_argb_as_rgba(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
msarett34e0ec42016-04-22 16:27:24 -0700197 if (a != 255) {
198 r = SkMulDiv255Round(r, a);
199 g = SkMulDiv255Round(g, a);
200 b = SkMulDiv255Round(b, a);
201 }
202
203 return SkPackARGB_as_RGBA(a, r, g, b);
204}
205
msarettd1ec89b2016-08-03 12:59:27 -0700206static inline SkPMColor premultiply_argb_as_bgra(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
msarett34e0ec42016-04-22 16:27:24 -0700207 if (a != 255) {
208 r = SkMulDiv255Round(r, a);
209 g = SkMulDiv255Round(g, a);
210 b = SkMulDiv255Round(b, a);
211 }
212
213 return SkPackARGB_as_BGRA(a, r, g, b);
214}
215
msarettd1ec89b2016-08-03 12:59:27 -0700216static inline bool is_rgba(SkColorType colorType) {
msarett34e0ec42016-04-22 16:27:24 -0700217#ifdef SK_PMCOLOR_IS_RGBA
218 return (kBGRA_8888_SkColorType != colorType);
219#else
220 return (kRGBA_8888_SkColorType == colorType);
221#endif
222}
223
224// Method for coverting to a 32 bit pixel.
225typedef uint32_t (*PackColorProc)(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
226
msarettd1ec89b2016-08-03 12:59:27 -0700227static inline PackColorProc choose_pack_color_proc(bool isPremul, SkColorType colorType) {
msarett34e0ec42016-04-22 16:27:24 -0700228 bool isRGBA = is_rgba(colorType);
229 if (isPremul) {
230 if (isRGBA) {
231 return &premultiply_argb_as_rgba;
232 } else {
233 return &premultiply_argb_as_bgra;
234 }
235 } else {
236 if (isRGBA) {
237 return &SkPackARGB_as_RGBA;
238 } else {
239 return &SkPackARGB_as_BGRA;
240 }
241 }
242}
243
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500244bool is_orientation_marker(const uint8_t* data, size_t data_length, SkEncodedOrigin* orientation);
245
msarett74114382015-03-16 11:55:18 -0700246#endif // SkCodecPriv_DEFINED