Improve readability of image::atlas padding logic
This commit is contained in:
parent
2bb7b50421
commit
67730a8bfa
1 changed files with 49 additions and 39 deletions
|
|
@ -337,70 +337,80 @@ impl Atlas {
|
||||||
device,
|
device,
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut padded_data = buffer_slice.get_mapped_range_mut();
|
const PIXEL: usize = 4;
|
||||||
let padding_width = padding.width as usize;
|
|
||||||
let padding_height = padding.height as usize;
|
let mut fragment = buffer_slice.get_mapped_range_mut();
|
||||||
|
let pad_w = padding.width as usize;
|
||||||
|
let pad_h = padding.height as usize;
|
||||||
|
let stride = PIXEL * width as usize;
|
||||||
|
|
||||||
// Copy image rows
|
// Copy image rows
|
||||||
for row in 0..height as usize {
|
for row in 0..height as usize {
|
||||||
let offset = offset + row * 4 * image_width as usize;
|
let src = offset + row * PIXEL * image_width as usize;
|
||||||
let start = (row + padding_height) * bytes_per_row;
|
let dst = (row + pad_h) * bytes_per_row;
|
||||||
let stride = 4 * width as usize;
|
|
||||||
|
|
||||||
padded_data
|
fragment[dst + PIXEL * pad_w..dst + PIXEL * pad_w + stride]
|
||||||
[start + 4 * padding_width..start + 4 * padding_width + stride]
|
.copy_from_slice(&pixels[src..src + stride]);
|
||||||
.copy_from_slice(&pixels[offset..offset + stride]);
|
|
||||||
|
|
||||||
// Add padding to the sides, if needed
|
// Add padding to the sides, if needed
|
||||||
for i in 0..padding_width {
|
for i in 0..pad_w {
|
||||||
padded_data[start + 4 * i..start + 4 * (i + 1)]
|
fragment[dst + PIXEL * i..dst + PIXEL * (i + 1)]
|
||||||
.copy_from_slice(&pixels[offset..offset + 4]);
|
.copy_from_slice(&pixels[src..src + PIXEL]);
|
||||||
|
|
||||||
padded_data[start + stride + 4 * (padding_width + i)
|
fragment[dst + stride + PIXEL * (pad_w + i)
|
||||||
..start + stride + 4 * (padding_width + i + 1)]
|
..dst + stride + PIXEL * (pad_w + i + 1)]
|
||||||
.copy_from_slice(
|
.copy_from_slice(
|
||||||
&pixels[offset + stride - 4..offset + stride],
|
&pixels[src + stride - PIXEL..src + stride],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add padding on top and bottom
|
// Add padding on top and bottom
|
||||||
for row in 0..padding_height {
|
for row in 0..pad_h {
|
||||||
let start = row * bytes_per_row;
|
let dst_top = row * bytes_per_row;
|
||||||
let end = (padding_height + height as usize + row) * bytes_per_row;
|
let dst_bottom = (pad_h + height as usize + row) * bytes_per_row;
|
||||||
let last =
|
let src_top = offset;
|
||||||
offset + (height - 1) as usize * 4 * image_width as usize;
|
let src_bottom =
|
||||||
|
offset + (height - 1) as usize * PIXEL * image_width as usize;
|
||||||
|
|
||||||
// Top
|
// Top
|
||||||
padded_data[start + 4 * padding_width
|
fragment[dst_top + PIXEL * pad_w
|
||||||
..start + 4 * (padding_width + width as usize)]
|
..dst_top + PIXEL * (pad_w + width as usize)]
|
||||||
.copy_from_slice(&pixels[offset..offset + 4 * width as usize]);
|
.copy_from_slice(
|
||||||
|
&pixels[src_top..src_top + PIXEL * width as usize],
|
||||||
|
);
|
||||||
|
|
||||||
// Bottom
|
// Bottom
|
||||||
padded_data[end + 4 * padding_width
|
fragment[dst_bottom + PIXEL * pad_w
|
||||||
..end + 4 * (padding_width + width as usize)]
|
..dst_bottom + PIXEL * (pad_w + width as usize)]
|
||||||
.copy_from_slice(&pixels[last..last + 4 * width as usize]);
|
.copy_from_slice(
|
||||||
|
&pixels[src_bottom..src_bottom + PIXEL * width as usize],
|
||||||
|
);
|
||||||
|
|
||||||
// Corners
|
// Corners
|
||||||
for i in 0..padding_width {
|
for i in 0..pad_w {
|
||||||
padded_data[start + 4 * i..start + 4 * (i + 1)]
|
// Top left
|
||||||
.copy_from_slice(&pixels[offset..offset + 4]);
|
fragment[dst_top + PIXEL * i..dst_top + PIXEL * (i + 1)]
|
||||||
|
.copy_from_slice(&pixels[offset..offset + PIXEL]);
|
||||||
|
|
||||||
padded_data[start + 4 * (width as usize + padding_width + i)
|
// Top right
|
||||||
..start + 4 * (width as usize + padding_width + i + 1)]
|
fragment[dst_top + PIXEL * (width as usize + pad_w + i)
|
||||||
|
..dst_top + PIXEL * (width as usize + pad_w + i + 1)]
|
||||||
.copy_from_slice(
|
.copy_from_slice(
|
||||||
&pixels[offset + 4 * (width - 1) as usize
|
&pixels[offset + PIXEL * (width - 1) as usize
|
||||||
..offset + 4 * width as usize],
|
..offset + PIXEL * width as usize],
|
||||||
);
|
);
|
||||||
|
|
||||||
padded_data[end + 4 * i..end + 4 * (i + 1)]
|
// Bottom left
|
||||||
.copy_from_slice(&pixels[last..last + 4]);
|
fragment[dst_bottom + PIXEL * i..dst_bottom + PIXEL * (i + 1)]
|
||||||
|
.copy_from_slice(&pixels[src_bottom..src_bottom + PIXEL]);
|
||||||
|
|
||||||
padded_data[end + 4 * (width as usize + padding_width + i)
|
// Bottom right
|
||||||
..end + 4 * (width as usize + padding_width + i + 1)]
|
fragment[dst_bottom + PIXEL * (width as usize + pad_w + i)
|
||||||
|
..dst_bottom + PIXEL * (width as usize + pad_w + i + 1)]
|
||||||
.copy_from_slice(
|
.copy_from_slice(
|
||||||
&pixels[last + 4 * (width - 1) as usize
|
&pixels[src_bottom + PIXEL * (width - 1) as usize
|
||||||
..last + 4 * width as usize],
|
..src_bottom + PIXEL * width as usize],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue