File: gamma.c

package info (click to toggle)
v4l-utils 1.32.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,276 kB
  • sloc: ansic: 85,528; cpp: 69,473; perl: 11,915; sh: 1,333; python: 883; php: 119; makefile: 39
file content (62 lines) | stat: -rw-r--r-- 1,873 bytes parent folder | download | duplicates (3)
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
/*
#             (C) 2008-2009 Hans de Goede <hdegoede@redhat.com>

# This program 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 program 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 program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335  USA
 */

#include <math.h>
#include "libv4lprocessing.h"
#include "libv4lprocessing-priv.h"

#define CLIP(color) (unsigned char)(((color) > 0xff) ? 0xff : (((color) < 0) ? 0 : (color)))

static int gamma_active(struct v4lprocessing_data *data)
{
	int gamma = v4lcontrol_get_ctrl(data->control, V4LCONTROL_GAMMA);

	return gamma && gamma != 1000;
}

static int gamma_calculate_lookup_tables(
		struct v4lprocessing_data *data,
		unsigned char *buf, const struct v4l2_format *fmt)
{
	int i, x, gamma;

	gamma = v4lcontrol_get_ctrl(data->control, V4LCONTROL_GAMMA);

	if (gamma == 0)
		return 0;

	if (gamma != data->last_gamma) {
		for (i = 0; i < 256; i++) {
			x = powf(i / 255.0, 1000.0 / gamma) * 255;
			data->gamma_table[i] = CLIP(x);
		}
		data->last_gamma = gamma;
	}

	for (i = 0; i < 256; i++) {
		data->comp1[i] = data->gamma_table[data->comp1[i]];
		data->green[i] = data->gamma_table[data->green[i]];
		data->comp2[i] = data->gamma_table[data->comp2[i]];
	}

	return 1;
}

const struct v4lprocessing_filter gamma_filter = {
	gamma_active, gamma_calculate_lookup_tables
};