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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use crate::*;

use riddle_math::{Rect, SpacialNumericConversion, Vector2};

use thiserror::Error;

/// Utility for packing multiple images to a single image.
///
/// # Example
///
/// ```
/// # use riddle_image::*; use riddle_math::*;
/// let mut img1 = Image::new(2,2);
/// let mut img2 = Image::new(3,3);
/// let packed = ImagePacker::new().pack(&[&img1, &img2]).unwrap();
///
/// assert!(packed.image().dimensions().x > 3 || packed.image().dimensions().y > 3);
/// assert_eq!(img1.dimensions(), packed.rects()[0].dimensions);
/// assert_eq!(img2.dimensions(), packed.rects()[1].dimensions);
/// ```
#[derive(Default)]
pub struct ImagePacker {
	size_policy: ImagePackerSizePolicy,
	padding: u32,
}

impl ImagePacker {
	/// Create the ImagePacker, with default settings.
	///
	/// * Size Policy: [`ImagePackerSizePolicy::Pow2Square`]
	/// * Padding: 0
	pub fn new() -> Self {
		ImagePacker::default()
	}

	/// Set the size policy for the packer. This controls how the packing algorithm sizes
	/// the output image, and how those dimensions grow if the algorithm needs to grow the output
	/// image.
	///
	/// # Example
	///
	/// ```
	/// # use riddle_image::{*, packer::*}; use riddle_math::*;
	/// let mut img1 = Image::new(2,2);
	/// let mut img2 = Image::new(3,3);
	/// let packed = ImagePacker::new()
	///     .size_policy(ImagePackerSizePolicy::Fixed(Vector2::new(10, 10)))
	///     .pack(&[&img1, &img2]).unwrap();
	///
	/// assert_eq!(Vector2::new(10, 10), packed.image().dimensions());
	/// ```
	pub fn size_policy(&mut self, policy: ImagePackerSizePolicy) -> &mut Self {
		self.size_policy = policy;
		self
	}

	/// The padding around each image in pixels.The gap between images will thus be twice the
	/// padding value.
	///
	/// # Example
	///
	/// ```
	/// # use riddle_image::{*, packer::*}; use riddle_math::*;
	/// let mut img1 = Image::new(2,2);
	/// let packed = ImagePacker::new()
	///     .size_policy(ImagePackerSizePolicy::Pow2Square)
	///     .padding(1)
	///     .pack(&[&img1]).unwrap();
	///
	/// assert_eq!(Vector2::new(4,4), packed.image().dimensions());
	/// assert_eq!(Vector2::new(1,1), packed.rects()[0].location);
	/// ```
	pub fn padding(&mut self, amount: u32) -> &mut Self {
		self.padding = amount;
		self
	}

	/// Pack the slice of images provides in to a single image using the settings stored in the
	/// image packer. The values in [`ImagePackerResult::rects()`] will reference images in the same
	/// order as the slice provided.
	///
	/// If the slice is empty an Error result will be returned
	///
	/// # Example
	///
	/// ```
	/// # use riddle_image::*; use riddle_math::*;
	/// let mut img1 = Image::new(2,2);
	/// let mut img2 = Image::new(3,3);
	/// let packed = ImagePacker::new()
	///     .pack(&[&img1, &img2]).unwrap();
	///
	/// assert_eq!(Vector2::new(2, 2), packed.rects()[0].dimensions);
	/// assert_eq!(Vector2::new(3, 3), packed.rects()[1].dimensions);
	/// ```
	pub fn pack(
		&self,
		images: &[&Image],
	) -> std::result::Result<ImagePackerResult, ImagePackerError> {
		if images.is_empty() {
			return Err(ImagePackerError::NoSourceImages);
		}

		let mut sorted_images: Vec<(usize, &Image)> = images
			.iter()
			.enumerate()
			.map(|(i, img)| (i, *img))
			.collect();
		sorted_images
			.sort_by(|(_, a), (_, b)| (b.height() * b.width()).cmp(&(a.height() * a.width())));

		let mut current_size = self.size_policy.initial_size();

		'PACK_WITH_SIZE: loop {
			let mut rects = vec![Rect::<u32>::default(); images.len()];

			let mut output_image = Image::new(current_size.x, current_size.y);
			let mut occupancy_image = Image::new(current_size.x, current_size.y);

			let mut current_y = 0;
			let mut current_x = 0;

			for (image_idx, image) in sorted_images.iter() {
				let padded_size = if image.dimensions() == Vector2::new(0, 0) {
					Vector2::new(0, 0)
				} else {
					let padded_size = Vector2::new(
						image.width() + (self.padding * 2),
						image.height() + (self.padding * 2),
					);

					'FIND_GAP: while current_y < output_image.height() {
						let mut gap_length = 0;
						while current_x < output_image.width() {
							if occupancy_image.get_pixel([current_x, current_y])
								== Color::<u8>::ZERO
							{
								gap_length += 1;
							} else {
								gap_length = 0;
							}

							current_x += 1;
							if gap_length == padded_size.x {
								current_x -= padded_size.x;
								break 'FIND_GAP;
							}
						}
						current_x = 0;
						current_y += 1;
					}
					padded_size
				};

				if current_y + padded_size.y > output_image.height() {
					current_size = self
						.size_policy
						.increase_size(current_size)
						.ok_or(ImagePackerError::UnableToFitImages)?;
					continue 'PACK_WITH_SIZE;
				}

				let blit_rect = Rect::new(
					Vector2::new(current_x + self.padding, current_y + self.padding),
					image.dimensions(),
				);
				let occupancy_rect = Rect::new([current_x, current_y].into(), padded_size);

				occupancy_image.fill_rect(occupancy_rect, Color::<u8>::WHITE);
				output_image.blit(image, blit_rect.location.convert());
				rects[*image_idx] = blit_rect;
			}

			return Ok(ImagePackerResult {
				image: output_image,
				rects,
			});
		}
	}
}

/// Packed images result, combining the generated image and the locations of all the images which
/// were packed.
///
/// See [`ImagePacker`] for how to pack images and create this result.
#[derive(Debug)]
pub struct ImagePackerResult {
	image: Image,
	rects: Vec<Rect<u32>>,
}

impl ImagePackerResult {
	/// Borrow the image containing the packed image results.
	pub fn image(&self) -> &Image {
		&self.image
	}

	/// Take the image, dropping the result.
	pub fn take_image(self) -> Image {
		self.image
	}

	/// The locations of each of the supplied [`Image`]s in the order in which they were supplied to
	/// [`ImagePacker::pack`].
	pub fn rects(&self) -> &Vec<Rect<u32>> {
		&self.rects
	}
}

#[derive(Debug, PartialEq, Eq, Error)]
pub enum ImagePackerError {
	#[error("No source images supplied")]
	NoSourceImages,

	#[error("Unable to fit all images in to packed image")]
	UnableToFitImages,
}

/// Controls the initial size of the output packed image size, and how that image grows over time
/// if more space is required to pack all the supplied images.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum ImagePackerSizePolicy {
	/// The output image will initially be the supplied size, and if the images can't be packed in
	/// to that size packing will fail.
	Fixed(Vector2<u32>),

	/// The output image will initially be the supplied value. If the images can't be packed in to
	/// that size, the output image will be increased by the same amount. The resultant image will
	/// always be `n * initial_dimensions`.
	LinearSteps(Vector2<u32>),

	/// The output image will initially be `[1, 1]`. If the images can't be packed in to that size
	/// the dimensions will double. The resultablt image will always be a square with side of `2^n`.
	Pow2Square,
}

impl ImagePackerSizePolicy {
	fn initial_size(&self) -> Vector2<u32> {
		match self {
			Self::Fixed(size) => *size,
			Self::LinearSteps(step) => *step,
			Self::Pow2Square => Vector2::new(1, 1),
		}
	}

	fn increase_size(&self, current_size: Vector2<u32>) -> Option<Vector2<u32>> {
		match self {
			Self::Fixed(_) => None,
			Self::LinearSteps(step) => Some(current_size + *step),
			Self::Pow2Square => Some(current_size * 2),
		}
	}
}

impl Default for ImagePackerSizePolicy {
	fn default() -> Self {
		Self::Pow2Square
	}
}

#[cfg(test)]
mod test {
	use super::*;
	use itertools::*;

	fn assert_valid_result(results: &ImagePackerResult) {
		// All images are disjoint
		for rects in results.rects().iter().combinations(2) {
			assert_eq!(None, rects[0].intersect(rects[1]));
		}

		// All images are in the output image
		for rect in results.rects() {
			assert_eq!(Some(rect.clone()), results.image().rect().intersect(rect));
		}
	}

	#[test]
	fn pow2square_pack_0x0_image() {
		let img = Image::new(0, 0);

		let packed = ImagePacker::new()
			.size_policy(ImagePackerSizePolicy::Pow2Square)
			.pack(&[&img])
			.unwrap();

		assert_valid_result(&packed);
		assert_eq!(Vector2::new(0, 0), packed.rects()[0].dimensions);
	}

	#[test]
	fn pow2square_pack_0x0_with_large_image() {
		let img1 = Image::new(0, 0);
		let mut img2 = Image::new(3, 3);
		img2.fill(Color::<u8>::RED);

		let packed = ImagePacker::new()
			.size_policy(ImagePackerSizePolicy::Pow2Square)
			.pack(&[&img1, &img2])
			.unwrap();

		assert_valid_result(&packed);
		assert_eq!(Vector2::new(0, 0), packed.rects()[0].dimensions);
		assert_eq!(Vector2::new(4, 4), packed.image().dimensions());
		assert_eq!(
			Color::<u8>::RED,
			packed.image().get_pixel(packed.rects()[1].location)
		);
	}

	#[test]
	fn pow2square_pack_single_pixel_image() {
		let mut img = Image::new(1, 1);
		img.fill(Color::<u8>::RED);

		let packed = ImagePacker::new()
			.size_policy(ImagePackerSizePolicy::Pow2Square)
			.pack(&[&img])
			.unwrap();

		assert_valid_result(&packed);
		assert_eq!(
			Color::<u8>::RED,
			packed.image().get_pixel(packed.rects()[0].location)
		);
	}

	#[test]
	fn pow2square_pack_3x3_in_pow2_square() {
		let mut img = Image::new(3, 3);
		img.fill(Color::<u8>::RED);
		let packed = ImagePacker::new()
			.size_policy(ImagePackerSizePolicy::Pow2Square)
			.pack(&[&img])
			.unwrap();

		assert_valid_result(&packed);
		assert_eq!(
			Color::<u8>::RED,
			packed.image().get_pixel(packed.rects()[0].location)
		);
	}

	#[test]
	fn pow2square_pack_single_big_multiple_small() {
		let mut img1 = Image::new(3, 3);
		img1.fill(Color::<u8>::RED);
		let mut img2 = Image::new(1, 1);
		img2.fill(Color::<u8>::BLUE);
		let packed = ImagePacker::new()
			.size_policy(ImagePackerSizePolicy::Pow2Square)
			.pack(&[&img1, &img2, &img2, &img2, &img2])
			.unwrap();

		assert_valid_result(&packed);
		assert_eq!(
			Color::<u8>::RED,
			packed.image().get_pixel(packed.rects()[0].location)
		);
		assert_eq!(
			Color::<u8>::BLUE,
			packed.image().get_pixel(packed.rects()[1].location)
		);
	}

	#[test]
	fn pow2square_pack_multiple_big_multiple_small() {
		let mut img1 = Image::new(3, 3);
		img1.fill(Color::<u8>::RED);
		let mut img2 = Image::new(4, 4);
		img2.fill(Color::<u8>::BLUE);
		let packed = ImagePacker::new()
			.size_policy(ImagePackerSizePolicy::Pow2Square)
			.pack(&[&img1, &img2])
			.unwrap();

		assert_valid_result(&packed);
		assert_eq!(
			Color::<u8>::RED,
			packed.image().get_pixel(packed.rects()[0].location)
		);
		assert_eq!(
			Color::<u8>::BLUE,
			packed.image().get_pixel(packed.rects()[1].location)
		);
	}

	#[test]
	fn fixed_pack_multiple_big_multiple_small() {
		let mut img1 = Image::new(3, 3);
		img1.fill(Color::<u8>::RED);
		let mut img2 = Image::new(4, 4);
		img2.fill(Color::<u8>::BLUE);
		let packed = ImagePacker::new()
			.size_policy(ImagePackerSizePolicy::Fixed(Vector2::new(10, 10)))
			.pack(&[&img1, &img2])
			.unwrap();

		assert_valid_result(&packed);
		assert_eq!(
			Color::<u8>::RED,
			packed.image().get_pixel(packed.rects()[0].location)
		);
		assert_eq!(
			Color::<u8>::BLUE,
			packed.image().get_pixel(packed.rects()[1].location)
		);
	}

	#[test]
	fn fixed_pack_too_small() {
		let mut img1 = Image::new(3, 3);
		img1.fill(Color::<u8>::RED);
		let mut img2 = Image::new(4, 4);
		img2.fill(Color::<u8>::BLUE);
		let packed = ImagePacker::new()
			.size_policy(ImagePackerSizePolicy::Fixed(Vector2::new(4, 4)))
			.pack(&[&img1, &img2]);

		assert_eq!(ImagePackerError::UnableToFitImages, packed.unwrap_err());
	}
}