Skip to content

Commit

Permalink
image_base64 fn
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Feb 12, 2025
1 parent c4a8eae commit 3659f99
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 24 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ ratatui = { version = "^0.29.0", features = ["crossterm"], optional = true }
icy_sixel = { version = "^0.1.1", optional = true }
image = { version = "^0.25.1", default-features = false, features = ["jpeg"], optional = true }
ratatui-image = { version = "4.2.0", optional = true }
base64 = { version = "0.21.7", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3.59"
Expand Down
3 changes: 2 additions & 1 deletion examples/desktop/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use gameboy::gameboy::{load_rom, Gameboy, RenderMode::Desktop};
fn main() {
// TODO: Allow receive path by arguments
// let gb = Gameboy::new("./../../tests/cpu_instrs/cpu_instrs.gb");
if let Ok((data, filepath)) = load_rom("./../the-machine.gb") {
// if let Ok((data, filepath)) = load_rom("./../the-machine.gb") {
if let Ok((data, filepath)) = load_rom("/Users/rapha/Documents/a/boyband/game/SuperWish/game.gb") {
// if let Ok((data, filepath)) = load_rom("./../bakery.gb") {
let gb = Gameboy::new(data, Some(filepath));
gb.render(Desktop);
Expand Down
2 changes: 1 addition & 1 deletion examples/terminal/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use gameboy::gameboy::{load_rom, Gameboy, RenderMode::Terminal};

fn main() {
// TODO: Allow receive path by arguments
if let Ok((data, filepath)) = load_rom("./../the-machine.gb") {
if let Ok((data, filepath)) = load_rom("/Users/rapha/Documents/a/boyband/game/SuperWish/game.gb") {
// if let Ok((data, filepath)) = load_rom("./../bakery.gb") {
let gb = Gameboy::new(data, Some(filepath));
gb.render(Terminal);
Expand Down
2 changes: 1 addition & 1 deletion gameboy.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ void keyup(enum KeypadKey key);

struct ImageBuffer image(void);

struct ImageBuffer scaled_image_png(uint8_t scale);
const char *image_base64(void);
57 changes: 36 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
use image::DynamicImage;
use std::sync::Mutex;
use std::sync::OnceLock;

use std::ffi::CString;

// #[cfg(feature = "ffi")]
// use base64::{Engine, engine::general_purpose};

// #[cfg(feature = "ffi")]
// use std::io::Cursor;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -95,7 +104,7 @@ pub extern "C" fn image() -> ImageBuffer {
}

#[no_mangle]
pub extern "C" fn scaled_image_png(scale: u8) -> ImageBuffer {
pub extern "C" fn image_base64() -> *const std::os::raw::c_char {
if let Some(gb) = GAMEBOY.get() {
if let Ok(mut locked_gb) = gb.lock() {
let width = 160;
Expand All @@ -115,28 +124,34 @@ pub extern "C" fn scaled_image_png(scale: u8) -> ImageBuffer {
i += 3;
}

let mut buffer =
let buffer =
image::ImageBuffer::from_raw(width, height, output_data).unwrap();
if scale > 1 {
buffer = image::imageops::resize(
&buffer,
width * (scale as u32),
height * (scale as u32),
image::imageops::FilterType::Nearest,
);
}
let result = image::DynamicImage::ImageRgb8(buffer);
let b = result.as_bytes();

return ImageBuffer {
len: b.len() as i32,
data: b.as_ptr(),
};
let img: DynamicImage = image::DynamicImage::ImageRgb8(buffer);
// if scale > 1 {
// buffer = image::imageops::resize(
// &buffer,
// width * (scale as u32),
// height * (scale as u32),
// image::imageops::FilterType::Nearest,
// );
// }

// let mut png: Vec<u8> = vec![];
// img.write_to(&mut Cursor::new(&mut png), image::ImageFormat::Png).expect("don't fail img write_to");
// let data = general_purpose::STANDARD.encode(&png);

let cstring_data = CString::new(img.as_bytes()).expect("don't fail");
return cstring_data.into_raw();

// let mut png: Vec<u8> = vec![];
// img.write_to(&mut Cursor::new(&mut png), image::ImageFormat::Png).expect("don't fail img write_to");
// let data = general_purpose::STANDARD.encode(&png);

// let cstring_data = CString::new(data).expect("don't fail");
// return cstring_data.into_raw();
}
}

ImageBuffer {
len: 0,
data: std::ptr::null_mut(),
}
let data = CString::new("").expect("don't fail");
data.into_raw()
}

0 comments on commit 3659f99

Please sign in to comment.