blob: 7c57a9fe773190bd3add08e44d02485516f70803 [file] [log] [blame]
msarett74114382015-03-16 11:55:18 -07001/*
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 */
36static 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 */
45static 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 */
54static 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 *
msarett74114382015-03-16 11:55:18 -070068 * Get a byte from a buffer
69 * This method is unsafe, the caller is responsible for performing a check
70 *
71 */
72static inline uint8_t get_byte(uint8_t* buffer, uint32_t i) {
73 return buffer[i];
74}
75
76/*
77 *
78 * Get a short from a buffer
79 * This method is unsafe, the caller is responsible for performing a check
80 *
81 */
82static inline uint16_t get_short(uint8_t* buffer, uint32_t i) {
83 uint16_t result;
84 memcpy(&result, &(buffer[i]), 2);
85#ifdef SK_CPU_BENDIAN
86 return SkEndianSwap16(result);
87#else
88 return result;
89#endif
90}
91
92/*
93 *
94 * Get an int from a buffer
95 * This method is unsafe, the caller is responsible for performing a check
96 *
97 */
98static inline uint32_t get_int(uint8_t* buffer, uint32_t i) {
99 uint32_t result;
100 memcpy(&result, &(buffer[i]), 4);
101#ifdef SK_CPU_BENDIAN
102 return SkEndianSwap32(result);
103#else
104 return result;
105#endif
106}
107
108#endif // SkCodecPriv_DEFINED