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