aboutsummaryrefslogtreecommitdiffstats
path: root/gamma_corr.c
blob: 7b344c1f1603f8d50404d131e98966adc9da8fcc (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
/**
* @file			gamma_corr.c
* @brief		gamma correction algorithm
* @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>
*
*/

#include <stdio.h>
#include <string.h>
#include <math.h>

#include "color_pipe_private.h"


/**
 * Apply gamma correction to given image type: RGB or monochrome with 8 bit per color channel
 * 
 * @param img_rgb On return: gamma corrected image
 * @param img_in image to be gamma corrected
 * @param height image height in number of pixels
 * @param width image width in number of pixels
 * @param max_val maximum pixel value
 * @param gamma gamma coefficient
 * @param is_color 1 if it's a color image, 0 if it's a monochrome image
 * @return 0 on success otherwise -1
 */
static void gamma_corr8(uint8_t *img_rgb, const uint8_t *img_in, const int height, const int width, const int *gamma_table, const int is_color)
#include "alg_gamma.h"


/**
 * Apply gamma correction to given image type: RGB or monochrome with 16 bit per color channel
 * 
 * @param img_rgb On return: gamma corrected image
 * @param img_in image to be gamma corrected
 * @param height image height in number of pixels
 * @param width image width in number of pixels
 * @param max_val maximum pixel value
 * @param gamma gamma coefficient
 * @param is_color 1 if it's a color image, 0 if it's a monochrome image
 * @return 0 on success otherwise -1
 */
static void gamma_corr16(uint16_t *img_rgb, const uint16_t *img_in, const int height, const int width, const int *gamma_table, const int is_color)
#include "alg_gamma.h"


/**
 * Apply gamma correction to given image type.
 * 
 * @param gamma_data required data for gamma correction
 * @return 0 on success otherwise -1
 */
int gamma_corr(struct gamma_data_t *gamma_data) {
	
	void *img_gamma, *img_in;
	int *gamma_table;
	int i, is_color, bit_channel, width, height, max_pix, gamma_table_bitdepth;
	float gamma, gamma_table_init;
	
	
	// put variables on stack
	is_color = gamma_data->is_color;
	img_gamma = gamma_data->img_gamma;
	img_in = gamma_data->img_in;
	bit_channel = gamma_data->bit_channel;
	width = gamma_data->width;
	height = gamma_data->height;
	gamma = gamma_data->gamma;
	max_pix = (1<<bit_channel)-1;
	gamma_table_bitdepth = gamma_data->gamma_table_bitdepth;
	gamma_table_init = gamma_data->gamma_table_init;
	gamma_table = gamma_data->gamma_table;
	
	/*
	 * Do check whether lookup table is initialzed with correct gamma value
	 */
	if(gamma_table_bitdepth != bit_channel || gamma != gamma_table_init) {
		for(i = 0; i <= max_pix; i++) {
			gamma_table[i] = roundf(max_pix*pow(i/((float)max_pix), gamma));
		}
		gamma_data->gamma_table_bitdepth = bit_channel;
		gamma_data->gamma_table_init = gamma;
	}
	
	if(bit_channel <= 8) {
		gamma_corr8(img_gamma, img_in, height, width, gamma_table, is_color);
	}
	else if(bit_channel <= 16) {
		gamma_corr16(img_gamma, img_in, height, width, gamma_table, is_color);
	}
	return 0;
}