add image rendering support
This commit is contained in:
parent
d3756e4645
commit
728847157b
3 changed files with 1148 additions and 53 deletions
1151
Cargo.lock
generated
1151
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -11,6 +11,7 @@ path = "src/cli.rs"
|
|||
required-features = ["cli"]
|
||||
|
||||
[dependencies]
|
||||
image = { version = "0.25.8", optional = true }
|
||||
rug = "1.27"
|
||||
|
||||
[dependencies.clap]
|
||||
|
|
@ -21,3 +22,4 @@ optional = true
|
|||
[features]
|
||||
default = []
|
||||
cli = ["dep:clap"]
|
||||
image = ["dep:image"]
|
||||
|
|
|
|||
48
src/lib.rs
48
src/lib.rs
|
|
@ -1,3 +1,6 @@
|
|||
#[cfg(feature = "image")]
|
||||
use image::{ImageBuffer, Pixel};
|
||||
|
||||
use rug::Integer as Int;
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -45,6 +48,7 @@ impl Dollcode {
|
|||
}
|
||||
|
||||
out.reverse();
|
||||
|
||||
Dollcode(out.into_iter().collect())
|
||||
}
|
||||
|
||||
|
|
@ -94,8 +98,52 @@ impl Dollcode {
|
|||
}
|
||||
|
||||
out.reverse();
|
||||
|
||||
out.into_iter().collect()
|
||||
}
|
||||
|
||||
#[cfg(feature = "image")]
|
||||
pub fn render_image<Pix>(&self) -> ImageBuffer<Pix, Vec<u8>>
|
||||
where
|
||||
Pix: Pixel<Subpixel = u8> + 'static,
|
||||
{
|
||||
let char_width = 16u32;
|
||||
let char_height = char_width * 4u32;
|
||||
|
||||
let img_width = ((char_width * 2) * self.0.chars().count() as u32) - char_width;
|
||||
let img_height = char_height;
|
||||
|
||||
let mut dark_pixel =
|
||||
*Pix::from_slice(&vec![Pix::Subpixel::MIN; Pix::CHANNEL_COUNT as usize]);
|
||||
let mut light_pixel =
|
||||
*Pix::from_slice(&vec![Pix::Subpixel::MAX; Pix::CHANNEL_COUNT as usize]);
|
||||
|
||||
if matches!(Pix::CHANNEL_COUNT, 2 | 4) {
|
||||
dark_pixel.channels_mut()[Pix::CHANNEL_COUNT as usize - 1] = Pix::Subpixel::MAX;
|
||||
light_pixel.channels_mut()[Pix::CHANNEL_COUNT as usize - 1] = Pix::Subpixel::MIN;
|
||||
}
|
||||
|
||||
let mut img = ImageBuffer::from_pixel(img_width, img_height, light_pixel);
|
||||
|
||||
for (i, ch) in self.0.chars().enumerate() {
|
||||
let x_offset = (i as u32) * char_width * 2;
|
||||
|
||||
let (x, y, width, height) = match ch {
|
||||
'▖' => (x_offset, char_height / 2, char_width, char_height / 2),
|
||||
'▘' => (x_offset, 0, char_width, char_height / 2),
|
||||
'▌' => (x_offset, 0, char_width, char_height),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
for y_ in y..(y + height) {
|
||||
for x_ in x..(x + width) {
|
||||
img.put_pixel(x_, y_, dark_pixel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
img
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for Dollcode {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue