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 | |
| 11 | #include "SkImageInfo.h" |
| 12 | #include "SkSwizzler.h" |
| 13 | #include "SkTypes.h" |
| 14 | |
| 15 | /* |
| 16 | * |
| 17 | * Helper routine for alpha result codes |
| 18 | * |
| 19 | */ |
| 20 | #define INIT_RESULT_ALPHA \ |
| 21 | uint8_t zeroAlpha = 0; \ |
| 22 | uint8_t maxAlpha = 0xFF; |
| 23 | |
| 24 | #define UPDATE_RESULT_ALPHA(alpha) \ |
| 25 | zeroAlpha |= (alpha); \ |
| 26 | maxAlpha &= (alpha); |
| 27 | |
| 28 | #define COMPUTE_RESULT_ALPHA \ |
| 29 | SkSwizzler::GetResult(zeroAlpha, maxAlpha); |
| 30 | |
| 31 | /* |
| 32 | * |
| 33 | * Compute row bytes for an image using pixels per byte |
| 34 | * |
| 35 | */ |
| 36 | static inline size_t compute_row_bytes_ppb(int width, uint32_t pixelsPerByte) { |
| 37 | return (width + pixelsPerByte - 1) / pixelsPerByte; |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * |
| 42 | * Compute row bytes for an image using bytes per pixel |
| 43 | * |
| 44 | */ |
| 45 | static inline size_t compute_row_bytes_bpp(int width, uint32_t bytesPerPixel) { |
| 46 | return width * bytesPerPixel; |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * |
| 51 | * Compute row bytes for an image |
| 52 | * |
| 53 | */ |
| 54 | static inline size_t compute_row_bytes(int width, uint32_t bitsPerPixel) { |
| 55 | if (bitsPerPixel < 16) { |
| 56 | SkASSERT(0 == 8 % bitsPerPixel); |
| 57 | const uint32_t pixelsPerByte = 8 / bitsPerPixel; |
| 58 | return compute_row_bytes_ppb(width, pixelsPerByte); |
| 59 | } else { |
| 60 | SkASSERT(0 == bitsPerPixel % 8); |
| 61 | const uint32_t bytesPerPixel = bitsPerPixel / 8; |
| 62 | return compute_row_bytes_bpp(width, bytesPerPixel); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * |
| 68 | * Checks if alpha types are premul and unpremul |
| 69 | * |
| 70 | */ |
| 71 | static inline bool premul_and_unpremul(SkAlphaType dst, SkAlphaType src) { |
| 72 | return kPremul_SkAlphaType == dst && kUnpremul_SkAlphaType == src; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * |
| 77 | * Get a byte from a buffer |
| 78 | * This method is unsafe, the caller is responsible for performing a check |
| 79 | * |
| 80 | */ |
| 81 | static inline uint8_t get_byte(uint8_t* buffer, uint32_t i) { |
| 82 | return buffer[i]; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * |
| 87 | * Get a short from a buffer |
| 88 | * This method is unsafe, the caller is responsible for performing a check |
| 89 | * |
| 90 | */ |
| 91 | static inline uint16_t get_short(uint8_t* buffer, uint32_t i) { |
| 92 | uint16_t result; |
| 93 | memcpy(&result, &(buffer[i]), 2); |
| 94 | #ifdef SK_CPU_BENDIAN |
| 95 | return SkEndianSwap16(result); |
| 96 | #else |
| 97 | return result; |
| 98 | #endif |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * |
| 103 | * Get an int from a buffer |
| 104 | * This method is unsafe, the caller is responsible for performing a check |
| 105 | * |
| 106 | */ |
| 107 | static inline uint32_t get_int(uint8_t* buffer, uint32_t i) { |
| 108 | uint32_t result; |
| 109 | memcpy(&result, &(buffer[i]), 4); |
| 110 | #ifdef SK_CPU_BENDIAN |
| 111 | return SkEndianSwap32(result); |
| 112 | #else |
| 113 | return result; |
| 114 | #endif |
| 115 | } |
| 116 | |
| 117 | #endif // SkCodecPriv_DEFINED |