Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkCodecPriv.h
Go to the documentation of this file.
1/*
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
17
18#include <string_view>
19
20#ifdef SK_PRINT_CODEC_MESSAGES
21 #define SkCodecPrintf SkDebugf
22#else
23 #define SkCodecPrintf(...)
24#endif
25
26// Defined in SkCodec.cpp
27bool sk_select_xform_format(SkColorType colorType, bool forColorTable,
28 skcms_PixelFormat* outFormat);
29
30// FIXME: Consider sharing with dm, nanbench, and tools.
31static inline float get_scale_from_sample_size(int sampleSize) {
32 return 1.0f / ((float) sampleSize);
33}
34
35static inline bool is_valid_subset(const SkIRect& subset, const SkISize& imageDims) {
36 return SkIRect::MakeSize(imageDims).contains(subset);
37}
38
39/*
40 * returns a scaled dimension based on the original dimension and the sampleSize
41 * NOTE: we round down here for scaled dimension to match the behavior of SkImageDecoder
42 * FIXME: I think we should call this get_sampled_dimension().
43 */
44static inline int get_scaled_dimension(int srcDimension, int sampleSize) {
45 if (sampleSize > srcDimension) {
46 return 1;
47 }
48 return srcDimension / sampleSize;
49}
50
51/*
52 * Returns the first coordinate that we will keep during a scaled decode.
53 * The output can be interpreted as an x-coordinate or a y-coordinate.
54 *
55 * This does not need to be called and is not called when sampleFactor == 1.
56 */
57static inline int get_start_coord(int sampleFactor) { return sampleFactor / 2; }
58
59/*
60 * Given a coordinate in the original image, this returns the corresponding
61 * coordinate in the scaled image. This function is meaningless if
62 * IsCoordNecessary returns false.
63 * The output can be interpreted as an x-coordinate or a y-coordinate.
64 *
65 * This does not need to be called and is not called when sampleFactor == 1.
66 */
67static inline int get_dst_coord(int srcCoord, int sampleFactor) { return srcCoord / sampleFactor; }
68
69/*
70 * When scaling, we will discard certain y-coordinates (rows) and
71 * x-coordinates (columns). This function returns true if we should keep the
72 * coordinate and false otherwise.
73 * The inputs may be x-coordinates or y-coordinates.
74 *
75 * This does not need to be called and is not called when sampleFactor == 1.
76 */
77static inline bool is_coord_necessary(int srcCoord, int sampleFactor, int scaledDim) {
78 // Get the first coordinate that we want to keep
79 int startCoord = get_start_coord(sampleFactor);
80
81 // Return false on edge cases
82 if (srcCoord < startCoord || get_dst_coord(srcCoord, sampleFactor) >= scaledDim) {
83 return false;
84 }
85
86 // Every sampleFactor rows are necessary
87 return ((srcCoord - startCoord) % sampleFactor) == 0;
88}
89
90static inline bool valid_alpha(SkAlphaType dstAlpha, bool srcIsOpaque) {
91 if (kUnknown_SkAlphaType == dstAlpha) {
92 return false;
93 }
94
95 if (srcIsOpaque) {
96 if (kOpaque_SkAlphaType != dstAlpha) {
97 SkCodecPrintf("Warning: an opaque image should be decoded as opaque "
98 "- it is being decoded as non-opaque, which will draw slower\n");
99 }
100 return true;
101 }
102
103 return dstAlpha != kOpaque_SkAlphaType;
104}
105
106/*
107 * If there is a color table, get a pointer to the colors, otherwise return nullptr
108 */
109static inline const SkPMColor* get_color_ptr(SkColorPalette* colorTable) {
110 return nullptr != colorTable ? colorTable->readColors() : nullptr;
111}
112
113/*
114 * Compute row bytes for an image using pixels per byte
115 */
116static inline size_t compute_row_bytes_ppb(int width, uint32_t pixelsPerByte) {
117 return (width + pixelsPerByte - 1) / pixelsPerByte;
118}
119
120/*
121 * Compute row bytes for an image using bytes per pixel
122 */
123static inline size_t compute_row_bytes_bpp(int width, uint32_t bytesPerPixel) {
124 return width * bytesPerPixel;
125}
126
127/*
128 * Compute row bytes for an image
129 */
130static inline size_t compute_row_bytes(int width, uint32_t bitsPerPixel) {
131 if (bitsPerPixel < 16) {
132 SkASSERT(0 == 8 % bitsPerPixel);
133 const uint32_t pixelsPerByte = 8 / bitsPerPixel;
134 return compute_row_bytes_ppb(width, pixelsPerByte);
135 } else {
136 SkASSERT(0 == bitsPerPixel % 8);
137 const uint32_t bytesPerPixel = bitsPerPixel / 8;
138 return compute_row_bytes_bpp(width, bytesPerPixel);
139 }
140}
141
142/*
143 * Get a byte from a buffer
144 * This method is unsafe, the caller is responsible for performing a check
145 */
146static inline uint8_t get_byte(const uint8_t* buffer, uint32_t i) {
147 return buffer[i];
148}
149
150/*
151 * Get a short from a buffer
152 * This method is unsafe, the caller is responsible for performing a check
153 */
154static inline uint16_t get_short(const uint8_t* buffer, uint32_t i) {
155 uint16_t result;
156 memcpy(&result, &(buffer[i]), 2);
157#ifdef SK_CPU_BENDIAN
158 return SkEndianSwap16(result);
159#else
160 return result;
161#endif
162}
163
164/*
165 * Get an int from a buffer
166 * This method is unsafe, the caller is responsible for performing a check
167 */
168static inline uint32_t get_int(const uint8_t* buffer, uint32_t i) {
169 uint32_t result;
170 memcpy(&result, &(buffer[i]), 4);
171#ifdef SK_CPU_BENDIAN
172 return SkEndianSwap32(result);
173#else
174 return result;
175#endif
176}
177
178/*
179 * @param data Buffer to read bytes from
180 * @param isLittleEndian Output parameter
181 * Indicates if the data is little endian
182 * Is unaffected on false returns
183 */
184static inline bool is_valid_endian_marker(const uint8_t* data, bool* isLittleEndian) {
185 // II indicates Intel (little endian) and MM indicates motorola (big endian).
186 if (('I' != data[0] || 'I' != data[1]) && ('M' != data[0] || 'M' != data[1])) {
187 return false;
188 }
189
190 *isLittleEndian = ('I' == data[0]);
191 return true;
192}
193
194static inline uint16_t get_endian_short(const uint8_t* data, bool littleEndian) {
195 if (littleEndian) {
196 return (data[1] << 8) | (data[0]);
197 }
198
199 return (data[0] << 8) | (data[1]);
200}
201
202static inline uint32_t get_endian_int(const uint8_t* data, bool littleEndian) {
203 if (littleEndian) {
204 return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | (data[0]);
205 }
206
207 return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | (data[3]);
208}
209
211 if (a != 255) {
212 r = SkMulDiv255Round(r, a);
213 g = SkMulDiv255Round(g, a);
214 b = SkMulDiv255Round(b, a);
215 }
216
217 return SkPackARGB_as_RGBA(a, r, g, b);
218}
219
221 if (a != 255) {
222 r = SkMulDiv255Round(r, a);
223 g = SkMulDiv255Round(g, a);
224 b = SkMulDiv255Round(b, a);
225 }
226
227 return SkPackARGB_as_BGRA(a, r, g, b);
228}
229
230static inline bool is_rgba(SkColorType colorType) {
231#ifdef SK_PMCOLOR_IS_RGBA
233#else
235#endif
236}
237
238// Method for coverting to a 32 bit pixel.
239typedef uint32_t (*PackColorProc)(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
240
242 bool isRGBA = is_rgba(colorType);
243 if (isPremul) {
244 if (isRGBA) {
246 } else {
248 }
249 } else {
250 if (isRGBA) {
251 return &SkPackARGB_as_RGBA;
252 } else {
253 return &SkPackARGB_as_BGRA;
254 }
255 }
256}
257
258namespace SkCodecs {
259bool HasDecoder(std::string_view id);
260}
261
262#endif // SkCodecPriv_DEFINED
SkAlphaType
Definition SkAlphaType.h:26
@ kUnknown_SkAlphaType
uninitialized
Definition SkAlphaType.h:27
@ kOpaque_SkAlphaType
pixel is opaque
Definition SkAlphaType.h:28
#define SkASSERT(cond)
Definition SkAssert.h:116
unsigned U8CPU
Definition SkCPUTypes.h:18
static bool is_coord_necessary(int srcCoord, int sampleFactor, int scaledDim)
Definition SkCodecPriv.h:77
static uint8_t get_byte(const uint8_t *buffer, uint32_t i)
uint32_t(* PackColorProc)(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
static uint32_t get_endian_int(const uint8_t *data, bool littleEndian)
static PackColorProc choose_pack_color_proc(bool isPremul, SkColorType colorType)
static size_t compute_row_bytes(int width, uint32_t bitsPerPixel)
static bool is_valid_subset(const SkIRect &subset, const SkISize &imageDims)
Definition SkCodecPriv.h:35
static const SkPMColor * get_color_ptr(SkColorPalette *colorTable)
static bool valid_alpha(SkAlphaType dstAlpha, bool srcIsOpaque)
Definition SkCodecPriv.h:90
static size_t compute_row_bytes_bpp(int width, uint32_t bytesPerPixel)
static int get_dst_coord(int srcCoord, int sampleFactor)
Definition SkCodecPriv.h:67
static size_t compute_row_bytes_ppb(int width, uint32_t pixelsPerByte)
static float get_scale_from_sample_size(int sampleSize)
Definition SkCodecPriv.h:31
static int get_scaled_dimension(int srcDimension, int sampleSize)
Definition SkCodecPriv.h:44
static SkPMColor premultiply_argb_as_rgba(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
bool sk_select_xform_format(SkColorType colorType, bool forColorTable, skcms_PixelFormat *outFormat)
Definition SkCodec.cpp:767
static uint32_t get_int(const uint8_t *buffer, uint32_t i)
static uint16_t get_endian_short(const uint8_t *data, bool littleEndian)
#define SkCodecPrintf(...)
Definition SkCodecPriv.h:23
static uint16_t get_short(const uint8_t *buffer, uint32_t i)
static bool is_valid_endian_marker(const uint8_t *data, bool *isLittleEndian)
static SkPMColor premultiply_argb_as_bgra(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
static bool is_rgba(SkColorType colorType)
static int get_start_coord(int sampleFactor)
Definition SkCodecPriv.h:57
static uint32_t SkPackARGB_as_RGBA(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition SkColorData.h:65
static uint32_t SkPackARGB_as_BGRA(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition SkColorData.h:74
SkColorType
Definition SkColorType.h:19
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition SkColorType.h:26
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
uint32_t SkPMColor
Definition SkColor.h:205
static uint16_t SkEndianSwap16(uint16_t value)
Definition SkEndian.h:33
static constexpr uint32_t SkEndianSwap32(uint32_t value)
Definition SkEndian.h:56
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
static U8CPU SkMulDiv255Round(U16CPU a, U16CPU b)
Definition SkMath.h:73
const SkPMColor * readColors() const
static bool b
struct MyStruct a[10]
static const uint8_t buffer[]
GAsyncResult * result
bool HasDecoder(std::string_view id)
Definition SkCodec.cpp:146
int32_t width
skcms_PixelFormat
static constexpr SkIRect MakeSize(const SkISize &size)
Definition SkRect.h:66
bool contains(int32_t x, int32_t y) const
Definition SkRect.h:463