Debian Codegraph @

src/filter/pixs0r/pixs0r.cc

raw symbols SCIP
1 #include <random>
2 #include <cstdint>
3
4 struct pixshift0r
5 {
6 unsigned int m_width;
7 unsigned int m_height;
8
9 unsigned int m_shift_intensity;
10 unsigned int m_block_height;
11
12 // If m_block_height == 0, then these bound block heights
13 unsigned int m_block_height_min;
14 unsigned int m_block_height_max;
15
16 std::mt19937 m_rng;
17
18 std::uniform_int_distribution<int64_t> m_shift_rng;
19 std::uniform_int_distribution<unsigned int> m_block_height_rng;
20
21 pixshift0r(unsigned int width, unsigned int height)
22 : m_width(width), m_height(height), m_block_height(0),
23 m_shift_intensity(0), m_block_height_min(1), m_block_height_max(1),
24 m_rng(std::random_device{}()),
25 m_shift_rng(0, 0), m_block_height_rng(1, 1) {}
26
27 void process(const uint32_t *inframe, uint32_t *outframe)
28 {
29 for (unsigned int b = 0; b < m_height;)
30 {
31 unsigned int block_height = m_block_height ? m_block_height : m_block_height_rng(m_rng);
32
33 // Number of rows to shift is either block size or
34 // what we can *safely* operate on (avoids corruption).
35 block_height = std::min(block_height, m_height - b);
36
37 // Ensure we always make progress
38 if (block_height == 0)
39 block_height = 1;
40
41 int64_t shift = m_shift_intensity ? m_shift_rng(m_rng) : 0;
42
43 for (unsigned int j = 0; j < block_height; ++j)
44 {
45 // NOTE: Faulty implementations, such as FFmpeg don't
46 // check for (width % 8 == 0 && height % 8 == 0).
47 //
48 // A possible workaround for all filters, is a scale filter:
49 // (e.g "scale=round(in_w/8)*8:round(in_h/8)*8:flags=neighbor").
50 //
51 // https://ffmpeg.org/doxygen/trunk/vf__frei0r_8c_source.html#l00352
52 size_t pitch = static_cast<size_t>(b + j) * m_width;
53
54 const uint32_t *inrow = inframe + pitch;
55 uint32_t *outrow = outframe + pitch;
56
57 if (shift > 0)
58 {
59 std::copy (inrow, inrow + m_width - shift, outrow + shift);
60 std::copy (inrow + m_width - shift, inrow + m_width, outrow);
61 }
62 else if (shift < 0)
63 {
64 std::copy (inrow, inrow - shift, outrow + m_width + shift);
65 std::copy (inrow - shift, inrow + m_width, outrow);
66 }
67 else
68 {
69 std::copy (inrow, inrow + m_width, outrow);
70 }
71 }
72
73 b += block_height;
74 }
75 }
76
77 void set_block_height_range(unsigned int min_bh, unsigned int max_bh)
78 {
79 this->m_block_height_min = min_bh;
80 this->m_block_height_max = max_bh;
81
82 auto rng_params = decltype(this->m_block_height_rng)::param_type(min_bh, max_bh);
83
84 this->m_block_height_rng.param(rng_params);
85 }
86
87 void set_shift_intensity(unsigned int shift_intensity)
88 {
89 this->m_shift_intensity = shift_intensity;
90
91 int64_t intensity = static_cast<int64_t>(shift_intensity);
92
93 auto rng_params = decltype(this->m_shift_rng)::param_type(-intensity, intensity);
94
95 this->m_shift_rng.param(rng_params);
96 }
97 };
98
99 extern "C" {
100 #include <frei0r.h>
101
102 int f0r_init()
103 {
104 return 0;
105 }
106
107 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
108 {
109 pixshift0r *instance = new pixshift0r(width, height);
110
111 instance->set_shift_intensity(width / 100);
112 instance->set_block_height_range(std::max(1u, height / 100), std::max(1u, height / 10));
113
114 return instance;
115 }
116
117 void f0r_get_plugin_info(f0r_plugin_info_t *info)
118 {
119 info->name = "pixs0r";
120 info->author = "xsbee";
121 info->explanation = "Glitch image shifting rows to and fro";
122 info->major_version = 1;
123 info->minor_version = 0;
124 info->frei0r_version = FREI0R_MAJOR_VERSION;
125
126 info->color_model = F0R_COLOR_MODEL_PACKED32;
127 info->plugin_type = F0R_PLUGIN_TYPE_FILTER;
128 info->num_params = 4;
129 }
130
131 void f0r_get_param_info (f0r_param_info_t *info, int param_index)
132 {
133 switch (param_index)
134 {
135 case 0:
136 info->name = "shift_intensity";
137 info->type = F0R_PARAM_DOUBLE;
138 info->explanation = "Aggressiveness of row/column-shifting";
139 break;
140
141 case 1:
142 info->name = "block_height";
143 info->type = F0R_PARAM_DOUBLE;
144 info->explanation = "Height of each block whose shift is invariant (0 = random)";
145 break;
146
147 case 2:
148 info->name = "block_height_min";
149 info->type = F0R_PARAM_DOUBLE;
150 info->explanation = "Minimum height of block (if random mode)";
151 break;
152
153 case 3:
154 info->name = "block_height_max";
155 info->type = F0R_PARAM_DOUBLE;
156 info->explanation = "Maximum height of block (if random mode)";
157 break;
158 }
159 }
160
161 void f0r_get_param_value(
162 f0r_instance_t instance,
163 f0r_param_t param,
164 int param_index
165 )
166 {
167 pixshift0r* context = (pixshift0r*)instance;
168
169 switch (param_index) {
170 case 0:
171 *static_cast<double*>(param) = static_cast<double>(context->m_shift_intensity) / context->m_width;
172 break;
173 case 1:
174 *static_cast<double*>(param) = static_cast<double>(context->m_block_height) / context->m_height;
175 break;
176 case 2:
177 *static_cast<double*>(param) = static_cast<double>(context->m_block_height_min) / context->m_height;
178 break;
179 case 3:
180 *static_cast<double*>(param) = static_cast<double>(context->m_block_height_max) / context->m_height;
181 break;
182 }
183 }
184
185 void f0r_set_param_value(
186 f0r_instance_t instance,
187 f0r_param_t param,
188 int param_index
189 )
190 {
191 pixshift0r* context = (pixshift0r*)instance;
192
193 switch (param_index) {
194 case 0:
195 context->set_shift_intensity (*static_cast<double*>(param) * context->m_width);
196 break;
197 case 1:
198 context->m_block_height = *static_cast<double*>(param) * context->m_height;
199 break;
200 case 2:
201 context->set_block_height_range (*static_cast<double*>(param) * context->m_height, context->m_block_height_max);
202 break;
203 case 3:
204 context->set_block_height_range (context->m_block_height_min, *static_cast<double*>(param) * context->m_height);
205 break;
206 }
207 }
208
209 void f0r_update (
210 f0r_instance_t instance,
211 double,
212 const uint32_t *inframe,
213 uint32_t *outframe
214 )
215 {
216 static_cast<pixshift0r*>(instance)->process(inframe, outframe);
217 }
218
219 void f0r_deinit() {}
220
221 void f0r_destruct (f0r_instance_t instance)
222 {
223 delete static_cast<pixshift0r*>(instance);
224 }
225 }