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 "SkColorTable.h"
12 #include "SkImageInfo.h"
13 #include "SkSwizzler.h"
14 #include "SkTypes.h"
15 #include "SkUtils.h"
16
17 /*
18 *
19 * Helper routine for alpha result codes
20 *
21 */
22 #define INIT_RESULT_ALPHA \
23 uint8_t zeroAlpha = 0; \
24 uint8_t maxAlpha = 0xFF;
25
26 #define UPDATE_RESULT_ALPHA(alpha) \
27 zeroAlpha |= (alpha); \
28 maxAlpha &= (alpha);
29
30 #define COMPUTE_RESULT_ALPHA \
31 SkSwizzler::GetResult(zeroAlpha, maxAlpha);
32
33 /*
34 *
35 * Copy the codec color table back to the client when kIndex8 color type is requested
36 *
37 */
copy_color_table(const SkImageInfo & dstInfo,SkColorTable * colorTable,SkPMColor * inputColorPtr,int * inputColorCount)38 static inline void copy_color_table(const SkImageInfo& dstInfo, SkColorTable* colorTable,
39 SkPMColor* inputColorPtr, int* inputColorCount) {
40 if (kIndex_8_SkColorType == dstInfo.colorType()) {
41 SkASSERT(NULL != inputColorPtr);
42 SkASSERT(NULL != inputColorCount);
43 SkASSERT(NULL != colorTable);
44 sk_memcpy32(inputColorPtr, colorTable->readColors(), *inputColorCount);
45 }
46 }
47
48 /*
49 *
50 * Compute row bytes for an image using pixels per byte
51 *
52 */
compute_row_bytes_ppb(int width,uint32_t pixelsPerByte)53 static inline size_t compute_row_bytes_ppb(int width, uint32_t pixelsPerByte) {
54 return (width + pixelsPerByte - 1) / pixelsPerByte;
55 }
56
57 /*
58 *
59 * Compute row bytes for an image using bytes per pixel
60 *
61 */
compute_row_bytes_bpp(int width,uint32_t bytesPerPixel)62 static inline size_t compute_row_bytes_bpp(int width, uint32_t bytesPerPixel) {
63 return width * bytesPerPixel;
64 }
65
66 /*
67 *
68 * Compute row bytes for an image
69 *
70 */
compute_row_bytes(int width,uint32_t bitsPerPixel)71 static inline size_t compute_row_bytes(int width, uint32_t bitsPerPixel) {
72 if (bitsPerPixel < 16) {
73 SkASSERT(0 == 8 % bitsPerPixel);
74 const uint32_t pixelsPerByte = 8 / bitsPerPixel;
75 return compute_row_bytes_ppb(width, pixelsPerByte);
76 } else {
77 SkASSERT(0 == bitsPerPixel % 8);
78 const uint32_t bytesPerPixel = bitsPerPixel / 8;
79 return compute_row_bytes_bpp(width, bytesPerPixel);
80 }
81 }
82
83 /*
84 *
85 * Get a byte from a buffer
86 * This method is unsafe, the caller is responsible for performing a check
87 *
88 */
get_byte(uint8_t * buffer,uint32_t i)89 static inline uint8_t get_byte(uint8_t* buffer, uint32_t i) {
90 return buffer[i];
91 }
92
93 /*
94 *
95 * Get a short from a buffer
96 * This method is unsafe, the caller is responsible for performing a check
97 *
98 */
get_short(uint8_t * buffer,uint32_t i)99 static inline uint16_t get_short(uint8_t* buffer, uint32_t i) {
100 uint16_t result;
101 memcpy(&result, &(buffer[i]), 2);
102 #ifdef SK_CPU_BENDIAN
103 return SkEndianSwap16(result);
104 #else
105 return result;
106 #endif
107 }
108
109 /*
110 *
111 * Get an int from a buffer
112 * This method is unsafe, the caller is responsible for performing a check
113 *
114 */
get_int(uint8_t * buffer,uint32_t i)115 static inline uint32_t get_int(uint8_t* buffer, uint32_t i) {
116 uint32_t result;
117 memcpy(&result, &(buffer[i]), 4);
118 #ifdef SK_CPU_BENDIAN
119 return SkEndianSwap32(result);
120 #else
121 return result;
122 #endif
123 }
124
125 #ifdef SK_PRINT_CODEC_MESSAGES
126 #define SkCodecPrintf SkDebugf
127 #else
128 #define SkCodecPrintf(...)
129 #endif
130
131 #endif // SkCodecPriv_DEFINED
132