Repository navigation
Expand file tree
/
Copy pathmedia_edit.cpp
More file actions
327 lines (270 loc) · 11.7 KB
/
Copy pathmedia_edit.cpp
File metadata and controls
327 lines (270 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// This file is part of the Diffractor photo and video organizer
// Copyright 2026 Zac Walker
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// Purpose: A photo's edits applied to its pixels, a byte a channel or sixteen bits: perspective,
// straightening, crop and scale - resampled with Catmull-Rom, as the edit preview draws - and then the
// colour adjustments.
#include "pch.h"
#include "files.h"
static simple_transform to_simple_transform(const image_edits& pe)
{
const auto a = pe.rotation_angle();
if (core::equiv(a, -90) || core::equiv(a, 270))
{
return core::simple_transform::rot_270;
}
if (core::equiv(a, 90) || core::equiv(a, -270))
{
return core::simple_transform::rot_90;
}
if (core::equiv(a, 180) || core::equiv(a, -180))
{
return core::simple_transform::rot_180;
}
return core::simple_transform::none;
}
// Pixels are four channels, a byte each or sixteen bits each, the fourth alpha where the format has it:
// what follows treats the three colour channels alike, so their order does not matter to it.
// get_pixel answers transparent black outside the surface, so an interpolated edge sample would drag
// every rotated, straightened or perspective-corrected border toward black. Clamp to the edge instead.
template <typename T> static __forceinline const T* clamped_pixel(const core::surface& src, const int x, const int y)
{
const auto cx = std::clamp(x, 0, static_cast<int>(src.width()) - 1);
const auto cy = std::clamp(y, 0, static_cast<int>(src.height()) - 1);
return std::bit_cast<const T*>(src.pixels_line(cy)) + static_cast<size_t>(cx) * 4;
}
// A pixel where it lies, or transparent black outside the surface, as get_pixel answers.
template <typename T>
static __forceinline void copy_pixel(const core::surface& src, const int x, const int y, T* const out)
{
if (x < 0 || y < 0 || x >= static_cast<int>(src.width()) || y >= static_cast<int>(src.height()))
{
out[0] = out[1] = out[2] = out[3] = 0;
return;
}
std::memcpy(out, std::bit_cast<const T*>(src.pixels_line(y)) + static_cast<size_t>(x) * 4, 4 * sizeof(T));
}
static __forceinline double catmull_rom_weight(const double t)
{
const auto a = std::abs(t);
if (a < 1.0) return ((1.5 * a - 2.5) * a) * a + 1.0;
if (a < 2.0) return ((-0.5 * a + 2.5) * a - 4.0) * a + 2.0;
return 0.0;
}
// Catmull-Rom, the same sampler the edit preview draws with, so a saved straighten keeps the detail
// the preview showed rather than the extra softening bilinear leaves behind.
template <typename T>
static void sample_catmull_rom(const core::surface& src, const double x, const double y, const bool has_alpha,
T* const out)
{
constexpr auto maximum = static_cast<double>(std::numeric_limits<T>::max());
const auto left_f = floor(x);
const auto top_f = floor(y);
const auto left = static_cast<int>(left_f);
const auto top = static_cast<int>(top_f);
const auto offset_x = x - left_f;
const auto offset_y = y - top_f;
double weights_x[4];
double weights_y[4];
for (auto i = 0; i < 4; ++i)
{
weights_x[i] = catmull_rom_weight(offset_x - (i - 1));
weights_y[i] = catmull_rom_weight(offset_y - (i - 1));
}
double colour[3] = {};
auto alpha = 0.0;
for (auto j = 0; j < 4; ++j)
{
for (auto i = 0; i < 4; ++i)
{
const auto* const c = clamped_pixel<T>(src, left + i - 1, top + j - 1);
const auto weight = weights_x[i] * weights_y[j];
// Filtering straight alpha would let a transparent pixel pull its colour into the result.
const auto coverage = has_alpha ? weight * (c[3] / maximum) : weight;
for (auto channel = 0; channel < 3; ++channel) colour[channel] += c[channel] * coverage;
alpha += coverage;
}
}
if (alpha > 1e-6)
{
for (auto& channel : colour) channel /= alpha;
}
for (auto channel = 0; channel < 3; ++channel)
{
out[channel] = static_cast<T>(std::clamp(std::round(colour[channel]), 0.0, maximum));
}
out[3] = static_cast<T>(has_alpha ? std::clamp(std::round(alpha * maximum), 0.0, maximum) : maximum);
}
core::const_surface_ptr apply_image_edits(const core::const_surface_ptr& source, const image_edits& photo_edits,
const core::cancel_token& token)
{
const auto dimensions = source->dimensions();
const auto format = source->format();
// Sixteen bits a channel are resampled at sixteen, so a wide picture keeps its precision to the save.
const auto wide = core::is_wide(format);
const auto has_alpha = core::has_alpha(format);
if (photo_edits.has_perspective())
{
auto canvas = std::make_shared<core::surface>();
if (!canvas->alloc(dimensions, format)) return {};
const auto display_angle = to_radian(-photo_edits.crop_bounds(dimensions).angle());
const auto angle_sin = sin(display_angle);
const auto angle_cos = cos(display_angle);
const auto horizontal = photo_edits.perspective_horizontal() * angle_cos -
photo_edits.perspective_vertical() * angle_sin;
const auto vertical = photo_edits.perspective_horizontal() * angle_sin +
photo_edits.perspective_vertical() * angle_cos;
const auto correct = [&](auto channel)
{
using T = decltype(channel);
for (auto y = 0u; y < canvas->height(); ++y)
{
if (token.is_cancelled()) return false;
auto* const dst_line = std::bit_cast<T*>(canvas->pixels_line(y));
const auto normalized_y = (y + 0.5) / canvas->height() - 0.5;
for (auto x = 0u; x < canvas->width(); ++x)
{
const auto normalized_x = (x + 0.5) / canvas->width() - 0.5;
const auto denominator = 1.0 + horizontal * normalized_x + vertical * normalized_y;
// A vanishing denominator produces infinite or NaN source coordinates, and the sampling
// below indexes with them.
if (std::abs(denominator) < 1e-12) continue;
const auto source_x = (0.5 + normalized_x / denominator) * dimensions.cx - 0.5;
const auto source_y = (0.5 + normalized_y / denominator) * dimensions.cy - 0.5;
sample_catmull_rom(*source, source_x, source_y, has_alpha, dst_line + static_cast<size_t>(x) * 4);
}
}
return true;
};
if (!(wide ? correct(uint16_t{}) : correct(uint8_t{}))) return {};
auto remaining_edits = photo_edits;
remaining_edits.perspective_horizontal(0);
remaining_edits.perspective_vertical(0);
if (photo_edits.has_crop_bounds())
{
remaining_edits.crop_bounds(photo_edits.effective_crop_bounds(dimensions));
}
return apply_image_edits(canvas, remaining_edits, token);
}
core::const_surface_ptr surface_result;
const auto rot_angle = photo_edits.rotation_angle();
const auto is_flip_rotate = !core::is_zero(rot_angle) && core::is_zero(fabs(fmod(rot_angle, 90))) &&
!photo_edits.has_crop(dimensions) && !photo_edits.has_scale();
if (is_flip_rotate)
{
surface_result = source->transform(to_simple_transform(photo_edits));
}
else if (!photo_edits.has_rotation() && !photo_edits.has_scale() && photo_edits.has_crop(dimensions))
{
// A crop must not resample. The rectangle arrives from device coordinates and lands on
// fractional source pixels, so interpolating would shift and soften every pixel it keeps.
const auto crop =
photo_edits.crop_bounds(dimensions).crop(rectd(0, 0, dimensions.cx, dimensions.cy)).bounding_rect();
const auto extent = crop.extent().round();
const auto canvas_extent = sizei(std::clamp(extent.cx, 1, dimensions.cx),
std::clamp(extent.cy, 1, dimensions.cy));
const auto left = std::clamp(core::round(crop.left()), 0, dimensions.cx - canvas_extent.cx);
const auto top = std::clamp(core::round(crop.top()), 0, dimensions.cy - canvas_extent.cy);
auto canvas = std::make_shared<core::surface>();
if (canvas->alloc(canvas_extent, format))
{
const auto pixel_bytes = static_cast<size_t>(core::bytes_per_pixel(format));
for (auto y = 0; y < canvas_extent.cy; ++y)
{
memcpy(canvas->pixels_line(y), source->pixels() + (top + y) * source->stride() + left * pixel_bytes,
static_cast<size_t>(canvas_extent.cx) * pixel_bytes);
}
surface_result = std::move(canvas);
}
}
else if (photo_edits.has_crop(dimensions) || photo_edits.has_scale() || photo_edits.has_rotation())
{
const auto crop = photo_edits.crop_bounds(dimensions).crop(rectd(0, 0, dimensions.cx, dimensions.cy));
const auto angle = -crop.angle();
const quadd bounds(dimensions);
auto aff = affined().translate(-bounds.center_point()).rotate(angle);
if (photo_edits.has_scale())
{
const auto result_dimensions = crop.transform(aff).bounding_rect().extent();
const auto limit = photo_edits.scale();
const auto scale = std::min(limit.cx / result_dimensions.Width, limit.cy / result_dimensions.Height);
aff = aff.scale(scale);
}
const auto transformed_crop = crop.transform(aff).bounding_rect();
aff = aff.translate(-transformed_crop.top_left());
const auto inc_aff = aff.invert();
const auto canvas_extent = transformed_crop.extent().round();
auto canvas = std::make_shared<core::surface>();
if (canvas->alloc(canvas_extent, format))
{
const auto dst_to_src_points0 = inc_aff.transform({0.0, 0.0});
const auto dst_to_src_points1 = inc_aff.transform({1.0, 0.0});
const auto dst_to_src_points2 = inc_aff.transform({0.0, 1.0});
const auto x_dx = dst_to_src_points1.X - dst_to_src_points0.X;
const auto x_dy = dst_to_src_points1.Y - dst_to_src_points0.Y;
const auto y_dx = dst_to_src_points2.X - dst_to_src_points0.X;
const auto y_dy = dst_to_src_points2.Y - dst_to_src_points0.Y;
const auto resample = [&](auto channel)
{
using T = decltype(channel);
for (auto y = 0u; y < canvas->height(); y++)
{
auto* const dst_line = std::bit_cast<T*>(canvas->pixels_line(y));
for (auto x = 0u; x < canvas->width(); x++)
{
const pointd src_pointf = {dst_to_src_points0.X + x * x_dx + y * y_dx,
dst_to_src_points0.Y + x * x_dy + y * y_dy};
const auto leftxf = floor(src_pointf.X);
const auto leftx = static_cast<int>(leftxf);
const auto rightx = static_cast<int>(ceil(src_pointf.X));
const auto topyf = floor(src_pointf.Y);
const auto topy = static_cast<int>(topyf);
const auto bottomy = static_cast<int>(ceil(src_pointf.Y));
auto* const dst = dst_line + static_cast<size_t>(x) * 4;
if (leftx == rightx && topy == bottomy)
{
copy_pixel(*source, leftx, topy, dst);
}
else
{
sample_catmull_rom(*source, src_pointf.X, src_pointf.Y, has_alpha, dst);
}
}
}
};
if (wide) resample(uint16_t{});
else resample(uint8_t{});
surface_result = std::move(canvas);
}
}
else
{
surface_result = source;
}
if (photo_edits.has_color_changes())
{
// A geometry step that could not allocate leaves nothing to adjust. Answering the empty
// surface here is the failure; reading its dimensions would be a null dereference.
if (!surface_result) return {};
render::color_adjust adjust;
adjust.color_params(photo_edits.vibrance(), photo_edits.saturation(), photo_edits.darks(),
photo_edits.midtones(), photo_edits.lights(), photo_edits.contrast(),
photo_edits.brightness(), photo_edits.temperature(), photo_edits.tint());
auto canvas = std::make_shared<core::surface>();
if (canvas->alloc(surface_result->dimensions(), surface_result->format()))
{
// A cancelled pass leaves the rows it never reached uninitialised, so the canvas is
// dropped rather than published: the caller asked for this result and no longer wants it.
if (!adjust.apply(surface_result, canvas->pixels(), canvas->stride(), token)) return {};
surface_result = std::move(canvas);
}
else
{
return {};
}
}
return surface_result;
}