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