png is a bitmap format? who knew!
bendn 2024-03-20
commit b5ce1a6
-rw-r--r--.gitignore1
-rw-r--r--Cargo.toml25
-rw-r--r--LICENSE21
-rw-r--r--README.md13
-rw-r--r--benches/bench.rs84
-rw-r--r--benches/catbin0 -> 9159168 bytes
-rw-r--r--benches/plot.dat5
-rw-r--r--benches/plot.gnuplot18
-rw-r--r--benches/sped.svg1
-rw-r--r--src/lib.rs207
10 files changed, 375 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ffa3bbd
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+Cargo.lock \ No newline at end of file
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..3ce512b
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,25 @@
+[package]
+name = "pngenc"
+version = "0.1.0"
+edition = "2021"
+description = "portable network graphics (PNG) encoding"
+authors = ["bend-n <[email protected]>"]
+license = "MIT"
+repository = "https://github.com/bend-n/pngenc"
+exclude = [".gitignore", "benches"]
+keywords = ["image", "format", "encoding"]
+categories = ["multimedia::images", "graphics", "encoding"]
+
+[dependencies]
+atools = "0.1.1"
+crc32fast = { version = "1.4", features = ["nightly"] }
+simd-adler32 = "0.3.7"
+
+[dev-dependencies]
+png = "0.17"
+
+[profile.release]
+debug = 2
+opt-level = 3
+lto = "thin"
+incremental = true
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..2f002a4
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2024 bendn
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..2154f32
--- /dev/null
+++ b/README.md
@@ -0,0 +1,13 @@
+# pngenc
+
+encode your pngs, uncompressed
+
+## rationale
+
+well, you see, sometimes pngs dont need to be compressed? what do you mean you want to send them over the network? well, what if, like, you have to encode a png, but then you just run a compressor over it? double the work, innit? just directly talk to the compressor? well, youve got me now, but, still, ya know, maybe you wanted to write a uncompressed png just for kicks anyways? like, the png is temporary? maybe you wanted to send them to your iterm2 apple terminal and didnt want to bother spending time waiting for compression? eeeh? yeah, now ive got you.
+
+### sped
+
+ya see, it does do plenty of speeding
+
+![](./benches/sped.svg) \ No newline at end of file
diff --git a/benches/bench.rs b/benches/bench.rs
new file mode 100644
index 0000000..decbccf
--- /dev/null
+++ b/benches/bench.rs
@@ -0,0 +1,84 @@
+#![feature(test, array_chunks)]
+extern crate test;
+const W: u32 = 2144;
+const H: u32 = 1424;
+use atools::prelude::*;
+
+fn rgba() -> &'static [u8] {
+ Box::leak(std::hint::black_box(
+ rgb()
+ .array_chunks::<3>()
+ .flat_map(|&x| x.join(255))
+ .collect::<Box<_>>(),
+ ))
+}
+
+fn rgb() -> &'static [u8] {
+ std::hint::black_box(include_bytes!("cat"))
+}
+
+fn ya() -> &'static [u8] {
+ Box::leak(std::hint::black_box(
+ y().iter().flat_map(|&x| x.join(255)).collect::<Box<_>>(),
+ ))
+}
+
+fn y() -> &'static [u8] {
+ Box::leak(std::hint::black_box(
+ include_bytes!("cat")
+ .array_chunks::<3>()
+ .map(|&[r, g, b]| ((2126 * r as u32 + 7152 * g as u32 + 722 * b as u32) / 10000) as u8)
+ .collect::<Box<_>>(),
+ ))
+}
+
+macro_rules! m {
+ ($f:ident, $c:ident, $c2:ident, me $me:ident them $them: ident) => {
+ #[bench]
+ fn $me(b: &mut test::Bencher) {
+ let mut v = Vec::with_capacity(10 << 20);
+ let dat = $f();
+ // pngenc::encode(
+ // pngenc::Color::$c,
+ // (W, H),
+ // dat,
+ // &mut std::fs::File::create(stringify!($f)).unwrap(),
+ // )
+ // .unwrap();
+ b.bytes = dat.len() as _;
+ b.iter(|| {
+ v.clear();
+ pngenc::ode(pngenc::Color::$c, (W, H), dat, &mut v).unwrap();
+ std::hint::black_box(&v);
+ })
+ }
+
+ #[bench]
+ fn $them(b: &mut test::Bencher) {
+ let mut v = Vec::with_capacity(10 << 20);
+ let dat = $f();
+ b.bytes = dat.len() as _;
+ b.iter(|| {
+ v.clear();
+ let mut enc = png::Encoder::new(&mut v, W, H);
+ enc.set_color(png::ColorType::$c2);
+ enc.set_depth(png::BitDepth::Eight);
+ enc.set_source_gamma(png::ScaledFloat::new(1.0 / 2.2));
+ enc.set_source_chromaticities(png::SourceChromaticities::new(
+ (0.31270, 0.32900),
+ (0.64000, 0.33000),
+ (0.30000, 0.60000),
+ (0.15000, 0.06000),
+ ));
+ let mut writer = enc.write_header().unwrap();
+ writer.write_image_data(dat).unwrap();
+ drop(writer);
+ std::hint::black_box(&v);
+ })
+ }
+ };
+}
+m![rgba, RGBA, Rgba, me pngenc_rgba them png_rgba];
+m![rgb, RGB, Rgb, me pngenc_rgb them png_rgb];
+m![ya, YA, GrayscaleAlpha, me pngenc_ya them png_ya];
+m![y, Y, Grayscale, me pngenc_y them png_y];
diff --git a/benches/cat b/benches/cat
new file mode 100644
index 0000000..7189d8d
--- /dev/null
+++ b/benches/cat
Binary files differ
diff --git a/benches/plot.dat b/benches/plot.dat
new file mode 100644
index 0000000..31fd702
--- /dev/null
+++ b/benches/plot.dat
@@ -0,0 +1,5 @@
+Op pngenc png
+RGBA 1.962 0.579
+RGB 2.596 0.482
+YA 3.481 0.567
+Y 2.922 0.503
diff --git a/benches/plot.gnuplot b/benches/plot.gnuplot
new file mode 100644
index 0000000..c5751e2
--- /dev/null
+++ b/benches/plot.gnuplot
@@ -0,0 +1,18 @@
+set terminal svg enhanced background rgb "#0D1117" size 480 480
+
+set style data histogram
+set style histogram cluster gap 1
+
+set ylabel "speed in GB/s (higher is better)"
+set auto x
+set yrange [0:4]
+
+set style line 12 lc rgb '#1F2430' lt 1 lw 2 dt 22
+
+set grid ytics ls 12
+
+set output "sped.svg"
+set title "encoding speed"
+
+plot 'plot.dat' using 2:xtic(1) title col, \
+ '' using 3:xtic(1) title col \ No newline at end of file
diff --git a/benches/sped.svg b/benches/sped.svg
new file mode 100644
index 0000000..d3a1b53
--- /dev/null
+++ b/benches/sped.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="480" height="480"><desc>Produced by GNUPLOT 6.0 patchlevel 0</desc><path fill="#0d1117" d="M0 0h480v480H0z"/><path fill="none" stroke="#1F2430" stroke-dasharray="5,8" stroke-width="2" d="M72.53 444h382.29" class="gridline" color="#000"/><g fill="none" stroke="currentColor" color="#000"><path stroke="#E6EDF3" d="M72.53 444h9m373.29 0h-9"/><text fill="#E6EDF3" stroke="none" font-family="Arial" font-size="12" text-anchor="end" transform="translate(64.14 447.9)"><tspan> 0</tspan></text></g><path fill="none" stroke="#1F2430" stroke-dasharray="5,8" stroke-width="2" d="M72.53 396.38h382.29" class="gridline" color="#000"/><g fill="none" stroke="currentColor" color="#000"><path stroke="#E6EDF3" d="M72.53 396.38h9m373.29 0h-9"/><text fill="#E6EDF3" stroke="none" font-family="Arial" font-size="12" text-anchor="end" transform="translate(64.14 400.28)"><tspan> 0.5</tspan></text></g><path fill="none" stroke="#1F2430" stroke-dasharray="5,8" stroke-width="2" d="M72.53 348.75h382.29" class="gridline" color="#000"/><g fill="none" stroke="currentColor" color="#000"><path stroke="#E6EDF3" d="M72.53 348.75h9m373.29 0h-9"/><text fill="#E6EDF3" stroke="none" font-family="Arial" font-size="12" text-anchor="end" transform="translate(64.14 352.65)"><tspan> 1</tspan></text></g><path fill="none" stroke="#1F2430" stroke-dasharray="5,8" stroke-width="2" d="M72.53 301.13h382.29" class="gridline" color="#000"/><g fill="none" stroke="currentColor" color="#000"><path stroke="#E6EDF3" d="M72.53 301.13h9m373.29 0h-9"/><text fill="#E6EDF3" stroke="none" font-family="Arial" font-size="12" text-anchor="end" transform="translate(64.14 305.03)"><tspan> 1.5</tspan></text></g><path fill="none" stroke="#1F2430" stroke-dasharray="5,8" stroke-width="2" d="M72.53 253.5h382.29" class="gridline" color="#000"/><g fill="none" stroke="currentColor" color="#000"><path stroke="#E6EDF3" d="M72.53 253.5h9m373.29 0h-9"/><text fill="#E6EDF3" stroke="none" font-family="Arial" font-size="12" text-anchor="end" transform="translate(64.14 257.4)"><tspan> 2</tspan></text></g><path fill="none" stroke="#1F2430" stroke-dasharray="5,8" stroke-width="2" d="M72.53 205.88h382.29" class="gridline" color="#000"/><g fill="none" stroke="currentColor" color="#000"><path stroke="#E6EDF3" d="M72.53 205.88h9m373.29 0h-9"/><text fill="#E6EDF3" stroke="none" font-family="Arial" font-size="12" text-anchor="end" transform="translate(64.14 209.78)"><tspan> 2.5</tspan></text></g><path fill="none" stroke="#1F2430" stroke-dasharray="5,8" stroke-width="2" d="M72.53 158.26h382.29" class="gridline" color="#000"/><g fill="none" stroke="currentColor" color="#000"><path stroke="#E6EDF3" d="M72.53 158.26h9m373.29 0h-9"/><text fill="#E6EDF3" stroke="none" font-family="Arial" font-size="12" text-anchor="end" transform="translate(64.14 162.16)"><tspan> 3</tspan></text></g><path fill="none" stroke="#1F2430" stroke-dasharray="5,8" stroke-width="2" d="M72.53 110.63h247.42m126.48 0h8.39" class="gridline" color="#000"/><g fill="none" stroke="currentColor" color="#000"><path stroke="#E6EDF3" d="M72.53 110.63h9m373.29 0h-9"/><text fill="#E6EDF3" stroke="none" font-family="Arial" font-size="12" text-anchor="end" transform="translate(64.14 114.53)"><tspan> 3.5</tspan></text></g><path fill="none" stroke="#1F2430" stroke-dasharray="5,8" stroke-width="2" d="M72.53 63.01h382.29" class="gridline" color="#000"/><g fill="none" stroke="currentColor" color="#000"><path stroke="#E6EDF3" d="M72.53 63.01h9m373.29 0h-9"/><text fill="#E6EDF3" stroke="none" font-family="Arial" font-size="12" text-anchor="end" transform="translate(64.14 66.91)"><tspan> 4</tspan></text></g><g fill="none" stroke="currentColor" color="#000"><path stroke="#E6EDF3" d="M148.99 444v-9"/><text fill="#E6EDF3" stroke="none" font-family="Arial" font-size="12" text-anchor="middle" transform="translate(148.99 465.9)"><tspan>RGBA</tspan></text></g><g fill="none" stroke="currentColor" color="#000"><path stroke="#E6EDF3" d="M225.45 444v-9"/><text fill="#E6EDF3" stroke="none" font-family="Arial" font-size="12" text-anchor="middle" transform="translate(225.45 465.9)"><tspan>RGB</tspan></text></g><g fill="none" stroke="currentColor" color="#000"><path stroke="#E6EDF3" d="M301.9 444v-9"/><text fill="#E6EDF3" stroke="none" font-family="Arial" font-size="12" text-anchor="middle" transform="translate(301.9 465.9)"><tspan>YA</tspan></text></g><g fill="none" stroke="currentColor" color="#000"><path stroke="#E6EDF3" d="M378.36 444v-9"/><text fill="#E6EDF3" stroke="none" font-family="Arial" font-size="12" text-anchor="middle" transform="translate(378.36 465.9)"><tspan>Y</tspan></text></g><path fill="none" stroke="#E6EDF3" d="M72.53 63.01V444h382.29V63.01z" color="#000"/><text fill="#E6EDF3" stroke-width="2" color="#000" font-family="Verdana" font-size="14" text-anchor="end" transform="translate(378.69 87.06)"><tspan>pngenc</tspan></text><g fill="none" stroke="currentColor" stroke-width="2" color="#000"><path fill="#73D0FF" stroke="none" d="M388.48 87.76h48.16v-10.5h-48.16z" shape-rendering="crispEdges"/><path stroke="#1A1F29" d="M388.48 87.76h48.16v-10.5h-48.16z"/></g><g fill="none" stroke="currentColor" stroke-width="2" color="#000"><path fill="#73D0FF" stroke="none" d="M123.5 444H149V257.11h-25.5z" shape-rendering="crispEdges"/><path stroke="#1A1F29" d="M123.5 444V257.12h25.49V444z"/></g><g fill="none" stroke="currentColor" stroke-width="2" color="#000"><path fill="#73D0FF" stroke="none" d="M199.96 444h25.5V196.73h-25.5z" shape-rendering="crispEdges"/><path stroke="#1A1F29" d="M199.96 444V196.74h25.49V444z"/></g><g fill="none" stroke="currentColor" stroke-width="2" color="#000"><path fill="#73D0FF" stroke="none" d="M276.42 444h25.49V112.43h-25.49z" shape-rendering="crispEdges"/><path stroke="#1A1F29" d="M276.42 444V112.44h25.48V444z"/></g><g fill="none" stroke="currentColor" stroke-width="2" color="#000"><path fill="#73D0FF" stroke="none" d="M352.88 444h25.49V165.68h-25.49z" shape-rendering="crispEdges"/><path stroke="#1A1F29" d="M352.88 444V165.69h25.48V444z"/></g><text fill="#E6EDF3" stroke-width="2" color="#000" font-family="Verdana" font-size="14" text-anchor="end" transform="translate(378.69 108.06)"><tspan>png</tspan></text><g fill="none" stroke="currentColor" stroke-width="2" color="#000"><path fill="#FFD173" stroke="none" d="M388.48 108.76h48.16v-10.5h-48.16z" shape-rendering="crispEdges"/><path stroke="#1A1F29" d="M388.48 108.76h48.16v-10.5h-48.16z"/></g><g fill="none" stroke="currentColor" stroke-width="2" color="#000"><path fill="#FFD173" stroke="none" d="M148.99 444h25.49v-55.16h-25.49z" shape-rendering="crispEdges"/><path stroke="#1A1F29" d="M148.99 444v-55.15h25.48V444z"/></g><g fill="none" stroke="currentColor" stroke-width="2" color="#000"><path fill="#FFD173" stroke="none" d="M225.45 444h25.49v-45.92h-25.49z" shape-rendering="crispEdges"/><path stroke="#1A1F29" d="M225.45 444v-45.91h25.48V444z"/></g><g fill="none" stroke="currentColor" stroke-width="2" color="#000"><path fill="#FFD173" stroke="none" d="M301.9 444h25.5v-54.02h-25.5z" shape-rendering="crispEdges"/><path stroke="#1A1F29" d="M301.9 444v-54.01h25.49V444z"/></g><g fill="none" stroke="currentColor" stroke-width="2" color="#000"><path fill="#FFD173" stroke="none" d="M378.36 444h25.5v-47.92h-25.5z" shape-rendering="crispEdges"/><path stroke="#1A1F29" d="M378.36 444v-47.91h25.49V444z"/></g><g fill="none" stroke="currentColor" color="#000"><path stroke="#E6EDF3" d="M72.53 63.01V444h382.29V63.01z"/><text fill="#E6EDF3" stroke="none" font-family="Verdana" font-size="18" text-anchor="middle" transform="rotate(-90 137.32 116.19)"><tspan>speed in GB/s (higher is better)</tspan></text></g><text fill="#E6EDF3" color="#000" font-family="Verdana" font-size="18" text-anchor="middle" transform="translate(263.67 32.86)"><tspan>encoding speed</tspan></text></svg> \ No newline at end of file
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..41d3e86
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,207 @@
+//! uncompressing png encoding crate
+//! ```
+//! # let mut v = vec![];
+//! pngenc::ode(
+//! pngenc::RGB, // color type
+//! (2144, 1424), // width and height
+//! include_bytes!("../benches/cat"), // image to encode
+//! # /*
+//! &mut std::fs::File::create("hey.png").unwrap(), // output writer
+//! # */
+//! # &mut v,
+//! ).unwrap();
+//! # assert_eq!(crc32fast::hash(&v), 0xd7d47c0e);
+//! ```
+#![allow(incomplete_features)]
+#![feature(generic_const_exprs, test, slice_as_chunks, array_chunks)]
+use atools::prelude::*;
+use std::{
+ io::{self, Write},
+ iter::once,
+};
+
+pub use Color::*;
+
+#[derive(Copy, Debug, Clone)]
+#[repr(u8)]
+/// Color types.
+pub enum Color {
+ /// Grayscale
+ Y = 1,
+ /// Grayscale with alpha
+ YA,
+ /// Red, green, blue
+ RGB,
+ /// RGB with alpha
+ RGBA,
+}
+
+impl Color {
+ /// Color depth (number of channels)
+ #[must_use]
+ pub const fn depth(self) -> u8 {
+ self as u8
+ }
+
+ const fn ty(self) -> u8 {
+ match self {
+ Color::Y => 0,
+ Color::YA => 4,
+ Color::RGB => 2,
+ Color::RGBA => 6,
+ }
+ }
+}
+
+trait W: Write {
+ fn u32(&mut self, x: u32) -> io::Result<()> {
+ self.write_all(&x.to_be_bytes())
+ }
+ fn w(&mut self, x: impl AsRef<[u8]>) -> io::Result<()> {
+ self.write_all(x.as_ref())
+ }
+}
+
+impl<T: Write> W for T {}
+
+const HEADER: &[u8; 8] = b"\x89PNG\x0d\x0a\x1a\x0a";
+
+fn chunk_len(x: usize) -> usize {
+ 4 // length
+ + 4 // type
+ + x // data
+ + 4 // crc
+}
+
+/// Get the size of an encoded png. Guaranteed to exactly equal the size of the encoded png.
+pub fn size(color: Color, (width, height): (u32, u32)) -> usize {
+ HEADER.len()
+ + chunk_len(13) // IHDR
+ + chunk_len(deflate_size(((width * color.depth() as u32 + 1) * height) as usize)) // IDAT
+ + chunk_len(1) // sRGB
+ + chunk_len(0) // IEND
+}
+
+#[doc(alias = "encode")]
+/// Encode a png without any compression.
+/// Takes advantage of the [Non-compressed blocks](http://www.zlib.org/rfc-deflate.html#noncompressed) deflate feature.
+///
+/// If you *do* want a compressed image, I recommend the [oxipng](https://docs.rs/oxipng/latest/oxipng/struct.RawImage.html) raw image api.
+///
+/// # Panics
+///
+/// if your width * height * color depth isnt data's length
+pub fn ode(
+ color: Color,
+ (width, height): (u32, u32),
+ data: &[u8],
+ to: &mut impl Write,
+) -> std::io::Result<()> {
+ assert_eq!(
+ (width as usize * height as usize)
+ .checked_mul(color.depth() as usize)
+ .unwrap(),
+ data.len(),
+ "please dont lie to me"
+ );
+ to.w(HEADER)?;
+ chunk(
+ *b"IHDR",
+ &width
+ .to_be_bytes()
+ .couple(height.to_be_bytes())
+ .join(8) // bit depth
+ .join(color.ty())
+ .join(0)
+ .join(0)
+ .join(0),
+ to,
+ )?;
+
+ // removing this allocation is not a performance gain
+ let mut scanned = Vec::<u8>::with_capacity(((width * color.depth() as u32 + 1) * height) as _);
+ let mut out = scanned.as_mut_ptr();
+
+ data.chunks(width as usize * color.depth() as usize)
+ // set filter type for each scanline
+ .flat_map(|x| once(0).chain(x.iter().copied()))
+ .for_each(|x| unsafe {
+ out.write(x);
+ out = out.add(1);
+ });
+ unsafe { scanned.set_len(((width * color.depth() as u32 + 1) * height) as _) };
+
+ let data = deflate(&scanned);
+ chunk(*b"sRGB", &[0], to)?;
+ chunk(*b"IDAT", &data, to)?;
+ chunk(*b"IEND", &[], to)?;
+ Ok(())
+}
+
+fn chunk(ty: [u8; 4], data: &[u8], to: &mut impl Write) -> std::io::Result<()> {
+ to.u32(data.len() as _)?;
+ to.w(ty)?;
+ to.w(data)?;
+ let mut crc = crc32fast::Hasher::new();
+ crc.update(&ty);
+ crc.update(data);
+ to.u32(crc.finalize())?;
+ Ok(())
+}
+
+fn deflate_size(x: usize) -> usize {
+ // 2 header bytes, each header of chunk, and add remainder chunk, along with 4 bytes for adler32
+ 2 + 5 * (x / CHUNK_SIZE) + usize::from(x != (x / CHUNK_SIZE) * CHUNK_SIZE || x == 0) + x + 4 + 4
+}
+
+trait P<T: Copy> {
+ unsafe fn put<const N: usize>(&mut self, x: [T; N]);
+}
+
+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);
+ }
+}
+
+const CHUNK_SIZE: usize = 0xffff;
+fn deflate(data: &[u8]) -> Vec<u8> {
+ let mut adler = simd_adler32::Adler32::new();
+ let (chunks, remainder) = data.as_chunks::<CHUNK_SIZE>();
+ // SAFETY: deflate_size is very correct.
+ let mut out = Vec::<u8>::with_capacity(deflate_size(data.len()));
+ let mut optr = out.as_mut_ptr();
+ /// return LSB and SLSB
+ fn split(n: u16) -> [u8; 2] {
+ [(n & 0xff) as u8, ((n >> 8) & 0xff) as u8]
+ }
+ // 32k window
+ unsafe { optr.put([0b1_111_000, 1]) };
+ chunks.iter().for_each(|x| unsafe {
+ adler.write(x);
+ // http://www.zlib.org/rfc-deflate.html#noncompressed
+ optr.put(
+ [0b000]
+ // lsb and slsb [255, 255]
+ .couple(split(CHUNK_SIZE as _))
+ // ones complement -- [0, 0]
+ .couple(split(CHUNK_SIZE as _).map(|x| !x)),
+ );
+ optr.put(*x);
+ });
+ unsafe {
+ adler.write(remainder);
+ optr.put(
+ [0b001]
+ .couple(split(CHUNK_SIZE as _))
+ .couple(split(CHUNK_SIZE as _).map(|x| !x)),
+ );
+ optr.copy_from(remainder.as_ptr(), remainder.len());
+ optr = optr.add(remainder.len());
+ };
+ unsafe { optr.put(adler.finish().to_be_bytes()) };
+ unsafe { out.set_len(deflate_size(data.len())) }
+ out
+}