Debian Codegraph @

src/filter/sopsat/sopsat.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2010 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 #include <math.h>
21 #include <stdlib.h>
22 #include "frei0r.hpp"cxx . . $ `<file>/include/frei0r.hpp`/
23 #include "frei0r/math.h"cxx . . $ `<file>/include/frei0r/math.h`/
24
25 /**
26 This filter implements a standard way of color correction proposed by
27 the American Society of Cinematographers: The Color Decision List, also
28 known as the
29
30 ASC CDL
31
32 More information about the ASC CDL can be found on Wikipedia[1], and
33 the current revision of the specification, including example code, can
34 be obtained by sending a mail to asc-cdl at theasc dot com. (Really works.)
35
36 The ASC CDL is a standard format for basic primary color correction (primary
37 meaning affecting the whole image and not only selected parts). Even big
38 editing systems use it :) This filter only obtains the values; Importing and
39 exporting to one of the possible ASC CDL exchange files must be done elsewhere.
40
41 Basically there are two stages in the correction:
42 1. SOP correction for each channel separately
43 2. Overall saturation correction
44 All corrections work on [0,1], so the RGB(A) values need to be transposed
45 from {0,...,255} to [0,1].
46 1. SOP correction
47 * Slope: out = in * slope; 0 <= slope < \infty
48 * Offset: out = in + offset; -\infty < offset < \infty
49 * Power: out = in^power; 0 < power < \infty
50 2. Saturation
51 * Luma: Y = 0.2126 R + 0.7152 G + 0.0722 B (according to Rec. 709)
52 * Forall channels:
53 out = luma + sat * (in-luma)
54 As the values may exceed 1 (or 0), they need to be clamped where necessary.
55
56
57 [1] http://en.wikipedia.org/wiki/Color_Decision_List
58 */
59
60 class SOPSatcxx . . $ SOPSat# : public frei0rcxx . . $ frei0r/::filtercxx . . $ frei0r/filter#
61 {
62
63 public:
64
65 double rSlopecxx . . $ SOPSat#rSlope.;
66 double gSlopecxx . . $ SOPSat#gSlope.;
67 double bSlopecxx . . $ SOPSat#bSlope.;
68 double aSlopecxx . . $ SOPSat#aSlope.;
69 double rOffsetcxx . . $ SOPSat#rOffset.;
70 double gOffsetcxx . . $ SOPSat#gOffset.;
71 double bOffsetcxx . . $ SOPSat#bOffset.;
72 double aOffsetcxx . . $ SOPSat#aOffset.;
73 double rPowercxx . . $ SOPSat#rPower.;
74 double gPowercxx . . $ SOPSat#gPower.;
75 double bPowercxx . . $ SOPSat#bPower.;
76 double aPowercxx . . $ SOPSat#aPower.;
77 double saturationcxx . . $ SOPSat#saturation.;
78
79 SOPSatcxx . . $ SOPSat#SOPSat(b4d55b1286ff73e8).(unsigned int, unsigned int)
80 {
81
82 register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(rSlopecxx . . $ SOPSat#rSlope., "rSlope"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "Slope of the red color component"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
83 register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(gSlopecxx . . $ SOPSat#gSlope., "gSlope"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "Slope of the green color component"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
84 register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(bSlopecxx . . $ SOPSat#bSlope., "bSlope"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "Slope of the blue color component"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
85 register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(aSlopecxx . . $ SOPSat#aSlope., "aSlope"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "Slope of the alpha component"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
86 register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(rOffsetcxx . . $ SOPSat#rOffset., "rOffset"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "Offset of the red color component"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
87 register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(gOffsetcxx . . $ SOPSat#gOffset., "gOffset"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "Offset of the green color component"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
88 register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(bOffsetcxx . . $ SOPSat#bOffset., "bOffset"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "Offset of the blue color component"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
89 register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(aOffsetcxx . . $ SOPSat#aOffset., "aOffset"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "Offset of the alpha component"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
90 register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(rPowercxx . . $ SOPSat#rPower., "rPower"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "Power (Gamma) of the red color component"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
91 register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(gPowercxx . . $ SOPSat#gPower., "gPower"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "Power (Gamma) of the green color component"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
92 register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(bPowercxx . . $ SOPSat#bPower., "bPower"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "Power (Gamma) of the blue color component"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
93 register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(aPowercxx . . $ SOPSat#aPower., "aPower"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "Power (Gamma) of the alpha component"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
94 register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(saturationcxx . . $ SOPSat#saturation., "saturation"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "Overall saturation"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
95 rSlopecxx . . $ SOPSat#rSlope. = 1 / 20.;
96 gSlopecxx . . $ SOPSat#gSlope. = 1 / 20.;
97 bSlopecxx . . $ SOPSat#bSlope. = 1 / 20.;
98 aSlopecxx . . $ SOPSat#aSlope. = 1 / 20.;
99 rOffsetcxx . . $ SOPSat#rOffset. = (0 - (-4)) / (double)(4 - (-4));
100 gOffsetcxx . . $ SOPSat#gOffset. = (0 - (-4)) / (double)(4 - (-4));
101 bOffsetcxx . . $ SOPSat#bOffset. = (0 - (-4)) / (double)(4 - (-4));
102 aOffsetcxx . . $ SOPSat#aOffset. = (0 - (-4)) / (double)(4 - (-4));
103 rPowercxx . . $ SOPSat#rPower. = 1 / 20.;
104 gPowercxx . . $ SOPSat#gPower. = 1 / 20.;
105 bPowercxx . . $ SOPSat#bPower. = 1 / 20.;
106 aPowercxx . . $ SOPSat#aPower. = 1 / 20.;
107 saturationcxx . . $ SOPSat#saturation. = 1 / 10.;
108
109 // Pre-build the lookup table.
110 // For 1080p, rendering a 5-second video took
111 // * 37 s without the LUT
112 // * 7 s with the LUT
113 // * 5 s without any effect applied (plain rendering).
114 // So the LUT brings about 15x speedup.
115 m_lutRcxx . . $ SOPSat#m_lutR. = (unsigned char *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(256*sizeof(char));
116 m_lutGcxx . . $ SOPSat#m_lutG. = (unsigned char *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(256*sizeof(char));
117 m_lutBcxx . . $ SOPSat#m_lutB. = (unsigned char *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(256*sizeof(char));
118 m_lutAcxx . . $ SOPSat#m_lutA. = (unsigned char *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(256*sizeof(char));
119 updateLUTcxx . . $ SOPSat#updateLUT(49f6e7a06ebc5aa8).();
120
121 }
122
123 ~cxx . . $ SOPSat#`~SOPSat`(49f6e7a06ebc5aa8).SOPSatcxx . . $ SOPSat#()
124 {
125 freecxx . . $ free(5d22bdacc48458e8).(m_lutRcxx . . $ SOPSat#m_lutR.);
126 freecxx . . $ free(5d22bdacc48458e8).(m_lutGcxx . . $ SOPSat#m_lutG.);
127 freecxx . . $ free(5d22bdacc48458e8).(m_lutBcxx . . $ SOPSat#m_lutB.);
128 freecxx . . $ free(5d22bdacc48458e8).(m_lutAcxx . . $ SOPSat#m_lutA.);
129 }
130
131 virtual void updatecxx . . $ SOPSat#update(da7b4a17ceb4e697).(double time,
132 uint32_t* out,
133 const uint32_t* in)
134 {
135 // Rebuild the lookup table in case the parameters have changed.
136 updateLUTcxx . . $ SOPSat#updateLUT(49f6e7a06ebc5aa8).();
137
138 unsigned char *pixel = (unsigned char *) in;
139 unsigned char *dest = (unsigned char *) out;
140
141 if (fabscxx . . $ fabs(447c67c6676f75c3).(m_satcxx . . $ SOPSat#m_sat.-1) < 0.001) {
142 // Calculating the saturation is expensive. So first check whether
143 // we really need to do it.
144 // Keeping the if/else outside of the loop gives a little speed gain.
145 // Worth the duplicate code, as only 4 lines so far :)
146
147 for (unsigned int i = 0; i < sizecxx . . $ frei0r/fx#size.; i++) {
148 *dest++ = m_lutRcxx . . $ SOPSat#m_lutR.[*pixel++];
149 *dest++ = m_lutGcxx . . $ SOPSat#m_lutG.[*pixel++];
150 *dest++ = m_lutBcxx . . $ SOPSat#m_lutB.[*pixel++];
151 *dest++ = m_lutAcxx . . $ SOPSat#m_lutA.[*pixel++];
152 }
153 } else {
154 double luma;
155 for (unsigned int i = 0; i < sizecxx . . $ frei0r/fx#size.; i++) {
156 luma = 0.2126 * m_lutRcxx . . $ SOPSat#m_lutR.[*(pixel+0)]
157 + 0.7152 * m_lutGcxx . . $ SOPSat#m_lutG.[*(pixel+1)]
158 + 0.0722 * m_lutBcxx . . $ SOPSat#m_lutB.[*(pixel+2)];
159 *dest++ = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).(luma + m_satcxx . . $ SOPSat#m_sat.*(m_lutRcxx . . $ SOPSat#m_lutR.[*pixel++]-luma));
160 *dest++ = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).(luma + m_satcxx . . $ SOPSat#m_sat.*(m_lutGcxx . . $ SOPSat#m_lutG.[*pixel++]-luma));
161 *dest++ = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).(luma + m_satcxx . . $ SOPSat#m_sat.*(m_lutBcxx . . $ SOPSat#m_lutB.[*pixel++]-luma));
162 *dest++ = m_lutAcxx . . $ SOPSat#m_lutA.[*pixel++];
163 }
164 }
165 }
166
167 private:
168 unsigned char *m_lutRcxx . . $ SOPSat#m_lutR.;
169 unsigned char *m_lutGcxx . . $ SOPSat#m_lutG.;
170 unsigned char *m_lutBcxx . . $ SOPSat#m_lutB.;
171 unsigned char *m_lutAcxx . . $ SOPSat#m_lutA.;
172
173 double m_satcxx . . $ SOPSat#m_sat.;
174
175 void updateLUTcxx . . $ SOPSat#updateLUT(49f6e7a06ebc5aa8).() {
176 double rS = rSlopecxx . . $ SOPSat#rSlope. * 20;
177 double gS = gSlopecxx . . $ SOPSat#gSlope. * 20;
178 double bS = bSlopecxx . . $ SOPSat#bSlope. * 20;
179 double aS = aSlopecxx . . $ SOPSat#aSlope. * 20;
180
181 double rO = -4 + rOffsetcxx . . $ SOPSat#rOffset. * (4 - (-4));
182 double gO = -4 + gOffsetcxx . . $ SOPSat#gOffset. * (4 - (-4));
183 double bO = -4 + bOffsetcxx . . $ SOPSat#bOffset. * (4 - (-4));
184 double aO = -4 + aOffsetcxx . . $ SOPSat#aOffset. * (4 - (-4));
185
186 double rP = rPowercxx . . $ SOPSat#rPower. * 20;
187 double gP = gPowercxx . . $ SOPSat#gPower. * 20;
188 double bP = bPowercxx . . $ SOPSat#bPower. * 20;
189 double aP = aPowercxx . . $ SOPSat#aPower. * 20;
190
191 m_satcxx . . $ SOPSat#m_sat. = saturationcxx . . $ SOPSat#saturation. * 10;
192
193 for (int i = 0; i < 256; i++) {
194 // above0 avoids overflows for negative numbers.
195 m_lutRcxx . . $ SOPSat#m_lutR.[i] = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).((int) (powcxx . . $ pow(d8e7e0733fa41c87).(above0cxx . . $ SOPSat#above0(447c67c6676f75c3).((float)i/255 * rS + rO), rP)*255));
196 m_lutGcxx . . $ SOPSat#m_lutG.[i] = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).((int) (powcxx . . $ pow(d8e7e0733fa41c87).(above0cxx . . $ SOPSat#above0(447c67c6676f75c3).((float)i/255 * gS + gO), gP)*255));
197 m_lutBcxx . . $ SOPSat#m_lutB.[i] = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).((int) (powcxx . . $ pow(d8e7e0733fa41c87).(above0cxx . . $ SOPSat#above0(447c67c6676f75c3).((float)i/255 * bS + bO), bP)*255));
198 m_lutAcxx . . $ SOPSat#m_lutA.[i] = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).((int) (powcxx . . $ pow(d8e7e0733fa41c87).(above0cxx . . $ SOPSat#above0(447c67c6676f75c3).((float)i/255 * aS + aO), aP)*255));
199 }
200 }
201
202 double above0cxx . . $ SOPSat#above0(447c67c6676f75c3).(double f) {
203 if (f < 0) {
204 return 0;
205 } else {
206 return f;
207 }
208 }
209
210 };
211
212
213
214 frei0rcxx . . $ frei0r/::constructcxx . . $ frei0r/construct#<SOPSatcxx . . $ SOPSat#> plugincxx . . $ plugin.("SOP/Sat"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
215 "Slope/Offset/Power and Saturation color corrections according to the ASC CDL (Color Decision List)"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
216 "Simon A. Eugster (Granjow)"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).,
217 0,3,
218 F0R_COLOR_MODEL_RGBA8888cxx . . $ `include/frei0r.h:308:9`!);