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
/*
* // Copyright (c) Radzivon Bartoshyk 5/2026. All rights reserved.
* //
* // Redistribution and use in source and binary forms, with or without modification,
* // are permitted provided that the following conditions are met:
* //
* // 1. Redistributions of source code must retain the above copyright notice, this
* // list of conditions and the following disclaimer.
* //
* // 2. Redistributions in binary form must reproduce the above copyright notice,
* // this list of conditions and the following disclaimer in the documentation
* // and/or other materials provided with the distribution.
* //
* // 3. Neither the name of the copyright holder nor the names of its
* // contributors may be used to endorse or promote products derived from
* // this software without specific prior written permission.
* //
* // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
use crate::util::{EncodeError, try_vec};
#[derive(Clone)]
pub(crate) struct Plane<T> {
data: Vec<T>,
xsize: usize,
ysize: usize,
}
impl<T: Copy + Default> Plane<T> {
pub(crate) fn new(xsize: usize, ysize: usize) -> Self {
Self {
data: vec![T::default(); xsize * ysize],
xsize,
ysize,
}
}
#[cfg(test)]
pub(crate) fn new_fill(xsize: usize, ysize: usize, val: T) -> Self {
Self {
data: vec![val; xsize * ysize],
xsize,
ysize,
}
}
pub(crate) fn try_new(xsize: usize, ysize: usize) -> Result<Self, EncodeError> {
Self::try_new_fill(xsize, ysize, T::default())
}
pub(crate) fn try_new_fill(xsize: usize, ysize: usize, val: T) -> Result<Self, EncodeError> {
let len = xsize
.checked_mul(ysize)
.ok_or(EncodeError::AllocationFailed { size: usize::MAX })?;
Ok(Self {
data: try_vec![val; len]?,
xsize,
ysize,
})
}
#[cfg(test)]
fn resize(&mut self, xsize: usize, ysize: usize) {
self.data.resize(xsize * ysize, T::default());
self.xsize = xsize;
self.ysize = ysize;
}
#[inline]
pub(crate) fn xsize(&self) -> usize {
self.xsize
}
#[inline]
pub(crate) fn ysize(&self) -> usize {
self.ysize
}
#[inline]
pub(crate) fn row(&self, y: usize) -> &[T] {
let w = self.xsize;
&self.data[y * w..(y + 1) * w]
}
#[inline]
pub(crate) fn as_slice(&self) -> &[T] {
&self.data
}
#[inline]
pub(crate) fn as_mut_slice(&mut self) -> &mut [T] {
&mut self.data
}
#[inline]
pub(crate) fn row_mut(&mut self, y: usize) -> &mut [T] {
let w = self.xsize;
&mut self.data[y * w..(y + 1) * w]
}
/// Borrow one row immutably and a different one mutably. Useful for
/// replicating a row to fill padding.
#[cfg(test)]
#[inline]
pub(crate) fn two_rows_mut_safe(&mut self, y_src: usize, y_dst: usize) -> (&[T], &mut [T]) {
let w = self.xsize;
debug_assert!(y_src != y_dst);
if y_src < y_dst {
let (a, b) = self.data.split_at_mut(y_dst * w);
(&a[y_src * w..(y_src + 1) * w], &mut b[..w])
} else {
let (a, b) = self.data.split_at_mut(y_src * w);
(&b[..w], &mut a[y_dst * w..(y_dst + 1) * w])
}
}
}
pub(crate) type ImageB = Plane<u8>;
pub(crate) type ImageSB = Plane<i8>;
#[derive(Clone)]
pub(crate) struct Image3<T> {
planes: [Plane<T>; 3],
}
impl<T: Copy + Default> Image3<T> {
/// Builds an image whose planes have independent dimensions, as chroma
/// subsampling requires.
pub(crate) fn try_new_per_plane(sizes: [(usize, usize); 3]) -> Result<Self, EncodeError> {
Ok(Self {
planes: [
Plane::try_new(sizes[0].0, sizes[0].1)?,
Plane::try_new(sizes[1].0, sizes[1].1)?,
Plane::try_new(sizes[2].0, sizes[2].1)?,
],
})
}
pub(crate) fn new(xsize: usize, ysize: usize) -> Self {
Self {
planes: [
Plane::new(xsize, ysize),
Plane::new(xsize, ysize),
Plane::new(xsize, ysize),
],
}
}
pub(crate) fn try_new(xsize: usize, ysize: usize) -> Result<Self, EncodeError> {
Ok(Self {
planes: [
Plane::try_new(xsize, ysize)?,
Plane::try_new(xsize, ysize)?,
Plane::try_new(xsize, ysize)?,
],
})
}
/// Reuse pixel storage for a new image that the caller will overwrite.
#[cfg(test)]
pub(crate) fn resize(&mut self, xsize: usize, ysize: usize) {
for plane in &mut self.planes {
plane.resize(xsize, ysize);
}
}
#[inline]
pub(crate) fn xsize(&self) -> usize {
self.planes[0].xsize
}
#[inline]
pub(crate) fn ysize(&self) -> usize {
self.planes[0].ysize
}
#[inline]
pub(crate) fn plane(&self, c: usize) -> &Plane<T> {
&self.planes[c]
}
#[inline]
pub(crate) fn plane_mut(&mut self, c: usize) -> &mut Plane<T> {
&mut self.planes[c]
}
#[inline]
pub(crate) fn all_planes_mut(&mut self) -> [&mut Plane<T>; 3] {
let [a, b, c] = &mut self.planes;
[a, b, c]
}
#[inline]
pub(crate) fn plane_row(&self, c: usize, y: usize) -> &[T] {
self.planes[c].row(y)
}
/// Whole plane as a contiguous row-major slice (length `xsize*ysize`).
/// Lets hot inner loops index `data[y*w + x]` directly instead of paying a
/// row-slice creation per access through `plane_row`.
pub(crate) fn plane_data(&self, c: usize) -> &[T] {
&self.planes[c].data
}
#[inline]
pub(crate) fn plane_row_mut(&mut self, c: usize, y: usize) -> &mut [T] {
self.planes[c].row_mut(y)
}
/// Borrow one row from each plane simultaneously (mutable).
pub(crate) fn all_plane_rows_mut(&mut self, y: usize) -> [&mut [T]; 3] {
let [p0, p1, p2] = &mut self.planes;
[p0.row_mut(y), p1.row_mut(y), p2.row_mut(y)]
}
/// Split every plane into `n` disjoint row-bands of `[r, g, b]` slices, for
/// per-pixel passes that run bands in parallel.
pub(crate) fn row_bands_mut(&mut self, n: usize) -> Vec<[&mut [T]; 3]> {
let (w, h) = (self.xsize(), self.ysize());
let n = n.max(1).min(h.max(1));
let band = h.div_ceil(n);
let [p0, p1, p2] = &mut self.planes;
let (mut d0, mut d1, mut d2) = (&mut p0.data[..], &mut p1.data[..], &mut p2.data[..]);
let mut bands = Vec::with_capacity(n);
let mut y = 0;
while y < h {
let cut = band.min(h - y) * w;
let (a0, r0) = d0.split_at_mut(cut);
let (a1, r1) = d1.split_at_mut(cut);
let (a2, r2) = d2.split_at_mut(cut);
bands.push([a0, a1, a2]);
(d0, d1, d2) = (r0, r1, r2);
y += cut / w;
}
bands
}
}
pub(crate) type Image3B = Image3<u8>;
pub(crate) type Image3S = Image3<i16>;
pub(crate) type Image3Si = Image3<i32>;
pub(crate) type Image3F = Image3<f32>;
/// Axis-aligned rectangle within an image (coordinates in units of the image:
/// pixels, blocks, or tiles depending on context).
#[derive(Clone, Copy, Debug)]
pub(crate) struct Rect {
pub(crate) x0: usize,
pub(crate) y0: usize,
pub(crate) xsize: usize,
pub(crate) ysize: usize,
}
impl Rect {
pub(crate) const fn new(x0: usize, y0: usize, xsize: usize, ysize: usize) -> Self {
Self {
x0,
y0,
xsize,
ysize,
}
}
}