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"cxx . . $ `<file>/include/frei0r.h`/
30 #include "frei0r/math.h"cxx . . $ `<file>/include/frei0r/math.h`/
31
32 double PIcxx . . $ PI. = 3.14159;
33 double pixelScalecxx . . $ pixelScale. = 255.9;
34
35 typedef struct emboss_instancecxx . . $ emboss_instance#
36 {
37 unsigned int widthcxx . . $ emboss_instance#width.;
38 unsigned int heightcxx . . $ emboss_instance#height.;
39 double azimuthcxx . . $ emboss_instance#azimuth.;
40 double elevationcxx . . $ emboss_instance#elevation.;
41 double width45cxx . . $ emboss_instance#width45.;
42 } emboss_instance_tcxx . . $ emboss_instance_t#;
43
44 int f0r_initcxx . . $ f0r_init(b126dc7c1de90089).()
45 {
46 return 1;
47 }
48
49 void f0r_deinitcxx . . $ f0r_deinit(49f6e7a06ebc5aa8).()
50 { /* no initialization required */ }
51
52 void f0r_get_plugin_infocxx . . $ f0r_get_plugin_info(35d753456c0ea59c).(f0r_plugin_info_tcxx . . $ f0r_plugin_info_t#* emposs_info)
53 {
54 emposs_info->namecxx . . $ f0r_plugin_info#name. = "emboss";
55 emposs_info->authorcxx . . $ f0r_plugin_info#author. = "Janne Liljeblad";
56 emposs_info->plugin_typecxx . . $ f0r_plugin_info#plugin_type. = F0R_PLUGIN_TYPE_FILTERcxx . . $ `include/frei0r.h:247:9`!;
57 emposs_info->color_modelcxx . . $ f0r_plugin_info#color_model. = F0R_COLOR_MODEL_RGBA8888cxx . . $ `include/frei0r.h:308:9`!;
58 emposs_info->frei0r_versioncxx . . $ f0r_plugin_info#frei0r_version. = FREI0R_MAJOR_VERSIONcxx . . $ `include/frei0r.h:217:9`!;
59 emposs_info->major_versioncxx . . $ f0r_plugin_info#major_version. = 0;
60 emposs_info->minor_versioncxx . . $ f0r_plugin_info#minor_version. = 1;
61 emposs_info->num_paramscxx . . $ f0r_plugin_info#num_params. = 3;
62 emposs_info->explanationcxx . . $ f0r_plugin_info#explanation. = "Creates embossed relief image of source image";
63 }
64
65 void f0r_get_param_infocxx . . $ f0r_get_param_info(a9f9aa7954cc4a37).(f0r_param_info_tcxx . . $ f0r_param_info_t#* info, int param_index)
66 {
67 switch(param_index)
68 {
69 case 0:
70 info->namecxx . . $ f0r_param_info#name. = "azimuth";
71 info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!;
72 info->explanationcxx . . $ f0r_param_info#explanation. = "Light direction";
73 break;
74 case 1:
75 info->namecxx . . $ f0r_param_info#name. = "elevation";
76 info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!;
77 info->explanationcxx . . $ f0r_param_info#explanation. = "Background lightness";
78 break;
79 case 2:
80 info->namecxx . . $ f0r_param_info#name. = "width45";
81 info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!;
82 info->explanationcxx . . $ f0r_param_info#explanation. = "Bump height";
83 break;
84 }
85 }
86
87 f0r_instance_tcxx . . $ f0r_instance_t# f0r_constructcxx . . $ f0r_construct(da3f471cd007a696).(unsigned int width, unsigned int height)
88 {
89 emboss_instance_tcxx . . $ emboss_instance_t#* inst = (emboss_instance_tcxx . . $ emboss_instance_t#*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*inst));
90 inst->widthcxx . . $ emboss_instance#width. = width;
91 inst->heightcxx . . $ emboss_instance#height. = height;
92 inst->azimuthcxx . . $ emboss_instance#azimuth. = 135.0 / 360.0; //input range 0 - 1 will be interpreted as angle 0 - 360
93 inst->elevationcxx . . $ emboss_instance#elevation. = 30.0 / 90.0;//input range 0 - 1 will be interpreted as lighness value 0 - 90
94 inst->width45cxx . . $ emboss_instance#width45. = 10.0 / 40.0;//input range 0 - 1 will be interpreted as bump height value 1 - 40
95 return (f0r_instance_tcxx . . $ f0r_instance_t#)inst;
96 }
97
98 void f0r_destructcxx . . $ f0r_destruct(5d22bdacc48458e8).(f0r_instance_tcxx . . $ f0r_instance_t# instance)
99 {
100 freecxx . . $ free(5d22bdacc48458e8).(instance);
101 }
102
103 void f0r_set_param_valuecxx . . $ f0r_set_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance,
104 f0r_param_tcxx . . $ f0r_param_t# param, int param_index)
105 {
106 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance);
107 emboss_instance_tcxx . . $ emboss_instance_t#* inst = (emboss_instance_tcxx . . $ emboss_instance_t#*)instance;
108
109 switch(param_index)
110 {
111 case 0:
112 inst->azimuthcxx . . $ emboss_instance#azimuth. = *((double*)param);
113 break;
114 case 1:
115 inst->elevationcxx . . $ emboss_instance#elevation. = *((double*)param);
116 break;
117 case 2:
118 inst->width45cxx . . $ emboss_instance#width45. = *((double*)param);
119 break;
120 }
121 }
122
123 void f0r_get_param_valuecxx . . $ f0r_get_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance,
124 f0r_param_tcxx . . $ f0r_param_t# param, int param_index)
125 {
126 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance);
127 emboss_instance_tcxx . . $ emboss_instance_t#* inst = (emboss_instance_tcxx . . $ emboss_instance_t#*)instance;
128
129 switch(param_index)
130 {
131 case 0:
132 *((double*)param) = inst->azimuthcxx . . $ emboss_instance#azimuth.;
133 break;
134 case 1:
135 *((double*)param) = inst->elevationcxx . . $ emboss_instance#elevation.;
136 break;
137 case 2:
138 *((double*)param) = inst->width45cxx . . $ emboss_instance#width45.;
139 break;
140 }
141 }
142
143 void f0r_updatecxx . . $ f0r_update(96cd7b27f2fca8a7).(f0r_instance_tcxx . . $ f0r_instance_t# instance, double time,
144 const uint32_tcxx . . $ uint32_t#* inframe, uint32_tcxx . . $ uint32_t#* outframe)
145 {
146 // Check and cast instance
147 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance);
148 emboss_instance_tcxx . . $ emboss_instance_t#* inst = (emboss_instance_tcxx . . $ emboss_instance_t#*)instance;
149
150 // Get render params values 0.0-1.0 in range used by filter
151 double azimuthInput = inst->azimuthcxx . . $ emboss_instance#azimuth. * 360.0;
152 double elevationInput = inst->elevationcxx . . $ emboss_instance#elevation. * 90.0;
153 double widthInput = inst->width45cxx . . $ emboss_instance#width45. * 40.0;
154
155 // Force correct ranges on input
156 azimuthInput = CLAMPcxx . . $ `include/frei0r/math.h:48:9`!(azimuthInput, 0.0, 360.0);
157 elevationInput = CLAMPcxx . . $ `include/frei0r/math.h:48:9`!(elevationInput, 0.0, 90.0);
158 widthInput = CLAMPcxx . . $ `include/frei0r/math.h:48:9`!(widthInput, 1.0, 40.0);
159
160 // Convert to filter input values
161 double azimuth = azimuthInput * PIcxx . . $ PI. / 180.0;
162 double elevation = elevationInput * PIcxx . . $ PI. / 180.0;
163 double width45 = widthInput;
164
165 // Create brightness image
166 unsigned int len = inst->widthcxx . . $ emboss_instance#width. * inst->heightcxx . . $ emboss_instance#height.;
167 unsigned char *bumpPixels=malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(len);
168 unsigned char *alphaVals=malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(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) * pixelScalecxx . . $ pixelScale.);
188 Ly = (int)(sin(azimuth) * cos(elevation) * pixelScalecxx . . $ pixelScale.);
189 Lz = (int)(sin(elevation) * pixelScalecxx . . $ 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->widthcxx . . $ emboss_instance#width.;
198 int height = inst->heightcxx . . $ emboss_instance#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 freecxx . . $ free(5d22bdacc48458e8).(alphaVals);
232 freecxx . . $ free(5d22bdacc48458e8).(bumpPixels);
233 }
234