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