1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef FRAMEWORKS_INNERKITSIMPL_COMMON_INCLUDE_PIXEL_MAP_UTILS_H_
17 #define FRAMEWORKS_INNERKITSIMPL_COMMON_INCLUDE_PIXEL_MAP_UTILS_H_
18
19 #include "image_type.h"
20 #include "log_tags.h"
21 #include "pixel_map.h"
22
23 namespace OHOS {
24 namespace Media {
25 // Define bytes per pixel
26 constexpr int8_t ALPHA_8_BYTES = 1;
27 constexpr int8_t RGB_565_BYTES = 2;
28 constexpr int8_t RGB_888_BYTES = 3;
29 constexpr int8_t ARGB_8888_BYTES = 4;
30 constexpr int8_t BGRA_F16_BYTES = 8;
31 constexpr int8_t YUV420_BYTES = 2; // in fact NV21 one pixel used 1.5 bytes.
32
33 // Define shift bits of bytes per pixel
34 constexpr int8_t ALPHA_8_SHIFT = 0;
35 constexpr int8_t RGB_565_SHIFT = 1;
36 constexpr int8_t ARGB_8888_SHIFT = 2;
37
38 // Convert RGB565 16bit pixel to 32bit pixel
39 constexpr uint8_t RGB565_R_BITS = 5;
40 constexpr uint8_t RGB565_G_BITS = 6;
41 constexpr uint8_t RGB565_B_BITS = 5;
42
43 #if __BYTE_ORDER == __LITTLE_ENDIAN
44 constexpr uint8_t RGB565_R_SHIFT = 0;
45 constexpr uint8_t RGB565_G_SHIFT = RGB565_R_BITS;
46 constexpr uint8_t RGB565_B_SHIFT = RGB565_R_BITS + RGB565_G_BITS;
47 constexpr uint16_t RGB565_R_MASK = 0x001F;
48 constexpr uint16_t RGB565_G_MASK = 0x07E0;
49 constexpr uint16_t RGB565_B_MASK = 0xF800;
50 #else
51 constexpr uint8_t RGB565_R_SHIFT = RGB565_B_BITS + RGB565_G_BITS;
52 constexpr uint8_t RGB565_G_SHIFT = RGB565_B_BITS;
53 constexpr uint8_t RGB565_B_SHIFT = 0;
54 constexpr uint16_t RGB565_R_MASK = 0xF800;
55 constexpr uint16_t RGB565_G_MASK = 0x07E0;
56 constexpr uint16_t RGB565_B_MASK = 0x001F;
57 #endif
58 constexpr uint8_t BYTE_BITS = 8;
59 constexpr uint8_t RGB565_CONVERT_BIT = 2;
60 constexpr uint8_t ARGB8888_CONVERT_BIT = 24;
61
62 // Convert for ARGB_8888 32bit pixel
63 #if __BYTE_ORDER == __LITTLE_ENDIAN
64 constexpr uint8_t ARGB32_A_SHIFT = 0;
65 constexpr uint8_t ARGB32_R_SHIFT = 8;
66 constexpr uint8_t ARGB32_G_SHIFT = 16;
67 constexpr uint8_t ARGB32_B_SHIFT = 24;
68 #else
69 constexpr uint8_t ARGB32_A_SHIFT = 24;
70 constexpr uint8_t ARGB32_R_SHIFT = 16;
71 constexpr uint8_t ARGB32_G_SHIFT = 8;
72 constexpr uint8_t ARGB32_B_SHIFT = 0;
73 #endif
74
75 // Convert for RGBA_8888 32bit pixel
76 #if __BYTE_ORDER == __LITTLE_ENDIAN
77 constexpr uint8_t RGBA32_R_SHIFT = 0;
78 constexpr uint8_t RGBA32_G_SHIFT = 8;
79 constexpr uint8_t RGBA32_B_SHIFT = 16;
80 constexpr uint8_t RGBA32_A_SHIFT = 24;
81 #else
82 constexpr uint8_t RGBA32_R_SHIFT = 24;
83 constexpr uint8_t RGBA32_G_SHIFT = 16;
84 constexpr uint8_t RGBA32_B_SHIFT = 8;
85 constexpr uint8_t RGBA32_A_SHIFT = 0;
86 #endif
87
88 // Convert for BGRA_8888 32bit pixel
89 #if __BYTE_ORDER == __LITTLE_ENDIAN
90 constexpr uint8_t BGRA32_B_SHIFT = 0;
91 constexpr uint8_t BGRA32_G_SHIFT = 8;
92 constexpr uint8_t BGRA32_R_SHIFT = 16;
93 constexpr uint8_t BGRA32_A_SHIFT = 24;
94 #else
95 constexpr uint8_t BGRA32_B_SHIFT = 24;
96 constexpr uint8_t BGRA32_G_SHIFT = 16;
97 constexpr uint8_t BGRA32_R_SHIFT = 8;
98 constexpr uint8_t BGRA32_A_SHIFT = 0;
99 #endif
100
101 constexpr uint8_t BYTE_FULL = 0xFF;
102 constexpr uint8_t BYTE_ZERO = 0;
103 constexpr uint8_t ONE_PIXEL_SIZE = 1;
104
105 /*
106 * For RGB_565
107 * 1. get R(5-bits)/G(6-bits)/B(5-bits) channel value form color value(uint16_t)
108 * 2. convert R(5-bits)/G(6-bits)/B(5-bits) value to R(8-bits)/G(8-bits)/B(8-bits)
109 * 3. construct normalized color value with A(255)/R(8-bits)/G(8-bits)/B(8-bits)
110 * 4. the normalized color format: (A << 24 | R << 16 | G << 8 | B << 0)
111 */
GetRGB565Channel(uint16_t color,uint16_t mask,uint8_t shift)112 static uint8_t GetRGB565Channel(uint16_t color, uint16_t mask, uint8_t shift)
113 {
114 return (color & mask) >> shift;
115 }
116
RGB565To32(uint8_t channel,uint8_t bits)117 static uint8_t RGB565To32(uint8_t channel, uint8_t bits)
118 {
119 return (channel << (BYTE_BITS - bits)) | (channel >> (RGB565_CONVERT_BIT * bits - BYTE_BITS));
120 }
121
RGB565ToR32(uint16_t color)122 static uint8_t RGB565ToR32(uint16_t color)
123 {
124 return RGB565To32(GetRGB565Channel(color, RGB565_R_MASK, RGB565_R_SHIFT), RGB565_R_BITS);
125 }
126
RGB565ToG32(uint16_t color)127 static uint8_t RGB565ToG32(uint16_t color)
128 {
129 return RGB565To32(GetRGB565Channel(color, RGB565_G_MASK, RGB565_G_SHIFT), RGB565_G_BITS);
130 }
131
RGB565ToB32(uint16_t color)132 static uint8_t RGB565ToB32(uint16_t color)
133 {
134 return RGB565To32(GetRGB565Channel(color, RGB565_B_MASK, RGB565_B_SHIFT), RGB565_B_BITS);
135 }
136
137 /*
138 * For ARGB_8888
139 * 1. get A(8-bits)/R(8-bits)/G(8-bits)/B(8-bits) channel value form color value(uint32_t)
140 * 2. construct normalized color value with A(8-bits)/R(8-bits)/G(8-bits)/B(8-bits)
141 * 3. the normalized color format: (A << 24 | R << 16 | G << 8 | B << 0)
142 */
GetColorComp(uint32_t color,uint8_t shift)143 static uint8_t GetColorComp(uint32_t color, uint8_t shift)
144 {
145 return ((color) << (ARGB8888_CONVERT_BIT - shift)) >> ARGB8888_CONVERT_BIT;
146 }
147
GetColorARGB(uint8_t a,uint8_t r,uint8_t g,uint8_t b)148 static uint32_t GetColorARGB(uint8_t a, uint8_t r, uint8_t g, uint8_t b)
149 {
150 return ((a << ARGB_A_SHIFT) | (r << ARGB_R_SHIFT) | (g << ARGB_G_SHIFT) | (b << ARGB_B_SHIFT));
151 }
152
153 static ImageInfo MakeImageInfo(int width, int height, PixelFormat pf, AlphaType at, ColorSpace cs = ColorSpace::SRGB)
154 {
155 ImageInfo info;
156 info.size.width = width;
157 info.size.height = height;
158 info.pixelFormat = pf;
159 info.alphaType = at;
160 info.colorSpace = cs;
161 return info;
162 }
163 } // namespace Media
164 } // namespace OHOS
165
166 #endif // FRAMEWORKS_INNERKITSIMPL_COMMON_INCLUDE_PIXEL_MAP_UTILS_H_
167