Skip to content

Commit

Permalink
doesn't work right now but oh well.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lokathor committed Dec 5, 2021
1 parent 072ce44 commit 1a7beb5
Show file tree
Hide file tree
Showing 8 changed files with 387 additions and 313 deletions.
58 changes: 48 additions & 10 deletions examples/demo.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use imagine::{png::*, RGB8, RGBA8};
use pixels::{Error, Pixels, SurfaceTexture};
use winit::{
dpi::LogicalSize,
Expand All @@ -6,12 +7,13 @@ use winit::{
window::WindowBuilder,
};

use imagine::{png::*, RGBA8};

#[allow(dead_code)]
fn main() -> Result<(), Error> {
const GLIDER_BIG_RAINBOW: &[u8] = include_bytes!("glider-big-rainbow.png");
const TILES_SHEET: &[u8] = include_bytes!("tiles-sheet.png");
const EXP2: &[u8] = include_bytes!("exp2_0.png");

let (rgba8, width, height) = match parse_me_a_png_yo(GLIDER_BIG_RAINBOW) {
let (mut rgba8, width, height) = match parse_me_a_png_yo(EXP2) {
Ok((rgba8, width, height)) => (rgba8, width, height),
Err(e) => panic!("Error: {:?}", e),
};
Expand All @@ -20,7 +22,7 @@ fn main() -> Result<(), Error> {
let window = {
let size = LogicalSize::new(width as f64, height as f64);
WindowBuilder::new()
.with_title("Demo window")
.with_title("imagine> demo window")
.with_inner_size(size)
.with_min_inner_size(size)
.build(&event_loop)
Expand All @@ -34,15 +36,39 @@ fn main() -> Result<(), Error> {
};

event_loop.run(move |event, _, control_flow| match event {
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
*control_flow = ControlFlow::Exit;
}
Event::RedrawRequested(_) => {
pixels.get_frame().copy_from_slice(bytemuck::cast_slice(&rgba8));
if pixels.render().map_err(|e| println!("pixels.render() failed: {}", e)).is_err() {
*control_flow = ControlFlow::Exit;
}
window.request_redraw();
}
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
*control_flow = ControlFlow::Exit;
Event::WindowEvent { event: WindowEvent::DroppedFile(path_buf), .. } => {
let file_bytes = match std::fs::read(path_buf.as_path()) {
Ok(bytes) => bytes,
Err(e) => {
eprintln!("Err opening `{path_buf}`: {e}", path_buf = path_buf.display(), e = e);
return;
}
};

let (new_rgba8, width, height) = match parse_me_a_png_yo(&file_bytes) {
Ok((rgba8, width, height)) => (rgba8, width, height),
Err(e) => {
eprintln!("Err parsing `{path_buf}`: {e:?}", path_buf = path_buf.display(), e = e);
return;
}
};

rgba8 = new_rgba8;
let size = LogicalSize::new(width as f64, height as f64);
window.set_min_inner_size(Some(size));
window.set_inner_size(size);
pixels.resize_buffer(width, height);
pixels.resize_surface(width, height);
}
_ => (),
});
Expand All @@ -52,6 +78,7 @@ fn parse_me_a_png_yo(png: &[u8]) -> Result<(Vec<RGBA8>, u32, u32), PngError> {
let mut it = RawPngChunkIter::new(png).map(PngChunk::try_from).filter(critical_errors_only);
let ihdr =
it.next().ok_or(PngError::NoChunksPresent)??.to_ihdr().ok_or(PngError::FirstChunkNotIHDR)?;
println!("{:?}", ihdr);

let idat_peek = it.peekable();
let idat_slice_it = idat_peek.filter_map(|r_chunk| match r_chunk {
Expand All @@ -64,10 +91,21 @@ fn parse_me_a_png_yo(png: &[u8]) -> Result<(Vec<RGBA8>, u32, u32), PngError> {
let mut vec = Vec::new();
vec.resize((ihdr.width * ihdr.height) as usize, RGBA8::default());
//
unfilter_decompressed_data(ihdr, &mut &mut temp_memory_buffer, |x, y, data| {
//println!("x: {x}, y: {y}, data: {data:?}", x = x, y = y, data = data);
vec[(y * ihdr.width + x) as usize] = bytemuck::cast_slice(data)[0];
})?;
match ihdr.pixel_format {
PngPixelFormat::RGBA8 => {
unfilter_decompressed_data(ihdr, &mut temp_memory_buffer, |x, y, data| {
vec[(y * ihdr.width + x) as usize] = bytemuck::cast_slice(data)[0];
})?
}
PngPixelFormat::RGB8 => {
unfilter_decompressed_data(ihdr, &mut temp_memory_buffer, |x, y, data| {
let rgb: RGB8 = bytemuck::cast_slice(data)[0];
vec[(y * ihdr.width + x) as usize] = RGBA8 { r: rgb.r, g: rgb.g, b: rgb.b, a: 0xFF };
})?
}
_ => return Err(PngError::Illegal_IHDR),
}
println!();
//
Ok((vec, ihdr.width, ihdr.height))
}
Binary file added examples/exp2_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/the_mandrill.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/tiles-sheet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 22 additions & 22 deletions src/pixel_formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ pub struct Y16_BE {
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct RGB8 {
r: u8,
g: u8,
b: u8,
pub r: u8,
pub g: u8,
pub b: u8,
}
/// An RGB value, 16-bits per channel.
///
Expand All @@ -100,9 +100,9 @@ pub struct RGB8 {
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct RGB16_BE {
r: [u8; 2],
g: [u8; 2],
b: [u8; 2],
pub r: [u8; 2],
pub g: [u8; 2],
pub b: [u8; 2],
}

/// Eight 1-bit indexd pixels, tightly packed.
Expand All @@ -111,37 +111,37 @@ pub struct RGB16_BE {
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct Index1x8 {
i: u8,
pub i: u8,
}
/// Four 2-bit indexed pixels, tightly packed.
///
/// The high bits are the leftmost packed pixel.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct Index2x4 {
i: u8,
pub i: u8,
}
/// Two 4-bit indexed pixels, tightly packed.
///
/// The high bits are the leftmost packed pixel.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct Index4x2 {
i: u8,
pub i: u8,
}
/// An 8-bit indexed pixel.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct Index8 {
i: u8,
pub i: u8,
}

/// An 8-bits per channel greyscale + alpha pixel.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct YA8 {
y: u8,
a: u8,
pub y: u8,
pub a: u8,
}
/// A 16-bits per channel greyscale + alpha pixel.
///
Expand All @@ -150,18 +150,18 @@ pub struct YA8 {
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct YA16_BE {
y: [u8; 2],
a: [u8; 2],
pub y: [u8; 2],
pub a: [u8; 2],
}

/// An 8-bits per channel RGBA pixel.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct RGBA8 {
r: u8,
g: u8,
b: u8,
a: u8,
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
/// A 16-bits per channel RGBA pixel.
///
Expand All @@ -170,10 +170,10 @@ pub struct RGBA8 {
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct RGBA16_BE {
r: [u8; 2],
g: [u8; 2],
b: [u8; 2],
a: [u8; 2],
pub r: [u8; 2],
pub g: [u8; 2],
pub b: [u8; 2],
pub a: [u8; 2],
}

unsafe impl Zeroable for Y1x8 {}
Expand Down
2 changes: 1 addition & 1 deletion src/png/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl PngPixelFormat {
}
#[inline]
#[must_use]
pub const fn bytes_per_pixel(self) -> usize {
pub const fn filter_chunk_size(self) -> usize {
use PngPixelFormat::*;
match self {
Y1 | Y2 | Y4 | Y8 | I1 | I2 | I4 | I8 => 1,
Expand Down
Loading

0 comments on commit 1a7beb5

Please sign in to comment.