| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 The Android Open Source Project | 2 * Copyright 2015 The Android Open Source Project |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkCodecPriv_DEFINED | 8 #ifndef SkCodecPriv_DEFINED |
| 9 #define SkCodecPriv_DEFINED | 9 #define SkCodecPriv_DEFINED |
| 10 | 10 |
| 11 #include "SkBmpCodec.h" |
| 11 #include "SkColorTable.h" | 12 #include "SkColorTable.h" |
| 12 #include "SkImageInfo.h" | 13 #include "SkImageInfo.h" |
| 13 #include "SkSwizzler.h" | 14 #include "SkSwizzler.h" |
| 14 #include "SkTypes.h" | 15 #include "SkTypes.h" |
| 15 #include "SkUtils.h" | 16 #include "SkUtils.h" |
| 16 | 17 |
| 17 /* | 18 /* |
| 18 * | 19 * |
| 19 * Helper routine for alpha result codes | 20 * Helper routine for alpha result codes |
| 20 * | 21 * |
| 21 */ | 22 */ |
| 22 #define INIT_RESULT_ALPHA \ | 23 #define INIT_RESULT_ALPHA \ |
| 23 uint8_t zeroAlpha = 0; \ | 24 uint8_t zeroAlpha = 0; \ |
| 24 uint8_t maxAlpha = 0xFF; | 25 uint8_t maxAlpha = 0xFF; |
| 25 | 26 |
| 26 #define UPDATE_RESULT_ALPHA(alpha) \ | 27 #define UPDATE_RESULT_ALPHA(alpha) \ |
| 27 zeroAlpha |= (alpha); \ | 28 zeroAlpha |= (alpha); \ |
| 28 maxAlpha &= (alpha); | 29 maxAlpha &= (alpha); |
| 29 | 30 |
| 30 #define COMPUTE_RESULT_ALPHA \ | 31 #define COMPUTE_RESULT_ALPHA \ |
| 31 SkSwizzler::GetResult(zeroAlpha, maxAlpha); | 32 SkSwizzler::GetResult(zeroAlpha, maxAlpha); |
| 32 | 33 |
| 34 static inline bool valid_alpha(SkAlphaType dstAlpha, SkAlphaType srcAlpha) { |
| 35 // Check for supported alpha types |
| 36 if (srcAlpha != dstAlpha) { |
| 37 if (kOpaque_SkAlphaType == srcAlpha) { |
| 38 // If the source is opaque, we must decode to opaque |
| 39 return false; |
| 40 } |
| 41 |
| 42 // The source is not opaque |
| 43 switch (dstAlpha) { |
| 44 case kPremul_SkAlphaType: |
| 45 case kUnpremul_SkAlphaType: |
| 46 // The source is not opaque, so either of these is okay |
| 47 break; |
| 48 default: |
| 49 // We cannot decode a non-opaque image to opaque (or unknown) |
| 50 return false; |
| 51 } |
| 52 } |
| 53 return true; |
| 54 } |
| 55 |
| 33 /* | 56 /* |
| 34 * | |
| 35 * Copy the codec color table back to the client when kIndex8 color type is requ
ested | 57 * Copy the codec color table back to the client when kIndex8 color type is requ
ested |
| 36 * | |
| 37 */ | 58 */ |
| 38 static inline void copy_color_table(const SkImageInfo& dstInfo, SkColorTable* co
lorTable, | 59 static inline void copy_color_table(const SkImageInfo& dstInfo, SkColorTable* co
lorTable, |
| 39 SkPMColor* inputColorPtr, int* inputColorCount) { | 60 SkPMColor* inputColorPtr, int* inputColorCount) { |
| 40 if (kIndex_8_SkColorType == dstInfo.colorType()) { | 61 if (kIndex_8_SkColorType == dstInfo.colorType()) { |
| 41 SkASSERT(NULL != inputColorPtr); | 62 SkASSERT(NULL != inputColorPtr); |
| 42 SkASSERT(NULL != inputColorCount); | 63 SkASSERT(NULL != inputColorCount); |
| 43 SkASSERT(NULL != colorTable); | 64 SkASSERT(NULL != colorTable); |
| 44 memcpy(inputColorPtr, colorTable->readColors(), *inputColorCount * 4); | 65 memcpy(inputColorPtr, colorTable->readColors(), *inputColorCount * 4); |
| 45 } | 66 } |
| 46 } | 67 } |
| 47 | 68 |
| 48 /* | 69 /* |
| 49 * | |
| 50 * Compute row bytes for an image using pixels per byte | 70 * Compute row bytes for an image using pixels per byte |
| 51 * | |
| 52 */ | 71 */ |
| 53 static inline size_t compute_row_bytes_ppb(int width, uint32_t pixelsPerByte) { | 72 static inline size_t compute_row_bytes_ppb(int width, uint32_t pixelsPerByte) { |
| 54 return (width + pixelsPerByte - 1) / pixelsPerByte; | 73 return (width + pixelsPerByte - 1) / pixelsPerByte; |
| 55 } | 74 } |
| 56 | 75 |
| 57 /* | 76 /* |
| 58 * | |
| 59 * Compute row bytes for an image using bytes per pixel | 77 * Compute row bytes for an image using bytes per pixel |
| 60 * | |
| 61 */ | 78 */ |
| 62 static inline size_t compute_row_bytes_bpp(int width, uint32_t bytesPerPixel) { | 79 static inline size_t compute_row_bytes_bpp(int width, uint32_t bytesPerPixel) { |
| 63 return width * bytesPerPixel; | 80 return width * bytesPerPixel; |
| 64 } | 81 } |
| 65 | 82 |
| 66 /* | 83 /* |
| 67 * | |
| 68 * Compute row bytes for an image | 84 * Compute row bytes for an image |
| 69 * | |
| 70 */ | 85 */ |
| 71 static inline size_t compute_row_bytes(int width, uint32_t bitsPerPixel) { | 86 static inline size_t compute_row_bytes(int width, uint32_t bitsPerPixel) { |
| 72 if (bitsPerPixel < 16) { | 87 if (bitsPerPixel < 16) { |
| 73 SkASSERT(0 == 8 % bitsPerPixel); | 88 SkASSERT(0 == 8 % bitsPerPixel); |
| 74 const uint32_t pixelsPerByte = 8 / bitsPerPixel; | 89 const uint32_t pixelsPerByte = 8 / bitsPerPixel; |
| 75 return compute_row_bytes_ppb(width, pixelsPerByte); | 90 return compute_row_bytes_ppb(width, pixelsPerByte); |
| 76 } else { | 91 } else { |
| 77 SkASSERT(0 == bitsPerPixel % 8); | 92 SkASSERT(0 == bitsPerPixel % 8); |
| 78 const uint32_t bytesPerPixel = bitsPerPixel / 8; | 93 const uint32_t bytesPerPixel = bitsPerPixel / 8; |
| 79 return compute_row_bytes_bpp(width, bytesPerPixel); | 94 return compute_row_bytes_bpp(width, bytesPerPixel); |
| 80 } | 95 } |
| 81 } | 96 } |
| 82 | 97 |
| 83 /* | 98 /* |
| 84 * | |
| 85 * Get a byte from a buffer | 99 * Get a byte from a buffer |
| 86 * This method is unsafe, the caller is responsible for performing a check | 100 * This method is unsafe, the caller is responsible for performing a check |
| 87 * | |
| 88 */ | 101 */ |
| 89 static inline uint8_t get_byte(uint8_t* buffer, uint32_t i) { | 102 static inline uint8_t get_byte(uint8_t* buffer, uint32_t i) { |
| 90 return buffer[i]; | 103 return buffer[i]; |
| 91 } | 104 } |
| 92 | 105 |
| 93 /* | 106 /* |
| 94 * | |
| 95 * Get a short from a buffer | 107 * Get a short from a buffer |
| 96 * This method is unsafe, the caller is responsible for performing a check | 108 * This method is unsafe, the caller is responsible for performing a check |
| 97 * | |
| 98 */ | 109 */ |
| 99 static inline uint16_t get_short(uint8_t* buffer, uint32_t i) { | 110 static inline uint16_t get_short(uint8_t* buffer, uint32_t i) { |
| 100 uint16_t result; | 111 uint16_t result; |
| 101 memcpy(&result, &(buffer[i]), 2); | 112 memcpy(&result, &(buffer[i]), 2); |
| 102 #ifdef SK_CPU_BENDIAN | 113 #ifdef SK_CPU_BENDIAN |
| 103 return SkEndianSwap16(result); | 114 return SkEndianSwap16(result); |
| 104 #else | 115 #else |
| 105 return result; | 116 return result; |
| 106 #endif | 117 #endif |
| 107 } | 118 } |
| 108 | 119 |
| 109 /* | 120 /* |
| 110 * | |
| 111 * Get an int from a buffer | 121 * Get an int from a buffer |
| 112 * This method is unsafe, the caller is responsible for performing a check | 122 * This method is unsafe, the caller is responsible for performing a check |
| 113 * | |
| 114 */ | 123 */ |
| 115 static inline uint32_t get_int(uint8_t* buffer, uint32_t i) { | 124 static inline uint32_t get_int(uint8_t* buffer, uint32_t i) { |
| 116 uint32_t result; | 125 uint32_t result; |
| 117 memcpy(&result, &(buffer[i]), 4); | 126 memcpy(&result, &(buffer[i]), 4); |
| 118 #ifdef SK_CPU_BENDIAN | 127 #ifdef SK_CPU_BENDIAN |
| 119 return SkEndianSwap32(result); | 128 return SkEndianSwap32(result); |
| 120 #else | 129 #else |
| 121 return result; | 130 return result; |
| 122 #endif | 131 #endif |
| 123 } | 132 } |
| 124 | 133 |
| 125 #ifdef SK_PRINT_CODEC_MESSAGES | 134 #ifdef SK_PRINT_CODEC_MESSAGES |
| 126 #define SkCodecPrintf SkDebugf | 135 #define SkCodecPrintf SkDebugf |
| 127 #else | 136 #else |
| 128 #define SkCodecPrintf(...) | 137 #define SkCodecPrintf(...) |
| 129 #endif | 138 #endif |
| 130 | 139 |
| 131 #endif // SkCodecPriv_DEFINED | 140 #endif // SkCodecPriv_DEFINED |
| OLD | NEW |