Skip to content

Commit

Permalink
feat: add --layout row (#60)
Browse files Browse the repository at this point in the history
Closes #20
  • Loading branch information
nekowinston authored Nov 4, 2023
1 parent d679cd2 commit 00f26b5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion catwalk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Usage:
| Parameter | Description |
| -------------- | -------------------------------------------------------------------------------------------------|
| `images[4]` | 4 images to merge into one. **REQUIRED**. *All other parameters are optional.* |
| `--layout` | Style of the showcase image. Available options are `composite` (default), `grid`, and `stacked`. |
| `--layout` | Style of the showcase image. Available options are `composite` (default), `grid`, `row`, and `stacked`. |
| `--gap` | Gap size for the `grid` layout. |
| `--radius` | Radius of rounded corners. |
| `--output` | Output file (defaults to `./result.webp`) |
Expand Down
39 changes: 30 additions & 9 deletions catwalk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ pub enum Layout {
Composite,
Stacked,
Grid,
Row,
}

enum GridLayouts {
Grid,
Row,
}

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -220,25 +226,39 @@ impl Magic {
}

/// Creates a grid image.
fn gen_grid(&self) -> Image<Rgba> {
fn gen_grid(&self, layout: &GridLayouts) -> Image<Rgba> {
// Round images
let gap = self.gap;
let rounded: Vec<Image<Rgba>> = self
.images
.iter()
.map(|x| self.rounding_mask.mask(x))
.collect();

// Create final
let mut result = Image::new(
(self.width * 2) + (gap * 3),
(self.height * 2) + (gap * 3),
Rgba::transparent(),
)
let mut result = match layout {
GridLayouts::Grid => Image::new(
(self.width * 2) + (gap * 3),
(self.height * 2) + (gap * 3),
Rgba::transparent(),
),
GridLayouts::Row => Image::new(
(self.width * 4) + (gap * 5),
self.height + (gap * 2),
Rgba::transparent(),
),
}
.with_overlay_mode(OverlayMode::Merge);
// calculate the top left coordinates for each image, and paste
rounded.iter().enumerate().for_each(|(i, img)| {
let x = i % 2;
let y = i / 2;
let x = match layout {
GridLayouts::Row => i % 4,
GridLayouts::Grid => i % 2,
};
let y = match layout {
GridLayouts::Row => 0,
GridLayouts::Grid => i / 2,
};
result.paste(
gap + (self.width + gap) * x as u32,
gap + (self.height + gap) * y as u32,
Expand Down Expand Up @@ -266,7 +286,8 @@ impl Magic {
match self.layout {
Layout::Composite => self.gen_composite(),
Layout::Stacked => self.gen_stacked(),
Layout::Grid => self.gen_grid(),
Layout::Grid => self.gen_grid(&GridLayouts::Grid),
Layout::Row => self.gen_grid(&GridLayouts::Row),
}
}

Expand Down

0 comments on commit 00f26b5

Please sign in to comment.