Debian Codegraph @

src/filter/vignette/vignette.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2011 Simon Andreas Eugster ([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 #if defined(_MSC_VER)
21 #define _USE_MATH_DEFINES
22 #endif /* _MSC_VER */
23 #include <cmath>
24 #include "frei0r.hpp"
25 #include "frei0r/math.h"
26
27
28 /**
29 Lens vignetting effect.
30
31 This effect simulates «natural vignetting» whose light falloff can be described
32 with a cos⁴ curve. Additionally the x:y aspect ratio of the vignette can be
33 changed (note that normal cameras with a round aperture always have an aspect ratio
34 of 1:1, but for cinematic effects the aspect ratio is often adjusted to match
35 the frame's aspect ratio). The ClearCenter value allows one to shift the
36 vignetting away from the center, preserving it from changes.
37
38 */
39 class Vignette : public frei0r::filter
40 {
41
42 public:
43
44 double m_aspect; ///< Neutral value: 0.5
45 double m_cc; ///< Neutral value: 0
46 double m_soft; ///< Suggested value: 0.6
47
48 Vignette(unsigned int width, unsigned int height) :
49 m_width(width),
50 m_height(height)
51 {
52 register_param(m_aspect, "aspect", "Aspect ratio");
53 register_param(m_cc, "clearCenter", "Size of the unaffected center");
54 register_param(m_soft, "soft", "Softness");
55
56 // Suggested default values
57 m_aspect = .5;
58 m_cc = 0;
59 m_soft = .6;
60
61 m_initialized = width*height > 0;
62 if (m_initialized) {
63 m_vignette = new float[width*height];
64 updateVignette();
65 }
66 }
67
68 ~Vignette()
69 {
70 if (m_initialized) {
71 delete[] m_vignette;
72 }
73 }
74
75 virtual void update(double time,
76 uint32_t* out,
77 const uint32_t* in)
78 {
79 std::copy(in, in + m_width*m_height, out);
80
81 // Rebuild the vignette matrix if a parameter has changed
82 if (m_prev_aspect != m_aspect
83 || m_prev_cc != m_cc
84 || m_prev_soft != m_soft) {
85 updateVignette();
86 }
87
88 unsigned char *pixel = (unsigned char *) in;
89 unsigned char *dest = (unsigned char *) out;
90
91 // Darken the pixels by multiplying with the vignette's factor
92 float *vignette = m_vignette;
93 for (unsigned int i = 0; i < size; i++) {
94 *dest++ = (char) (*vignette * *pixel++);
95 *dest++ = (char) (*vignette * *pixel++);
96 *dest++ = (char) (*vignette * *pixel++);
97 *dest++ = *pixel++;
98 vignette++;
99 }
100
101 }
102
103 private:
104 double m_prev_aspect;
105 double m_prev_cc;
106 double m_prev_soft;
107
108 float *m_vignette;
109 bool m_initialized;
110
111 unsigned int m_width;
112 unsigned int m_height;
113
114 void updateVignette()
115 {
116 // std::cout << "New settings: aspect = " << m_aspect << ", clear center = " << m_cc << ", soft = " << m_soft << std::endl;
117 m_prev_aspect = m_aspect;
118 m_prev_cc = m_cc;
119 m_prev_soft = m_soft;
120
121 float soft = 5*std::pow(float(1)-m_soft,2)+.01;
122 float scaleX = 1;
123 float scaleY = 1;
124
125 // Distance from 0.5 (\in [0,0.5]) scaled to [0,1]
126 float scale = std::fabs(m_aspect-.5)*2;
127 // Map scale to [0,5] in a way that values near 0 can be adjusted more precisely
128 scale = 1 + 4*std::pow(scale, 3);
129 // Scale either x or y, depending on the aspect value being above or below 0.5
130 if (m_aspect > 0.5) {
131 scaleX = scale;
132 } else {
133 scaleY = scale;
134 }
135 // std::cout << "Used values: soft=" << soft << ", x=" << scaleX << ", y=" << scaleY << std::endl;
136
137 int cx = m_width/2;
138 int cy = m_height/2;
139 float rmax = std::sqrt(std::pow(float(cx), 2) + std::pow(float(cy), 2));
140 float r;
141
142 for (int y = 0; y < m_height; y++) {
143 for (int x = 0; x < m_width; x++) {
144
145 // Euclidean distance to the center, normalized to [0,1]
146 r = std::sqrt(std::pow(scaleX*(x-cx), 2) + std::pow(scaleY*(y-cy), 2))/rmax;
147
148 // Subtract the clear center
149 r -= m_cc;
150
151 if (r <= 0) {
152 // Clear center: Do not modify the brightness here
153 m_vignette[y*m_width+x] = 1;
154 } else {
155 r *= soft;
156 if (r > M_PI_2) {
157 m_vignette[y*m_width+x] = 0;
158 } else {
159 m_vignette[y*m_width+x] = std::pow(std::cos(r), 4);
160 }
161 }
162
163 }
164 }
165
166
167 }
168
169 };
170
171
172
173 frei0r::construct<Vignette> plugin("Vignette",
174 "Lens vignetting effect, applies natural vignetting",
175 "Simon A. Eugster (Granjow)",
176 0,2,
177 F0R_COLOR_MODEL_RGBA8888);