aboutsummaryrefslogtreecommitdiffstats
path: root/alg_ccm.h
blob: 0bc4ac2533deebbc2ad178b2e297b2116ebf2ea4 (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
/**
* @file			alg_ccm.h
* @brief		color correction algorithm definition
* @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>
*
*/


/*
 * This code inplements color correction algorithm and is pixel size independent. Including this code at the
 * C-Source file will define the pixel-bit-depth (see color.c).
 */
// static void rgb_color_correction8(uint8_t *img_calib, const uint8_t *img_uncalib, const int color_bit_depth, const int height, const int width, float a[3][3])
// static void rgb_color_correction16(uint16_t *img_calib, const uint16_t *img_uncalib, const int color_bit_depth, const int height, const int width, float a[3][3])
{
	int y, x;
	int uncal_red, uncal_green, uncal_blue;
	int cal_red, cal_green, cal_blue;
	int index;
	int pix_max_val;
	const int shift_fact = 16;
	
	const int a11 = (int) (a[0][0]*(1<<shift_fact));
	const int a12 = (int) (a[0][1]*(1<<shift_fact));
	const int a13 = (int) (a[0][2]*(1<<shift_fact));
	const int a21 = (int) (a[1][0]*(1<<shift_fact));
	const int a22 = (int) (a[1][1]*(1<<shift_fact));
	const int a23 = (int) (a[1][2]*(1<<shift_fact));
	const int a31 = (int) (a[2][0]*(1<<shift_fact));
	const int a32 = (int) (a[2][1]*(1<<shift_fact));
	const int a33 = (int) (a[2][2]*(1<<shift_fact));
	
	index = 0;
	pix_max_val = (1<<color_bit_depth)-1;
	
	for(y = 0; y < height; y++) {
		for(x = 0; x < width; x++) {
			uncal_red = img_uncalib[index];
			uncal_green = img_uncalib[index+1];
			uncal_blue = img_uncalib[index+2];
			
			// apply color correction matrix
			cal_red		= (uncal_red*a11 + uncal_green*a12 + uncal_blue*a13) >> shift_fact;
			cal_green	= (uncal_red*a21 + uncal_green*a22 + uncal_blue*a23) >> shift_fact;
			cal_blue	= (uncal_red*a31 + uncal_green*a32 + uncal_blue*a33) >> shift_fact;
			
			// range check
			if(cal_red > pix_max_val)		cal_red = pix_max_val;
			else if(cal_red < 0)			cal_red = 0;
			
			if(cal_green > pix_max_val)		cal_green = pix_max_val;
			else if(cal_green < 0)			cal_green = 0;
			
			if(cal_blue > pix_max_val)		cal_blue = pix_max_val;
			else if(cal_blue < 0)			cal_blue = 0;
			
			// save calibrated color values at output image
			img_calib[index] = cal_red;
			img_calib[index+1] = cal_green;
			img_calib[index+2] = cal_blue;
			
			index += 3;
			
			
		}
	}
}