Skip to content

Commit

Permalink
start reworking api based on learnings from doing a web front-end to it
Browse files Browse the repository at this point in the history
  • Loading branch information
knzai committed Jul 22, 2024
1 parent 91eae48 commit 8d018d5
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 42 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ Options:
-i, --image-parser <IMAGE_PARSER> [default: cga] [possible values: ega_row_planar, erp, cga]
-c, --custom-ascii <CUSTOM_ASCII> 4 or 16 chars palette like -a " +%0"
-w, --width <WIDTH> [default: 320]
-m, --max-width <MAX_WIDTH> used for wrapping rows if retiling with tile_height
-o, --output-file <OUTPUT_FILE> format based on extension - see image crate
-t, --tile-height <TILE_HEIGHT>
-s, --sdl
Expand Down
32 changes: 8 additions & 24 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::{parser, RawGrid};
pub struct Image(RawGrid);

impl Image {
const MAX_WIDTH: usize = 320;

pub fn new(buffer: &[u8], width: usize, parser: parser::ParserType) -> Self {
Self(parser.process_input(buffer, width))
}
Expand Down Expand Up @@ -61,17 +63,9 @@ impl Image {
}
}

pub fn tile<T: std::clone::Clone>(
data: Vec<Vec<T>>,
tile_height: usize,
max_width: Option<usize>,
) -> Vec<Vec<T>> {
pub fn tile<T: std::clone::Clone>(data: Vec<Vec<T>>, tile_height: usize) -> Vec<Vec<T>> {
let width = data[0].len();
let tiles_per_row = if let Some(mw) = max_width {
mw / width
} else {
width
};
let tiles_per_row = Image::MAX_WIDTH / width;

data.chunks(tiles_per_row * tile_height)
.flat_map(|tile_row| concat_tiles(tile_row.to_vec(), tile_height))
Expand Down Expand Up @@ -132,38 +126,28 @@ mod tests {
}

#[test]
//rework these tests to actually be wider than max_width, or do something clever to overwrite it
fn tiling() {
let data: u32 = 0b00011011000110110001101100011011;
let tiled = image::tile(
Image::new(&data.to_be_bytes(), 2, ParserType::type_str("cga")).data(),
2,
Some(4),
);
assert_eq!(
tiled,
[
vec![0, 1, 0, 1],
vec![2, 3, 2, 3],
vec![0, 1, 0, 1],
vec![2, 3, 2, 3],
]
[vec![0, 1, 0, 1, 0, 1, 0, 1], vec![2, 3, 2, 3, 2, 3, 2, 3],]
);

let data: u64 = 0b0001101100011011000110110001101100011011000110110001101100011011;
let tiled = image::tile(
Image::new(&data.to_be_bytes(), 2, ParserType::type_str("cga")).data(),
2,
Some(6),
);
assert_eq!(
tiled,
vec![
vec![0, 1, 0, 1, 0, 1],
vec![2, 3, 2, 3, 2, 3],
vec![0, 1, 0, 1, 0, 1],
vec![2, 3, 2, 3, 2, 3],
vec![0, 1, 0, 1],
vec![2, 3, 2, 3],
vec![0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
vec![2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3],
]
);
}
Expand Down
7 changes: 0 additions & 7 deletions src/terminal/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ pub struct Args {
#[clap(short, long, default_value_t = 320)]
pub width: usize,

#[clap(
short,
long,
help = "used for wrapping rows if retiling with tile_height"
)]
pub max_width: Option<usize>,

#[clap(short, long, help = "format based on extension - see image crate")]
pub output_file: Option<PathBuf>,

Expand Down
2 changes: 1 addition & 1 deletion src/terminal/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let image = Image::new(&reader, args.width, parser);

let image_data = if args.tile_height.is_some() {
image::tile(image.data(), args.tile_height.unwrap(), args.max_width)
image::tile(image.data(), args.tile_height.unwrap())
} else {
image.data()
};
Expand Down
15 changes: 6 additions & 9 deletions src/wasm/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::wasm::FileUpload;
pub struct ImageFile<'a> {
file_input: &'a FileUpload,
width: usize,
height: usize
height: usize,
}

impl ImageFile<'_> {
Expand Down Expand Up @@ -42,13 +42,7 @@ impl ImageFile<'_> {
let palette = palette_from_abbr("cga0");
let mut bytes: Vec<u8> = Vec::new();

let data = if self.width == 320 {
image.data()
} else {
tile(image.data(), self.height, Some(320))
};

let _ = png::write_to(&mut bytes, data, palette.clone());
let _ = png::write_to(&mut bytes, tile(image.data(), self.height), palette.clone());
bytes
}
}
Expand All @@ -74,7 +68,10 @@ impl Component for ImageComponent {
type Properties = Props;

fn create(_ctx: &Context<Self>) -> Self {
Self { width: 320, height: 200 }
Self {
width: 320,
height: 200,
}
}

fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
Expand Down
73 changes: 73 additions & 0 deletions src/wasm/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
* {
box-sizing: border-box;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
}

html,
body {
margin: 0;
padding: 0;
background: #2d3131;
color: #fcfcfc;
}

img {
display: block;
}

p {
text-align: center;
}

label {
cursor: pointer;
}


#wrapper {
width: 70%;
margin: auto;
}

#title {
font-size: 2rem;
text-align: center;
}

#drop-container {
padding: 4rem;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background: #3d4141;
border: 1px dashed #fcfcfc;
border-radius: 1rem;
}

#drop-container i {
font-size: 4rem;
}

#preview-area {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: stretch;
}

.preview-tile {
display: flex;
flex-direction: column;
padding: 2rem;
margin: 1rem;
background: #313737;
border-radius: 1rem;
}

.preview-media {
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
}

0 comments on commit 8d018d5

Please sign in to comment.