aboutsummaryrefslogtreecommitdiffstats
path: root/color_pipe.h
blob: 26b229b83ac7083a2419d51765b5affcd5bb9379 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/**
* @file			color_pipe.h
* @brief		Color Processing Pipeline Definitions
* @author		Patrick Roth - roth@stettbacher.ch
* @copyright	Stettbacher Signal Processing AG
* 
* @remarks
*
* <PRE>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* </PRE>
*
*/


#ifndef _COLOR_PIPE_H
#define _COLOR_PIPE_H


#include <o3000/o3000_portable.h>
#include <o3000/image_header.h>


/**
 * Coordinate definition.
 */
struct coord_t {
	int x;			///< x-coordinate of calibrated pixel
	int y;			///< y-coordinate of calibrated pixel
};


/**
 * demosaicing algorithm definition
 */
enum bayer_alg_t {
	BAYER_ALG_BILINEAR = 0,		///< bilinear interpolation
};


/**
 * Demosaicing (debayer) data structure
 * This algorithm is enabled always.
 */
struct debayer_data_t {
	void *img_rgb;							///< RGB output image. This buffer must be allocated externly.
	void *img_raw;							///< Pointer to input image containing raw bayer image.
	int height;								///< image height in number of pixels
	int width;								///< image width in number of pixels
	enum enumDataFormat_t format;			///< data format of input image
	enum enumBayerPattern_t start_pattern;	///< first pixel starts with this bayer pattern
	enum bayer_alg_t alg;					///< debayer algorithm type
	enum bayer_alg_t alg_new;				///< this debayer algorithm type is changed by API call (use double buffering)
};


/**
 * automatic white balance (AWB) data structure
 */
struct awb_data_t {
	int enable;						///< flag to enable this algorithm
	void *img_rgb_balanced;			///< White balanced RGB output image. This buffer must be allocated externly.
	void *img_in;					///< Unbalanced RGB input image.
	int bit_channel;				///< Bits per color channel.
	int height;						///< image height in number of pixels
	int width;						///< image width in number of pixels
	int16_t *img_yuv;				///< Image buffer holding YUV image. This buffer must be allocated externly.
	float gray_threshold;			///< gray value threshold
	float gray_threshold_new;		///< this gray value threshold is changed by API call (use double buffering)
	float ctrl_k;					///< controller gain
	float ctrl_k_new;				///< this controller gain is changed by API call (use double buffering)
	float gain_red;					///< red color gain
	float gain_blue;				///< blue color gain
};



/**
 * camera distortion coefficient definition
 */
struct dist_coeff_t {
	float k1;			///< radial distortion factor k1
	float k2;			///< radial distortion factor k2
	float p1;			///< tangential distortion factor p1
	float p2;			///< tangential distortion factor p2
	float k3;			///< radial distortion factor k3
};


/**
 * camera matrix definition
 */
struct camera_matrix_t {
	float a11;			///< fx: focal length in x-direction
	float a12;			///< skew: axis skew
	float a13;			///< cx: optical center in x-direction
	float a21;			///< must be 0.0
	float a22;			///< focal length in y-direction
	float a23;			///< optical center in y-direction
	float a31;			///< must be 0.0
	float a32;			///< must be 0.0
	float a33;			///< must be 1.0
};


/**
 * O-3000 lense definitions supplied by Stettbacher Signal Processing.
 * Lense definitions are used to load lens specific distortion coefficients.
 */
enum o3000_lenses_t {
	
	// S-mount lenses
	O3000_LS_F2_8 = 0,			///< S-mount, focal length 2.8mm, aperture 2.0
	O3000_LS_F4_2,				///< S-mount, focal length 4.2mm, aperture 1.8
	O3000_LS_F6_0,				///< S-mount, focal length 6.0mm, aperture 1.8
	O3000_LS_F8_0,				///< S-mount, focal length 8.0mm, aperture 1.8
	O3000_LS_F12_0,				///< S-mount, focal length 12.0mm, aperture 1.8
	
	// CS-mount lenses
	O3000_LCS_F2_8,				///< CS-mount, focal length 2.8mm, aperture 1.6
	O3000_LCS_F4_2,				///< CS-mount, focal length 4.2mm, aperture 1.4
	O3000_LCS_F6_0,				///< CS-mount, focal length 6.0mm, aperture 1.4
	O3000_LCS_F8_0,				///< CS-mount, focal length 8.0mm, aperture 1.4
	O3000_LCS_F12_0,			///< CS-mount, focal length 12.0mm, aperture 1.4
};


/**
 * camera calibration structure
 * 
 * The total width @ref tot_width and height @ref tot_height define the image size take during calibration processs.
 * The field of view defines the current image windows. This window is less or equal to the total image size.
 */
struct cam_calib_data_t {
	int enable;								///< flag to enable this algorithm
	void *img_calib;						///< Undistorted (calibrated) output image. This buffer must be allocated externly.
	void *img_in;							///< Uncalibrated distorted input image.
	int is_color;							///< Not 0 if it's a color image.
	int bit_channel;						///< Bits per color channel.
	int tot_width;							///< total image width in pixels used during calibration process
	int tot_height;							///< total image height in pixels used during calibration process
	int fov_x_start;						///< field of view start pixel coordinate in x-direction
	int fov_x_end;							///< field of view end pixel coordinate in x-direction
	int fov_y_start;						///< field of view start pixel coordinate in y-direction
	int fov_y_end;							///< field of view end pixel coordinate in y-direction
	struct dist_coeff_t dist_coeff;			///< distortion coefficients
	struct camera_matrix_t camera_matrix;	///< camera matrix
	enum o3000_lenses_t lense;				///< lense type
	enum o3000_lenses_t lense_new;			///< lense type is changed by API call (use double buffering)
	int undistort_map_init;					///< flag indicating if lens undistortion map is initialized
	
	/**
	 * Lense undistortion map.
	 * A coordinate pair defines the undistorted pixel location. It may be shifted (see @ref calib_map_scale_fact).
	 * 
	 * This buffer must be allocated externly.
	 */ 
	struct coord_t *calib_map;
	
	/**
	 * Bit shift applied on undistortion map @ref calib_map.
	 * The pixel location may lay between a pixel pair. Therefore the coordinates defined at the undistortion map
	 * @ref calib_map are (scaled) shifted by this factor.
	 */ 
	int calib_map_scale_fact;
};


/**
 * Color Correction Matrix presets (CCM) for various camera types
 */
enum ccm_preset_t {
	CCM_PRESET_O3020 = 0,					///< O-3020 camera from Stettbacher Signal Processing
};


/**
 * color calibration structure definition
 * 
 * 
 * The color correction matrix A maps the uncalibrated image (img_uncal) to the
 * calibrated image (img_calib) according to the following transformation:
 * 
 * img_calib = A * img_uncal
 * 
 * where
 * 
 *             | a11  a12  a13 |
 * A =         | a21  a22  a23 |
 *             | a31  a32  a33 |
 * 
 * 
 *             | red_uncal   |
 * img_uncal = | green_uncal |
 *             | blue_uncal  |
 * 
 *             | red_calib   |
 * img_calib = | green_calib |
 *             | blue_calib  |
 * 
 * 
 * red_calib	= a11*red_uncal + a12*green_uncal + a12*blue_uncal
 * green_calib	= a21*red_uncal + a22*green_uncal + a22*blue_uncal
 * blue_calib	= a31*red_uncal + a32*green_uncal + a32*blue_uncal
 * 
 */
struct color_calib_data_t {
	int enable;								///< flag to enable this algorithm
	void *img_calib;						///< Color calibrated output image.  This buffer must be allocated externly.
	void *img_in;							///< Uncalibrated input image
	int bit_channel;						///< bits per color channel
	int width;								///< image width in number of pixels
	int height;								///< image height in number of pixels
	float a[3][3];							///< 3x3 color correction matrix
	enum ccm_preset_t ccm;					///< color correction matrix type loaded
	enum ccm_preset_t ccm_new;				///< this color correction matrix type is changed by API call (use double buffering)
};


/**
 * Sharpening algorithm definition
 */
enum sharp_alg_t {
	SHARP_ALG_GLOBAL = 0,					///< global sharpening algorithm
	SHARP_ALG_LOCAL,						///< local sharpening algorithm
};


/**
 * Sharpening data definition
 */
struct sharp_data_t {
	int enable;								///< flag to enable this algorithm
	void *img_sharp;						///< Sharpened output RGB image. This buffer must be allocated externly.
	void *img_in;							///< Unsharp RGB input image.
	int is_color;							///< Not 0 if it's a color image
	int bit_channel;						///< Bits per color channel.
	int width;								///< image width in number of pixels
	int height;								///< image height in number of pixels
	int16_t *img_yuv;						///< YUV image buffer. This buffer must be allocated externly.
	float sharp_factor;						///< Sharpening factor: As higher as stronger sharpening is done.
	float sharp_factor_new;					///< this sharpening factor is changed by API call (use double buffering)
	enum sharp_alg_t sharp_alg;				///< sharpening algorithm type
	enum sharp_alg_t sharp_alg_new;			///< this algorithm type is changed by API call (use double buffering)
	float local_sens;						///< Sensitivity setting of local sharpening algorithm in per cent. 100 % means everything is sharpened like the global algorithm does
	float local_sens_new;					///< this sensitivity setting is changed by API call (use double buffering)
	int16_t *img_yuv_sharp;					///< YUV image buffer holding sharpened Y-channel. This buffer must be allocated externly.
	int16_t *img_sobel;						///< Sobel image in YUV color space used by local sharpening algorithm. This buffer must be allocated externly.
	int16_t *img_gauss;						///< Gaussian low-pass filtered image in YUV color space used by local sharpening algorithm. This buffer must be allocated externly.
	int8_t *sharp_mask;						///< Binary sharpening mask image used by local sharpening algorithm. This buffer must be allocated externly.
};


/**
 * Gamma correction data definition
 */
struct gamma_data_t {
	int enable;								///< flag to enable this algorithm
	void *img_gamma;						///< Gamma corrected output image. This buffer must be allocated externly.
	void *img_in;							///< Input image.
	int is_color;							///< Not 0 if it's a color image
	int bit_channel;						///< Bits per color channel.
	int width;								///< image width in number of pixels
	int height;								///< image height in number of pixels
	float gamma;							///< gamma coefficient
	float gamma_new;						///< this gamma coefficient is changed by API call (use double buffering)
	int gamma_table_bitdepth;				///< gamma table is initialized with this bit-depth 
	float gamma_table_init;					///< gamma table is initialized with this coefficient 
	int *gamma_table;						///< Lookup table containg gamma corrected value.  This buffer must be allocated externly.
};


/**
 * Isosceles trapezoid correction definition
 */
struct trapcorr_data_t {
	int enable;								///< flag to enable this algorithm
	void *img_out;							///< Isosceles corrected output image. This buffer must be allocated externly.
	void *img_in;							///< Input image.
	int is_color;							///< Not 0 if it's a color image
	int bit_channel;						///< Bits per color channel.
	int width;								///< image width in number of pixels
	int height;								///< image height in number of pixels
	float wv;								///< vertical correction weight in per cent (range: -100.0 to +100.0) (NOTE not implemented yet!)
	float wh;								///< horizontal correction weight in per cent (range: -100.0 to +100.0)
	float wv_new;							///< double buffered vertical correction weight
	float wh_new;							///< double buffered horizontal correction weight
	 
	int map_init;							///< flag indicating perspective correction map is initialized
	
	/**
	 * perspective correction map
	 * A coordinate pair defines the pixel location to be corrected. It may be shifted (see @ref map_scale_fact).
	 * 
	 * This buffer must be allocated externly.
	 */
	struct coord_t *map;
	
	/**
	 * Bit shift applied on correction map @ref map.
	 * The pixel location may lay between a pixel pair. Therefore the coordinates defined at the correction map
	 * @ref map are (scaled) shifted by this factor.
	 */
	int map_scale_fact;
};


/**
 * Projective transformation definition 
 */
struct projection_data_t {
	int enable;								///< flag to enable projective transformation
	void *img_out;							///< projected image. This buffer must be allocated externly.
	void *img_in;							///< Input image.
	int is_color;							///< Not 0 if it's a color image
	int bit_channel;						///< Bits per color channel.
	int width;								///< image width in number of pixels
	int height;								///< image height in number of pixels
	float c_inv[3][3];						///< inverse of 3x3 projection matrix C
	float c_inv_new[3][3];					///< double buffered matrix
	int c_upd;								///< projection matrix update flag
	
	int map_init;							///< flag indicating transformation map is initialized
	
	/**
	 * projective transfomration map
	 * A coordinate pair defines the transformed pixel location. It may be shifted (see @ref map_scale_fact).
	 * 
	 * This buffer must be allocated externly.
	 */
	struct coord_t *map;
	
	/**
	 * Bit shift applied on correction map @ref map.
	 * The pixel location may lay between a pixel pair. Therefore the coordinates defined at the map
	 * @ref map are (scaled) shifted by this factor.
	 */
	int map_scale_fact;
};


/**
 * Color pipe definition structure holding all memory data from different pipeline
 * stages. This structure and image buffers are allocated dynamically.
 */
struct color_pipe_t {
	
	void *img_out;									///< Points to processed output image in RGB format.
	int is_color;									///< Not 0 if it's a color image
	int bit_channel;								///< Bits per color channel.
	int width;										///< image width in number of pixels
	int height;										///< image height in number of pixels
	
	struct debayer_data_t debayer_data;				///< demosaicing data
	struct awb_data_t awb_data;						///< auto white balancing data
	struct cam_calib_data_t cam_calib_data;			///< camera calibration data used for lens distortion correction
	struct color_calib_data_t color_calib_data;		///< color calibration data used for color corretion
	struct sharp_data_t sharp_data;					///< image sharpening data
	struct gamma_data_t gamma_data;					///< gamma correction data
	struct trapcorr_data_t trapcorr_data;			///< isosceles trapezoid correction data
	struct projection_data_t proj_data;				///< projective transformation data
};



#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif

void __stdcall color_pipe_process(struct color_pipe_t *color_pipe, void *img_buf, struct img_header_t *img_header);
int __stdcall color_pipe_open(struct color_pipe_t **color_pipe, const int max_img_height, const int max_img_width, const int bits_per_channel);
int __stdcall color_pipe_close(struct color_pipe_t *data);
void __stdcall color_pipe_get_version(int *major, int *minor, int *release);

void __stdcall color_pipe_stageconf_debayer(struct color_pipe_t *color_pipe, enum bayer_alg_t alg);
void __stdcall color_pipe_stageconf_awb(struct color_pipe_t *color_pipe, int enable, float gray_threshold, float ctrl_gain);
void __stdcall color_pipe_stageconf_cam_calib(struct color_pipe_t *color_pipe, int enable, enum o3000_lenses_t lense);
void __stdcall color_pipe_stageconf_color_calib(struct color_pipe_t *color_pipe, int enable, enum ccm_preset_t ccm_preset);
void __stdcall color_pipe_stageconf_sharp(struct color_pipe_t *color_pipe, int enable, float factor, enum sharp_alg_t alg, float sens);
void __stdcall color_pipe_stageconf_gamma(struct color_pipe_t *color_pipe, int enable, float gamma);
void __stdcall color_pipe_stageconf_trapcorr(struct color_pipe_t *color_pipe, int enable, float wv, float wh);
void __stdcall color_pipe_stageconf_projection(struct color_pipe_t *color_pipe, int enable, float c_inv[3][3]);

#if defined(__cplusplus) || defined(c_plusplus)
} // extern "C"
#endif


#endif // _COLOR_PIPE_H