1/*
2 * 1394-Based Digital Camera Control Library
3 *
4 * Color conversion functions, including Bayer pattern decoding
5 *
6 * Written by Damien Douxchamps and Frederic Devernay
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include <dc1394/log.h>
24
25#ifndef __DC1394_CONVERSIONS_H__
26#define __DC1394_CONVERSIONS_H__
27
28/*! \file dc1394/conversions.h
29 \brief functions to convert video formats
30 \author Damien Douxchamps: coding
31 \author Frederic Devernay: coding
32 \author Peter Antoniac: documentation maintainer
33
34 More details soon
35*/
36
37#define restrict __restrict
38
39/**
40 * A list of de-mosaicing techniques for Bayer-patterns.
41 *
42 * The speed of the techniques can vary greatly, as well as their quality.
43 */
44typedef enum {
45 DC1394_BAYER_METHOD_NEAREST=0,
46 DC1394_BAYER_METHOD_SIMPLE,
47 DC1394_BAYER_METHOD_BILINEAR,
48 DC1394_BAYER_METHOD_HQLINEAR,
49 DC1394_BAYER_METHOD_DOWNSAMPLE,
50 DC1394_BAYER_METHOD_EDGESENSE,
51 DC1394_BAYER_METHOD_VNG,
52 DC1394_BAYER_METHOD_AHD
53} dc1394bayer_method_t;
54#define DC1394_BAYER_METHOD_MIN DC1394_BAYER_METHOD_NEAREST
55#define DC1394_BAYER_METHOD_MAX DC1394_BAYER_METHOD_AHD
56#define DC1394_BAYER_METHOD_NUM (DC1394_BAYER_METHOD_MAX-DC1394_BAYER_METHOD_MIN+1)
57
58/**
59 * A list of known stereo-in-normal-video modes used by manufacturers like Point Grey Research and Videre Design.
60 */
61typedef enum {
62 DC1394_STEREO_METHOD_INTERLACED=0,
63 DC1394_STEREO_METHOD_FIELD
64} dc1394stereo_method_t;
65#define DC1394_STEREO_METHOD_MIN DC1394_STEREO_METHOD_INTERLACED
66#define DC1394_STEREO_METHOD_MAX DC1394_STEREO_METHOD_FIELD
67#define DC1394_STEREO_METHOD_NUM (DC1394_STEREO_METHOD_MAX-DC1394_STEREO_METHOD_MIN+1)
68
69
70// color conversion functions from Bart Nabbe.
71// corrected by Damien: bad coeficients in YUV2RGB
72#define YUV2RGB(y, u, v, r, g, b) {\
73 r = y + ((v*1436) >> 10);\
74 g = y - ((u*352 + v*731) >> 10);\
75 b = y + ((u*1814) >> 10);\
76 r = r < 0 ? 0 : r;\
77 g = g < 0 ? 0 : g;\
78 b = b < 0 ? 0 : b;\
79 r = r > 255 ? 255 : r;\
80 g = g > 255 ? 255 : g;\
81 b = b > 255 ? 255 : b; }
82
83
84#define RGB2YUV(r, g, b, y, u, v) {\
85 y = (306*r + 601*g + 117*b) >> 10;\
86 u = ((-172*r - 340*g + 512*b) >> 10) + 128;\
87 v = ((512*r - 429*g - 83*b) >> 10) + 128;\
88 y = y < 0 ? 0 : y;\
89 u = u < 0 ? 0 : u;\
90 v = v < 0 ? 0 : v;\
91 y = y > 255 ? 255 : y;\
92 u = u > 255 ? 255 : u;\
93 v = v > 255 ? 255 : v; }
94
95#ifdef __cplusplus
96extern "C" {
97#endif
98
99/**********************************************************************
100 * CONVERSION FUNCTIONS TO YUV422, MONO8 and RGB8
101 **********************************************************************/
102
103/**
104 * Converts an image buffer to YUV422
105 */
106dc1394error_t
107dc1394_convert_to_YUV422(uint8_t *src, uint8_t *dest, uint32_t width, uint32_t height, uint32_t byte_order,
108 dc1394color_coding_t source_coding, uint32_t bits);
109
110/**
111 * Converts an image buffer to MONO8
112 */
113dc1394error_t
114dc1394_convert_to_MONO8(uint8_t *src, uint8_t *dest, uint32_t width, uint32_t height, uint32_t byte_order,
115 dc1394color_coding_t source_coding, uint32_t bits);
116
117/**
118 * Converts an image buffer to RGB8
119 */
120dc1394error_t
121dc1394_convert_to_RGB8(uint8_t *src, uint8_t *dest, uint32_t width, uint32_t height, uint32_t byte_order,
122 dc1394color_coding_t source_coding, uint32_t bits);
123
124/**********************************************************************
125 * CONVERSION FUNCTIONS FOR STEREO IMAGES
126 **********************************************************************/
127
128/**
129 * changes a 16bit stereo image (8bit/channel) into two 8bit images on top of each other
130 */
131dc1394error_t
132dc1394_deinterlace_stereo(uint8_t *src, uint8_t *dest, uint32_t width, uint32_t height);
133
134/************************************************************************************************
135 * *
136 * Color conversion functions for cameras that can output raw Bayer pattern images (color *
137 * codings DC1394_COLOR_CODING_RAW8 and DC1394_COLOR_CODING_RAW16). *
138 * *
139 * Credits and sources: *
140 * - Nearest Neighbor : OpenCV library *
141 * - Bilinear : OpenCV library *
142 * - HQLinear : High-Quality Linear Interpolation For Demosaicing Of Bayer-Patterned *
143 * Color Images, by Henrique S. Malvar, Li-wei He, and Ross Cutler, *
144 * in Proceedings of the ICASSP'04 Conference. *
145 * - Edge Sense II : Laroche, Claude A. "Apparatus and method for adaptively interpolating *
146 * a full color image utilizing chrominance gradients" *
147 * U.S. Patent 5,373,322. Based on the code found on the website *
148 * http://www-ise.stanford.edu/~tingchen/ Converted to C and adapted to *
149 * all four elementary patterns. *
150 * - Downsample : "Known to the Ancients" *
151 * - Simple : Implemented from the information found in the manual of Allied Vision *
152 * Technologies (AVT) cameras. *
153 * - VNG : Variable Number of Gradients, a method described in *
154 * http://www-ise.stanford.edu/~tingchen/algodep/vargra.html *
155 * Sources import from DCRAW by Frederic Devernay. DCRAW is a RAW *
156 * converter program by Dave Coffin. URL: *
157 * http://www.cybercom.net/~dcoffin/dcraw/ *
158 * - AHD : Adaptive Homogeneity-Directed Demosaicing Algorithm, by K. Hirakawa *
159 * and T.W. Parks, IEEE Transactions on Image Processing, Vol. 14, Nr. 3, *
160 * March 2005, pp. 360 - 369. *
161 * *
162 ************************************************************************************************/
163
164/**
165 * Perform de-mosaicing on an 8-bit image buffer
166 */
167dc1394error_t
168dc1394_bayer_decoding_8bit(const uint8_t *bayer, uint8_t *rgb,
169 uint32_t width, uint32_t height, dc1394color_filter_t tile,
170 dc1394bayer_method_t method);
171
172/**
173 * Perform de-mosaicing on an 16-bit image buffer
174 */
175dc1394error_t
176dc1394_bayer_decoding_16bit(const uint16_t *bayer, uint16_t *rgb,
177 uint32_t width, uint32_t height, dc1394color_filter_t tile,
178 dc1394bayer_method_t method, uint32_t bits);
179
180
181/**********************************************************************************
182 * Frame based conversions
183 **********************************************************************************/
184
185/**
186 * Converts the format of a video frame.
187 *
188 * To set the format of the output, simply set the values of the corresponding fields in the output frame
189 */
190dc1394error_t
191dc1394_convert_frames(dc1394video_frame_t *in, dc1394video_frame_t *out);
192
193/**
194 * De-mosaicing of a Bayer-encoded video frame
195 *
196 * To set the format of the output, simply set the values of the corresponding fields in the output frame
197 * @param in is a pointer to the bayer video frame that is to be converted
198 * @param out is a pointer to the frame to be converted to. If there is memory allocated to the image field,
199 * then it will be adjusted accordingly by this function. If there is no memory allocated to the image
200 * field, then ensure that out->image == NULL and out->allocated_image_bytes == 0
201 * @param method is the bayer method to interpolate the frame.
202 */
203dc1394error_t
204dc1394_debayer_frames(dc1394video_frame_t *in, dc1394video_frame_t *out, dc1394bayer_method_t method);
205
206/**
207 * De-interlacing of stereo data for cideo frames
208 *
209 * To set the format of the output, simply set the values of the corresponding fields in the output frame
210 */
211dc1394error_t
212dc1394_deinterlace_stereo_frames(dc1394video_frame_t *in, dc1394video_frame_t *out, dc1394stereo_method_t method);
213
214#ifdef __cplusplus
215}
216#endif
217
218#endif /* _DC1394_CONVERSIONS_H */
219
220
221

source code of include/dc1394/conversions.h