/src/guetzli/guetzli/butteraugli_comparator.cc
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2016 Google Inc. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | #include "guetzli/butteraugli_comparator.h" |
18 | | |
19 | | #include <algorithm> |
20 | | |
21 | | #include "guetzli/debug_print.h" |
22 | | #include "guetzli/gamma_correct.h" |
23 | | #include "guetzli/score.h" |
24 | | |
25 | | namespace guetzli { |
26 | | |
27 | | namespace { |
28 | | using ::butteraugli::ImageF; |
29 | | using ::butteraugli::CreatePlanes; |
30 | | using ::butteraugli::PlanesFromPacked; |
31 | | using ::butteraugli::PackedFromPlanes; |
32 | | |
33 | | std::vector<ImageF> LinearRgb(const size_t xsize, const size_t ysize, |
34 | 190k | const std::vector<uint8_t>& rgb) { |
35 | 190k | const double* lut = Srgb8ToLinearTable(); |
36 | 190k | std::vector<ImageF> planes = CreatePlanes<float>(xsize, ysize, 3); |
37 | 760k | for (int c = 0; c < 3; ++c) { |
38 | 29.0M | for (size_t y = 0; y < ysize; ++y) { |
39 | 28.4M | const uint8_t* const BUTTERAUGLI_RESTRICT row_in = &rgb[3 * xsize * y]; |
40 | 28.4M | float* const BUTTERAUGLI_RESTRICT row_out = planes[c].Row(y); |
41 | 1.53G | for (size_t x = 0; x < xsize; ++x) { |
42 | 1.50G | row_out[x] = lut[row_in[3 * x + c]]; |
43 | 1.50G | } |
44 | 28.4M | } |
45 | 570k | } |
46 | 190k | return planes; |
47 | 190k | } |
48 | | |
49 | | } // namespace |
50 | | |
51 | | ButteraugliComparator::ButteraugliComparator(const int width, const int height, |
52 | | const std::vector<uint8_t>* rgb, |
53 | | const float target_distance, |
54 | | ProcessStats* stats) |
55 | 3.03k | : width_(width), |
56 | 3.03k | height_(height), |
57 | 3.03k | target_distance_(target_distance), |
58 | 3.03k | rgb_orig_(*rgb), |
59 | 3.03k | comparator_(LinearRgb(width_, height_, *rgb)), |
60 | 3.03k | distance_(0.0), |
61 | 3.03k | stats_(stats) {} |
62 | | |
63 | 182k | void ButteraugliComparator::Compare(const OutputImage& img) { |
64 | 182k | std::vector<ImageF> rgb0 = |
65 | 182k | ::butteraugli::OpsinDynamicsImage(LinearRgb(width_, height_, rgb_orig_)); |
66 | 182k | std::vector<std::vector<float> > rgb(3, std::vector<float>(width_ * height_)); |
67 | 182k | img.ToLinearRGB(&rgb); |
68 | 182k | const std::vector<ImageF> rgb_planes = PlanesFromPacked(width_, height_, rgb); |
69 | 182k | std::vector<float>(width_ * height_).swap(distmap_); |
70 | 182k | ImageF distmap; |
71 | 182k | comparator_.Diffmap(rgb_planes, distmap); |
72 | 182k | CopyToPacked(distmap, &distmap_); |
73 | 182k | distance_ = ::butteraugli::ButteraugliScoreFromDiffmap(distmap); |
74 | 182k | GUETZLI_LOG(stats_, " BA[100.00%%] D[%6.4f]", distance_); |
75 | 182k | } |
76 | | |
77 | | namespace { |
78 | | |
79 | | // To change this to n, add the relevant FFTn function and kFFTnMapIndexTable. |
80 | | constexpr size_t kBlockEdge = 8; |
81 | | constexpr size_t kBlockSize = kBlockEdge * kBlockEdge; |
82 | | constexpr size_t kBlockEdgeHalf = kBlockEdge / 2; |
83 | | constexpr size_t kBlockHalf = kBlockEdge * kBlockEdgeHalf; |
84 | | |
85 | | // Contrast sensitivity related weights. |
86 | | // Contrast sensitivity matrix is a model of the contrast sensitivity of the |
87 | | // human eye. The contrast sensitivity function, also known as contrast |
88 | | // sensitivity curve, is a basic psychovisual function and it roughly means |
89 | | // that highest frequences have less impact than middle and low frequences. |
90 | | // |
91 | | // The order of coefficients here is slightly confusing. |
92 | | // It is a creative order that benefits from the mirroring of the fft. |
93 | 10.4M | static const double *GetContrastSensitivityMatrix() { |
94 | 10.4M | static double csf8x8[kBlockHalf + kBlockEdgeHalf + 1] = { |
95 | 10.4M | 0.0, // no 8x8 dc, enough 'dc' is calculated from low/mid freq images. |
96 | 10.4M | 0.0, |
97 | 10.4M | 0.0, |
98 | 10.4M | 0.0, |
99 | 10.4M | 0.3831134973, |
100 | 10.4M | 0.676303603859, |
101 | 10.4M | 1.1550451483, |
102 | 10.4M | 8, |
103 | 10.4M | 8, |
104 | 10.4M | 0.692062533689, |
105 | 10.4M | 0.847511538605, |
106 | 10.4M | 0.498250875965, |
107 | 10.4M | 0.36198671102, |
108 | 10.4M | 0.308982169883, |
109 | 10.4M | 0.1312701920435, |
110 | 10.4M | 4.71274312228, |
111 | 10.4M | 1.1550451483, |
112 | 10.4M | 0.847511538605, |
113 | 10.4M | 4.71274312228, |
114 | 10.4M | 0.991205724152, |
115 | 10.4M | 1.30229591239, |
116 | 10.4M | 0.627264168628, |
117 | 10.4M | 0.4, |
118 | 10.4M | 0.1312701920435, |
119 | 10.4M | 0.676303603859, |
120 | 10.4M | 0.498250875965, |
121 | 10.4M | 0.991205724152, |
122 | 10.4M | 0.5, |
123 | 10.4M | 0.3831134973, |
124 | 10.4M | 0.349686450518, |
125 | 10.4M | 0.627264168628, |
126 | 10.4M | 0.308982169883, |
127 | 10.4M | 0.3831134973, |
128 | 10.4M | 0.36198671102, |
129 | 10.4M | 1.30229591239, |
130 | 10.4M | 0.3831134973, |
131 | 10.4M | 0.323078800177, |
132 | 10.4M | }; |
133 | 10.4M | return &csf8x8[0]; |
134 | 10.4M | } |
135 | | |
136 | | struct Complex { |
137 | | double real; |
138 | | double imag; |
139 | | }; |
140 | | |
141 | 1.03G | inline double abssq(const Complex& c) { |
142 | 1.03G | return c.real * c.real + c.imag * c.imag; |
143 | 1.03G | } |
144 | | |
145 | 31.3M | static void TransposeBlock(Complex data[kBlockSize]) { |
146 | 281M | for (int i = 0; i < kBlockEdge; i++) { |
147 | 1.12G | for (int j = 0; j < i; j++) { |
148 | 877M | std::swap(data[kBlockEdge * i + j], data[kBlockEdge * j + i]); |
149 | 877M | } |
150 | 250M | } |
151 | 31.3M | } |
152 | | |
153 | | // D. J. Bernstein's Fast Fourier Transform algorithm on 4 elements. |
154 | 93.9M | inline void FFT4(Complex* a) { |
155 | 93.9M | double t1, t2, t3, t4, t5, t6, t7, t8; |
156 | 93.9M | t5 = a[2].real; |
157 | 93.9M | t1 = a[0].real - t5; |
158 | 93.9M | t7 = a[3].real; |
159 | 93.9M | t5 += a[0].real; |
160 | 93.9M | t3 = a[1].real - t7; |
161 | 93.9M | t7 += a[1].real; |
162 | 93.9M | t8 = t5 + t7; |
163 | 93.9M | a[0].real = t8; |
164 | 93.9M | t5 -= t7; |
165 | 93.9M | a[1].real = t5; |
166 | 93.9M | t6 = a[2].imag; |
167 | 93.9M | t2 = a[0].imag - t6; |
168 | 93.9M | t6 += a[0].imag; |
169 | 93.9M | t5 = a[3].imag; |
170 | 93.9M | a[2].imag = t2 + t3; |
171 | 93.9M | t2 -= t3; |
172 | 93.9M | a[3].imag = t2; |
173 | 93.9M | t4 = a[1].imag - t5; |
174 | 93.9M | a[3].real = t1 + t4; |
175 | 93.9M | t1 -= t4; |
176 | 93.9M | a[2].real = t1; |
177 | 93.9M | t5 += a[1].imag; |
178 | 93.9M | a[0].imag = t6 + t5; |
179 | 93.9M | t6 -= t5; |
180 | 93.9M | a[1].imag = t6; |
181 | 93.9M | } |
182 | | |
183 | | const double kSqrtHalf = 0.70710678118654752440084436210484903; |
184 | | |
185 | | // D. J. Bernstein's Fast Fourier Transform algorithm on 8 elements. |
186 | 93.9M | void FFT8(Complex* a) { |
187 | 93.9M | double t1, t2, t3, t4, t5, t6, t7, t8; |
188 | | |
189 | 93.9M | t7 = a[4].imag; |
190 | 93.9M | t4 = a[0].imag - t7; |
191 | 93.9M | t7 += a[0].imag; |
192 | 93.9M | a[0].imag = t7; |
193 | | |
194 | 93.9M | t8 = a[6].real; |
195 | 93.9M | t5 = a[2].real - t8; |
196 | 93.9M | t8 += a[2].real; |
197 | 93.9M | a[2].real = t8; |
198 | | |
199 | 93.9M | t7 = a[6].imag; |
200 | 93.9M | a[6].imag = t4 - t5; |
201 | 93.9M | t4 += t5; |
202 | 93.9M | a[4].imag = t4; |
203 | | |
204 | 93.9M | t6 = a[2].imag - t7; |
205 | 93.9M | t7 += a[2].imag; |
206 | 93.9M | a[2].imag = t7; |
207 | | |
208 | 93.9M | t8 = a[4].real; |
209 | 93.9M | t3 = a[0].real - t8; |
210 | 93.9M | t8 += a[0].real; |
211 | 93.9M | a[0].real = t8; |
212 | | |
213 | 93.9M | a[4].real = t3 - t6; |
214 | 93.9M | t3 += t6; |
215 | 93.9M | a[6].real = t3; |
216 | | |
217 | 93.9M | t7 = a[5].real; |
218 | 93.9M | t3 = a[1].real - t7; |
219 | 93.9M | t7 += a[1].real; |
220 | 93.9M | a[1].real = t7; |
221 | | |
222 | 93.9M | t8 = a[7].imag; |
223 | 93.9M | t6 = a[3].imag - t8; |
224 | 93.9M | t8 += a[3].imag; |
225 | 93.9M | a[3].imag = t8; |
226 | 93.9M | t1 = t3 - t6; |
227 | 93.9M | t3 += t6; |
228 | | |
229 | 93.9M | t7 = a[5].imag; |
230 | 93.9M | t4 = a[1].imag - t7; |
231 | 93.9M | t7 += a[1].imag; |
232 | 93.9M | a[1].imag = t7; |
233 | | |
234 | 93.9M | t8 = a[7].real; |
235 | 93.9M | t5 = a[3].real - t8; |
236 | 93.9M | t8 += a[3].real; |
237 | 93.9M | a[3].real = t8; |
238 | | |
239 | 93.9M | t2 = t4 - t5; |
240 | 93.9M | t4 += t5; |
241 | | |
242 | 93.9M | t6 = t1 - t4; |
243 | 93.9M | t8 = kSqrtHalf; |
244 | 93.9M | t6 *= t8; |
245 | 93.9M | a[5].real = a[4].real - t6; |
246 | 93.9M | t1 += t4; |
247 | 93.9M | t1 *= t8; |
248 | 93.9M | a[5].imag = a[4].imag - t1; |
249 | 93.9M | t6 += a[4].real; |
250 | 93.9M | a[4].real = t6; |
251 | 93.9M | t1 += a[4].imag; |
252 | 93.9M | a[4].imag = t1; |
253 | | |
254 | 93.9M | t5 = t2 - t3; |
255 | 93.9M | t5 *= t8; |
256 | 93.9M | a[7].imag = a[6].imag - t5; |
257 | 93.9M | t2 += t3; |
258 | 93.9M | t2 *= t8; |
259 | 93.9M | a[7].real = a[6].real - t2; |
260 | 93.9M | t2 += a[6].real; |
261 | 93.9M | a[6].real = t2; |
262 | 93.9M | t5 += a[6].imag; |
263 | 93.9M | a[6].imag = t5; |
264 | | |
265 | 93.9M | FFT4(a); |
266 | | |
267 | | // Reorder to the correct output order. |
268 | | // TODO(szabadka): Modify the above computation so that this is not needed. |
269 | 93.9M | Complex tmp = a[2]; |
270 | 93.9M | a[2] = a[3]; |
271 | 93.9M | a[3] = a[5]; |
272 | 93.9M | a[5] = a[7]; |
273 | 93.9M | a[7] = a[4]; |
274 | 93.9M | a[4] = a[1]; |
275 | 93.9M | a[1] = a[6]; |
276 | 93.9M | a[6] = tmp; |
277 | 93.9M | } |
278 | | |
279 | | // Same as FFT8, but all inputs are real. |
280 | | // TODO(szabadka): Since this does not need to be in-place, maybe there is a |
281 | | // faster FFT than this one, which is derived from DJB's in-place complex FFT. |
282 | 313M | void RealFFT8(const double* in, Complex* out) { |
283 | 313M | double t1, t2, t3, t5, t6, t7, t8; |
284 | 313M | t8 = in[6]; |
285 | 313M | t5 = in[2] - t8; |
286 | 313M | t8 += in[2]; |
287 | 313M | out[2].real = t8; |
288 | 313M | out[6].imag = -t5; |
289 | 313M | out[4].imag = t5; |
290 | 313M | t8 = in[4]; |
291 | 313M | t3 = in[0] - t8; |
292 | 313M | t8 += in[0]; |
293 | 313M | out[0].real = t8; |
294 | 313M | out[4].real = t3; |
295 | 313M | out[6].real = t3; |
296 | 313M | t7 = in[5]; |
297 | 313M | t3 = in[1] - t7; |
298 | 313M | t7 += in[1]; |
299 | 313M | out[1].real = t7; |
300 | 313M | t8 = in[7]; |
301 | 313M | t5 = in[3] - t8; |
302 | 313M | t8 += in[3]; |
303 | 313M | out[3].real = t8; |
304 | 313M | t2 = -t5; |
305 | 313M | t6 = t3 - t5; |
306 | 313M | t8 = kSqrtHalf; |
307 | 313M | t6 *= t8; |
308 | 313M | out[5].real = out[4].real - t6; |
309 | 313M | t1 = t3 + t5; |
310 | 313M | t1 *= t8; |
311 | 313M | out[5].imag = out[4].imag - t1; |
312 | 313M | t6 += out[4].real; |
313 | 313M | out[4].real = t6; |
314 | 313M | t1 += out[4].imag; |
315 | 313M | out[4].imag = t1; |
316 | 313M | t5 = t2 - t3; |
317 | 313M | t5 *= t8; |
318 | 313M | out[7].imag = out[6].imag - t5; |
319 | 313M | t2 += t3; |
320 | 313M | t2 *= t8; |
321 | 313M | out[7].real = out[6].real - t2; |
322 | 313M | t2 += out[6].real; |
323 | 313M | out[6].real = t2; |
324 | 313M | t5 += out[6].imag; |
325 | 313M | out[6].imag = t5; |
326 | 313M | t5 = out[2].real; |
327 | 313M | t1 = out[0].real - t5; |
328 | 313M | t7 = out[3].real; |
329 | 313M | t5 += out[0].real; |
330 | 313M | t3 = out[1].real - t7; |
331 | 313M | t7 += out[1].real; |
332 | 313M | t8 = t5 + t7; |
333 | 313M | out[0].real = t8; |
334 | 313M | t5 -= t7; |
335 | 313M | out[1].real = t5; |
336 | 313M | out[2].imag = t3; |
337 | 313M | out[3].imag = -t3; |
338 | 313M | out[3].real = t1; |
339 | 313M | out[2].real = t1; |
340 | 313M | out[0].imag = 0; |
341 | 313M | out[1].imag = 0; |
342 | | |
343 | | // Reorder to the correct output order. |
344 | | // TODO(szabadka): Modify the above computation so that this is not needed. |
345 | 313M | Complex tmp = out[2]; |
346 | 313M | out[2] = out[3]; |
347 | 313M | out[3] = out[5]; |
348 | 313M | out[5] = out[7]; |
349 | 313M | out[7] = out[4]; |
350 | 313M | out[4] = out[1]; |
351 | 313M | out[1] = out[6]; |
352 | 313M | out[6] = tmp; |
353 | 313M | } |
354 | | |
355 | | // Fills in block[kBlockEdgeHalf..(kBlockHalf+kBlockEdgeHalf)], and leaves the |
356 | | // rest unmodified. |
357 | 31.3M | void ButteraugliFFTSquared(double block[kBlockSize]) { |
358 | 31.3M | double global_mul = 0.000064; |
359 | 31.3M | Complex block_c[kBlockSize]; |
360 | 31.3M | assert(kBlockEdge == 8); |
361 | 281M | for (int y = 0; y < kBlockEdge; ++y) { |
362 | 250M | RealFFT8(block + y * kBlockEdge, block_c + y * kBlockEdge); |
363 | 250M | } |
364 | 31.3M | TransposeBlock(block_c); |
365 | 31.3M | double r0[kBlockEdge]; |
366 | 31.3M | double r1[kBlockEdge]; |
367 | 281M | for (int x = 0; x < kBlockEdge; ++x) { |
368 | 250M | r0[x] = block_c[x].real; |
369 | 250M | r1[x] = block_c[kBlockHalf + x].real; |
370 | 250M | } |
371 | 31.3M | RealFFT8(r0, block_c); |
372 | 31.3M | RealFFT8(r1, block_c + kBlockHalf); |
373 | 125M | for (int y = 1; y < kBlockEdgeHalf; ++y) { |
374 | 93.9M | FFT8(block_c + y * kBlockEdge); |
375 | 93.9M | } |
376 | 1.06G | for (int i = kBlockEdgeHalf; i < kBlockHalf + kBlockEdgeHalf + 1; ++i) { |
377 | 1.03G | block[i] = abssq(block_c[i]); |
378 | 1.03G | block[i] *= global_mul; |
379 | 1.03G | } |
380 | 31.3M | } |
381 | | |
382 | | void ButteraugliBlockDiff(double xyb0[3 * kBlockSize], |
383 | | double xyb1[3 * kBlockSize], |
384 | 10.4M | double* diff_xyb) { |
385 | 10.4M | const double *csf8x8 = GetContrastSensitivityMatrix(); |
386 | 10.4M | double avgdiff_xyb[3] = {0.0}; |
387 | 2.01G | for (int i = 0; i < 3 * kBlockSize; ++i) { |
388 | 2.00G | avgdiff_xyb[i / kBlockSize] += xyb0[i] - xyb1[i]; |
389 | 2.00G | } |
390 | 41.7M | for (int c = 0; c < 3; ++c) { |
391 | 31.3M | double avgdiff = avgdiff_xyb[c] / kBlockSize; |
392 | 31.3M | diff_xyb[c] += 4.0 * avgdiff * avgdiff; |
393 | 31.3M | } |
394 | 10.4M | double x_diff[kBlockSize]; |
395 | 10.4M | double y_diff[kBlockSize]; |
396 | 10.4M | double b_diff[kBlockSize]; |
397 | 678M | for (int i = 0; i < kBlockSize; ++i) { |
398 | 668M | x_diff[i] = (xyb0[i] - xyb1[i]); |
399 | 668M | y_diff[i] = (xyb0[kBlockSize + i] - xyb1[kBlockSize + i]); |
400 | 668M | b_diff[i] = (xyb0[2 * kBlockSize + i] - xyb1[2 * kBlockSize + i]); |
401 | 668M | } |
402 | 10.4M | ButteraugliFFTSquared(x_diff); |
403 | 10.4M | ButteraugliFFTSquared(y_diff); |
404 | 10.4M | ButteraugliFFTSquared(b_diff); |
405 | 355M | for (size_t i = kBlockEdgeHalf; i < kBlockHalf + kBlockEdgeHalf + 1; ++i) { |
406 | 344M | double d = csf8x8[i]; |
407 | 344M | diff_xyb[0] += d * x_diff[i]; |
408 | 344M | diff_xyb[1] += d * y_diff[i]; |
409 | 344M | diff_xyb[2] += d * b_diff[i]; |
410 | 344M | } |
411 | 10.4M | } |
412 | | |
413 | | } // namespace |
414 | | |
415 | 4.17k | void ButteraugliComparator::StartBlockComparisons() { |
416 | 4.17k | std::vector<ImageF> dummy(3); |
417 | 4.17k | std::vector<ImageF> rgb0 = |
418 | 4.17k | ::butteraugli::OpsinDynamicsImage(LinearRgb(width_, height_, rgb_orig_)); |
419 | 4.17k | ::butteraugli::Mask(rgb0, rgb0, |
420 | 4.17k | &mask_xyz_, &dummy); |
421 | 4.17k | } |
422 | | |
423 | 4.17k | void ButteraugliComparator::FinishBlockComparisons() { |
424 | 4.17k | mask_xyz_.clear(); |
425 | 4.17k | } |
426 | | |
427 | | void ButteraugliComparator::SwitchBlock(int block_x, int block_y, |
428 | 159k | int factor_x, int factor_y) { |
429 | 159k | block_x_ = block_x; |
430 | 159k | block_y_ = block_y; |
431 | 159k | factor_x_ = factor_x; |
432 | 159k | factor_y_ = factor_y; |
433 | 159k | per_block_pregamma_.resize(factor_x_ * factor_y_); |
434 | 159k | const double* lut = Srgb8ToLinearTable(); |
435 | 337k | for (int off_y = 0, bx = 0; off_y < factor_y_; ++off_y) { |
436 | 391k | for (int off_x = 0; off_x < factor_x_; ++off_x, ++bx) { |
437 | 214k | per_block_pregamma_[bx].resize(3, std::vector<float>(kDCTBlockSize)); |
438 | 214k | int block_xx = block_x_ * factor_x_ + off_x; |
439 | 214k | int block_yy = block_y_ * factor_y_ + off_y; |
440 | 1.92M | for (int iy = 0, i = 0; iy < 8; ++iy) { |
441 | 15.4M | for (int ix = 0; ix < 8; ++ix, ++i) { |
442 | 13.7M | int x = std::min(8 * block_xx + ix, width_ - 1); |
443 | 13.7M | int y = std::min(8 * block_yy + iy, height_ - 1); |
444 | 13.7M | int px = y * width_ + x; |
445 | 54.8M | for (int c = 0; c < 3; ++c) { |
446 | 41.1M | per_block_pregamma_[bx][c][i] = lut[rgb_orig_[3 * px + c]]; |
447 | 41.1M | } |
448 | 13.7M | } |
449 | 1.71M | } |
450 | 214k | per_block_pregamma_[bx] = |
451 | 214k | ::butteraugli::PackedFromPlanes(::butteraugli::OpsinDynamicsImage( |
452 | 214k | ::butteraugli::PlanesFromPacked(8, 8, per_block_pregamma_[bx]))); |
453 | 214k | } |
454 | 177k | } |
455 | 159k | } |
456 | | |
457 | | double ButteraugliComparator::CompareBlock(const OutputImage& img, |
458 | 10.4M | int off_x, int off_y) const { |
459 | 10.4M | int block_x = block_x_ * factor_x_ + off_x; |
460 | 10.4M | int block_y = block_y_ * factor_y_ + off_y; |
461 | 10.4M | int xmin = 8 * block_x; |
462 | 10.4M | int ymin = 8 * block_y; |
463 | 10.4M | int block_ix = off_y * factor_x_ + off_x; |
464 | 10.4M | const std::vector<std::vector<float> >& rgb0 = |
465 | 10.4M | per_block_pregamma_[block_ix]; |
466 | | |
467 | 10.4M | std::vector<std::vector<float> > rgb1(3, std::vector<float>(kDCTBlockSize)); |
468 | 10.4M | img.ToLinearRGB(xmin, ymin, 8, 8, &rgb1); |
469 | 10.4M | rgb1 = ::butteraugli::PackedFromPlanes(::butteraugli::OpsinDynamicsImage( |
470 | 10.4M | ::butteraugli::PlanesFromPacked(8, 8, rgb1))); |
471 | | |
472 | 10.4M | double b0[3 * kDCTBlockSize]; |
473 | 10.4M | double b1[3 * kDCTBlockSize]; |
474 | 41.7M | for (int c = 0; c < 3; ++c) { |
475 | 2.03G | for (int ix = 0; ix < kDCTBlockSize; ++ix) { |
476 | 2.00G | b0[c * kDCTBlockSize + ix] = rgb0[c][ix]; |
477 | 2.00G | b1[c * kDCTBlockSize + ix] = rgb1[c][ix]; |
478 | 2.00G | } |
479 | 31.3M | } |
480 | 10.4M | double diff_xyz[3] = { 0.0 }; |
481 | 10.4M | ButteraugliBlockDiff(b0, b1, diff_xyz); |
482 | | |
483 | 10.4M | double diff = 0.0; |
484 | 41.7M | for (int c = 0; c < 3; ++c) { |
485 | 31.3M | diff += diff_xyz[c] * mask_xyz_[c].Row(ymin)[xmin]; |
486 | 31.3M | } |
487 | 10.4M | return sqrt(diff); |
488 | 10.4M | } |
489 | | |
490 | 2.06M | float ButteraugliComparator::BlockErrorLimit() const { |
491 | 2.06M | return target_distance_; |
492 | 2.06M | } |
493 | | |
494 | | void ButteraugliComparator::ComputeBlockErrorAdjustmentWeights( |
495 | | int direction, |
496 | | int max_block_dist, |
497 | | double target_mul, |
498 | | int factor_x, int factor_y, |
499 | | const std::vector<float>& distmap, |
500 | 170k | std::vector<float>* block_weight) { |
501 | 170k | const double target_distance = target_distance_ * target_mul; |
502 | 170k | const int sizex = 8 * factor_x; |
503 | 170k | const int sizey = 8 * factor_y; |
504 | 170k | const int block_width = (width_ + sizex - 1) / sizex; |
505 | 170k | const int block_height = (height_ + sizey - 1) / sizey; |
506 | 170k | std::vector<float> max_dist_per_block(block_width * block_height); |
507 | 1.20M | for (int block_y = 0; block_y < block_height; ++block_y) { |
508 | 8.31M | for (int block_x = 0; block_x < block_width; ++block_x) { |
509 | 7.28M | int block_ix = block_y * block_width + block_x; |
510 | 7.28M | int x_max = std::min(width_, sizex * (block_x + 1)); |
511 | 7.28M | int y_max = std::min(height_, sizey * (block_y + 1)); |
512 | 7.28M | float max_dist = 0.0; |
513 | 64.6M | for (int y = sizey * block_y; y < y_max; ++y) { |
514 | 517M | for (int x = sizex * block_x; x < x_max; ++x) { |
515 | 460M | max_dist = std::max(max_dist, distmap[y * width_ + x]); |
516 | 460M | } |
517 | 57.3M | } |
518 | 7.28M | max_dist_per_block[block_ix] = max_dist; |
519 | 7.28M | } |
520 | 1.03M | } |
521 | 1.20M | for (int block_y = 0; block_y < block_height; ++block_y) { |
522 | 8.31M | for (int block_x = 0; block_x < block_width; ++block_x) { |
523 | 7.28M | int block_ix = block_y * block_width + block_x; |
524 | 7.28M | float max_local_dist = static_cast<float>(target_distance); |
525 | 7.28M | int x_min = std::max(0, block_x - max_block_dist); |
526 | 7.28M | int y_min = std::max(0, block_y - max_block_dist); |
527 | 7.28M | int x_max = std::min(block_width, block_x + 1 + max_block_dist); |
528 | 7.28M | int y_max = std::min(block_height, block_y + 1 + max_block_dist); |
529 | 29.3M | for (int y = y_min; y < y_max; ++y) { |
530 | 95.4M | for (int x = x_min; x < x_max; ++x) { |
531 | 73.2M | max_local_dist = |
532 | 73.2M | std::max(max_local_dist, max_dist_per_block[y * block_width + x]); |
533 | 73.2M | } |
534 | 22.1M | } |
535 | 7.28M | if (direction > 0) { |
536 | 742k | if (max_dist_per_block[block_ix] <= target_distance && |
537 | 438k | max_local_dist <= 1.1 * target_distance) { |
538 | 378k | (*block_weight)[block_ix] = 1.0; |
539 | 378k | } |
540 | 6.53M | } else { |
541 | 6.53M | constexpr double kLocalMaxWeight = 0.5; |
542 | 6.53M | if (max_dist_per_block[block_ix] <= |
543 | 6.53M | (1 - kLocalMaxWeight) * target_distance + |
544 | 6.53M | kLocalMaxWeight * max_local_dist) { |
545 | 3.79M | continue; |
546 | 3.79M | } |
547 | 10.1M | for (int y = y_min; y < y_max; ++y) { |
548 | 27.8M | for (int x = x_min; x < x_max; ++x) { |
549 | 20.4M | int d = std::max(std::abs(y - block_y), std::abs(x - block_x)); |
550 | 20.4M | int ix = y * block_width + x; |
551 | 20.4M | (*block_weight)[ix] = std::max<float>( |
552 | 20.4M | (*block_weight)[ix], 1.0f / (d + 1.0f)); |
553 | 20.4M | } |
554 | 7.45M | } |
555 | 2.74M | } |
556 | 7.28M | } |
557 | 1.03M | } |
558 | 170k | } |
559 | | |
560 | 182k | double ButteraugliComparator::ScoreOutputSize(int size) const { |
561 | 182k | return ScoreJPEG(distance_, size, target_distance_); |
562 | 182k | } |
563 | | |
564 | | |
565 | | } // namespace guetzli |