• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include "SkColorPriv.h"
12 #include "SkColorSpaceXform.h"
13 #include "SkColorSpaceXformPriv.h"
14 #include "SkColorTable.h"
15 #include "SkEncodedInfo.h"
16 #include "SkImageInfo.h"
17 #include "SkTypes.h"
18 
19 #ifdef SK_PRINT_CODEC_MESSAGES
20     #define SkCodecPrintf SkDebugf
21 #else
22     #define SkCodecPrintf(...)
23 #endif
24 
25 // FIXME: Consider sharing with dm, nanbench, and tools.
get_scale_from_sample_size(int sampleSize)26 static inline float get_scale_from_sample_size(int sampleSize) {
27     return 1.0f / ((float) sampleSize);
28 }
29 
is_valid_subset(const SkIRect & subset,const SkISize & imageDims)30 static inline bool is_valid_subset(const SkIRect& subset, const SkISize& imageDims) {
31     return SkIRect::MakeSize(imageDims).contains(subset);
32 }
33 
34 /*
35  * returns a scaled dimension based on the original dimension and the sampleSize
36  * NOTE: we round down here for scaled dimension to match the behavior of SkImageDecoder
37  * FIXME: I think we should call this get_sampled_dimension().
38  */
get_scaled_dimension(int srcDimension,int sampleSize)39 static inline int get_scaled_dimension(int srcDimension, int sampleSize) {
40     if (sampleSize > srcDimension) {
41         return 1;
42     }
43     return srcDimension / sampleSize;
44 }
45 
46 /*
47  * Returns the first coordinate that we will keep during a scaled decode.
48  * The output can be interpreted as an x-coordinate or a y-coordinate.
49  *
50  * This does not need to be called and is not called when sampleFactor == 1.
51  */
get_start_coord(int sampleFactor)52 static inline int get_start_coord(int sampleFactor) { return sampleFactor / 2; };
53 
54 /*
55  * Given a coordinate in the original image, this returns the corresponding
56  * coordinate in the scaled image.  This function is meaningless if
57  * IsCoordNecessary returns false.
58  * The output can be interpreted as an x-coordinate or a y-coordinate.
59  *
60  * This does not need to be called and is not called when sampleFactor == 1.
61  */
get_dst_coord(int srcCoord,int sampleFactor)62 static inline int get_dst_coord(int srcCoord, int sampleFactor) { return srcCoord / sampleFactor; };
63 
64 /*
65  * When scaling, we will discard certain y-coordinates (rows) and
66  * x-coordinates (columns).  This function returns true if we should keep the
67  * coordinate and false otherwise.
68  * The inputs may be x-coordinates or y-coordinates.
69  *
70  * This does not need to be called and is not called when sampleFactor == 1.
71  */
is_coord_necessary(int srcCoord,int sampleFactor,int scaledDim)72 static inline bool is_coord_necessary(int srcCoord, int sampleFactor, int scaledDim) {
73     // Get the first coordinate that we want to keep
74     int startCoord = get_start_coord(sampleFactor);
75 
76     // Return false on edge cases
77     if (srcCoord < startCoord || get_dst_coord(srcCoord, sampleFactor) >= scaledDim) {
78         return false;
79     }
80 
81     // Every sampleFactor rows are necessary
82     return ((srcCoord - startCoord) % sampleFactor) == 0;
83 }
84 
valid_alpha(SkAlphaType dstAlpha,SkAlphaType srcAlpha)85 static inline bool valid_alpha(SkAlphaType dstAlpha, SkAlphaType srcAlpha) {
86     if (kUnknown_SkAlphaType == dstAlpha) {
87         return false;
88     }
89 
90     if (srcAlpha != dstAlpha) {
91         if (kOpaque_SkAlphaType == srcAlpha) {
92             // If the source is opaque, we can support any.
93             SkCodecPrintf("Warning: an opaque image should be decoded as opaque "
94                           "- it is being decoded as non-opaque, which will draw slower\n");
95             return true;
96         }
97 
98         // The source is not opaque
99         switch (dstAlpha) {
100             case kPremul_SkAlphaType:
101             case kUnpremul_SkAlphaType:
102                 // The source is not opaque, so either of these is okay
103                 break;
104             default:
105                 // We cannot decode a non-opaque image to opaque (or unknown)
106                 return false;
107         }
108     }
109     return true;
110 }
111 
112 /*
113  * If there is a color table, get a pointer to the colors, otherwise return nullptr
114  */
get_color_ptr(SkColorTable * colorTable)115 static inline const SkPMColor* get_color_ptr(SkColorTable* colorTable) {
116      return nullptr != colorTable ? colorTable->readColors() : nullptr;
117 }
118 
119 /*
120  * Given that the encoded image uses a color table, return the fill value
121  */
get_color_table_fill_value(SkColorType dstColorType,SkAlphaType alphaType,const SkPMColor * colorPtr,uint8_t fillIndex,SkColorSpaceXform * colorXform,bool isRGBA)122 static inline uint64_t get_color_table_fill_value(SkColorType dstColorType, SkAlphaType alphaType,
123         const SkPMColor* colorPtr, uint8_t fillIndex, SkColorSpaceXform* colorXform, bool isRGBA) {
124     SkASSERT(nullptr != colorPtr);
125     switch (dstColorType) {
126         case kRGBA_8888_SkColorType:
127         case kBGRA_8888_SkColorType:
128             return colorPtr[fillIndex];
129         case kRGB_565_SkColorType:
130             return SkPixel32ToPixel16(colorPtr[fillIndex]);
131         case kRGBA_F16_SkColorType: {
132             SkASSERT(colorXform);
133             uint64_t dstColor;
134             uint32_t srcColor = colorPtr[fillIndex];
135             SkColorSpaceXform::ColorFormat srcFormat =
136                     isRGBA ? SkColorSpaceXform::kRGBA_8888_ColorFormat
137                            : SkColorSpaceXform::kBGRA_8888_ColorFormat;
138             SkAssertResult(colorXform->apply(select_xform_format(dstColorType), &dstColor,
139                                              srcFormat, &srcColor, 1, alphaType));
140             return dstColor;
141         }
142         default:
143             SkASSERT(false);
144             return 0;
145     }
146 }
147 
148 /*
149  * Compute row bytes for an image using pixels per byte
150  */
compute_row_bytes_ppb(int width,uint32_t pixelsPerByte)151 static inline size_t compute_row_bytes_ppb(int width, uint32_t pixelsPerByte) {
152     return (width + pixelsPerByte - 1) / pixelsPerByte;
153 }
154 
155 /*
156  * Compute row bytes for an image using bytes per pixel
157  */
compute_row_bytes_bpp(int width,uint32_t bytesPerPixel)158 static inline size_t compute_row_bytes_bpp(int width, uint32_t bytesPerPixel) {
159     return width * bytesPerPixel;
160 }
161 
162 /*
163  * Compute row bytes for an image
164  */
compute_row_bytes(int width,uint32_t bitsPerPixel)165 static inline size_t compute_row_bytes(int width, uint32_t bitsPerPixel) {
166     if (bitsPerPixel < 16) {
167         SkASSERT(0 == 8 % bitsPerPixel);
168         const uint32_t pixelsPerByte = 8 / bitsPerPixel;
169         return compute_row_bytes_ppb(width, pixelsPerByte);
170     } else {
171         SkASSERT(0 == bitsPerPixel % 8);
172         const uint32_t bytesPerPixel = bitsPerPixel / 8;
173         return compute_row_bytes_bpp(width, bytesPerPixel);
174     }
175 }
176 
177 /*
178  * Get a byte from a buffer
179  * This method is unsafe, the caller is responsible for performing a check
180  */
get_byte(uint8_t * buffer,uint32_t i)181 static inline uint8_t get_byte(uint8_t* buffer, uint32_t i) {
182     return buffer[i];
183 }
184 
185 /*
186  * Get a short from a buffer
187  * This method is unsafe, the caller is responsible for performing a check
188  */
get_short(uint8_t * buffer,uint32_t i)189 static inline uint16_t get_short(uint8_t* buffer, uint32_t i) {
190     uint16_t result;
191     memcpy(&result, &(buffer[i]), 2);
192 #ifdef SK_CPU_BENDIAN
193     return SkEndianSwap16(result);
194 #else
195     return result;
196 #endif
197 }
198 
199 /*
200  * Get an int from a buffer
201  * This method is unsafe, the caller is responsible for performing a check
202  */
get_int(uint8_t * buffer,uint32_t i)203 static inline uint32_t get_int(uint8_t* buffer, uint32_t i) {
204     uint32_t result;
205     memcpy(&result, &(buffer[i]), 4);
206 #ifdef SK_CPU_BENDIAN
207     return SkEndianSwap32(result);
208 #else
209     return result;
210 #endif
211 }
212 
213 /*
214  * @param data           Buffer to read bytes from
215  * @param isLittleEndian Output parameter
216  *                       Indicates if the data is little endian
217  *                       Is unaffected on false returns
218  */
is_valid_endian_marker(const uint8_t * data,bool * isLittleEndian)219 static inline bool is_valid_endian_marker(const uint8_t* data, bool* isLittleEndian) {
220     // II indicates Intel (little endian) and MM indicates motorola (big endian).
221     if (('I' != data[0] || 'I' != data[1]) && ('M' != data[0] || 'M' != data[1])) {
222         return false;
223     }
224 
225     *isLittleEndian = ('I' == data[0]);
226     return true;
227 }
228 
get_endian_short(const uint8_t * data,bool littleEndian)229 static inline uint16_t get_endian_short(const uint8_t* data, bool littleEndian) {
230     if (littleEndian) {
231         return (data[1] << 8) | (data[0]);
232     }
233 
234     return (data[0] << 8) | (data[1]);
235 }
236 
premultiply_argb_as_rgba(U8CPU a,U8CPU r,U8CPU g,U8CPU b)237 static inline SkPMColor premultiply_argb_as_rgba(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
238     if (a != 255) {
239         r = SkMulDiv255Round(r, a);
240         g = SkMulDiv255Round(g, a);
241         b = SkMulDiv255Round(b, a);
242     }
243 
244     return SkPackARGB_as_RGBA(a, r, g, b);
245 }
246 
premultiply_argb_as_bgra(U8CPU a,U8CPU r,U8CPU g,U8CPU b)247 static inline SkPMColor premultiply_argb_as_bgra(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
248     if (a != 255) {
249         r = SkMulDiv255Round(r, a);
250         g = SkMulDiv255Round(g, a);
251         b = SkMulDiv255Round(b, a);
252     }
253 
254     return SkPackARGB_as_BGRA(a, r, g, b);
255 }
256 
is_rgba(SkColorType colorType)257 static inline bool is_rgba(SkColorType colorType) {
258 #ifdef SK_PMCOLOR_IS_RGBA
259     return (kBGRA_8888_SkColorType != colorType);
260 #else
261     return (kRGBA_8888_SkColorType == colorType);
262 #endif
263 }
264 
265 // Method for coverting to a 32 bit pixel.
266 typedef uint32_t (*PackColorProc)(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
267 
choose_pack_color_proc(bool isPremul,SkColorType colorType)268 static inline PackColorProc choose_pack_color_proc(bool isPremul, SkColorType colorType) {
269     bool isRGBA = is_rgba(colorType);
270     if (isPremul) {
271         if (isRGBA) {
272             return &premultiply_argb_as_rgba;
273         } else {
274             return &premultiply_argb_as_bgra;
275         }
276     } else {
277         if (isRGBA) {
278             return &SkPackARGB_as_RGBA;
279         } else {
280             return &SkPackARGB_as_BGRA;
281         }
282     }
283 }
284 
needs_premul(const SkImageInfo & dstInfo,const SkEncodedInfo & encodedInfo)285 static inline bool needs_premul(const SkImageInfo& dstInfo, const SkEncodedInfo& encodedInfo) {
286     return kPremul_SkAlphaType == dstInfo.alphaType() &&
287            SkEncodedInfo::kUnpremul_Alpha == encodedInfo.alpha();
288 }
289 
needs_color_xform(const SkImageInfo & dstInfo,const SkImageInfo & srcInfo,bool needsColorCorrectPremul)290 static inline bool needs_color_xform(const SkImageInfo& dstInfo, const SkImageInfo& srcInfo,
291                                      bool needsColorCorrectPremul) {
292     // We never perform a color xform in legacy mode.
293     if (!dstInfo.colorSpace()) {
294         return false;
295     }
296 
297     // F16 is by definition a linear space, so we always must perform a color xform.
298     bool isF16 = kRGBA_F16_SkColorType == dstInfo.colorType();
299 
300     // Need a color xform when dst space does not match the src.
301     bool srcDstNotEqual = !SkColorSpace::Equals(srcInfo.colorSpace(), dstInfo.colorSpace());
302 
303     return needsColorCorrectPremul || isF16 || srcDstNotEqual;
304 }
305 
select_xform_alpha(SkAlphaType dstAlphaType,SkAlphaType srcAlphaType)306 static inline SkAlphaType select_xform_alpha(SkAlphaType dstAlphaType, SkAlphaType srcAlphaType) {
307     return (kOpaque_SkAlphaType == srcAlphaType) ? kOpaque_SkAlphaType : dstAlphaType;
308 }
309 
310 /*
311  * Alpha Type Conversions
312  * - kOpaque to kOpaque, kUnpremul, kPremul is valid
313  * - kUnpremul to kUnpremul, kPremul is valid
314  *
315  * Color Type Conversions
316  * - Always support kRGBA_8888, kBGRA_8888
317  * - Support kRGBA_F16 when there is a linear dst color space
318  * - Support k565 if kOpaque and color correction is not required
319  * - Support k565 if it matches the src, kOpaque, and color correction is not required
320  */
conversion_possible(const SkImageInfo & dst,const SkImageInfo & src)321 static inline bool conversion_possible(const SkImageInfo& dst, const SkImageInfo& src) {
322     // Ensure the alpha type is valid.
323     if (!valid_alpha(dst.alphaType(), src.alphaType())) {
324         return false;
325     }
326 
327     // Check for supported color types.
328     switch (dst.colorType()) {
329         case kRGBA_8888_SkColorType:
330         case kBGRA_8888_SkColorType:
331             return true;
332         case kRGBA_F16_SkColorType:
333             return dst.colorSpace() && dst.colorSpace()->gammaIsLinear();
334         case kRGB_565_SkColorType:
335             return kOpaque_SkAlphaType == src.alphaType();
336         case kGray_8_SkColorType:
337             return kGray_8_SkColorType == src.colorType() &&
338                    kOpaque_SkAlphaType == src.alphaType() && !needs_color_xform(dst, src, false);
339         default:
340             return false;
341     }
342 }
343 
344 #endif // SkCodecPriv_DEFINED
345