Chromium Code Reviews| 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) { | |
|
scroggo
2015/07/31 15:05:44
Is this used by all codecs? Or just BMP?
Nvm, it
msarett
2015/08/03 22:52:35
Acknowledged :)
| |
| 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 * | 99 * Get the destination row to start filling from |
|
scroggo
2015/07/31 15:05:44
This probably belongs in SkBmpCodec.
msarett
2015/08/03 22:52:35
Done.
| |
| 100 * Used to fill the remainder of the image on incomplete input for bmps | |
| 101 */ | |
| 102 static inline void* get_dst_start_row(void* dst, size_t dstRowBytes, int32_t y, | |
| 103 SkBmpCodec::RowOrder rowOrder) { | |
| 104 return (SkBmpCodec::kTopDown_RowOrder == rowOrder) ? | |
| 105 SkTAddOffset<void*>(dst, y * dstRowBytes) : dst; | |
| 106 } | |
| 107 | |
| 108 /* | |
| 85 * Get a byte from a buffer | 109 * Get a byte from a buffer |
| 86 * This method is unsafe, the caller is responsible for performing a check | 110 * This method is unsafe, the caller is responsible for performing a check |
| 87 * | |
| 88 */ | 111 */ |
| 89 static inline uint8_t get_byte(uint8_t* buffer, uint32_t i) { | 112 static inline uint8_t get_byte(uint8_t* buffer, uint32_t i) { |
| 90 return buffer[i]; | 113 return buffer[i]; |
| 91 } | 114 } |
| 92 | 115 |
| 93 /* | 116 /* |
| 94 * | |
| 95 * Get a short from a buffer | 117 * Get a short from a buffer |
| 96 * This method is unsafe, the caller is responsible for performing a check | 118 * This method is unsafe, the caller is responsible for performing a check |
| 97 * | |
| 98 */ | 119 */ |
| 99 static inline uint16_t get_short(uint8_t* buffer, uint32_t i) { | 120 static inline uint16_t get_short(uint8_t* buffer, uint32_t i) { |
| 100 uint16_t result; | 121 uint16_t result; |
| 101 memcpy(&result, &(buffer[i]), 2); | 122 memcpy(&result, &(buffer[i]), 2); |
| 102 #ifdef SK_CPU_BENDIAN | 123 #ifdef SK_CPU_BENDIAN |
| 103 return SkEndianSwap16(result); | 124 return SkEndianSwap16(result); |
| 104 #else | 125 #else |
| 105 return result; | 126 return result; |
| 106 #endif | 127 #endif |
| 107 } | 128 } |
| 108 | 129 |
| 109 /* | 130 /* |
| 110 * | |
| 111 * Get an int from a buffer | 131 * Get an int from a buffer |
| 112 * This method is unsafe, the caller is responsible for performing a check | 132 * This method is unsafe, the caller is responsible for performing a check |
| 113 * | |
| 114 */ | 133 */ |
| 115 static inline uint32_t get_int(uint8_t* buffer, uint32_t i) { | 134 static inline uint32_t get_int(uint8_t* buffer, uint32_t i) { |
| 116 uint32_t result; | 135 uint32_t result; |
| 117 memcpy(&result, &(buffer[i]), 4); | 136 memcpy(&result, &(buffer[i]), 4); |
| 118 #ifdef SK_CPU_BENDIAN | 137 #ifdef SK_CPU_BENDIAN |
| 119 return SkEndianSwap32(result); | 138 return SkEndianSwap32(result); |
| 120 #else | 139 #else |
| 121 return result; | 140 return result; |
| 122 #endif | 141 #endif |
| 123 } | 142 } |
| 124 | 143 |
| 125 #ifdef SK_PRINT_CODEC_MESSAGES | 144 #ifdef SK_PRINT_CODEC_MESSAGES |
| 126 #define SkCodecPrintf SkDebugf | 145 #define SkCodecPrintf SkDebugf |
| 127 #else | 146 #else |
| 128 #define SkCodecPrintf(...) | 147 #define SkCodecPrintf(...) |
| 129 #endif | 148 #endif |
| 130 | 149 |
| 131 #endif // SkCodecPriv_DEFINED | 150 #endif // SkCodecPriv_DEFINED |
| OLD | NEW |