ombre: Add support for little endian encodings
8bd2aac722aff8eaec34f9e437ddbed94632898a
1 parent
8b1b2b97
ombre/examples/screenshot.rs
+2 -1
| 1 | 1 | #![allow(clippy::unwrap_used)] |
|
| 2 | + | use ombre::bytes::encoding::Encode as _; |
|
| 2 | 3 | use ombre::derive::Packed; |
|
| 3 | 4 | use ombre::prelude::*; |
|
| 4 | 5 | ||
| 5 | 6 | #[repr(C)] |
|
| 6 | 7 | #[derive(Packed)] |
| 225 | 226 | .backend |
|
| 226 | 227 | .pass_read_color_attachment(renderer.offscreen_pass, 0, &mut pixels)?; |
|
| 227 | 228 | ||
| 228 | 229 | let img = gfx::Image::new(pixels, Size::new(size.width as u32, size.height as u32)); |
|
| 229 | 230 | let mut file = std::fs::File::create("./screenshot.rgba")?; |
|
| 230 | - | img.write(&mut file)?; |
|
| 231 | + | img.encode(&mut file)?; |
|
| 231 | 232 | ||
| 232 | 233 | Ok(()) |
|
| 233 | 234 | } |
|
| 234 | 235 | ||
| 235 | 236 | mod display_shader { |
ombre/src/bytes.rs
+149 -1
| 1 | 1 | //! Byte slices. |
|
| 2 | - | use std::{any, mem, slice}; |
|
| 2 | + | use std::{any, io, mem, slice}; |
|
| 3 | 3 | ||
| 4 | 4 | pub mod encoding; |
|
| 5 | 5 | pub mod rle; |
|
| 6 | 6 | ||
| 7 | 7 | /// Used when an empty [`AsBytes`] instance is needed. |
| 98 | 98 | ||
| 99 | 99 | aligned |
|
| 100 | 100 | } |
|
| 101 | 101 | } |
|
| 102 | 102 | ||
| 103 | + | /// Extension trait for [`io::Read`] that provides methods for reading |
|
| 104 | + | /// primitive types in different endianness. |
|
| 105 | + | pub trait ReadBytes: io::Read { |
|
| 106 | + | /// Read a single byte. |
|
| 107 | + | fn read_u8(&mut self) -> io::Result<u8> { |
|
| 108 | + | let mut buf = [0; 1]; |
|
| 109 | + | self.read_exact(&mut buf)?; |
|
| 110 | + | Ok(buf[0]) |
|
| 111 | + | } |
|
| 112 | + | ||
| 113 | + | /// Read a 16-bit unsigned integer in little-endian format. |
|
| 114 | + | fn read_u16_le(&mut self) -> io::Result<u16> { |
|
| 115 | + | let mut buf = [0; 2]; |
|
| 116 | + | self.read_exact(&mut buf)?; |
|
| 117 | + | Ok(u16::from_le_bytes(buf)) |
|
| 118 | + | } |
|
| 119 | + | ||
| 120 | + | /// Read a 16-bit unsigned integer in big-endian format. |
|
| 121 | + | fn read_u16_be(&mut self) -> io::Result<u16> { |
|
| 122 | + | let mut buf = [0; 2]; |
|
| 123 | + | self.read_exact(&mut buf)?; |
|
| 124 | + | Ok(u16::from_be_bytes(buf)) |
|
| 125 | + | } |
|
| 126 | + | ||
| 127 | + | /// Read a 32-bit unsigned integer in little-endian format. |
|
| 128 | + | fn read_u32_le(&mut self) -> io::Result<u32> { |
|
| 129 | + | let mut buf = [0; 4]; |
|
| 130 | + | self.read_exact(&mut buf)?; |
|
| 131 | + | Ok(u32::from_le_bytes(buf)) |
|
| 132 | + | } |
|
| 133 | + | ||
| 134 | + | /// Read a 32-bit unsigned integer in big-endian format. |
|
| 135 | + | fn read_u32_be(&mut self) -> io::Result<u32> { |
|
| 136 | + | let mut buf = [0; 4]; |
|
| 137 | + | self.read_exact(&mut buf)?; |
|
| 138 | + | Ok(u32::from_be_bytes(buf)) |
|
| 139 | + | } |
|
| 140 | + | ||
| 141 | + | /// Read a 64-bit unsigned integer in little-endian format. |
|
| 142 | + | fn read_u64_le(&mut self) -> io::Result<u64> { |
|
| 143 | + | let mut buf = [0; 8]; |
|
| 144 | + | self.read_exact(&mut buf)?; |
|
| 145 | + | Ok(u64::from_le_bytes(buf)) |
|
| 146 | + | } |
|
| 147 | + | ||
| 148 | + | /// Read a 64-bit unsigned integer in big-endian format. |
|
| 149 | + | fn read_u64_be(&mut self) -> io::Result<u64> { |
|
| 150 | + | let mut buf = [0; 8]; |
|
| 151 | + | self.read_exact(&mut buf)?; |
|
| 152 | + | Ok(u64::from_be_bytes(buf)) |
|
| 153 | + | } |
|
| 154 | + | ||
| 155 | + | /// Read a 32-bit float in little-endian format. |
|
| 156 | + | fn read_f32_le(&mut self) -> io::Result<f32> { |
|
| 157 | + | let mut buf = [0; 4]; |
|
| 158 | + | self.read_exact(&mut buf)?; |
|
| 159 | + | Ok(f32::from_le_bytes(buf)) |
|
| 160 | + | } |
|
| 161 | + | ||
| 162 | + | /// Read a 32-bit float in big-endian format. |
|
| 163 | + | fn read_f32_be(&mut self) -> io::Result<f32> { |
|
| 164 | + | let mut buf = [0; 4]; |
|
| 165 | + | self.read_exact(&mut buf)?; |
|
| 166 | + | Ok(f32::from_be_bytes(buf)) |
|
| 167 | + | } |
|
| 168 | + | ||
| 169 | + | /// Read a 64-bit float in little-endian format. |
|
| 170 | + | fn read_f64_le(&mut self) -> io::Result<f64> { |
|
| 171 | + | let mut buf = [0; 8]; |
|
| 172 | + | self.read_exact(&mut buf)?; |
|
| 173 | + | Ok(f64::from_le_bytes(buf)) |
|
| 174 | + | } |
|
| 175 | + | ||
| 176 | + | /// Read a 64-bit float in big-endian format. |
|
| 177 | + | fn read_f64_be(&mut self) -> io::Result<f64> { |
|
| 178 | + | let mut buf = [0; 8]; |
|
| 179 | + | self.read_exact(&mut buf)?; |
|
| 180 | + | Ok(f64::from_be_bytes(buf)) |
|
| 181 | + | } |
|
| 182 | + | } |
|
| 183 | + | ||
| 184 | + | /// Extension trait for [`io::Write`] that provides methods for writing |
|
| 185 | + | /// primitive types in different endianness. |
|
| 186 | + | pub trait WriteBytes: io::Write { |
|
| 187 | + | /// Write a 16-bit unsigned integer in little-endian format. |
|
| 188 | + | fn write_u16_le(&mut self, n: u16) -> io::Result<usize> { |
|
| 189 | + | self.write_all(&n.to_le_bytes())?; |
|
| 190 | + | Ok(mem::size_of::<u16>()) |
|
| 191 | + | } |
|
| 192 | + | ||
| 193 | + | /// Write a 16-bit unsigned integer in big-endian format. |
|
| 194 | + | fn write_u16_be(&mut self, n: u16) -> io::Result<usize> { |
|
| 195 | + | self.write_all(&n.to_be_bytes())?; |
|
| 196 | + | Ok(mem::size_of::<u16>()) |
|
| 197 | + | } |
|
| 198 | + | ||
| 199 | + | /// Write a 32-bit unsigned integer in little-endian format. |
|
| 200 | + | fn write_u32_le(&mut self, n: u32) -> io::Result<usize> { |
|
| 201 | + | self.write_all(&n.to_le_bytes())?; |
|
| 202 | + | Ok(mem::size_of::<u32>()) |
|
| 203 | + | } |
|
| 204 | + | ||
| 205 | + | /// Write a 32-bit unsigned integer in big-endian format. |
|
| 206 | + | fn write_u32_be(&mut self, n: u32) -> io::Result<usize> { |
|
| 207 | + | self.write_all(&n.to_be_bytes())?; |
|
| 208 | + | Ok(mem::size_of::<u32>()) |
|
| 209 | + | } |
|
| 210 | + | ||
| 211 | + | /// Write a 64-bit unsigned integer in little-endian format. |
|
| 212 | + | fn write_u64_le(&mut self, n: u64) -> io::Result<usize> { |
|
| 213 | + | self.write_all(&n.to_le_bytes())?; |
|
| 214 | + | Ok(mem::size_of::<u64>()) |
|
| 215 | + | } |
|
| 216 | + | ||
| 217 | + | /// Write a 64-bit unsigned integer in big-endian format. |
|
| 218 | + | fn write_u64_be(&mut self, n: u64) -> io::Result<usize> { |
|
| 219 | + | self.write_all(&n.to_be_bytes())?; |
|
| 220 | + | Ok(mem::size_of::<u64>()) |
|
| 221 | + | } |
|
| 222 | + | ||
| 223 | + | /// Write a 32-bit float in little-endian format. |
|
| 224 | + | fn write_f32_le(&mut self, n: f32) -> io::Result<usize> { |
|
| 225 | + | self.write_all(&n.to_le_bytes())?; |
|
| 226 | + | Ok(mem::size_of::<f32>()) |
|
| 227 | + | } |
|
| 228 | + | ||
| 229 | + | /// Write a 32-bit float in big-endian format. |
|
| 230 | + | fn write_f32_be(&mut self, n: f32) -> io::Result<usize> { |
|
| 231 | + | self.write_all(&n.to_be_bytes())?; |
|
| 232 | + | Ok(mem::size_of::<f32>()) |
|
| 233 | + | } |
|
| 234 | + | ||
| 235 | + | /// Write a 64-bit float in little-endian format. |
|
| 236 | + | fn write_f64_le(&mut self, n: f64) -> io::Result<usize> { |
|
| 237 | + | self.write_all(&n.to_le_bytes())?; |
|
| 238 | + | Ok(mem::size_of::<f64>()) |
|
| 239 | + | } |
|
| 240 | + | ||
| 241 | + | /// Write a 64-bit float in big-endian format. |
|
| 242 | + | fn write_f64_be(&mut self, n: f64) -> io::Result<usize> { |
|
| 243 | + | self.write_all(&n.to_be_bytes())?; |
|
| 244 | + | Ok(mem::size_of::<f64>()) |
|
| 245 | + | } |
|
| 246 | + | } |
|
| 247 | + | ||
| 248 | + | impl<R: io::Read + ?Sized> ReadBytes for R {} |
|
| 249 | + | impl<W: io::Write + ?Sized> WriteBytes for W {} |
|
| 250 | + | ||
| 103 | 251 | /// Align a byte slice to any slice type. |
|
| 104 | 252 | /// |
|
| 105 | 253 | /// # Panics |
|
| 106 | 254 | /// |
|
| 107 | 255 | /// If the byte slice length is not a multiple of the size of a `T`. |
ombre/src/bytes/encoding.rs
+18 -50
| 1 | 1 | //! Binary encoding of `ombre` and `std` types. Useful for implementing file formats. |
|
| 2 | + | #![warn(missing_docs)] |
|
| 2 | 3 | use std::{io, mem}; |
|
| 3 | 4 | ||
| 5 | + | use super::{ReadBytes, WriteBytes}; |
|
| 6 | + | ||
| 4 | 7 | /// Things that can be encoded as binary. |
|
| 5 | 8 | pub trait Encode { |
|
| 6 | 9 | /// Encode an object into a writer. |
|
| 7 | 10 | fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error>; |
|
| 8 | 11 | } |
| 61 | 64 | writer.write_all(&[*self])?; |
|
| 62 | 65 | Ok(mem::size_of::<Self>()) |
|
| 63 | 66 | } |
|
| 64 | 67 | } |
|
| 65 | 68 | ||
| 66 | - | impl Encode for u16 { |
|
| 67 | - | fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> { |
|
| 68 | - | writer.write_all(&self.to_be_bytes())?; |
|
| 69 | - | Ok(mem::size_of::<Self>()) |
|
| 70 | - | } |
|
| 71 | - | } |
|
| 72 | - | ||
| 73 | - | impl Encode for u32 { |
|
| 74 | - | fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> { |
|
| 75 | - | writer.write_all(&self.to_be_bytes())?; |
|
| 76 | - | Ok(mem::size_of::<Self>()) |
|
| 77 | - | } |
|
| 78 | - | } |
|
| 79 | - | ||
| 80 | - | impl Encode for u64 { |
|
| 81 | - | fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> { |
|
| 82 | - | writer.write_all(&self.to_be_bytes())?; |
|
| 83 | - | Ok(mem::size_of::<Self>()) |
|
| 84 | - | } |
|
| 85 | - | } |
|
| 86 | - | ||
| 87 | - | impl Encode for f64 { |
|
| 88 | - | fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> { |
|
| 89 | - | writer.write_all(&self.to_be_bytes())?; |
|
| 90 | - | Ok(mem::size_of::<Self>()) |
|
| 69 | + | impl Decode for u8 { |
|
| 70 | + | fn decode<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, io::Error> { |
|
| 71 | + | reader.read_u8() |
|
| 91 | 72 | } |
|
| 92 | 73 | } |
|
| 93 | 74 | ||
| 94 | - | impl Encode for f32 { |
|
| 75 | + | impl Encode for u16 { |
|
| 95 | 76 | fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> { |
|
| 96 | - | writer.write_all(&self.to_be_bytes())?; |
|
| 97 | - | Ok(mem::size_of::<Self>()) |
|
| 77 | + | writer.write_u16_be(*self) |
|
| 98 | 78 | } |
|
| 99 | 79 | } |
|
| 100 | 80 | ||
| 101 | - | impl Decode for u8 { |
|
| 81 | + | impl Decode for u16 { |
|
| 102 | 82 | fn decode<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, io::Error> { |
|
| 103 | - | let buf: [u8; 1] = Decode::decode(reader)?; |
|
| 104 | - | Ok(u8::from_be_bytes(buf)) |
|
| 83 | + | reader.read_u16_be() |
|
| 105 | 84 | } |
|
| 106 | 85 | } |
|
| 107 | 86 | ||
| 108 | - | impl Decode for u16 { |
|
| 109 | - | fn decode<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, io::Error> { |
|
| 110 | - | let buf: [u8; 2] = Decode::decode(reader)?; |
|
| 111 | - | Ok(u16::from_be_bytes(buf)) |
|
| 87 | + | impl Encode for u32 { |
|
| 88 | + | fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> { |
|
| 89 | + | writer.write_u32_be(*self) |
|
| 112 | 90 | } |
|
| 113 | 91 | } |
|
| 114 | 92 | ||
| 115 | 93 | impl Decode for u32 { |
|
| 116 | 94 | fn decode<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, io::Error> { |
|
| 117 | - | let buf: [u8; 4] = Decode::decode(reader)?; |
|
| 118 | - | Ok(u32::from_be_bytes(buf)) |
|
| 95 | + | reader.read_u32_be() |
|
| 119 | 96 | } |
|
| 120 | 97 | } |
|
| 121 | 98 | ||
| 122 | - | impl Decode for u64 { |
|
| 123 | - | fn decode<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, io::Error> { |
|
| 124 | - | let buf: [u8; 8] = Decode::decode(reader)?; |
|
| 125 | - | Ok(u64::from_be_bytes(buf)) |
|
| 99 | + | impl Encode for f32 { |
|
| 100 | + | fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> { |
|
| 101 | + | writer.write_f32_be(*self) |
|
| 126 | 102 | } |
|
| 127 | 103 | } |
|
| 128 | 104 | ||
| 129 | 105 | impl Decode for f32 { |
|
| 130 | 106 | fn decode<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, io::Error> { |
|
| 131 | - | let buf: [u8; 4] = Decode::decode(reader)?; |
|
| 132 | - | Ok(f32::from_be_bytes(buf)) |
|
| 133 | - | } |
|
| 134 | - | } |
|
| 135 | - | ||
| 136 | - | impl Decode for f64 { |
|
| 137 | - | fn decode<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, io::Error> { |
|
| 138 | - | let buf: [u8; 8] = Decode::decode(reader)?; |
|
| 139 | - | Ok(f64::from_be_bytes(buf)) |
|
| 107 | + | reader.read_f32_be() |
|
| 140 | 108 | } |
|
| 141 | 109 | } |
|
| 142 | 110 | ||
| 143 | 111 | impl Encode for crate::gfx::Rgba8 { |
|
| 144 | 112 | fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> { |
ombre/src/gfx/image.rs
+18 -16
| 3 | 3 | use std::sync::Arc; |
|
| 4 | 4 | ||
| 5 | 5 | use super::{pixels, Color as _, Point2D, Rect, Rgba8, Size}; |
|
| 6 | 6 | use crate::{ |
|
| 7 | 7 | bytes::encoding::{Decode, Encode}, |
|
| 8 | + | bytes::{ReadBytes, WriteBytes}, |
|
| 8 | 9 | math::{Local, Zero}, |
|
| 9 | 10 | }; |
|
| 10 | 11 | ||
| 11 | 12 | /// File extension for RGBA images. |
|
| 12 | 13 | pub const FILE_EXTENSION: &str = "rgba"; |
| 98 | 99 | let size = size.into(); |
|
| 99 | 100 | ||
| 100 | 101 | Self::new(vec![Rgba8::TRANSPARENT; size.area() as usize], size) |
|
| 101 | 102 | } |
|
| 102 | 103 | ||
| 103 | - | /// Write the image to the writer in `.rgba` format. |
|
| 104 | - | pub fn write(&self, writer: &mut impl io::Write) -> Result<usize, io::Error> { |
|
| 105 | - | let mut n = 0; |
|
| 106 | - | ||
| 107 | - | n += MAGIC.encode(writer)?; |
|
| 108 | - | n += self.size.w.encode(writer)?; |
|
| 109 | - | n += self.size.h.encode(writer)?; |
|
| 110 | - | ||
| 111 | - | for p in self.pixels.iter() { |
|
| 112 | - | n += p.encode(writer)?; |
|
| 113 | - | } |
|
| 114 | - | Ok(n) |
|
| 115 | - | } |
|
| 116 | - | ||
| 117 | 104 | /// Get the texel color at the given position in the image. |
|
| 118 | 105 | pub fn sample(&self, point: Point2D<u32>) -> Option<&Rgba8> { |
|
| 119 | 106 | let offset = self.size.w * point.y + point.x; |
|
| 120 | 107 | self.pixels.get(offset as usize) |
|
| 121 | 108 | } |
| 141 | 128 | fn decode<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, io::Error> { |
|
| 142 | 129 | let magic: [u8; 4] = Decode::decode(reader)?; |
|
| 143 | 130 | if magic != MAGIC { |
|
| 144 | 131 | return Err(io::ErrorKind::InvalidData.into()); |
|
| 145 | 132 | } |
|
| 146 | - | let w = u32::decode(reader)? as usize; |
|
| 147 | - | let h = u32::decode(reader)? as usize; |
|
| 133 | + | let w = reader.read_u32_be()? as usize; |
|
| 134 | + | let h = reader.read_u32_be()? as usize; |
|
| 148 | 135 | let s = Size::new(w, h); |
|
| 149 | 136 | let n = s.area(); |
|
| 150 | 137 | ||
| 151 | 138 | let mut pixels = Vec::with_capacity(n); |
|
| 152 | 139 | while pixels.len() < n { |
| 155 | 142 | } |
|
| 156 | 143 | Ok(Self::new(pixels, s)) |
|
| 157 | 144 | } |
|
| 158 | 145 | } |
|
| 159 | 146 | ||
| 147 | + | impl Encode for Image { |
|
| 148 | + | fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> { |
|
| 149 | + | let mut n = 0; |
|
| 150 | + | ||
| 151 | + | n += MAGIC.encode(writer)?; |
|
| 152 | + | n += writer.write_u32_be(self.size.w)?; |
|
| 153 | + | n += writer.write_u32_be(self.size.h)?; |
|
| 154 | + | ||
| 155 | + | for pixel in self.pixels.iter() { |
|
| 156 | + | n += pixel.encode(writer)?; |
|
| 157 | + | } |
|
| 158 | + | Ok(n) |
|
| 159 | + | } |
|
| 160 | + | } |
|
| 161 | + | ||
| 160 | 162 | impl TryFrom<&[u8]> for Image { |
|
| 161 | 163 | type Error = io::Error; |
|
| 162 | 164 | ||
| 163 | 165 | fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> { |
|
| 164 | 166 | let mut cursor = io::Cursor::new(bytes); |