blob: 1602130aeed58e4ec807c2173517170bf39ded8f [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
Leon Scroggins III179559f2018-12-10 12:30:12 -050024// Defined in SkCodec.cpp
25bool sk_select_xform_format(SkColorType colorType, bool forColorTable,
26 skcms_PixelFormat* outFormat);
27
msarett3d9d7a72015-10-21 10:27:10 -070028// FIXME: Consider sharing with dm, nanbench, and tools.
msarettd1ec89b2016-08-03 12:59:27 -070029static inline float get_scale_from_sample_size(int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -070030 return 1.0f / ((float) sampleSize);
31}
32
msarettd1ec89b2016-08-03 12:59:27 -070033static inline bool is_valid_subset(const SkIRect& subset, const SkISize& imageDims) {
msarett3d9d7a72015-10-21 10:27:10 -070034 return SkIRect::MakeSize(imageDims).contains(subset);
35}
36
msarett5406d6f2015-08-31 06:55:13 -070037/*
msarett10522ff2015-09-07 08:54:01 -070038 * returns a scaled dimension based on the original dimension and the sampleSize
39 * NOTE: we round down here for scaled dimension to match the behavior of SkImageDecoder
msarett3d9d7a72015-10-21 10:27:10 -070040 * FIXME: I think we should call this get_sampled_dimension().
msarett10522ff2015-09-07 08:54:01 -070041 */
msarettd1ec89b2016-08-03 12:59:27 -070042static inline int get_scaled_dimension(int srcDimension, int sampleSize) {
msarett10522ff2015-09-07 08:54:01 -070043 if (sampleSize > srcDimension) {
44 return 1;
45 }
46 return srcDimension / sampleSize;
47}
48
49/*
msarett5406d6f2015-08-31 06:55:13 -070050 * Returns the first coordinate that we will keep during a scaled decode.
51 * The output can be interpreted as an x-coordinate or a y-coordinate.
52 *
53 * This does not need to be called and is not called when sampleFactor == 1.
54 */
msarettd1ec89b2016-08-03 12:59:27 -070055static inline int get_start_coord(int sampleFactor) { return sampleFactor / 2; };
msarett5406d6f2015-08-31 06:55:13 -070056
57/*
58 * Given a coordinate in the original image, this returns the corresponding
59 * coordinate in the scaled image. This function is meaningless if
60 * IsCoordNecessary returns false.
61 * The output can be interpreted as an x-coordinate or a y-coordinate.
62 *
63 * This does not need to be called and is not called when sampleFactor == 1.
64 */
msarettd1ec89b2016-08-03 12:59:27 -070065static inline int get_dst_coord(int srcCoord, int sampleFactor) { return srcCoord / sampleFactor; };
msarett5406d6f2015-08-31 06:55:13 -070066
67/*
68 * When scaling, we will discard certain y-coordinates (rows) and
69 * x-coordinates (columns). This function returns true if we should keep the
70 * coordinate and false otherwise.
71 * The inputs may be x-coordinates or y-coordinates.
72 *
73 * This does not need to be called and is not called when sampleFactor == 1.
74 */
msarettd1ec89b2016-08-03 12:59:27 -070075static inline bool is_coord_necessary(int srcCoord, int sampleFactor, int scaledDim) {
msarett5406d6f2015-08-31 06:55:13 -070076 // Get the first coordinate that we want to keep
77 int startCoord = get_start_coord(sampleFactor);
78
79 // Return false on edge cases
80 if (srcCoord < startCoord || get_dst_coord(srcCoord, sampleFactor) >= scaledDim) {
81 return false;
82 }
83
84 // Every sampleFactor rows are necessary
85 return ((srcCoord - startCoord) % sampleFactor) == 0;
86}
87
Leon Scroggins III07418182017-08-15 12:24:02 -040088static inline bool valid_alpha(SkAlphaType dstAlpha, bool srcIsOpaque) {
scroggoc5560be2016-02-03 09:42:42 -080089 if (kUnknown_SkAlphaType == dstAlpha) {
90 return false;
91 }
92
Leon Scroggins III07418182017-08-15 12:24:02 -040093 if (srcIsOpaque) {
94 if (kOpaque_SkAlphaType != dstAlpha) {
scroggoc5560be2016-02-03 09:42:42 -080095 SkCodecPrintf("Warning: an opaque image should be decoded as opaque "
96 "- it is being decoded as non-opaque, which will draw slower\n");
msarett4ab9d5f2015-08-06 15:34:42 -070097 }
Leon Scroggins III07418182017-08-15 12:24:02 -040098 return true;
msarett4ab9d5f2015-08-06 15:34:42 -070099 }
Leon Scroggins III07418182017-08-15 12:24:02 -0400100
101 return dstAlpha != kOpaque_SkAlphaType;
msarett4ab9d5f2015-08-06 15:34:42 -0700102}
103
msarett74114382015-03-16 11:55:18 -0700104/*
halcanary96fcdcc2015-08-27 07:41:13 -0700105 * If there is a color table, get a pointer to the colors, otherwise return nullptr
msarett99f567e2015-08-05 12:58:26 -0700106 */
msarettd1ec89b2016-08-03 12:59:27 -0700107static inline const SkPMColor* get_color_ptr(SkColorTable* colorTable) {
halcanary96fcdcc2015-08-27 07:41:13 -0700108 return nullptr != colorTable ? colorTable->readColors() : nullptr;
msarett99f567e2015-08-05 12:58:26 -0700109}
110
111/*
msarett74114382015-03-16 11:55:18 -0700112 * Compute row bytes for an image using pixels per byte
msarett74114382015-03-16 11:55:18 -0700113 */
msarettd1ec89b2016-08-03 12:59:27 -0700114static inline size_t compute_row_bytes_ppb(int width, uint32_t pixelsPerByte) {
msarett74114382015-03-16 11:55:18 -0700115 return (width + pixelsPerByte - 1) / pixelsPerByte;
116}
117
118/*
msarett74114382015-03-16 11:55:18 -0700119 * Compute row bytes for an image using bytes per pixel
msarett74114382015-03-16 11:55:18 -0700120 */
msarettd1ec89b2016-08-03 12:59:27 -0700121static inline size_t compute_row_bytes_bpp(int width, uint32_t bytesPerPixel) {
msarett74114382015-03-16 11:55:18 -0700122 return width * bytesPerPixel;
123}
124
125/*
msarett74114382015-03-16 11:55:18 -0700126 * Compute row bytes for an image
msarett74114382015-03-16 11:55:18 -0700127 */
msarettd1ec89b2016-08-03 12:59:27 -0700128static inline size_t compute_row_bytes(int width, uint32_t bitsPerPixel) {
msarett74114382015-03-16 11:55:18 -0700129 if (bitsPerPixel < 16) {
130 SkASSERT(0 == 8 % bitsPerPixel);
131 const uint32_t pixelsPerByte = 8 / bitsPerPixel;
132 return compute_row_bytes_ppb(width, pixelsPerByte);
133 } else {
134 SkASSERT(0 == bitsPerPixel % 8);
135 const uint32_t bytesPerPixel = bitsPerPixel / 8;
136 return compute_row_bytes_bpp(width, bytesPerPixel);
137 }
138}
139
140/*
msarett74114382015-03-16 11:55:18 -0700141 * Get a byte from a buffer
142 * This method is unsafe, the caller is responsible for performing a check
msarett74114382015-03-16 11:55:18 -0700143 */
msarettd1ec89b2016-08-03 12:59:27 -0700144static inline uint8_t get_byte(uint8_t* buffer, uint32_t i) {
msarett74114382015-03-16 11:55:18 -0700145 return buffer[i];
146}
147
148/*
msarett74114382015-03-16 11:55:18 -0700149 * Get a short from a buffer
150 * This method is unsafe, the caller is responsible for performing a check
msarett74114382015-03-16 11:55:18 -0700151 */
msarettd1ec89b2016-08-03 12:59:27 -0700152static inline uint16_t get_short(uint8_t* buffer, uint32_t i) {
msarett74114382015-03-16 11:55:18 -0700153 uint16_t result;
154 memcpy(&result, &(buffer[i]), 2);
155#ifdef SK_CPU_BENDIAN
156 return SkEndianSwap16(result);
157#else
158 return result;
159#endif
160}
161
162/*
msarett74114382015-03-16 11:55:18 -0700163 * Get an int from a buffer
164 * This method is unsafe, the caller is responsible for performing a check
msarett74114382015-03-16 11:55:18 -0700165 */
msarettd1ec89b2016-08-03 12:59:27 -0700166static inline uint32_t get_int(uint8_t* buffer, uint32_t i) {
msarett74114382015-03-16 11:55:18 -0700167 uint32_t result;
168 memcpy(&result, &(buffer[i]), 4);
169#ifdef SK_CPU_BENDIAN
170 return SkEndianSwap32(result);
171#else
172 return result;
173#endif
174}
175
msarett0e6274f2016-03-21 08:04:40 -0700176/*
177 * @param data Buffer to read bytes from
178 * @param isLittleEndian Output parameter
179 * Indicates if the data is little endian
180 * Is unaffected on false returns
181 */
msarettd1ec89b2016-08-03 12:59:27 -0700182static inline bool is_valid_endian_marker(const uint8_t* data, bool* isLittleEndian) {
msarett0e6274f2016-03-21 08:04:40 -0700183 // II indicates Intel (little endian) and MM indicates motorola (big endian).
184 if (('I' != data[0] || 'I' != data[1]) && ('M' != data[0] || 'M' != data[1])) {
185 return false;
186 }
187
188 *isLittleEndian = ('I' == data[0]);
189 return true;
190}
191
msarettd1ec89b2016-08-03 12:59:27 -0700192static inline uint16_t get_endian_short(const uint8_t* data, bool littleEndian) {
msarett0e6274f2016-03-21 08:04:40 -0700193 if (littleEndian) {
194 return (data[1] << 8) | (data[0]);
195 }
196
197 return (data[0] << 8) | (data[1]);
198}
199
msarettd1ec89b2016-08-03 12:59:27 -0700200static inline SkPMColor premultiply_argb_as_rgba(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
msarett34e0ec42016-04-22 16:27:24 -0700201 if (a != 255) {
202 r = SkMulDiv255Round(r, a);
203 g = SkMulDiv255Round(g, a);
204 b = SkMulDiv255Round(b, a);
205 }
206
207 return SkPackARGB_as_RGBA(a, r, g, b);
208}
209
msarettd1ec89b2016-08-03 12:59:27 -0700210static inline SkPMColor premultiply_argb_as_bgra(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
msarett34e0ec42016-04-22 16:27:24 -0700211 if (a != 255) {
212 r = SkMulDiv255Round(r, a);
213 g = SkMulDiv255Round(g, a);
214 b = SkMulDiv255Round(b, a);
215 }
216
217 return SkPackARGB_as_BGRA(a, r, g, b);
218}
219
msarettd1ec89b2016-08-03 12:59:27 -0700220static inline bool is_rgba(SkColorType colorType) {
msarett34e0ec42016-04-22 16:27:24 -0700221#ifdef SK_PMCOLOR_IS_RGBA
222 return (kBGRA_8888_SkColorType != colorType);
223#else
224 return (kRGBA_8888_SkColorType == colorType);
225#endif
226}
227
228// Method for coverting to a 32 bit pixel.
229typedef uint32_t (*PackColorProc)(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
230
msarettd1ec89b2016-08-03 12:59:27 -0700231static inline PackColorProc choose_pack_color_proc(bool isPremul, SkColorType colorType) {
msarett34e0ec42016-04-22 16:27:24 -0700232 bool isRGBA = is_rgba(colorType);
233 if (isPremul) {
234 if (isRGBA) {
235 return &premultiply_argb_as_rgba;
236 } else {
237 return &premultiply_argb_as_bgra;
238 }
239 } else {
240 if (isRGBA) {
241 return &SkPackARGB_as_RGBA;
242 } else {
243 return &SkPackARGB_as_BGRA;
244 }
245 }
246}
247
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500248bool is_orientation_marker(const uint8_t* data, size_t data_length, SkEncodedOrigin* orientation);
249
msarett74114382015-03-16 11:55:18 -0700250#endif // SkCodecPriv_DEFINED