Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --layout row #60

Merged
merged 3 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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