-
Notifications
You must be signed in to change notification settings - Fork 1
/
known_size_merging.rs
259 lines (214 loc) · 7.24 KB
/
known_size_merging.rs
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
use image::imageops::overlay;
use image_merger::*;
static IMAGES_PER_ROW: u32 = 10;
static TOTAL_ROWS: u32 = 10;
static TOTAL_IMAGES: u32 = 100;
static PADDING_X: u32 = 10;
static PADDING_Y: u32 = 10;
static IMAGE_WIDTH: u32 = 100;
static IMAGE_HEIGHT: u32 = 100;
type RgbaImageBuffer = BufferedImage<Rgba<u8>>;
// TODO: Move this to a test utils file, make it generic, and take in an image index for unique colors
// across an entire image.
fn generate_test_square() -> RgbaImageBuffer {
let color = |x: u32, y: u32| -> Rgba<u8> {
let r = x as u8;
let g = y as u8;
let b = (x + y) as u8;
Rgba([r, g, b, 255])
};
let mut image = RgbaImageBuffer::new(IMAGE_WIDTH, IMAGE_HEIGHT);
for x in 0..IMAGE_WIDTH {
for y in 0..IMAGE_HEIGHT {
image.put_pixel(x, y, color(x, y));
}
}
image
}
fn merge_images_slow(
images_per_row: u32,
total_images: u32,
padding_x: u32,
padding_y: u32,
) -> RgbaImageBuffer {
// Cieling division for total rows.
let total_rows = (total_images + images_per_row - 1) / images_per_row;
let test_square = generate_test_square();
let mut canvas = RgbaImageBuffer::new(
IMAGE_WIDTH * images_per_row + (padding_x * (images_per_row - 1)),
IMAGE_HEIGHT * total_rows + (padding_y * (total_rows - 1)),
);
for index in 0..total_images {
let global_x = index % images_per_row;
let global_y = index / images_per_row;
let x = (global_x * IMAGE_WIDTH) + (global_x * padding_x);
let y = (global_y * IMAGE_HEIGHT) + (global_y * padding_y);
overlay(&mut *canvas, &*test_square, x as i64, y as i64)
}
canvas
}
#[test]
fn test_slow_merge() {
let merged = merge_images_slow(IMAGES_PER_ROW, TOTAL_IMAGES, 0, 0);
assert_eq!(merged.width(), IMAGES_PER_ROW * IMAGE_WIDTH);
assert_eq!(merged.height(), IMAGES_PER_ROW * IMAGE_HEIGHT);
}
#[test]
fn test_slow_merge_padding() {
let merged = merge_images_slow(IMAGES_PER_ROW, TOTAL_IMAGES, PADDING_X, PADDING_Y);
assert_eq!(
merged.width(),
IMAGES_PER_ROW * IMAGE_WIDTH + (PADDING_X * (IMAGES_PER_ROW - 1))
);
assert_eq!(
merged.height(),
TOTAL_ROWS * IMAGE_HEIGHT + (PADDING_Y * (TOTAL_ROWS - 1))
);
}
#[test]
fn test_push_merge() {
let test_square = generate_test_square();
let slow_merge = merge_images_slow(10, TOTAL_IMAGES, 0, 0);
let mut merger: KnownSizeMerger<Rgba<u8>, _> = KnownSizeMerger::new(
(IMAGE_WIDTH, IMAGE_HEIGHT),
IMAGES_PER_ROW,
TOTAL_IMAGES,
None,
);
for _ in 0..TOTAL_IMAGES {
merger.push(&test_square);
}
assert_eq!(merger.get_canvas(), &slow_merge);
}
#[test]
fn test_bulk_push_merge() {
let test_square = generate_test_square();
let slow_merge = merge_images_slow(10, TOTAL_IMAGES, 0, 0);
let mut merger: KnownSizeMerger<Rgba<u8>, _> = KnownSizeMerger::new(
(IMAGE_WIDTH, IMAGE_HEIGHT),
IMAGES_PER_ROW,
TOTAL_IMAGES,
None,
);
merger.bulk_push(&vec![&test_square; TOTAL_IMAGES as usize]);
assert_eq!(merger.get_canvas(), &slow_merge);
}
#[test]
fn test_push_merge_padding() {
let test_square: RgbaImageBuffer = generate_test_square();
let slow_merge = merge_images_slow(IMAGES_PER_ROW, TOTAL_IMAGES, PADDING_X, PADDING_Y);
let mut merger: KnownSizeMerger<Rgba<u8>, _> = KnownSizeMerger::new(
(IMAGE_WIDTH, IMAGE_HEIGHT),
IMAGES_PER_ROW,
TOTAL_IMAGES,
Some(Padding {
x: PADDING_X,
y: PADDING_Y,
}),
);
for _ in 0..TOTAL_IMAGES {
merger.push(&test_square);
}
assert_eq!(merger.get_canvas(), &slow_merge);
}
#[test]
fn test_bulk_push_merge_padding() {
let test_square = generate_test_square();
let slow_merge = merge_images_slow(IMAGES_PER_ROW, TOTAL_IMAGES, PADDING_X, PADDING_Y);
let mut merger: KnownSizeMerger<Rgba<u8>, _> = KnownSizeMerger::new(
(IMAGE_WIDTH, IMAGE_HEIGHT),
IMAGES_PER_ROW,
TOTAL_IMAGES,
Some(Padding {
x: PADDING_X,
y: PADDING_Y,
}),
);
merger.bulk_push(&vec![&test_square; TOTAL_IMAGES as usize]);
assert_eq!(merger.get_canvas(), &slow_merge);
}
#[test]
fn test_remove_image() {
// 99 images on the slow merge should be equal to 100 images on the fast merge minus the 1 removed image.
let test_square = generate_test_square();
let slow_merge = merge_images_slow(IMAGES_PER_ROW, TOTAL_IMAGES - 1, 0, 0);
let mut merger: KnownSizeMerger<Rgba<u8>, _> = KnownSizeMerger::new(
(IMAGE_WIDTH, IMAGE_HEIGHT),
IMAGES_PER_ROW,
TOTAL_IMAGES,
None,
);
merger.bulk_push(&vec![&test_square; TOTAL_IMAGES as usize]);
merger.remove_image(99);
assert_eq!(merger.get_canvas(), &slow_merge);
}
#[test]
fn test_raw_create_image() {
type P = Rgb<u8>;
let container: Vec<u8> =
vec![0; (IMAGE_WIDTH * IMAGE_HEIGHT * <P as image::Pixel>::CHANNEL_COUNT as u32) as usize];
let image: Option<BufferedImage<P>> = Image::new_from_raw(IMAGE_WIDTH, IMAGE_HEIGHT, container);
assert!(image.is_some());
let image = image.unwrap();
assert_eq!(image.width(), IMAGE_WIDTH);
assert_eq!(image.height(), IMAGE_HEIGHT);
}
#[test]
fn test_raw_known_size_merger_create() {
type P = Rgb<u8>;
let container = vec![
0;
(IMAGE_WIDTH * IMAGE_HEIGHT * <P as image::Pixel>::CHANNEL_COUNT as u32 * TOTAL_IMAGES)
as usize
];
let merger: Option<KnownSizeMerger<P, Vec<u8>>> = KnownSizeMerger::new_from_raw(
(IMAGE_WIDTH, IMAGE_HEIGHT),
IMAGES_PER_ROW,
TOTAL_IMAGES,
None,
container,
);
assert!(merger.is_some());
let merger = merger.unwrap();
assert_eq!(merger.get_canvas().width(), IMAGE_WIDTH * IMAGES_PER_ROW);
assert_eq!(merger.get_canvas().height(), IMAGE_HEIGHT * TOTAL_ROWS);
}
#[test]
fn test_resizable_known_size_merger() {
let mut merger: KnownSizeMerger<Rgba<u8>, _> = KnownSizeMerger::new(
(IMAGE_WIDTH, IMAGE_HEIGHT),
IMAGES_PER_ROW,
TOTAL_IMAGES,
None,
);
// Create an image that is twice the size of the original image. Using a known
// resize function to ensure the image is resized correctly.
let test_square = image::imageops::resize(
&generate_test_square().into_buffer(),
IMAGE_WIDTH * 2,
IMAGE_HEIGHT * 2,
image::imageops::FilterType::Nearest,
);
// Push the image into the merger.
merger.push_resized(&Image::from(test_square));
assert!(merger.get_num_images() == 1);
}
#[test]
fn test_resizable_bulk_known_size_merger() {
let mut merger: KnownSizeMerger<Rgba<u8>, _> = KnownSizeMerger::new(
(IMAGE_WIDTH, IMAGE_HEIGHT),
IMAGES_PER_ROW,
TOTAL_IMAGES,
None,
);
// Create an image that is twice the size of the original image.
let test_square = image::imageops::resize(
&generate_test_square().into_buffer(),
IMAGE_WIDTH * 2,
IMAGE_HEIGHT * 2,
image::imageops::FilterType::Nearest,
);
// Push the image into the merger.
merger.bulk_push_resized(&vec![&Image::from(test_square); TOTAL_IMAGES as usize]);
assert!(merger.get_num_images() == TOTAL_IMAGES);
}