Skip to content

Commit cb129b4

Browse files
committed
[examples][rasterize] support for output formats
1 parent 2e0adf9 commit cb129b4

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rasterize"
3-
version = "0.6.2"
3+
version = "0.6.3"
44
authors = ["Pavel Aslanov <asl.pavel@gmail.com>"]
55
description = "Simple and small 2D rendering library"
66
edition = "2024"
@@ -48,6 +48,9 @@ name = "scene_bench"
4848
harness = false
4949
name = "color_bench"
5050

51+
[[example]]
52+
name = "rasterize"
53+
required-features = ["png"]
5154

5255
[[example]]
5356
name = "scene"

examples/rasterize.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use rasterize::*;
55
use std::{
66
env,
77
fs::File,
8-
io::{BufWriter, Read},
8+
io::{BufWriter, Read, Write},
9+
str::FromStr,
910
sync::Arc,
1011
};
1112
use tracing_subscriber::{EnvFilter, fmt::format::FmtSpan};
@@ -18,10 +19,42 @@ enum RasterizerType {
1819
SignedDifference,
1920
}
2021

22+
#[derive(Debug, Clone, Copy)]
23+
enum OutputFormat {
24+
Bmp,
25+
Rgba,
26+
Png,
27+
}
28+
29+
impl OutputFormat {
30+
fn write(self, image: &Layer<LinColor>, out: impl Write) -> Result<(), Error> {
31+
match self {
32+
OutputFormat::Bmp => image.write_bmp(out)?,
33+
OutputFormat::Png => image.write_png(out)?,
34+
OutputFormat::Rgba => image.write_rgba(out)?,
35+
}
36+
Ok(())
37+
}
38+
}
39+
40+
impl FromStr for OutputFormat {
41+
type Err = Error;
42+
43+
fn from_str(s: &str) -> Result<Self, Self::Err> {
44+
match s {
45+
"bmp" => Ok(OutputFormat::Bmp),
46+
"png" => Ok(OutputFormat::Png),
47+
"rgba" => Ok(OutputFormat::Rgba),
48+
_ => Err(format!("Invalid output format: {s}").into()),
49+
}
50+
}
51+
}
52+
2153
#[derive(Debug)]
2254
struct Args {
2355
input_file: String,
2456
output_file: String,
57+
output_format: OutputFormat,
2558
outline: bool,
2659
width: Option<usize>,
2760
stroke: Option<Scalar>,
@@ -46,6 +79,7 @@ impl Args {
4679
let mut result = Args {
4780
input_file: String::new(),
4881
output_file: String::new(),
82+
output_format: OutputFormat::Bmp,
4983
outline: false,
5084
width: None,
5185
stroke: None,
@@ -83,6 +117,10 @@ impl Args {
83117
"-o" => {
84118
result.outline = true;
85119
}
120+
"-of" => {
121+
let format = args.next().ok_or("-of requries argument")?.parse()?;
122+
result.output_format = format;
123+
}
86124
"-a" => {
87125
result.rasterizer = RasterizerType::ActiveEdge;
88126
}
@@ -123,7 +161,7 @@ impl Args {
123161
);
124162
eprintln!("\nUSAGE:");
125163
eprintln!(
126-
" {} [-w <width>] [-b <bbox>] [-s <stroke>] [-f <flatness>] [-o] [-a] [-fg <color>] [-bg <color>] <file.path> <out.bmp>",
164+
" {} [-w <width>] [-b <bbox>] [-s <stroke>] [-f <flatness>] [-o] [-of <format>] [-a] [-fg <color>] [-bg <color>] <file.path> <output_file>",
127165
cmd
128166
);
129167
eprintln!("\nARGS:");
@@ -132,6 +170,7 @@ impl Args {
132170
eprintln!(" -t <transform> apply transform");
133171
eprintln!(" -s <stroke_width> stroke path before rendering");
134172
eprintln!(" -o show outline with control points instead of filling");
173+
eprintln!(" -of <format> output file format (bmp, png, rgba)");
135174
eprintln!(
136175
" -a use active-edge instead of signed-difference rasterizer"
137176
);
@@ -315,9 +354,9 @@ fn main() -> Result<(), Error> {
315354
let _ = save.enter();
316355
if args.output_file != "-" {
317356
let mut image_file = BufWriter::new(File::create(args.output_file)?);
318-
image.write_bmp(&mut image_file)?;
357+
args.output_format.write(&image, &mut image_file)?;
319358
} else {
320-
image.write_bmp(std::io::stdout())?;
359+
args.output_format.write(&image, std::io::stdout())?;
321360
}
322361
}
323362

rasterize.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env bash
22
CARGO=$(dirname "$(realpath "${BASH_SOURCE[0]}")")/Cargo.toml
33
# export RUST_LOG=debug
4-
exec cargo run --quiet --manifest-path="$CARGO" --release --example rasterize -- "${@}"
4+
exec cargo run --quiet --manifest-path="$CARGO" --release --example rasterize --features png -- "${@}"

src/image.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,15 @@ pub trait Image {
118118
}
119119
}
120120

121-
/// Write raw RGBA data
121+
/// Write raw (height, width, RGBA...) data
122122
fn write_rgba<W>(&self, mut out: W) -> Result<(), std::io::Error>
123123
where
124124
W: Write,
125125
Self::Pixel: Color,
126126
Self: Sized,
127127
{
128+
out.write_all(&u32::to_le_bytes(self.height() as u32))?;
129+
out.write_all(&u32::to_le_bytes(self.width() as u32))?;
128130
for color in self.iter() {
129131
out.write_all(&color.to_rgba())?;
130132
}

0 commit comments

Comments
 (0)