~shabbyrobe/pngbanger

b1b49e45b6d6d1f3277c7bbe39dbd02dc17d1673 — Blake Williams 10 months ago e0dbea9
Add README, misc tweaks
2 files changed, 65 insertions(+), 36 deletions(-)

A README.md
M main.go
A README.md => README.md +39 -0
@@ 0,0 1,39 @@
# PNG Banger

Hand rolled, artisinal, piece of junk PNG encoder.

Features:

- 🎢 Hand rolled CRC32
- 🎨 Artisinal Adler32
- 🖼️  Piece of junk uncompressed zlib/DEFLATE framing
- ⛳ Adequate, if sub-par, performance
- 🛏️  Arbitrary limitations due to laziness
- 🚮 No tests
- 🍝 Not even useful as copypasta


## Whyyy?

I have a very handy piece of Javascript copypasta that encodes BMP files (most
underrated image format ever, not joking). It's surprisingly useful for adding
image grinding functionalty to existing code and avoiding grappling with
shithouse Javascript image libraries when they're not needed.

I wanted to see if a similar thing was viable for encoding basic, no-frills,
don't-really-care-how-big-it-is PNG files. Looks like it probably isn't. Oh
well.


## Expectation management

This is a tool I hack on for my own amusement in an ad-hoc fashion. *No
stability guarantees are made*, the code is *not guaranteed to work*, and
anything may be changed, renamed or removed at any time as I see fit.

If you wish to use any of this, I strongly recommend you copy-paste pieces
as-needed (including tests and license/attribution) into your own project,
or fork it for your own purposes.

Bug reports are welcome, feature requests discouraged, and code contributions
will not be accepted.

M main.go => main.go +26 -36
@@ 149,10 149,8 @@ func appendZlibFraming(out []byte, data []byte) []byte {
		var sz16 = uint16(sz) // Encoded size includes the size bytes and mark
		out = append(out,
			mark,
			uint8(sz16),
			uint8(sz16>>8),
			uint8(^sz16),
			uint8(^sz16>>8))
			uint8(sz16), uint8(sz16>>8),
			uint8(^sz16), uint8(^sz16>>8))
		out = append(out, data[:sz]...)
		data = data[sz:]
	}


@@ 360,9 358,9 @@ func pngBangPalettedEntangled(img *image.Paletted) []byte {

	var blockSizeIdx = len(out) + 1 // When we write the zlib block size, write it to this index
	out = append(out,
		0,    // mark
		0, 0, // len
		0, 0, // ^len
		0x00,       // mark
		0x00, 0x00, // len
		0x00, 0x00, // ^len
	)

	var blockSize int


@@ 372,17 370,15 @@ func pngBangPalettedEntangled(img *image.Paletted) []byte {
		if blockSize+len(row)+1 > maxFrame {
			// Don't bother with perfectly stuffed frames, this is simpler.
			sz16 := uint16(blockSize)
			out[blockSizeIdx+0] = uint8(sz16)
			out[blockSizeIdx+1] = uint8(sz16 >> 8)
			out[blockSizeIdx+2] = uint8(^sz16)
			out[blockSizeIdx+3] = uint8(^sz16 >> 8)
			out[blockSizeIdx+0], out[blockSizeIdx+1] = uint8(sz16), uint8(sz16>>8)
			out[blockSizeIdx+2], out[blockSizeIdx+3] = uint8(^sz16), uint8(^sz16>>8)
			blockSize = 0

			blockSizeIdx = len(out) + 1
			out = append(out,
				0,    // mark
				0, 0, // len
				0, 0, // ^len
				0x00,       // mark
				0x00, 0x00, // len
				0x00, 0x00, // ^len
			)
		}



@@ 396,13 392,11 @@ func pngBangPalettedEntangled(img *image.Paletted) []byte {
	}

	sz16 := uint16(blockSize)
	out[blockSizeIdx+0] = uint8(sz16)
	out[blockSizeIdx+1] = uint8(sz16 >> 8)
	out[blockSizeIdx+2] = uint8(^sz16)
	out[blockSizeIdx+3] = uint8(^sz16 >> 8)
	out[blockSizeIdx+0], out[blockSizeIdx+1] = uint8(sz16), uint8(sz16>>8)
	out[blockSizeIdx+2], out[blockSizeIdx+3] = uint8(^sz16), uint8(^sz16>>8)
	out = append(out,
		1,    // mark
		0, 0, // len
		0x01,       // mark
		0x00, 0x00, // len
		0xff, 0xff, // ^len
	)



@@ 483,9 477,9 @@ func pngBangTrueEntangled(img *image.NRGBA) []byte {

	var blockSizeIdx = len(out) + 1 // When we write the zlib block size, write it to this index
	out = append(out,
		0,    // mark
		0, 0, // len
		0, 0, // ^len
		0x00,       // mark
		0x00, 0x00, // len
		0x00, 0x00, // ^len
	)

	var blockSize int


@@ 496,17 490,15 @@ func pngBangTrueEntangled(img *image.NRGBA) []byte {
			// FIXME: split rows will be necessary if we want to support rows
			// greater than 8191px.
			sz16 := uint16(blockSize)
			out[blockSizeIdx+0] = uint8(sz16)
			out[blockSizeIdx+1] = uint8(sz16 >> 8)
			out[blockSizeIdx+2] = uint8(^sz16)
			out[blockSizeIdx+3] = uint8(^sz16 >> 8)
			out[blockSizeIdx+0], out[blockSizeIdx+1] = uint8(sz16), uint8(sz16>>8)
			out[blockSizeIdx+2], out[blockSizeIdx+3] = uint8(^sz16), uint8(^sz16>>8)

			blockSize = 0
			blockSizeIdx = len(out) + 1
			out = append(out,
				0,    // mark
				0, 0, // len
				0, 0, // ^len
				0x00,       // mark
				0x00, 0x00, // len
				0x00, 0x00, // ^len
			)
		}



@@ 520,13 512,11 @@ func pngBangTrueEntangled(img *image.NRGBA) []byte {
	}

	sz16 := uint16(blockSize)
	out[blockSizeIdx+0] = uint8(sz16)
	out[blockSizeIdx+1] = uint8(sz16 >> 8)
	out[blockSizeIdx+2] = uint8(^sz16)
	out[blockSizeIdx+3] = uint8(^sz16 >> 8)
	out[blockSizeIdx+0], out[blockSizeIdx+1] = uint8(sz16), uint8(sz16>>8)
	out[blockSizeIdx+2], out[blockSizeIdx+3] = uint8(^sz16), uint8(^sz16>>8)
	out = append(out,
		1,    // mark
		0, 0, // len
		0x01,       // mark
		0x00, 0x00, // len
		0xff, 0xff, // ^len
	)