Debian Codegraph @

src/filter/emboss/emboss.c

raw symbols SCIP
1 /*
2 * This file is a port of com.jhlabs.image.EmbossFilter.java
3 * Copyright 2006 Jerry Huxtable
4 *
5 * emboss.c
6 * Copyright 2012 Janne Liljeblad
7 *
8 * This file is a Frei0r plugin.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include <stdlib.h>
26 #include <assert.h>
27 #include <math.h>
28
29 #include "frei0r.h"
30 #include "frei0r/math.h"
31
32 double PI = 3.14159;
33 double pixelScale = 255.9;
34
35 typedef struct emboss_instance
36 {
37 unsigned int width;
38 unsigned int height;
39 double azimuth;
40 double elevation;
41 double width45;
42 } emboss_instance_t;
43
44 int f0r_init()
45 {
46 return 1;
47 }
48
49 void f0r_deinit()
50 { /* no initialization required */ }
51
52 void f0r_get_plugin_info(f0r_plugin_info_t* emposs_info)
53 {
54 emposs_info->name = "emboss";
55 emposs_info->author = "Janne Liljeblad";
56 emposs_info->plugin_type = F0R_PLUGIN_TYPE_FILTER;
57 emposs_info->color_model = F0R_COLOR_MODEL_RGBA8888;
58 emposs_info->frei0r_version = FREI0R_MAJOR_VERSION;
59 emposs_info->major_version = 0;
60 emposs_info->minor_version = 1;
61 emposs_info->num_params = 3;
62 emposs_info->explanation = "Creates embossed relief image of source image";
63 }
64
65 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
66 {
67 switch(param_index)
68 {
69 case 0:
70 info->name = "azimuth";
71 info->type = F0R_PARAM_DOUBLE;
72 info->explanation = "Light direction";
73 break;
74 case 1:
75 info->name = "elevation";
76 info->type = F0R_PARAM_DOUBLE;
77 info->explanation = "Background lightness";
78 break;
79 case 2:
80 info->name = "width45";
81 info->type = F0R_PARAM_DOUBLE;
82 info->explanation = "Bump height";
83 break;
84 }
85 }
86
87 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
88 {
89 emboss_instance_t* inst = (emboss_instance_t*)calloc(1, sizeof(*inst));
90 inst->width = width;
91 inst->height = height;
92 inst->azimuth = 135.0 / 360.0; //input range 0 - 1 will be interpreted as angle 0 - 360
93 inst->elevation = 30.0 / 90.0;//input range 0 - 1 will be interpreted as lighness value 0 - 90
94 inst->width45 = 10.0 / 40.0;//input range 0 - 1 will be interpreted as bump height value 1 - 40
95 return (f0r_instance_t)inst;
96 }
97
98 void f0r_destruct(f0r_instance_t instance)
99 {
100 free(instance);
101 }
102
103 void f0r_set_param_value(f0r_instance_t instance,
104 f0r_param_t param, int param_index)
105 {
106 assert(instance);
107 emboss_instance_t* inst = (emboss_instance_t*)instance;
108
109 switch(param_index)
110 {
111 case 0:
112 inst->azimuth = *((double*)param);
113 break;
114 case 1:
115 inst->elevation = *((double*)param);
116 break;
117 case 2:
118 inst->width45 = *((double*)param);
119 break;
120 }
121 }
122
123 void f0r_get_param_value(f0r_instance_t instance,
124 f0r_param_t param, int param_index)
125 {
126 assert(instance);
127 emboss_instance_t* inst = (emboss_instance_t*)instance;
128
129 switch(param_index)
130 {
131 case 0:
132 *((double*)param) = inst->azimuth;
133 break;
134 case 1:
135 *((double*)param) = inst->elevation;
136 break;
137 case 2:
138 *((double*)param) = inst->width45;
139 break;
140 }
141 }
142
143 void f0r_update(f0r_instance_t instance, double time,
144 const uint32_t* inframe, uint32_t* outframe)
145 {
146 // Check and cast instance
147 assert(instance);
148 emboss_instance_t* inst = (emboss_instance_t*)instance;
149
150 // Get render params values 0.0-1.0 in range used by filter
151 double azimuthInput = inst->azimuth * 360.0;
152 double elevationInput = inst->elevation * 90.0;
153 double widthInput = inst->width45 * 40.0;
154
155 // Force correct ranges on input
156 azimuthInput = CLAMP(azimuthInput, 0.0, 360.0);
157 elevationInput = CLAMP(elevationInput, 0.0, 90.0);
158 widthInput = CLAMP(widthInput, 1.0, 40.0);
159
160 // Convert to filter input values
161 double azimuth = azimuthInput * PI / 180.0;
162 double elevation = elevationInput * PI / 180.0;
163 double width45 = widthInput;
164
165 // Create brightness image
166 unsigned int len = inst->width * inst->height;
167 unsigned char *bumpPixels=malloc(len);
168 unsigned char *alphaVals=malloc(len);
169 unsigned int index = 0, r = 0, g = 0, b = 0, a = 0;
170 const unsigned char* src = (unsigned char*)inframe;
171 while (len--)
172 {
173 r = *src++;
174 g = *src++;
175 b = *src++;
176 a = *src++;
177
178 bumpPixels[index++] = (r + g + b)/3;
179 alphaVals[index - 1] = a;
180 }
181
182 // Create embossed image from brightness image
183 unsigned char* dst = (unsigned char*)outframe;
184 unsigned char shade, background = 0;
185 int Nx, Ny, Nz, Lx, Ly, Lz, Nz2, NzLz, NdotL;
186
187 Lx = (int)(cos(azimuth) * cos(elevation) * pixelScale);
188 Ly = (int)(sin(azimuth) * cos(elevation) * pixelScale);
189 Lz = (int)(sin(elevation) * pixelScale);
190
191 Nz = (int)(6 * 255 / width45);
192 Nz2 = Nz * Nz;
193 NzLz = Nz * Lz;
194
195 background = Lz;
196 int bumpIndex = 0;
197 int width = inst->width;
198 int height = inst->height;
199 int s1, s2, s3 = 0;
200 int x, y = 0;
201 for (y = 0; y < height; y++, bumpIndex += width)
202 {
203 s1 = bumpIndex;
204 s2 = s1 + width;
205 s3 = s2 + width;
206 for (x = 0; x < width; x++, s1++, s2++, s3++)
207 {
208 if (y != 0 && y < height-2 && x != 0 && x < width-2)
209 {
210 Nx = bumpPixels[s1-1] + bumpPixels[s2-1] + bumpPixels[s3-1] - bumpPixels[s1+1] - bumpPixels[s2+1] - bumpPixels[s3+1];
211 Ny = bumpPixels[s3-1] + bumpPixels[s3] + bumpPixels[s3+1] - bumpPixels[s1-1] - bumpPixels[s1] - bumpPixels[s1+1];
212 if (Nx == 0 && Ny == 0)
213 shade = background;
214 else if ((NdotL = Nx*Lx + Ny*Ly + NzLz) < 0)
215 shade = 0;
216 else
217 shade = (int)(NdotL / sqrt(Nx*Nx + Ny*Ny + Nz2));
218 }
219 else
220 {
221 shade = background;
222 }
223
224 // Write value
225 *dst++ = shade;
226 *dst++ = shade;
227 *dst++ = shade;
228 *dst++ = alphaVals[s1]; //copy alpha
229 }
230 }
231 free(alphaVals);
232 free(bumpPixels);
233 }
234