2 * Copyright (C) 2011 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef HW_EMULATOR_CAMERA_CONVERTERS_H
18 #define HW_EMULATOR_CAMERA_CONVERTERS_H
23 #error "could not determine byte order"
27 * Contains declaration of framebuffer conversion routines.
29 * NOTE: RGB and big/little endian considerations. Wherewer in this code RGB
30 * pixels are represented as WORD, or DWORD, the color order inside the
31 * WORD / DWORD matches the one that would occur if that WORD / DWORD would have
32 * been read from the typecasted framebuffer:
34 * const uint32_t rgb = *reinterpret_cast<const uint32_t*>(framebuffer);
36 * So, if this code runs on the little endian CPU, red color in 'rgb' would be
37 * masked as 0x000000ff, and blue color would be masked as 0x00ff0000, while if
38 * the code runs on a big endian CPU, the red color in 'rgb' would be masked as
39 * 0xff000000, and blue color would be masked as 0x0000ff00,
48 #if __BYTE_ORDER == __LITTLE_ENDIAN
49 static const uint16_t kRed5 = 0x001f;
50 static const uint16_t kGreen6 = 0x07e0;
51 static const uint16_t kBlue5 = 0xf800;
53 static const uint16_t kRed5 = 0xf800;
54 static const uint16_t kGreen6 = 0x07e0;
55 static const uint16_t kBlue5 = 0x001f;
56 #endif // __BYTE_ORDER
57 static const uint32_t kBlack16 = 0x0000;
58 static const uint32_t kWhite16 = kRed5 | kGreen6 | kBlue5;
64 #if __BYTE_ORDER == __LITTLE_ENDIAN
65 static const uint32_t kRed8 = 0x000000ff;
66 static const uint32_t kGreen8 = 0x0000ff00;
67 static const uint32_t kBlue8 = 0x00ff0000;
69 static const uint32_t kRed8 = 0x00ff0000;
70 static const uint32_t kGreen8 = 0x0000ff00;
71 static const uint32_t kBlue8 = 0x000000ff;
72 #endif // __BYTE_ORDER
73 static const uint32_t kBlack32 = 0x00000000;
74 static const uint32_t kWhite32 = kRed8 | kGreen8 | kBlue8;
77 * Extracting, and saving color bytes from / to WORD / DWORD RGB.
80 #if __BYTE_ORDER == __LITTLE_ENDIAN
81 /* Extract red, green, and blue bytes from RGB565 word. */
82 #define R16(rgb) static_cast<uint8_t>(rgb & kRed5)
83 #define G16(rgb) static_cast<uint8_t>((rgb & kGreen6) >> 5)
84 #define B16(rgb) static_cast<uint8_t>((rgb & kBlue5) >> 11)
85 /* Make 8 bits red, green, and blue, extracted from RGB565 word. */
86 #define R16_32(rgb) static_cast<uint8_t>(((rgb & kRed5) << 3) | ((rgb & kRed5) >> 2))
87 #define G16_32(rgb) static_cast<uint8_t>(((rgb & kGreen6) >> 3) | ((rgb & kGreen6) >> 9))
88 #define B16_32(rgb) static_cast<uint8_t>(((rgb & kBlue5) >> 8) | ((rgb & kBlue5) >> 14))
89 /* Extract red, green, and blue bytes from RGB32 dword. */
90 #define R32(rgb) static_cast<uint8_t>(rgb & kRed8)
91 #define G32(rgb) static_cast<uint8_t>(((rgb & kGreen8) >> 8) & 0xff)
92 #define B32(rgb) static_cast<uint8_t>(((rgb & kBlue8) >> 16) & 0xff)
93 /* Build RGB565 word from red, green, and blue bytes. */
94 #define RGB565(r, g, b) static_cast<uint16_t>((((static_cast<uint16_t>(b) << 6) | g) << 5) | r)
95 /* Build RGB32 dword from red, green, and blue bytes. */
96 #define RGB32(r, g, b) static_cast<uint32_t>((((static_cast<uint32_t>(b) << 8) | g) << 8) | r)
98 /* Extract red, green, and blue bytes from RGB565 word. */
99 #define R16(rgb) static_cast<uint8_t>((rgb & kRed5) >> 11)
100 #define G16(rgb) static_cast<uint8_t>((rgb & kGreen6) >> 5)
101 #define B16(rgb) static_cast<uint8_t>(rgb & kBlue5)
102 /* Make 8 bits red, green, and blue, extracted from RGB565 word. */
103 #define R16_32(rgb) static_cast<uint8_t>(((rgb & kRed5) >> 8) | ((rgb & kRed5) >> 14))
104 #define G16_32(rgb) static_cast<uint8_t>(((rgb & kGreen6) >> 3) | ((rgb & kGreen6) >> 9))
105 #define B16_32(rgb) static_cast<uint8_t>(((rgb & kBlue5) << 3) | ((rgb & kBlue5) >> 2))
106 /* Extract red, green, and blue bytes from RGB32 dword. */
107 #define R32(rgb) static_cast<uint8_t>((rgb & kRed8) >> 16)
108 #define G32(rgb) static_cast<uint8_t>((rgb & kGreen8) >> 8)
109 #define B32(rgb) static_cast<uint8_t>(rgb & kBlue8)
110 /* Build RGB565 word from red, green, and blue bytes. */
111 #define RGB565(r, g, b) static_cast<uint16_t>((((static_cast<uint16_t>(r) << 6) | g) << 5) | b)
112 /* Build RGB32 dword from red, green, and blue bytes. */
113 #define RGB32(r, g, b) static_cast<uint32_t>((((static_cast<uint32_t>(r) << 8) | g) << 8) | b)
114 #endif // __BYTE_ORDER
116 /* An union that simplifies breaking 32 bit RGB into separate R, G, and B colors.
118 typedef union RGB32_t {
121 #if __BYTE_ORDER == __LITTLE_ENDIAN
122 uint8_t r; uint8_t g; uint8_t b; uint8_t a;
123 #else // __BYTE_ORDER
124 uint8_t a; uint8_t b; uint8_t g; uint8_t r;
125 #endif // __BYTE_ORDER
130 /* Clips a value to the unsigned 0-255 range, treating negative values as zero.
132 static __inline__ int
135 if (x > 255) return 255;
140 /********************************************************************************
141 * Basics of RGB -> YUV conversion
142 *******************************************************************************/
145 * RGB -> YUV conversion macros
147 #define RGB2Y(r, g, b) (uint8_t)(((66 * (r) + 129 * (g) + 25 * (b) + 128) >> 8) + 16)
148 #define RGB2U(r, g, b) (uint8_t)(((-38 * (r) - 74 * (g) + 112 * (b) + 128) >> 8) + 128)
149 #define RGB2V(r, g, b) (uint8_t)(((112 * (r) - 94 * (g) - 18 * (b) + 128) >> 8) + 128)
151 /* Converts R8 G8 B8 color to YUV. */
152 static __inline__ void
153 R8G8B8ToYUV(uint8_t r, uint8_t g, uint8_t b, uint8_t* y, uint8_t* u, uint8_t* v)
155 *y = RGB2Y((int)r, (int)g, (int)b);
156 *u = RGB2U((int)r, (int)g, (int)b);
157 *v = RGB2V((int)r, (int)g, (int)b);
160 /* Converts RGB565 color to YUV. */
161 static __inline__ void
162 RGB565ToYUV(uint16_t rgb, uint8_t* y, uint8_t* u, uint8_t* v)
164 R8G8B8ToYUV(R16_32(rgb), G16_32(rgb), B16_32(rgb), y, u, v);
167 /* Converts RGB32 color to YUV. */
168 static __inline__ void
169 RGB32ToYUV(uint32_t rgb, uint8_t* y, uint8_t* u, uint8_t* v)
173 R8G8B8ToYUV(rgb_c.r, rgb_c.g, rgb_c.b, y, u, v);
176 /********************************************************************************
177 * Basics of YUV -> RGB conversion.
178 * Note that due to the fact that guest uses RGB only on preview window, and the
179 * RGB format that is used is RGB565, we can limit YUV -> RGB conversions to
181 *******************************************************************************/
184 * YUV -> RGB conversion macros
187 /* "Optimized" macros that take specialy prepared Y, U, and V values:
192 #define YUV2RO(C, D, E) clamp((298 * (C) + 409 * (E) + 128) >> 8)
193 #define YUV2GO(C, D, E) clamp((298 * (C) - 100 * (D) - 208 * (E) + 128) >> 8)
194 #define YUV2BO(C, D, E) clamp((298 * (C) + 516 * (D) + 128) >> 8)
197 * Main macros that take the original Y, U, and V values
199 #define YUV2R(y, u, v) clamp((298 * ((y)-16) + 409 * ((v)-128) + 128) >> 8)
200 #define YUV2G(y, u, v) clamp((298 * ((y)-16) - 100 * ((u)-128) - 208 * ((v)-128) + 128) >> 8)
201 #define YUV2B(y, u, v) clamp((298 * ((y)-16) + 516 * ((u)-128) + 128) >> 8)
204 /* Converts YUV color to RGB565. */
205 static __inline__ uint16_t
206 YUVToRGB565(int y, int u, int v)
208 /* Calculate C, D, and E values for the optimized macro. */
209 y -= 16; u -= 128; v -= 128;
210 const uint16_t r = (YUV2RO(y,u,v) >> 3) & 0x1f;
211 const uint16_t g = (YUV2GO(y,u,v) >> 2) & 0x3f;
212 const uint16_t b = (YUV2BO(y,u,v) >> 3) & 0x1f;
213 return RGB565(r, g, b);
216 /* Converts YUV color to RGB32. */
217 static __inline__ uint32_t
218 YUVToRGB32(int y, int u, int v)
220 /* Calculate C, D, and E values for the optimized macro. */
221 y -= 16; u -= 128; v -= 128;
223 rgb.r = YUV2RO(y,u,v) & 0xff;
224 rgb.g = YUV2GO(y,u,v) & 0xff;
225 rgb.b = YUV2BO(y,u,v) & 0xff;
229 /* YUV pixel descriptor. */
240 inline explicit YUVPixel(uint16_t rgb565)
242 RGB565ToYUV(rgb565, &Y, &U, &V);
245 inline explicit YUVPixel(uint32_t rgb32)
247 RGB32ToYUV(rgb32, &Y, &U, &V);
250 inline void get(uint8_t* pY, uint8_t* pU, uint8_t* pV) const
252 *pY = Y; *pU = U; *pV = V;
256 /* Converts an YV12 framebuffer to RGB565 framebuffer.
258 * yv12 - YV12 framebuffer.
259 * rgb - RGB565 framebuffer.
260 * width, height - Dimensions for both framebuffers.
262 void YV12ToRGB565(const void* yv12, void* rgb, int width, int height);
264 /* Converts an YV12 framebuffer to RGB32 framebuffer.
266 * yv12 - YV12 framebuffer.
267 * rgb - RGB32 framebuffer.
268 * width, height - Dimensions for both framebuffers.
270 void YV12ToRGB32(const void* yv12, void* rgb, int width, int height);
272 /* Converts an YU12 framebuffer to RGB32 framebuffer.
274 * yu12 - YU12 framebuffer.
275 * rgb - RGB32 framebuffer.
276 * width, height - Dimensions for both framebuffers.
278 void YU12ToRGB32(const void* yu12, void* rgb, int width, int height);
280 /* Converts an NV12 framebuffer to RGB565 framebuffer.
282 * nv12 - NV12 framebuffer.
283 * rgb - RGB565 framebuffer.
284 * width, height - Dimensions for both framebuffers.
286 void NV12ToRGB565(const void* nv12, void* rgb, int width, int height);
288 /* Converts an NV12 framebuffer to RGB32 framebuffer.
290 * nv12 - NV12 framebuffer.
291 * rgb - RGB32 framebuffer.
292 * width, height - Dimensions for both framebuffers.
294 void NV12ToRGB32(const void* nv12, void* rgb, int width, int height);
296 /* Converts an NV21 framebuffer to RGB565 framebuffer.
298 * nv21 - NV21 framebuffer.
299 * rgb - RGB565 framebuffer.
300 * width, height - Dimensions for both framebuffers.
302 void NV21ToRGB565(const void* nv21, void* rgb, int width, int height);
304 /* Converts an NV21 framebuffer to RGB32 framebuffer.
306 * nv21 - NV21 framebuffer.
307 * rgb - RGB32 framebuffer.
308 * width, height - Dimensions for both framebuffers.
310 void NV21ToRGB32(const void* nv21, void* rgb, int width, int height);
312 }; /* namespace android */
314 #endif /* HW_EMULATOR_CAMERA_CONVERTERS_H */