Debian Codegraph @

src/filter/brightness/brightness.c

raw symbols SCIP
1 /* brightness.c
2 * Copyright (C) 2004 Jean-Sebastien Senecal ([email protected])
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 <assert.h>
22
23 #include "frei0r.h"
24 #include "frei0r/math.h"
25
26 typedef struct brightness_instance
27 {
28 unsigned int width;
29 unsigned int height;
30 int brightness; /* the brightness [-256, 256] */
31 unsigned char lut[256]; /* look-up table */
32 } brightness_instance_t;
33
34 /* Updates the look-up-table. */
35 void update_lut(brightness_instance_t *inst)
36 {
37 int i;
38 unsigned char *lut = inst->lut;
39 int brightness = inst->brightness;
40
41 if (brightness < 0)
42 {
43 for (i=0; i<256; ++i)
44 lut[i] = CLAMP0255((i * (256 + brightness))>>8);
45 }
46 else
47 {
48 for (i=0; i<256; ++i)
49 lut[i] = CLAMP0255(i + (((256 - i) * brightness)>>8));
50 }
51 }
52
53 int f0r_init()
54 {
55 return 1;
56 }
57
58 void f0r_deinit()
59 { /* no initialization required */ }
60
61 void f0r_get_plugin_info(f0r_plugin_info_t* brightness_info)
62 {
63 brightness_info->name = "Brightness";
64 brightness_info->author = "Jean-Sebastien Senecal";
65 brightness_info->plugin_type = F0R_PLUGIN_TYPE_FILTER;
66 brightness_info->color_model = F0R_COLOR_MODEL_RGBA8888;
67 brightness_info->frei0r_version = FREI0R_MAJOR_VERSION;
68 brightness_info->major_version = 0;
69 brightness_info->minor_version = 2;
70 brightness_info->num_params = 1;
71 brightness_info->explanation = "Adjusts the brightness of a source image";
72 }
73
74 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
75 {
76 switch(param_index)
77 {
78 case 0:
79 info->name = "Brightness";
80 info->type = F0R_PARAM_DOUBLE;
81 info->explanation = "The brightness value";
82 break;
83 }
84 }
85
86 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
87 {
88 brightness_instance_t* inst = (brightness_instance_t*)calloc(1, sizeof(*inst));
89 inst->width = width; inst->height = height;
90 /* init look-up-table */
91 update_lut(inst);
92 return (f0r_instance_t)inst;
93 }
94
95 void f0r_destruct(f0r_instance_t instance)
96 {
97 free(instance);
98 }
99
100 void f0r_set_param_value(f0r_instance_t instance,
101 f0r_param_t param, int param_index)
102 {
103 assert(instance);
104 brightness_instance_t* inst = (brightness_instance_t*)instance;
105
106 switch(param_index)
107 {
108 int val;
109 case 0:
110 /* contrast */
111 val = (int) ((*((double*)param) - 0.5) * 512.0); /* remap to [-256, 256] */
112 if (val != inst->brightness)
113 {
114 inst->brightness = val;
115 update_lut(inst);
116 }
117 break;
118 }
119 }
120
121 void f0r_get_param_value(f0r_instance_t instance,
122 f0r_param_t param, int param_index)
123 {
124 assert(instance);
125 brightness_instance_t* inst = (brightness_instance_t*)instance;
126
127 switch(param_index)
128 {
129 case 0:
130 *((double*)param) = (double) ( (inst->brightness + 256.0) / 512.0 );
131 break;
132 }
133 }
134
135 void f0r_update(f0r_instance_t instance, double time,
136 const uint32_t* inframe, uint32_t* outframe)
137 {
138 assert(instance);
139 brightness_instance_t* inst = (brightness_instance_t*)instance;
140 unsigned int len = inst->width * inst->height;
141
142 unsigned char* lut = inst->lut;
143 unsigned char* dst = (unsigned char*)outframe;
144 const unsigned char* src = (unsigned char*)inframe;
145 while (len--)
146 {
147 *dst++ = lut[*src++];
148 *dst++ = lut[*src++];
149 *dst++ = lut[*src++];
150 *dst++ = *src++;// copy alpha
151 }
152 }
153