OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The Android Open Source Project | 2 * Copyright 2015 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkCodecPriv_DEFINED | 8 #ifndef SkCodecPriv_DEFINED |
9 #define SkCodecPriv_DEFINED | 9 #define SkCodecPriv_DEFINED |
10 | 10 |
11 #include "SkColorPriv.h" | 11 #include "SkColorPriv.h" |
12 #include "SkColorTable.h" | 12 #include "SkColorTable.h" |
13 #include "SkImageInfo.h" | 13 #include "SkImageInfo.h" |
14 #include "SkTypes.h" | 14 #include "SkTypes.h" |
15 | 15 |
16 #ifdef SK_PRINT_CODEC_MESSAGES | 16 #ifdef SK_PRINT_CODEC_MESSAGES |
17 #define SkCodecPrintf SkDebugf | 17 #define SkCodecPrintf SkDebugf |
18 #else | 18 #else |
19 #define SkCodecPrintf(...) | 19 #define SkCodecPrintf(...) |
20 #endif | 20 #endif |
21 | 21 |
22 // FIXME: Consider sharing with dm, nanbench, and tools. | 22 // FIXME: Consider sharing with dm, nanbench, and tools. |
23 inline float get_scale_from_sample_size(int sampleSize) { | 23 static inline float get_scale_from_sample_size(int sampleSize) { |
24 return 1.0f / ((float) sampleSize); | 24 return 1.0f / ((float) sampleSize); |
25 } | 25 } |
26 | 26 |
27 inline bool is_valid_subset(const SkIRect& subset, const SkISize& imageDims) { | 27 static inline bool is_valid_subset(const SkIRect& subset, const SkISize& imageDi
ms) { |
28 return SkIRect::MakeSize(imageDims).contains(subset); | 28 return SkIRect::MakeSize(imageDims).contains(subset); |
29 } | 29 } |
30 | 30 |
31 /* | 31 /* |
32 * returns a scaled dimension based on the original dimension and the sampleSize | 32 * returns a scaled dimension based on the original dimension and the sampleSize |
33 * NOTE: we round down here for scaled dimension to match the behavior of SkImag
eDecoder | 33 * NOTE: we round down here for scaled dimension to match the behavior of SkImag
eDecoder |
34 * FIXME: I think we should call this get_sampled_dimension(). | 34 * FIXME: I think we should call this get_sampled_dimension(). |
35 */ | 35 */ |
36 inline int get_scaled_dimension(int srcDimension, int sampleSize) { | 36 static inline int get_scaled_dimension(int srcDimension, int sampleSize) { |
37 if (sampleSize > srcDimension) { | 37 if (sampleSize > srcDimension) { |
38 return 1; | 38 return 1; |
39 } | 39 } |
40 return srcDimension / sampleSize; | 40 return srcDimension / sampleSize; |
41 } | 41 } |
42 | 42 |
43 /* | 43 /* |
44 * Returns the first coordinate that we will keep during a scaled decode. | 44 * Returns the first coordinate that we will keep during a scaled decode. |
45 * The output can be interpreted as an x-coordinate or a y-coordinate. | 45 * The output can be interpreted as an x-coordinate or a y-coordinate. |
46 * | 46 * |
47 * This does not need to be called and is not called when sampleFactor == 1. | 47 * This does not need to be called and is not called when sampleFactor == 1. |
48 */ | 48 */ |
49 inline int get_start_coord(int sampleFactor) { return sampleFactor / 2; }; | 49 static inline int get_start_coord(int sampleFactor) { return sampleFactor / 2; }
; |
50 | 50 |
51 /* | 51 /* |
52 * Given a coordinate in the original image, this returns the corresponding | 52 * Given a coordinate in the original image, this returns the corresponding |
53 * coordinate in the scaled image. This function is meaningless if | 53 * coordinate in the scaled image. This function is meaningless if |
54 * IsCoordNecessary returns false. | 54 * IsCoordNecessary returns false. |
55 * The output can be interpreted as an x-coordinate or a y-coordinate. | 55 * The output can be interpreted as an x-coordinate or a y-coordinate. |
56 * | 56 * |
57 * This does not need to be called and is not called when sampleFactor == 1. | 57 * This does not need to be called and is not called when sampleFactor == 1. |
58 */ | 58 */ |
59 inline int get_dst_coord(int srcCoord, int sampleFactor) { return srcCoord / sam
pleFactor; }; | 59 static inline int get_dst_coord(int srcCoord, int sampleFactor) { return srcCoor
d / sampleFactor; }; |
60 | 60 |
61 /* | 61 /* |
62 * When scaling, we will discard certain y-coordinates (rows) and | 62 * When scaling, we will discard certain y-coordinates (rows) and |
63 * x-coordinates (columns). This function returns true if we should keep the | 63 * x-coordinates (columns). This function returns true if we should keep the |
64 * coordinate and false otherwise. | 64 * coordinate and false otherwise. |
65 * The inputs may be x-coordinates or y-coordinates. | 65 * The inputs may be x-coordinates or y-coordinates. |
66 * | 66 * |
67 * This does not need to be called and is not called when sampleFactor == 1. | 67 * This does not need to be called and is not called when sampleFactor == 1. |
68 */ | 68 */ |
69 inline bool is_coord_necessary(int srcCoord, int sampleFactor, int scaledDim) { | 69 static inline bool is_coord_necessary(int srcCoord, int sampleFactor, int scaled
Dim) { |
70 // Get the first coordinate that we want to keep | 70 // Get the first coordinate that we want to keep |
71 int startCoord = get_start_coord(sampleFactor); | 71 int startCoord = get_start_coord(sampleFactor); |
72 | 72 |
73 // Return false on edge cases | 73 // Return false on edge cases |
74 if (srcCoord < startCoord || get_dst_coord(srcCoord, sampleFactor) >= scaled
Dim) { | 74 if (srcCoord < startCoord || get_dst_coord(srcCoord, sampleFactor) >= scaled
Dim) { |
75 return false; | 75 return false; |
76 } | 76 } |
77 | 77 |
78 // Every sampleFactor rows are necessary | 78 // Every sampleFactor rows are necessary |
79 return ((srcCoord - startCoord) % sampleFactor) == 0; | 79 return ((srcCoord - startCoord) % sampleFactor) == 0; |
80 } | 80 } |
81 | 81 |
82 inline bool valid_alpha(SkAlphaType dstAlpha, SkAlphaType srcAlpha) { | 82 static inline bool valid_alpha(SkAlphaType dstAlpha, SkAlphaType srcAlpha) { |
83 if (kUnknown_SkAlphaType == dstAlpha) { | 83 if (kUnknown_SkAlphaType == dstAlpha) { |
84 return false; | 84 return false; |
85 } | 85 } |
86 | 86 |
87 if (srcAlpha != dstAlpha) { | 87 if (srcAlpha != dstAlpha) { |
88 if (kOpaque_SkAlphaType == srcAlpha) { | 88 if (kOpaque_SkAlphaType == srcAlpha) { |
89 // If the source is opaque, we can support any. | 89 // If the source is opaque, we can support any. |
90 SkCodecPrintf("Warning: an opaque image should be decoded as opaque
" | 90 SkCodecPrintf("Warning: an opaque image should be decoded as opaque
" |
91 "- it is being decoded as non-opaque, which will draw
slower\n"); | 91 "- it is being decoded as non-opaque, which will draw
slower\n"); |
92 return true; | 92 return true; |
93 } | 93 } |
94 | 94 |
95 // The source is not opaque | 95 // The source is not opaque |
96 switch (dstAlpha) { | 96 switch (dstAlpha) { |
97 case kPremul_SkAlphaType: | 97 case kPremul_SkAlphaType: |
98 case kUnpremul_SkAlphaType: | 98 case kUnpremul_SkAlphaType: |
99 // The source is not opaque, so either of these is okay | 99 // The source is not opaque, so either of these is okay |
100 break; | 100 break; |
101 default: | 101 default: |
102 // We cannot decode a non-opaque image to opaque (or unknown) | 102 // We cannot decode a non-opaque image to opaque (or unknown) |
103 return false; | 103 return false; |
104 } | 104 } |
105 } | 105 } |
106 return true; | 106 return true; |
107 } | 107 } |
108 | 108 |
109 /* | 109 /* |
110 * Most of our codecs support the same conversions: | 110 * Most of our codecs support the same conversions: |
111 * - profileType must be the same | |
112 * - opaque to any alpha type | 111 * - opaque to any alpha type |
113 * - 565 only if opaque | 112 * - 565 only if opaque |
114 * - premul to unpremul and vice versa | 113 * - premul to unpremul and vice versa |
115 * - always support N32 | 114 * - always support N32 |
116 * - otherwise match the src color type | 115 * - otherwise match the src color type |
117 */ | 116 */ |
118 inline bool conversion_possible(const SkImageInfo& dst, const SkImageInfo& src)
{ | 117 static inline bool conversion_possible(const SkImageInfo& dst, const SkImageInfo
& src) { |
119 // FIXME: skbug.com/4895 | |
120 // Currently, we ignore the SkColorProfileType on the SkImageInfo. We | |
121 // will treat the encoded data as linear regardless of what the client | |
122 // requests. | |
123 | |
124 // Ensure the alpha type is valid | 118 // Ensure the alpha type is valid |
125 if (!valid_alpha(dst.alphaType(), src.alphaType())) { | 119 if (!valid_alpha(dst.alphaType(), src.alphaType())) { |
126 return false; | 120 return false; |
127 } | 121 } |
128 | 122 |
129 // Check for supported color types | 123 // Check for supported color types |
130 switch (dst.colorType()) { | 124 switch (dst.colorType()) { |
131 case kRGBA_8888_SkColorType: | 125 case kRGBA_8888_SkColorType: |
132 case kBGRA_8888_SkColorType: | 126 case kBGRA_8888_SkColorType: |
133 return true; | 127 return true; |
134 case kRGB_565_SkColorType: | 128 case kRGB_565_SkColorType: |
135 return kOpaque_SkAlphaType == src.alphaType(); | 129 return kOpaque_SkAlphaType == src.alphaType(); |
136 default: | 130 default: |
137 return dst.colorType() == src.colorType(); | 131 return dst.colorType() == src.colorType(); |
138 } | 132 } |
139 } | 133 } |
140 | 134 |
141 /* | 135 /* |
142 * If there is a color table, get a pointer to the colors, otherwise return null
ptr | 136 * If there is a color table, get a pointer to the colors, otherwise return null
ptr |
143 */ | 137 */ |
144 inline const SkPMColor* get_color_ptr(SkColorTable* colorTable) { | 138 static inline const SkPMColor* get_color_ptr(SkColorTable* colorTable) { |
145 return nullptr != colorTable ? colorTable->readColors() : nullptr; | 139 return nullptr != colorTable ? colorTable->readColors() : nullptr; |
146 } | 140 } |
147 | 141 |
148 /* | 142 /* |
149 * Given that the encoded image uses a color table, return the fill value | 143 * Given that the encoded image uses a color table, return the fill value |
150 */ | 144 */ |
151 inline uint32_t get_color_table_fill_value(SkColorType colorType, const SkPMColo
r* colorPtr, | 145 static inline uint32_t get_color_table_fill_value(SkColorType colorType, const S
kPMColor* colorPtr, |
152 uint8_t fillIndex) { | 146 uint8_t fillIndex) { |
153 SkASSERT(nullptr != colorPtr); | 147 SkASSERT(nullptr != colorPtr); |
154 switch (colorType) { | 148 switch (colorType) { |
155 case kRGBA_8888_SkColorType: | 149 case kRGBA_8888_SkColorType: |
156 case kBGRA_8888_SkColorType: | 150 case kBGRA_8888_SkColorType: |
157 return colorPtr[fillIndex]; | 151 return colorPtr[fillIndex]; |
158 case kRGB_565_SkColorType: | 152 case kRGB_565_SkColorType: |
159 return SkPixel32ToPixel16(colorPtr[fillIndex]); | 153 return SkPixel32ToPixel16(colorPtr[fillIndex]); |
160 case kIndex_8_SkColorType: | 154 case kIndex_8_SkColorType: |
161 return fillIndex; | 155 return fillIndex; |
162 default: | 156 default: |
163 SkASSERT(false); | 157 SkASSERT(false); |
164 return 0; | 158 return 0; |
165 } | 159 } |
166 } | 160 } |
167 | 161 |
168 /* | 162 /* |
169 * | 163 * |
170 * Copy the codec color table back to the client when kIndex8 color type is requ
ested | 164 * Copy the codec color table back to the client when kIndex8 color type is requ
ested |
171 */ | 165 */ |
172 inline void copy_color_table(const SkImageInfo& dstInfo, SkColorTable* colorTabl
e, | 166 static inline void copy_color_table(const SkImageInfo& dstInfo, SkColorTable* co
lorTable, |
173 SkPMColor* inputColorPtr, int* inputColorCount) { | 167 SkPMColor* inputColorPtr, int* inputColorCount) { |
174 if (kIndex_8_SkColorType == dstInfo.colorType()) { | 168 if (kIndex_8_SkColorType == dstInfo.colorType()) { |
175 SkASSERT(nullptr != inputColorPtr); | 169 SkASSERT(nullptr != inputColorPtr); |
176 SkASSERT(nullptr != inputColorCount); | 170 SkASSERT(nullptr != inputColorCount); |
177 SkASSERT(nullptr != colorTable); | 171 SkASSERT(nullptr != colorTable); |
178 memcpy(inputColorPtr, colorTable->readColors(), *inputColorCount * sizeo
f(SkPMColor)); | 172 memcpy(inputColorPtr, colorTable->readColors(), *inputColorCount * sizeo
f(SkPMColor)); |
179 } | 173 } |
180 } | 174 } |
181 | 175 |
182 /* | 176 /* |
183 * Compute row bytes for an image using pixels per byte | 177 * Compute row bytes for an image using pixels per byte |
184 */ | 178 */ |
185 inline size_t compute_row_bytes_ppb(int width, uint32_t pixelsPerByte) { | 179 static inline size_t compute_row_bytes_ppb(int width, uint32_t pixelsPerByte) { |
186 return (width + pixelsPerByte - 1) / pixelsPerByte; | 180 return (width + pixelsPerByte - 1) / pixelsPerByte; |
187 } | 181 } |
188 | 182 |
189 /* | 183 /* |
190 * Compute row bytes for an image using bytes per pixel | 184 * Compute row bytes for an image using bytes per pixel |
191 */ | 185 */ |
192 inline size_t compute_row_bytes_bpp(int width, uint32_t bytesPerPixel) { | 186 static inline size_t compute_row_bytes_bpp(int width, uint32_t bytesPerPixel) { |
193 return width * bytesPerPixel; | 187 return width * bytesPerPixel; |
194 } | 188 } |
195 | 189 |
196 /* | 190 /* |
197 * Compute row bytes for an image | 191 * Compute row bytes for an image |
198 */ | 192 */ |
199 inline size_t compute_row_bytes(int width, uint32_t bitsPerPixel) { | 193 static inline size_t compute_row_bytes(int width, uint32_t bitsPerPixel) { |
200 if (bitsPerPixel < 16) { | 194 if (bitsPerPixel < 16) { |
201 SkASSERT(0 == 8 % bitsPerPixel); | 195 SkASSERT(0 == 8 % bitsPerPixel); |
202 const uint32_t pixelsPerByte = 8 / bitsPerPixel; | 196 const uint32_t pixelsPerByte = 8 / bitsPerPixel; |
203 return compute_row_bytes_ppb(width, pixelsPerByte); | 197 return compute_row_bytes_ppb(width, pixelsPerByte); |
204 } else { | 198 } else { |
205 SkASSERT(0 == bitsPerPixel % 8); | 199 SkASSERT(0 == bitsPerPixel % 8); |
206 const uint32_t bytesPerPixel = bitsPerPixel / 8; | 200 const uint32_t bytesPerPixel = bitsPerPixel / 8; |
207 return compute_row_bytes_bpp(width, bytesPerPixel); | 201 return compute_row_bytes_bpp(width, bytesPerPixel); |
208 } | 202 } |
209 } | 203 } |
210 | 204 |
211 /* | 205 /* |
212 * Get a byte from a buffer | 206 * Get a byte from a buffer |
213 * This method is unsafe, the caller is responsible for performing a check | 207 * This method is unsafe, the caller is responsible for performing a check |
214 */ | 208 */ |
215 inline uint8_t get_byte(uint8_t* buffer, uint32_t i) { | 209 static inline uint8_t get_byte(uint8_t* buffer, uint32_t i) { |
216 return buffer[i]; | 210 return buffer[i]; |
217 } | 211 } |
218 | 212 |
219 /* | 213 /* |
220 * Get a short from a buffer | 214 * Get a short from a buffer |
221 * This method is unsafe, the caller is responsible for performing a check | 215 * This method is unsafe, the caller is responsible for performing a check |
222 */ | 216 */ |
223 inline uint16_t get_short(uint8_t* buffer, uint32_t i) { | 217 static inline uint16_t get_short(uint8_t* buffer, uint32_t i) { |
224 uint16_t result; | 218 uint16_t result; |
225 memcpy(&result, &(buffer[i]), 2); | 219 memcpy(&result, &(buffer[i]), 2); |
226 #ifdef SK_CPU_BENDIAN | 220 #ifdef SK_CPU_BENDIAN |
227 return SkEndianSwap16(result); | 221 return SkEndianSwap16(result); |
228 #else | 222 #else |
229 return result; | 223 return result; |
230 #endif | 224 #endif |
231 } | 225 } |
232 | 226 |
233 /* | 227 /* |
234 * Get an int from a buffer | 228 * Get an int from a buffer |
235 * This method is unsafe, the caller is responsible for performing a check | 229 * This method is unsafe, the caller is responsible for performing a check |
236 */ | 230 */ |
237 inline uint32_t get_int(uint8_t* buffer, uint32_t i) { | 231 static inline uint32_t get_int(uint8_t* buffer, uint32_t i) { |
238 uint32_t result; | 232 uint32_t result; |
239 memcpy(&result, &(buffer[i]), 4); | 233 memcpy(&result, &(buffer[i]), 4); |
240 #ifdef SK_CPU_BENDIAN | 234 #ifdef SK_CPU_BENDIAN |
241 return SkEndianSwap32(result); | 235 return SkEndianSwap32(result); |
242 #else | 236 #else |
243 return result; | 237 return result; |
244 #endif | 238 #endif |
245 } | 239 } |
246 | 240 |
247 /* | 241 /* |
248 * @param data Buffer to read bytes from | 242 * @param data Buffer to read bytes from |
249 * @param isLittleEndian Output parameter | 243 * @param isLittleEndian Output parameter |
250 * Indicates if the data is little endian | 244 * Indicates if the data is little endian |
251 * Is unaffected on false returns | 245 * Is unaffected on false returns |
252 */ | 246 */ |
253 inline bool is_valid_endian_marker(const uint8_t* data, bool* isLittleEndian) { | 247 static inline bool is_valid_endian_marker(const uint8_t* data, bool* isLittleEnd
ian) { |
254 // II indicates Intel (little endian) and MM indicates motorola (big endian)
. | 248 // II indicates Intel (little endian) and MM indicates motorola (big endian)
. |
255 if (('I' != data[0] || 'I' != data[1]) && ('M' != data[0] || 'M' != data[1])
) { | 249 if (('I' != data[0] || 'I' != data[1]) && ('M' != data[0] || 'M' != data[1])
) { |
256 return false; | 250 return false; |
257 } | 251 } |
258 | 252 |
259 *isLittleEndian = ('I' == data[0]); | 253 *isLittleEndian = ('I' == data[0]); |
260 return true; | 254 return true; |
261 } | 255 } |
262 | 256 |
263 inline uint16_t get_endian_short(const uint8_t* data, bool littleEndian) { | 257 static inline uint16_t get_endian_short(const uint8_t* data, bool littleEndian)
{ |
264 if (littleEndian) { | 258 if (littleEndian) { |
265 return (data[1] << 8) | (data[0]); | 259 return (data[1] << 8) | (data[0]); |
266 } | 260 } |
267 | 261 |
268 return (data[0] << 8) | (data[1]); | 262 return (data[0] << 8) | (data[1]); |
269 } | 263 } |
270 | 264 |
271 inline SkPMColor premultiply_argb_as_rgba(U8CPU a, U8CPU r, U8CPU g, U8CPU b) { | 265 static inline SkPMColor premultiply_argb_as_rgba(U8CPU a, U8CPU r, U8CPU g, U8CP
U b) { |
272 if (a != 255) { | 266 if (a != 255) { |
273 r = SkMulDiv255Round(r, a); | 267 r = SkMulDiv255Round(r, a); |
274 g = SkMulDiv255Round(g, a); | 268 g = SkMulDiv255Round(g, a); |
275 b = SkMulDiv255Round(b, a); | 269 b = SkMulDiv255Round(b, a); |
276 } | 270 } |
277 | 271 |
278 return SkPackARGB_as_RGBA(a, r, g, b); | 272 return SkPackARGB_as_RGBA(a, r, g, b); |
279 } | 273 } |
280 | 274 |
281 inline SkPMColor premultiply_argb_as_bgra(U8CPU a, U8CPU r, U8CPU g, U8CPU b) { | 275 static inline SkPMColor premultiply_argb_as_bgra(U8CPU a, U8CPU r, U8CPU g, U8CP
U b) { |
282 if (a != 255) { | 276 if (a != 255) { |
283 r = SkMulDiv255Round(r, a); | 277 r = SkMulDiv255Round(r, a); |
284 g = SkMulDiv255Round(g, a); | 278 g = SkMulDiv255Round(g, a); |
285 b = SkMulDiv255Round(b, a); | 279 b = SkMulDiv255Round(b, a); |
286 } | 280 } |
287 | 281 |
288 return SkPackARGB_as_BGRA(a, r, g, b); | 282 return SkPackARGB_as_BGRA(a, r, g, b); |
289 } | 283 } |
290 | 284 |
291 inline bool is_rgba(SkColorType colorType) { | 285 static inline bool is_rgba(SkColorType colorType) { |
292 #ifdef SK_PMCOLOR_IS_RGBA | 286 #ifdef SK_PMCOLOR_IS_RGBA |
293 return (kBGRA_8888_SkColorType != colorType); | 287 return (kBGRA_8888_SkColorType != colorType); |
294 #else | 288 #else |
295 return (kRGBA_8888_SkColorType == colorType); | 289 return (kRGBA_8888_SkColorType == colorType); |
296 #endif | 290 #endif |
297 } | 291 } |
298 | 292 |
299 // Method for coverting to a 32 bit pixel. | 293 // Method for coverting to a 32 bit pixel. |
300 typedef uint32_t (*PackColorProc)(U8CPU a, U8CPU r, U8CPU g, U8CPU b); | 294 typedef uint32_t (*PackColorProc)(U8CPU a, U8CPU r, U8CPU g, U8CPU b); |
301 | 295 |
302 inline PackColorProc choose_pack_color_proc(bool isPremul, SkColorType colorType
) { | 296 static inline PackColorProc choose_pack_color_proc(bool isPremul, SkColorType co
lorType) { |
303 bool isRGBA = is_rgba(colorType); | 297 bool isRGBA = is_rgba(colorType); |
304 if (isPremul) { | 298 if (isPremul) { |
305 if (isRGBA) { | 299 if (isRGBA) { |
306 return &premultiply_argb_as_rgba; | 300 return &premultiply_argb_as_rgba; |
307 } else { | 301 } else { |
308 return &premultiply_argb_as_bgra; | 302 return &premultiply_argb_as_bgra; |
309 } | 303 } |
310 } else { | 304 } else { |
311 if (isRGBA) { | 305 if (isRGBA) { |
312 return &SkPackARGB_as_RGBA; | 306 return &SkPackARGB_as_RGBA; |
313 } else { | 307 } else { |
314 return &SkPackARGB_as_BGRA; | 308 return &SkPackARGB_as_BGRA; |
315 } | 309 } |
316 } | 310 } |
317 } | 311 } |
318 | 312 |
| 313 static inline bool needs_color_xform(const SkImageInfo& dstInfo, const SkImageIn
fo& srcInfo) { |
| 314 return (kRGBA_F16_SkColorType == dstInfo.colorType()) || |
| 315 (dstInfo.colorSpace() && !SkColorSpace::Equals(srcInfo.colorSpace(), |
| 316 dstInfo.colorSpace()))
; |
| 317 } |
| 318 |
319 #endif // SkCodecPriv_DEFINED | 319 #endif // SkCodecPriv_DEFINED |
OLD | NEW |