MCPcopy Create free account
hub / github.com/daniel-e/rustml / grid

Function grid

src/opencv/mod.rs:422–456  ·  view source on GitHub ↗

Creates a new image by organizing the given list of images into a grid. All images must have the same size, otherwise returns `None`. The number of columns of the grid is specified with the parameter `cols`. The parameter `space` specifies the number of pixels between each image that is used to separate them.

(images: &Vec<T>, cols: usize, space: usize)

Source from the content-addressed store, hash-verified

420/// parameter `space` specifies the number of pixels between each image that
421/// is used to separate them.
422fn grid<T: Image>(images: &Vec<T>, cols: usize, space: usize) -> Option<T> {
423
424 if images.len() == 0 || cols == 0 {
425 return None;
426 }
427
428 let mut rows = images.len() / cols;
429 if rows * cols < images.len() {
430 rows += 1;
431 }
432
433 let img_width = images.last().unwrap().width();
434 let img_height = images.last().unwrap().height();
435 let w = img_width * cols + (cols - 1) * space;
436 let h = img_height * rows + (rows - 1) * space;
437
438 let mut dst = T::new(w, h);
439 let mut col = 0;
440 let mut row = 0;
441
442 for img in images {
443 if img_width != img.width() || img_height != img.height() {
444 return None;
445 }
446 dst.copy_from(
447 img, 0, 0, img_width, img_height, col * (img_width + space), row * (img_height + space)
448 );
449 col += 1;
450 if col >= cols {
451 col = 0;
452 row += 1;
453 }
454 }
455 Some(dst)
456}
457
458// ------------------------------------------------------------------
459

Callers 1

mainFunction · 0.85

Calls 4

widthMethod · 0.80
heightMethod · 0.80
copy_fromMethod · 0.80
lenMethod · 0.45

Tested by

no test coverage detected