pnm decoding and encoding
Diffstat (limited to 'src/encode.rs')
| -rw-r--r-- | src/encode.rs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/encode.rs b/src/encode.rs new file mode 100644 index 0000000..c2a8c09 --- /dev/null +++ b/src/encode.rs @@ -0,0 +1,73 @@ +pub trait P<T: Copy> { + unsafe fn put<const N: usize>(&mut self, x: [T; N]); + #[cfg_attr(debug_assertions, track_caller)] + unsafe fn push(&mut self, x: T) { + self.put([x]); + } +} + +impl<T: Copy> P<T> for *mut T { + #[cfg_attr(debug_assertions, track_caller)] + unsafe fn put<const N: usize>(&mut self, x: [T; N]) { + self.copy_from(x.as_ptr(), N); + *self = self.add(N); + } +} + +pub const fn encode_bool(x: bool) -> u8 { + (x as u8) + b'0' +} + +const MAGIC: [u32; 256] = [ + 538976304, 538976305, 538976306, 538976307, 538976308, 538976309, 538976310, 538976311, + 538976312, 538976313, 538980401, 538980657, 538980913, 538981169, 538981425, 538981681, + 538981937, 538982193, 538982449, 538982705, 538980402, 538980658, 538980914, 538981170, + 538981426, 538981682, 538981938, 538982194, 538982450, 538982706, 538980403, 538980659, + 538980915, 538981171, 538981427, 538981683, 538981939, 538982195, 538982451, 538982707, + 538980404, 538980660, 538980916, 538981172, 538981428, 538981684, 538981940, 538982196, + 538982452, 538982708, 538980405, 538980661, 538980917, 538981173, 538981429, 538981685, + 538981941, 538982197, 538982453, 538982709, 538980406, 538980662, 538980918, 538981174, + 538981430, 538981686, 538981942, 538982198, 538982454, 538982710, 538980407, 538980663, + 538980919, 538981175, 538981431, 538981687, 538981943, 538982199, 538982455, 538982711, + 538980408, 538980664, 538980920, 538981176, 538981432, 538981688, 538981944, 538982200, + 538982456, 538982712, 538980409, 538980665, 538980921, 538981177, 538981433, 538981689, + 538981945, 538982201, 538982457, 538982713, 540028977, 540094513, 540160049, 540225585, + 540291121, 540356657, 540422193, 540487729, 540553265, 540618801, 540029233, 540094769, + 540160305, 540225841, 540291377, 540356913, 540422449, 540487985, 540553521, 540619057, + 0x20303231, 540095025, 540160561, 540226097, 540291633, 540357169, 540422705, 540488241, + 540553777, 540619313, 540029745, 540095281, 540160817, 540226353, 540291889, 540357425, + 540422961, 540488497, 540554033, 540619569, 540030001, 540095537, 540161073, 540226609, + 540292145, 540357681, 540423217, 540488753, 540554289, 540619825, 540030257, 540095793, + 540161329, 540226865, 540292401, 540357937, 540423473, 540489009, 540554545, 540620081, + 540030513, 540096049, 540161585, 540227121, 540292657, 540358193, 540423729, 540489265, + 540554801, 540620337, 540030769, 540096305, 540161841, 540227377, 540292913, 540358449, + 540423985, 540489521, 540555057, 540620593, 540031025, 540096561, 540162097, 540227633, + 540293169, 540358705, 540424241, 540489777, 540555313, 540620849, 540031281, 540096817, + 540162353, 540227889, 540293425, 540358961, 540424497, 540490033, 540555569, 540621105, + 540028978, 540094514, 540160050, 540225586, 540291122, 540356658, 540422194, 540487730, + 540553266, 540618802, 540029234, 540094770, 540160306, 540225842, 540291378, 540356914, + 540422450, 540487986, 540553522, 540619058, 540029490, 540095026, 540160562, 540226098, + 540291634, 540357170, 540422706, 540488242, 540553778, 540619314, 540029746, 540095282, + 540160818, 540226354, 540291890, 540357426, 540422962, 540488498, 540554034, 540619570, + 540030002, 540095538, 540161074, 540226610, 540292146, 540357682, 540423218, 540488754, + 540554290, 540619826, 540030258, 540095794, 540161330, 540226866, 540292402, 540357938, +]; + +/// Has a space. (_) +pub const fn encode_(x: u8) -> [u8; 4] { + MAGIC[x as usize].to_le_bytes() +} + +pub unsafe fn encodeu32(mut x: u32, buf: &mut *mut u8) { + let mut tmp = [0; 10]; + let mut tp = tmp.as_mut_ptr(); + while x >= 10 { + tp.write((x % 10) as u8 + b'0'); + tp = tp.add(1); + x /= 10; + } + tp.write(x as u8 + b'0'); + for b in tmp.into_iter().rev().skip_while(|&b| b == 0) { + buf.push(b); + } +} |