Debian Codegraph @

src/filter/tutorial/tutorial.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2010-2011 Simon Andreas Eugster ([email protected])
3 * This file is not a Frei0r plugin but a collection of ideas.
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 "frei0r.hpp"
21
22 // Other includes used for the examples below.
23 // Can be removed on copy/paste.
24
25 // Limits (min/max values) of various data types
26 #include <limits>
27 // For the CHAR_BIT constant
28 #include <climits>
29 // pow() and other mathematical functions
30 #include <cmath>
31
32 /**
33 This is a sample filter for easy copy/pasting.
34
35 The CMakeLists.txt needs to be adjusted as well, both in this directory and in
36 the parent filter directory.
37 */
38
39 class Tutorial : public frei0r::filter
40 {
41
42 public:
43
44 Tutorial(unsigned int width, unsigned int height)
45
46 {
47 // Everything here is example code and can be removed on copy/paste.
48
49
50 register_param(m_barSize, "barSize", "Size of the black bar");
51 register_param(m_pointerMethod, "pointerMethod", "Pointer Method (internal)");
52 m_barSize = 0.1;
53
54
55
56 // Create the lookup table (see update()). Use std::vector instead of an array here, since:
57 // http://stackoverflow.com/questions/381621/using-arrays-or-stdvectors-in-c-whats-the-performance-gap
58 // Using std::numeric_limits just for fun here.
59 lookupTable = std::vector<uint8_t>(std::numeric_limits<uint8_t>::max()+1, 0);
60 // std::cerr << lookupTable.size() << " elements in the lookup table." << std::endl;
61
62 // Calculate the entries in the lookup table. Applied on the R value in this example.
63 // If the R value of an input pixel is r, then the output R value will be mapped to lookupTable[r].
64 // We'll use a calculation that looks expensive here.
65 float f, h;
66 int tempVal;
67 for (size_t i = 0; i < lookupTable.size(); i++) {
68 f = i / (float)std::numeric_limits<uint8_t>::max(); // Normalize to [0,1]
69 h = f/5;
70 f = 2*f - 1; // Stretch to [-1,1]
71 f = pow(f, 2); // Parabola
72 f = f*.8 + h; // Modification to the parabola
73
74 // Since we might get negative values above, directly putting f into an unsigned data type
75 // may lead to interesting effects. To avoid this, use a signed integer and clamp it to valid ranges.
76 tempVal = f * std::numeric_limits<uint8_t>::max();
77 if (tempVal < 0) {
78 tempVal = 0;
79 }
80 if (tempVal > std::numeric_limits<uint8_t>::max()) {
81 tempVal = std::numeric_limits<uint8_t>::max();
82 }
83 lookupTable[i] = tempVal;
84 }
85
86
87 // This is a second lookup table for addition of two uint8 numbers. The result of {0,255}+{0,255}
88 // ranges in {0,511}. The usual way to clamp the values to the {0,255} range is to use if/else
89 // (if (k > 255) { k = 255; }), however using a lookup table is slightly faster:
90 // http://stackoverflow.com/questions/4783674/lookup-table-vs-if-else
91 additionTable = std::vector<uint8_t>( 2*std::numeric_limits<uint8_t>::max() + 1, 0 );
92 for (int i = 0; i < 2*std::numeric_limits<uint8_t>::max(); i++) {
93 if (i <= std::numeric_limits<uint8_t>::max()) {
94 additionTable[i] = i;
95 } else {
96 additionTable[i] = std::numeric_limits<uint8_t>::max();
97 }
98 }
99 }
100
101 ~Tutorial()
102 {
103 // Delete member variables if necessary.
104 }
105
106 virtual void update(double time,
107 uint32_t* out,
108 const uint32_t* in)
109 {
110 // Just copy input to output.
111 // This is useful if only few changes are made to the output.
112 // If the whole image is processed, this makes no sense!
113 std::copy(in, in + width*height, out);
114
115 // Fill the given amount of the top part with black.
116 std::fill(&out[0], &out[(int) (m_barSize*width*height)], 0);
117
118 // Performance is important!
119 // One way to improve the performance is to use a LOOKUP TABLE
120 // instead of doing the same calculation several times. For example, the
121 // SOP/Sat effect uses pow(), division, and multiplication for the Power parameter.
122 // Since this only depends on the R/G/B value and not on previous frames, the target value
123 // can be pre-computed; when applying the filter, only thing left to do is reading the value
124 // in the lookup table.
125
126 // This parameter allows one to do simple benchmarking: Rendering a
127 // video with uint8_t pointers and with uint32_t pointers.
128 // (Don't forget to subtract the rendering time without this effect applied to avoid counting
129 // encoding and decoding as well!)
130 if (m_pointerMethod == 0) {
131 uint8_t *in_pointer = (uint8_t *) in;
132 uint8_t *out_pointer = (uint8_t *) out;
133 for (unsigned int x = 0; x < width; x++) {
134 for (unsigned int y = 0; y < height; y++) {
135
136 // Apply the parabola to the R channel with a single lookup
137 *out_pointer++ = lookupTable[*in_pointer++];
138
139 // Add g+b and clamp with the second lookup table
140 *out_pointer = additionTable[*in_pointer + *(in_pointer+1)];
141 out_pointer++; in_pointer++;
142
143 // Copy the other channels
144 *out_pointer++ = *in_pointer++;
145 *out_pointer++ = *in_pointer++;
146 }
147 }
148 } else {
149 // This method takes only 80% of the time if only processing the R channel,
150 // and 90% of the time with additionally the G channel,
151 // compared to the above solution using uint8_t pointers.
152 for (unsigned int px = 0; px < width*height; px++) {
153 out[px] =
154 // Parabola to Red channel
155 lookupTable[(in[px] & 0xFF)]
156 // g+b to Green channel
157 | (additionTable[((in[px] & 0xFF00) >> CHAR_BIT) + ((in[px] & 0xFF0000) >> 2*CHAR_BIT)] << CHAR_BIT)
158 // copy blue and alpha
159 | (in[px] & 0xFFFF0000);
160 }
161 }
162 }
163
164 private:
165 // The various f0r_params are adjustable parameters.
166 // This one determines the size of the black bar in this example.
167 double m_barSize;
168 bool m_pointerMethod;
169 std::vector<uint8_t> lookupTable;
170 std::vector<uint8_t> additionTable;
171
172 };
173
174
175
176 frei0r::construct<Tutorial> plugin("Tutorial filter",
177 "This is an example filter, kind of a quick howto showing how to add a frei0r filter.",
178 "Your Name",
179 0,2,
180 F0R_COLOR_MODEL_RGBA8888);