Add image scale option
This commit is contained in:
parent
f8ff567a00
commit
a6d5c9f671
4 changed files with 27 additions and 3 deletions
|
|
@ -29,6 +29,7 @@ pub struct Image {
|
|||
image: RgbaImage,
|
||||
shrink: bool,
|
||||
grow: bool,
|
||||
scale: u32,
|
||||
filter: FilterType,
|
||||
|
||||
dither_palette: Option<Palette<LinSrgb>>,
|
||||
|
|
@ -42,6 +43,7 @@ impl Image {
|
|||
shrink: true,
|
||||
grow: false,
|
||||
filter: FilterType::CatmullRom,
|
||||
scale: 1,
|
||||
dither_palette: None,
|
||||
dither_algorithm: DitherAlgorithm::FloydSteinberg,
|
||||
}
|
||||
|
|
@ -57,6 +59,11 @@ impl Image {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn with_scale(mut self, scale: u32) -> Self {
|
||||
self.scale = scale.max(1);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_filter(mut self, filter: FilterType) -> Self {
|
||||
self.filter = filter;
|
||||
self
|
||||
|
|
@ -94,8 +101,8 @@ impl<C> Widget<C> for Image {
|
|||
}
|
||||
|
||||
let size = Size {
|
||||
width: self.image.width() as f32,
|
||||
height: self.image.height() as f32,
|
||||
width: (self.image.width() * self.scale) as f32,
|
||||
height: (self.image.height() * self.scale) as f32,
|
||||
};
|
||||
|
||||
let max_width = known.width.or(match available.width {
|
||||
|
|
@ -134,7 +141,11 @@ impl<C> Widget<C> for Image {
|
|||
_layout: &Layout,
|
||||
) -> anyhow::Result<()> {
|
||||
let (width, height) = view.size().to_u32();
|
||||
let image = imageops::resize(&self.image, width, height, self.filter);
|
||||
|
||||
let iwidth = width / self.scale;
|
||||
let iheight = height / self.scale;
|
||||
|
||||
let image = imageops::resize(&self.image, iwidth, iheight, self.filter);
|
||||
|
||||
let image = if let Some(palette) = &self.dither_palette {
|
||||
self.dither_algorithm.dither(image, palette)
|
||||
|
|
@ -142,6 +153,8 @@ impl<C> Widget<C> for Image {
|
|||
image
|
||||
};
|
||||
|
||||
let image = imageops::resize(&image, width, height, FilterType::Nearest);
|
||||
|
||||
view.image(&image);
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ pub struct ImageDrawing {
|
|||
pub image: RgbaImage,
|
||||
pub bright: bool,
|
||||
pub algo: DitherAlgorithm,
|
||||
pub scale: u32,
|
||||
}
|
||||
|
||||
impl Drawing for ImageDrawing {
|
||||
|
|
@ -33,6 +34,7 @@ impl Drawing for ImageDrawing {
|
|||
let image = Image::new(image)
|
||||
.with_dither_palette(&[BLACK, WHITE])
|
||||
.with_dither_algorithm(self.algo)
|
||||
.with_scale(self.scale)
|
||||
.node()
|
||||
.register(&mut tree)?;
|
||||
|
||||
|
|
|
|||
|
|
@ -114,6 +114,7 @@ async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow:
|
|||
let mut image = None;
|
||||
let mut bright = false;
|
||||
let mut algo = DitherAlgorithm::FloydSteinberg;
|
||||
let mut scale = 1_u32;
|
||||
|
||||
while let Some(field) = multipart.next_field().await? {
|
||||
match field.name() {
|
||||
|
|
@ -130,6 +131,9 @@ async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow:
|
|||
"stucki" => algo = DitherAlgorithm::Stucki,
|
||||
_ => {}
|
||||
},
|
||||
Some("scale") => {
|
||||
scale = field.text().await?.parse::<u32>()?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
|
@ -144,6 +148,7 @@ async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow:
|
|||
image,
|
||||
bright,
|
||||
algo,
|
||||
scale,
|
||||
}))
|
||||
.await;
|
||||
Ok(Redirect::to("image").into_response())
|
||||
|
|
|
|||
|
|
@ -20,6 +20,10 @@
|
|||
<option value="stucki">Stucki</option>
|
||||
</select>
|
||||
</li>
|
||||
<li>
|
||||
Scale:
|
||||
<input name="scale" type="number" min="1" max="8" value="1" />
|
||||
</li>
|
||||
<li><button>Print!</button></li>
|
||||
</ol>
|
||||
</form>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue