Debian Codegraph @

src/filter/gamma/gamma.c

raw symbols SCIP
1 /* gamma.c
2 * Copyright (C) 2004 Jean-Sebastien Senecal
3 * This file is a Frei0r plugin.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <stdlib.h>
21 #include <math.h>
22 #include <assert.h>
23
24 #include "frei0r.h"
25 #include "frei0r/math.h"
26
27 #define MAX_GAMMA 4.0
28
29 typedef struct gamma_instance
30 {
31 unsigned int width;
32 unsigned int height;
33 double gamma; /* the gamma value [0, 1] */
34 unsigned char lut[256]; /* look-up table */
35 } gamma_instance_t;
36
37 /* Updates the look-up-table. */
38 void update_lut(gamma_instance_t *inst)
39 {
40 int i;
41 unsigned char *lut = inst->lut;
42 double inv_gamma = 1.0 / (inst->gamma * MAX_GAMMA); /* set gamma in the range [0,MAX_GAMMA] and take its inverse */
43
44 lut[0] = 0;
45 for (i=1; i<256; ++i)
46 lut[i] = CLAMP0255( ROUND(255.0 * pow( (double)i / 255.0, inv_gamma ) ) );
47 }
48
49 int f0r_init()
50 {
51 return 1;
52 }
53
54 void f0r_deinit()
55 { /* no initialization required */ }
56
57 void f0r_get_plugin_info(f0r_plugin_info_t* gamma_info)
58 {
59 gamma_info->name = "Gamma";
60 gamma_info->author = "Jean-Sebastien Senecal";
61 gamma_info->plugin_type = F0R_PLUGIN_TYPE_FILTER;
62 gamma_info->color_model = F0R_COLOR_MODEL_RGBA8888;
63 gamma_info->frei0r_version = FREI0R_MAJOR_VERSION;
64 gamma_info->major_version = 0;
65 gamma_info->minor_version = 2;
66 gamma_info->num_params = 1;
67 gamma_info->explanation = "Adjusts the gamma value of a source image";
68 }
69
70 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
71 {
72 switch(param_index)
73 {
74 case 0:
75 info->name = "Gamma";
76 info->type = F0R_PARAM_DOUBLE;
77 info->explanation = "The gamma value";
78 break;
79 }
80 }
81
82 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
83 {
84 gamma_instance_t* inst = (gamma_instance_t*)calloc(1, sizeof(*inst));
85 inst->width = width; inst->height = height;
86 /* init look-up-table */
87 inst->gamma = 1.0;
88 update_lut(inst);
89 return (f0r_instance_t)inst;
90 }
91
92 void f0r_destruct(f0r_instance_t instance)
93 {
94 free(instance);
95 }
96
97 void f0r_set_param_value(f0r_instance_t instance,
98 f0r_param_t param, int param_index)
99 {
100 assert(instance);
101 gamma_instance_t* inst = (gamma_instance_t*)instance;
102
103 switch(param_index)
104 {
105 double val;
106 case 0:
107 /* gamma */
108 val = *((double*)param);
109 if (val != inst->gamma)
110 {
111 inst->gamma = val;
112 update_lut(inst);
113 }
114 break;
115 }
116 }
117
118 void f0r_get_param_value(f0r_instance_t instance,
119 f0r_param_t param, int param_index)
120 {
121 assert(instance);
122 gamma_instance_t* inst = (gamma_instance_t*)instance;
123
124 switch(param_index)
125 {
126 case 0:
127 *((double*)param) = inst->gamma;
128 break;
129 }
130 }
131
132 void f0r_update(f0r_instance_t instance, double time,
133 const uint32_t* inframe, uint32_t* outframe)
134 {
135 assert(instance);
136 gamma_instance_t* inst = (gamma_instance_t*)instance;
137 unsigned int len = inst->width * inst->height;
138
139 unsigned char* lut = inst->lut;
140 unsigned char* dst = (unsigned char*)outframe;
141 const unsigned char* src = (unsigned char*)inframe;
142 while (len--)
143 {
144 *dst++ = lut[*src++];
145 *dst++ = lut[*src++];
146 *dst++ = lut[*src++];
147 *dst++ = *src++;// copy alpha
148 }
149 }
150