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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use std::path::Path;
#[cfg(not(target_family = "wasm"))]
use std::time;
extern crate image;
use image::{GenericImage, RgbImage};
use log::info;
#[cfg(feature = "parallel")]
use rayon::prelude::*;
use crate::distance_matrix::DistanceMatrix;
use crate::error::{MosaicError, PhomoError};
use crate::macros;
use crate::master::Master;
use crate::metrics::{norm_l1, MetricFn};
use crate::solvers::{hungarian::Hungarian, Solve, SolverConfig};
use crate::utils;
#[derive(Debug, Clone)]
pub struct Mosaic {
/// The [`Master`] image to reconstruct.
pub master: Master,
/// The tile images to use to reconstruct the [`Master`] image. The tile images should be the
/// same size as the [`Master::cell_size`]. There should also be at least `Master::cells.len()`
/// tiles.
pub tiles: Vec<RgbImage>,
/// The number of cells horizontally and vertically in the mosaic.
pub grid_size: (u32, u32),
}
/// Represents a photo mosaic.
impl Mosaic {
/// Construct a [`Mosaic`] from a master image file and a directory of tile images.
///
/// # Arguments
/// - `master_file`: The path to the master image file.
/// - `tile_dir`: The path to the directory containing the tile images.
/// - `grid_size`: The grid size of the mosaic, the number of cells horizontally and vertically.
///
/// # Errors
/// - [`PhomoError::ImageError`]: An error occurred while reading the master image.
/// - [`PhomoError::MasterError`]: An error occurred constructing the [`Master`] from the master image.
/// - [`PhomoError::IoError`]: An error occurred while reading the tile images.
pub fn from_file_and_dir<P: AsRef<Path>, Q: AsRef<Path>>(
master_file: P,
tile_dir: Q,
grid_size: (u32, u32),
) -> Result<Self, PhomoError> {
let master_img = image::open(master_file)?.to_rgb8();
info!("Loading tiles");
let tiles = utils::read_images_from_dir(tile_dir)?;
Self::from_images(master_img, tiles, grid_size)
}
/// Construct a [`Mosaic`] from [`RgbImage`] buffers of the master images and the tile
/// images.
///
/// # Arguments
/// - `master_img`: The master image buffer.
/// - `tiles`: The tile image buffers.
/// - `grid_size`: The grid size of the mosaic, the number of cells horizontally and vertically.
/// - `max_tile_occurrences`: The maximum number of times a tile can be repeated in the mosaic.
/// Should be greater than 0.
///
/// # Errors
/// - [`PhomoError::MasterError`]: An error occurred while constructing the [`Master`].
/// - [`PhomoError::MosaicError`]: An error occurred while constructing the [`Mosaic`].
pub fn from_images(
master_img: RgbImage,
tiles: Vec<RgbImage>,
grid_size: (u32, u32),
) -> Result<Self, PhomoError> {
let master = Master::from_image(master_img, grid_size)?;
Self::new(master, tiles, grid_size)
}
/// Create a new [`Mosaic`] from the provided [`Master`] and tiles.
///
/// # Arguments
/// - `master`: The master.
/// - `tiles`: The tile image buffers.
/// - `grid_size`: The grid size of the mosaic, the number of cells horizontally and vertically.
///
/// # Errors
/// - [`PhomoError::MosaicError`]: An error occurred while constructing the [`Mosaic`].
pub fn new(
master: Master,
tiles: Vec<RgbImage>,
grid_size: (u32, u32),
) -> Result<Self, PhomoError> {
if let Some(mismatched_tile) = tiles
.iter()
.find(|img| img.dimensions() != master.cell_size)
{
return Err(MosaicError::TileSizeMismatch {
expected: master.cell_size,
found: mismatched_tile.dimensions(),
}
.into());
}
Ok(Self {
master,
tiles,
grid_size,
})
}
/// Compute the [`DistanceMatrix`] between the tiles and the master cells, using the
/// [`norm_l1`] metric.
///
/// To use a different distance metric, use the [`distance_matrix_with_metric`](Mosaic::distance_matrix_with_metric) method.
///
/// The row index is the cell index and the column index is the tile index.
pub fn distance_matrix(&self) -> DistanceMatrix {
self.distance_matrix_with_metric(norm_l1)
}
/// Compute the [`DistanceMatrix`] between the tiles and the master cells using the provided
/// `metric` function. See [`phomo::metrics`](crate::metrics) for implemented distance metrics.
///
/// The row index is the cell index and the column index is the tile index.
pub fn distance_matrix_with_metric(&self, metric: MetricFn) -> DistanceMatrix {
#[cfg(not(target_family = "wasm"))]
info!("Starting distance matrix computation...");
#[cfg(not(target_family = "wasm"))]
let start_time = time::Instant::now();
let d_matrix = macros::maybe_progress_bar!(
macros::iter_or_par_iter!(self.master.cells),
"Computing distance matrix",
par
)
.flat_map(|cell| macros::iter_or_par_iter!(self.tiles).map(|tile| metric(tile, cell)))
.collect();
#[cfg(not(target_family = "wasm"))]
info!("Completed in {:?}", start_time.elapsed());
// We can construct the struct directly because we know the sizes should line up
DistanceMatrix {
rows: self.master.cells.len(),
columns: self.tiles.len(),
data: d_matrix,
}
}
pub(crate) fn check_distance_matrix(
&self,
distance_matrix: &DistanceMatrix,
) -> Result<(), PhomoError> {
if distance_matrix.rows != self.master.cells.len()
|| distance_matrix.columns < self.tiles.len()
{
return Err(MosaicError::DistanceMatrixSizeMismatch {
expected: (self.master.cells.len(), self.tiles.len()),
found: (distance_matrix.rows, distance_matrix.columns),
}
.into());
}
Ok(())
}
/// Render the photo mosaic image using the provided tile assignments.
///
/// # Arguments
/// - `assignments`: The tile index assigned to each master cell.
/// The length of the assignments should be equal to the number of master cells.
/// The tile index should be less than the number of tiles.
///
/// # Errors
/// - [`PhomoError::MosaicError`]: An error occurred while rendering the mosaic.
/// - [`PhomoError::ImageError`]: An error occurred while copying the tiles to the mosaic image.
pub fn render(&self, assignments: Vec<usize>) -> Result<RgbImage, PhomoError> {
if assignments.len() != self.master.cells.len() {
return Err(MosaicError::InvalidAssignmentsLength {
expected: self.master.cells.len(),
found: assignments.len(),
}
.into());
}
let (grid_width, _) = self.grid_size;
let (cell_width, cell_height) = self.master.cell_size;
let mut mosaic_img = RgbImage::new(self.master.img.width(), self.master.img.height());
for (cell_idx, tile_idx) in assignments.into_iter().enumerate() {
let x = (cell_idx as u32 % grid_width) * cell_width;
let y = (cell_idx as u32 / grid_width) * cell_height;
let tile = self
.tiles
.get(tile_idx % self.tiles.len())
.ok_or(MosaicError::InvalidTileIndex(tile_idx))?;
mosaic_img.copy_from(tile, x, y)?;
}
Ok(mosaic_img)
}
/// Compute the tile to master cell assignments using the [`Hungarian`] solver
/// algorithm, and build the photo mosaic image.
///
/// # Errors
/// - [`PhomoError::MosaicError`]: An error occurred while building the mosaic.
/// - [`PhomoError::SolverError`]: An error occurred while solving the tile to cell assignments.
pub fn build(
&self,
distance_matrix: DistanceMatrix,
config: SolverConfig,
) -> Result<RgbImage, PhomoError> {
let solver = Hungarian::new(config);
self.build_with_solver(distance_matrix, solver)
}
/// Compute the tile to master cell assignments using the provided solver algorithm, and build
/// the photo mosaic image. See [`phomo::solvers`](crate::solvers) for implemented solvers.
///
/// # Arguments
/// - `distance_matrix`: The distance matrix between the master image and the tiles.
/// - `solver`: The solver algorithm to use for the assignment problem.
///
/// # Errors
/// - [`PhomoError::MosaicError`]: An error occurred while building the mosaic.
/// - [`PhomoError::SolverError`]: An error occurred while solving the tile to cell assignments.
pub fn build_with_solver<S: Solve>(
&self,
distance_matrix: DistanceMatrix,
mut solver: S,
) -> Result<RgbImage, PhomoError> {
self.check_distance_matrix(&distance_matrix)?;
let assignments = distance_matrix.assignments(&mut solver)?;
self.render(assignments)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
fn test_dir() -> PathBuf {
PathBuf::from("tests/data/mosaic")
}
fn test_master_img() -> PathBuf {
// image is 256x256
PathBuf::from("tests/data/master/master.png")
}
fn test_tile_dir() -> PathBuf {
// tiles are 64x64
test_dir().join("tiles/")
}
#[test]
fn test_mosaic_creation_from_valid_data() {
let grid_size = (4, 4);
let mosaic = Mosaic::from_file_and_dir(test_master_img(), test_tile_dir(), grid_size);
// Check if the mosaic was created successfully
assert!(mosaic.is_ok());
let mosaic = mosaic.unwrap();
// Check that the master image has the expected dimensions
assert_eq!(mosaic.master.img.width(), 256);
assert_eq!(mosaic.master.img.height(), 256);
// Check that the number of tiles matches the number of grid cells
assert!(mosaic.tiles.len() >= mosaic.master.cells.len());
// Make sure the tiles have the same size as the master cells
assert!(mosaic
.tiles
.iter()
.all(|tile| tile.dimensions() == mosaic.master.cell_size));
}
#[test]
fn test_mosaic_creation_with_mismatched_tile_sizes() {
// 5x5 grid which will not work with a 256x256 master image and 64x64 tiles
let grid_size = (5, 5);
// Attempt to create a mosaic and expect an error due to tile size mismatch
let mosaic = Mosaic::from_file_and_dir(test_master_img(), test_tile_dir(), grid_size);
assert!(mosaic.is_err());
}
#[test]
fn test_invalid_master_file_path() {
let grid_size = (4, 4);
let mosaic = Mosaic::from_file_and_dir("invalid/master.png", test_tile_dir(), grid_size);
assert!(mosaic.is_err());
}
#[test]
fn test_invalid_tile_directory() {
let grid_size = (4, 4);
let mosaic = Mosaic::from_file_and_dir(test_master_img(), "invalid/tile_dir", grid_size);
assert!(mosaic.is_err());
}
#[test]
fn test_distance_matrix() {
let master_img = image::open(test_master_img()).unwrap().to_rgb8();
let tiles = utils::read_images_from_dir(test_tile_dir()).unwrap();
let mosaic = Mosaic::from_images(master_img, tiles, (4, 4)).unwrap();
let distance_matrix = mosaic.distance_matrix();
assert_eq!(
distance_matrix.data.len(),
mosaic.master.cells.len() * mosaic.tiles.len()
);
}
#[test]
fn test_too_few_tiles() {
let master_img = image::open(test_master_img()).unwrap().to_rgb8();
let tiles = utils::read_images_from_dir(test_tile_dir())
.unwrap()
.into_iter()
.take(2)
.collect::<Vec<_>>();
let mosaic = Mosaic::from_images(master_img, tiles, (4, 4)).unwrap();
let d_matrix = mosaic.distance_matrix();
let result = mosaic.build(d_matrix, SolverConfig::default());
assert!(result.is_err());
}
#[test]
fn test_too_few_tiles_unless_repeats() {
let master_img = image::open(test_master_img()).unwrap().to_rgb8();
let tiles = utils::read_images_from_dir(test_tile_dir())
.unwrap()
.into_iter()
.take(2)
.collect::<Vec<_>>();
let mosaic = Mosaic::from_images(master_img, tiles, (4, 4)).unwrap();
let d_matrix = mosaic.distance_matrix();
let result = mosaic.build(
d_matrix,
SolverConfig {
max_tile_occurrences: 8,
},
);
assert!(result.is_ok());
}
}