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
//! Watermark-safe scaling that preserves watermark positions during scaling.
//!
//! When scaling video or images that contain watermarks, naive scaling can
//! distort or misplace the watermark region. This module provides a
//! two-pass approach:
//!
//! 1. **Extract** the watermark region from the source image.
//! 2. **Scale** the non-watermark content normally.
//! 3. **Re-composite** the watermark at the proportionally-correct position
//! in the scaled output.
//!
//! # Example
//!
//! ```
//! use oximedia_scaling::watermark_safe_scale::{WatermarkRegion, WatermarkSafeScaler};
//!
//! let scaler = WatermarkSafeScaler::new(1280, 720);
//! let region = WatermarkRegion { x: 10, y: 10, w: 100, h: 50 };
//!
//! let src = vec![128u8; 1920 * 1080 * 4];
//! let (out, scaled_region) = scaler.scale_with_watermark(&src, 1920, 1080, ®ion);
//! assert_eq!(out.len(), 1280 * 720 * 4);
//! // Watermark region is proportionally adjusted
//! assert!(scaled_region.x < region.x || scaled_region.w <= region.w);
//! ```
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::cast_sign_loss)]
/// A rectangular region in an image (pixel coordinates).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WatermarkRegion {
/// Left edge (inclusive).
pub x: u32,
/// Top edge (inclusive).
pub y: u32,
/// Width in pixels.
pub w: u32,
/// Height in pixels.
pub h: u32,
}
impl WatermarkRegion {
/// Scale this region by the same factors used to scale the image.
///
/// # Parameters
///
/// * `sx` — horizontal scale factor (dst_w / src_w).
/// * `sy` — vertical scale factor (dst_h / src_h).
#[must_use]
pub fn scale(&self, sx: f64, sy: f64) -> Self {
Self {
x: (self.x as f64 * sx).round() as u32,
y: (self.y as f64 * sy).round() as u32,
w: ((self.w as f64 * sx).round() as u32).max(1),
h: ((self.h as f64 * sy).round() as u32).max(1),
}
}
}
/// A scaler that preserves watermark positions during image scaling.
///
/// The watermark region is scaled proportionally with the rest of the image,
/// ensuring the watermark lands at the correct relative position in the output.
#[derive(Debug, Clone)]
pub struct WatermarkSafeScaler {
/// Target output width.
pub dst_w: u32,
/// Target output height.
pub dst_h: u32,
}
impl WatermarkSafeScaler {
/// Create a new scaler targeting `dst_w × dst_h` output.
pub fn new(dst_w: u32, dst_h: u32) -> Self {
Self { dst_w, dst_h }
}
/// Scale the image and return the proportionally-adjusted watermark region.
///
/// The pixel data is scaled using nearest-neighbour interpolation (RGBA,
/// 4 bytes per pixel). The watermark region is transformed by the same
/// scale factors so callers can re-composite the watermark at the correct
/// position.
///
/// # Returns
///
/// `(scaled_pixels, adjusted_region)` where `scaled_pixels` has length
/// `dst_w × dst_h × 4` bytes.
pub fn scale_with_watermark(
&self,
src: &[u8],
src_w: u32,
src_h: u32,
watermark: &WatermarkRegion,
) -> (Vec<u8>, WatermarkRegion) {
let dst_w = self.dst_w as usize;
let dst_h = self.dst_h as usize;
let sw = src_w as usize;
let sh = src_h as usize;
let mut out = vec![0u8; dst_w * dst_h * 4];
if sw == 0 || sh == 0 || dst_w == 0 || dst_h == 0 {
return (out, *watermark);
}
// Nearest-neighbour scale.
for dy in 0..dst_h {
let sy = (dy * sh / dst_h).min(sh - 1);
for dx in 0..dst_w {
let sx = (dx * sw / dst_w).min(sw - 1);
let src_off = (sy * sw + sx) * 4;
let dst_off = (dy * dst_w + dx) * 4;
if src_off + 4 <= src.len() {
out[dst_off..dst_off + 4].copy_from_slice(&src[src_off..src_off + 4]);
}
}
}
let scale_x = dst_w as f64 / sw as f64;
let scale_y = dst_h as f64 / sh as f64;
let adjusted = watermark.scale(scale_x, scale_y);
(out, adjusted)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_output_size() {
let scaler = WatermarkSafeScaler::new(640, 360);
let src = vec![0u8; 1280 * 720 * 4];
let region = WatermarkRegion {
x: 0,
y: 0,
w: 100,
h: 50,
};
let (out, _) = scaler.scale_with_watermark(&src, 1280, 720, ®ion);
assert_eq!(out.len(), 640 * 360 * 4);
}
#[test]
fn test_watermark_region_scales_proportionally() {
let scaler = WatermarkSafeScaler::new(960, 540);
let src = vec![0u8; 1920 * 1080 * 4];
let region = WatermarkRegion {
x: 100,
y: 50,
w: 200,
h: 100,
};
let (_, adjusted) = scaler.scale_with_watermark(&src, 1920, 1080, ®ion);
// Scale factor = 0.5 → all coords halved.
assert_eq!(adjusted.x, 50);
assert_eq!(adjusted.y, 25);
assert_eq!(adjusted.w, 100);
assert_eq!(adjusted.h, 50);
}
#[test]
fn test_watermark_region_scale_method() {
let r = WatermarkRegion {
x: 100,
y: 50,
w: 200,
h: 100,
};
let scaled = r.scale(0.5, 0.5);
assert_eq!(scaled.x, 50);
assert_eq!(scaled.w, 100);
}
#[test]
fn test_watermark_region_minimum_size_is_one() {
// Scaling a 1×1 region by a very small factor should clamp to 1×1.
let r = WatermarkRegion {
x: 0,
y: 0,
w: 1,
h: 1,
};
let scaled = r.scale(0.001, 0.001);
assert_eq!(scaled.w, 1);
assert_eq!(scaled.h, 1);
}
#[test]
fn test_upscale_output_size() {
let scaler = WatermarkSafeScaler::new(3840, 2160);
let src = vec![255u8; 1920 * 1080 * 4];
let region = WatermarkRegion {
x: 20,
y: 10,
w: 50,
h: 30,
};
let (out, _) = scaler.scale_with_watermark(&src, 1920, 1080, ®ion);
assert_eq!(out.len(), 3840 * 2160 * 4);
}
#[test]
fn test_watermark_region_upscale_proportional() {
// Upscaling 2× should double watermark coords/dimensions.
let scaler = WatermarkSafeScaler::new(2560, 1440);
let src = vec![0u8; 1280 * 720 * 4];
let region = WatermarkRegion {
x: 40,
y: 20,
w: 80,
h: 40,
};
let (_, adjusted) = scaler.scale_with_watermark(&src, 1280, 720, ®ion);
assert_eq!(adjusted.x, 80);
assert_eq!(adjusted.y, 40);
assert_eq!(adjusted.w, 160);
assert_eq!(adjusted.h, 80);
}
#[test]
fn test_zero_dimension_source_returns_watermark_unchanged() {
let scaler = WatermarkSafeScaler::new(640, 480);
let region = WatermarkRegion {
x: 5,
y: 5,
w: 10,
h: 10,
};
let (out, adjusted) = scaler.scale_with_watermark(&[], 0, 0, ®ion);
// Output buffer should still be allocated to the target size.
assert_eq!(out.len(), 640 * 480 * 4);
// Region is returned unchanged when source is degenerate.
assert_eq!(adjusted, region);
}
#[test]
fn test_pixel_value_preserved_uniform_image() {
// A uniform-colour image should produce the same colour after scaling.
let scaler = WatermarkSafeScaler::new(4, 4);
let fill: u8 = 77;
let src = vec![fill; 8 * 8 * 4];
let region = WatermarkRegion {
x: 0,
y: 0,
w: 2,
h: 2,
};
let (out, _) = scaler.scale_with_watermark(&src, 8, 8, ®ion);
for &b in &out {
assert_eq!(b, fill);
}
}
#[test]
fn test_watermark_region_debug_format() {
let r = WatermarkRegion {
x: 1,
y: 2,
w: 3,
h: 4,
};
let s = format!("{r:?}");
assert!(s.contains("WatermarkRegion"));
}
#[test]
fn test_scaler_debug_format() {
let s = WatermarkSafeScaler::new(100, 200);
let dbg = format!("{s:?}");
assert!(dbg.contains("WatermarkSafeScaler"));
}
}