Add image scale option

This commit is contained in:
Joscha 2025-02-22 21:54:54 +01:00
parent f8ff567a00
commit a6d5c9f671
4 changed files with 27 additions and 3 deletions

View file

@ -29,6 +29,7 @@ pub struct Image {
image: RgbaImage, image: RgbaImage,
shrink: bool, shrink: bool,
grow: bool, grow: bool,
scale: u32,
filter: FilterType, filter: FilterType,
dither_palette: Option<Palette<LinSrgb>>, dither_palette: Option<Palette<LinSrgb>>,
@ -42,6 +43,7 @@ impl Image {
shrink: true, shrink: true,
grow: false, grow: false,
filter: FilterType::CatmullRom, filter: FilterType::CatmullRom,
scale: 1,
dither_palette: None, dither_palette: None,
dither_algorithm: DitherAlgorithm::FloydSteinberg, dither_algorithm: DitherAlgorithm::FloydSteinberg,
} }
@ -57,6 +59,11 @@ impl Image {
self 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 { pub fn with_filter(mut self, filter: FilterType) -> Self {
self.filter = filter; self.filter = filter;
self self
@ -94,8 +101,8 @@ impl<C> Widget<C> for Image {
} }
let size = Size { let size = Size {
width: self.image.width() as f32, width: (self.image.width() * self.scale) as f32,
height: self.image.height() as f32, height: (self.image.height() * self.scale) as f32,
}; };
let max_width = known.width.or(match available.width { let max_width = known.width.or(match available.width {
@ -134,7 +141,11 @@ impl<C> Widget<C> for Image {
_layout: &Layout, _layout: &Layout,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let (width, height) = view.size().to_u32(); 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 { let image = if let Some(palette) = &self.dither_palette {
self.dither_algorithm.dither(image, palette) self.dither_algorithm.dither(image, palette)
@ -142,6 +153,8 @@ impl<C> Widget<C> for Image {
image image
}; };
let image = imageops::resize(&image, width, height, FilterType::Nearest);
view.image(&image); view.image(&image);
Ok(()) Ok(())
} }

View file

@ -15,6 +15,7 @@ pub struct ImageDrawing {
pub image: RgbaImage, pub image: RgbaImage,
pub bright: bool, pub bright: bool,
pub algo: DitherAlgorithm, pub algo: DitherAlgorithm,
pub scale: u32,
} }
impl Drawing for ImageDrawing { impl Drawing for ImageDrawing {
@ -33,6 +34,7 @@ impl Drawing for ImageDrawing {
let image = Image::new(image) let image = Image::new(image)
.with_dither_palette(&[BLACK, WHITE]) .with_dither_palette(&[BLACK, WHITE])
.with_dither_algorithm(self.algo) .with_dither_algorithm(self.algo)
.with_scale(self.scale)
.node() .node()
.register(&mut tree)?; .register(&mut tree)?;

View file

@ -114,6 +114,7 @@ async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow:
let mut image = None; let mut image = None;
let mut bright = false; let mut bright = false;
let mut algo = DitherAlgorithm::FloydSteinberg; let mut algo = DitherAlgorithm::FloydSteinberg;
let mut scale = 1_u32;
while let Some(field) = multipart.next_field().await? { while let Some(field) = multipart.next_field().await? {
match field.name() { match field.name() {
@ -130,6 +131,9 @@ async fn post_image(server: State<Server>, mut multipart: Multipart) -> somehow:
"stucki" => algo = DitherAlgorithm::Stucki, "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, image,
bright, bright,
algo, algo,
scale,
})) }))
.await; .await;
Ok(Redirect::to("image").into_response()) Ok(Redirect::to("image").into_response())

View file

@ -20,6 +20,10 @@
<option value="stucki">Stucki</option> <option value="stucki">Stucki</option>
</select> </select>
</li> </li>
<li>
Scale:
<input name="scale" type="number" min="1" max="8" value="1" />
</li>
<li><button>Print!</button></li> <li><button>Print!</button></li>
</ol> </ol>
</form> </form>