Debian Codegraph @

src/filter/heatmap0r/heatmap0r.c

raw symbols SCIP
1 /*
2 * heatmap0r.c -- feel the heat, frei0r style.
3 *
4 * Copyright (C) 2025 Cynthia ([email protected])
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <stdlib.h>
22 #include <assert.h>
23
24 #include "frei0r.h"
25 #include "frei0r/math.h"
26
27 typedef struct {
28 unsigned int width, height;
29 uint8_t rlut[256], glut[256], blut[256];
30
31 f0r_param_color_t black, grey, white;
32 double gp; //< Grey point
33 } heatmap0r_t;
34
35 static void gen_heatmap0r_luts(heatmap0r_t *inst)
36 {
37 double luma;
38 double L0, L1, L2;
39 double gp = inst->gp;
40 double l0 = 1.0 / gp,
41 l1 = 1.0 / (gp * (gp - 1.0)),
42 l2 = 1.0 / (1.0 - gp);
43
44 f0r_param_color_t black = inst->black,
45 grey = inst->grey,
46 white = inst->white;
47
48 for (int i = 0; i < 256; ++i)
49 {
50 luma = 1.0 / 255.0 * (double)i;
51
52 L0 = (luma - gp) * (luma - 1.0) * l0;
53 L1 = luma * (luma - 1.0) * l1;
54 L2 = luma * (luma - gp) * l2;
55
56 inst->rlut[i] = CLAMP(255.0 * (L0 * black.r + L1 * grey.r + L2 * white.r), 0, 255);
57 inst->glut[i] = CLAMP(255.0 * (L0 * black.g + L1 * grey.g + L2 * white.g), 0, 255);
58 inst->blut[i] = CLAMP(255.0 * (L0 * black.b + L1 * grey.b + L2 * white.b), 0, 255);
59 }
60 }
61
62 int f0r_init()
63 {
64 return 1;
65 }
66
67 void f0r_deinit() {}
68
69 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
70 {
71 heatmap0r_t* inst = calloc(1, sizeof(*inst));
72
73 inst->width = width; inst->height = height;
74 inst->gp = 0.5;
75 inst->white = (f0r_param_color_t) {/* Yellow */ 1.0, 1.0, 0.0};
76 inst->grey = (f0r_param_color_t) {/* Deeppink */ 1.0, 0.0, 0.5};
77 inst->black = (f0r_param_color_t) {/* Darkviolet */ 0.27, 0.0, 0.5};
78
79 gen_heatmap0r_luts (inst);
80
81 return (f0r_instance_t)inst;
82 }
83
84 void f0r_destruct(f0r_instance_t instance)
85 {
86 free(instance);
87 }
88
89 void f0r_get_plugin_info(f0r_plugin_info_t* info)
90 {
91 info->name = "heatmap0r";
92 info->author = "Cynthia";
93 info->plugin_type = F0R_PLUGIN_TYPE_FILTER;
94 info->color_model = F0R_COLOR_MODEL_RGBA8888;
95 info->frei0r_version = FREI0R_MAJOR_VERSION;
96 info->major_version = 0;
97 info->minor_version = 1;
98 info->num_params = 4;
99 info->explanation = "Performs a continuous trichromatic tinting";
100 }
101
102 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
103 {
104 switch(param_index)
105 {
106 case 0:
107 info->name = "Map black to";
108 info->type = F0R_PARAM_COLOR;
109 info->explanation = "The color to map source color with zero luminance";
110 break;
111 case 1:
112 info->name = "Map grey to";
113 info->type = F0R_PARAM_COLOR;
114 info->explanation = "The color to map source color with mid luminance";
115 break;
116 case 2:
117 info->name = "Map white to";
118 info->type = F0R_PARAM_COLOR;
119 info->explanation = "The color to map source color with full luminance";
120 break;
121 case 3:
122 info->name = "Grey point";
123 info->type = F0R_PARAM_DOUBLE;
124 info->explanation = "Point in the luminance axis grey color is located";
125 break;
126 }
127 }
128
129 void f0r_set_param_value(f0r_instance_t instance,
130 f0r_param_t param, int param_index)
131 {
132 assert(instance);
133 heatmap0r_t* inst = (heatmap0r_t*)instance;
134
135 switch(param_index)
136 {
137 case 0:
138 inst->black = *((f0r_param_color_t *)param);
139 break;
140 case 1:
141 inst->grey = *((f0r_param_color_t *)param);
142 break;
143 case 2:
144 inst->white = *((f0r_param_color_t *)param);
145 break;
146 case 3:
147 inst->gp = *((double *)param);
148 break;
149 }
150
151 gen_heatmap0r_luts (inst);
152 }
153
154 void f0r_get_param_value(f0r_instance_t instance,
155 f0r_param_t param, int param_index)
156 {
157 assert(instance);
158 heatmap0r_t* inst = (heatmap0r_t*)instance;
159
160 switch(param_index)
161 {
162 case 0:
163 *((f0r_param_color_t*)param) = inst->black;
164 break;
165 case 1:
166 *((f0r_param_color_t*)param) = inst->grey;
167 break;
168 case 2:
169 *((f0r_param_color_t*)param) = inst->white;
170 break;
171 case 3:
172 *((double *)param) = inst->gp;
173 break;
174 }
175 }
176
177 void f0r_update(f0r_instance_t instance, double time,
178 const uint32_t* inframe, uint32_t* outframe)
179 {
180 assert(instance);
181 heatmap0r_t* inst = (heatmap0r_t*)instance;
182 unsigned int len = inst->width * inst->height;
183
184 const unsigned char* src = (void*)inframe;
185 unsigned char* dst = (void*)outframe;
186
187 unsigned char luma, r, g, b;
188 while (len--)
189 {
190 r = *src++;
191 g = *src++;
192 b = *src++;
193
194 luma = 0.299 * r + 0.587 * g + 0.114 * b;
195
196 *dst++ = inst->rlut[luma];
197 *dst++ = inst->glut[luma];
198 *dst++ = inst->blut[luma];
199 *dst++ = *src++;
200 }
201 }