Skip to content

Instantly share code, notes, and snippets.

@LingDong-
LingDong- / png_write.js
Last active October 10, 2022 20:06
a bare minimum, non-compressing PNG writer in 80 lines
// a bare minimum, non-compressing PNG writer in 80 lines
//
// - supports gray, gray+A, RGB, RGBA, bit depth: 8 (0-255)
// - uses "non-compressed blocks" of DEFLATE spec
// - writes IHDR, IDAT, IEND -- optional chunks can be
// passed as function argument to be encoded (see example)
//
// reference:
// - https://en.wikipedia.org/wiki/Portable_Network_Graphics
// - https://datatracker.ietf.org/doc/html/rfc1951
@LingDong-
LingDong- / png_read.js
Created October 10, 2022 15:05
a baseline PNG decoder in 400 lines
// baseline PNG decoder in 400 lines
//
// - supports all color types: RGBA, RGB, gray+A, gray, paletted
// - supports all bit depths: 1, 2, 4, 8, 16 bits (color type dependant, per spec)
// - allways return RGBA(0-255) format: other color+bit combinations will be scaled
// - downscaled 16-bit values retain floating point, e.g. 1000/65535 -> 3.89/255
// - support transparency palette / chroma-key
// - supports interlaced / non-interlaced
// - supports all zlib methods: non-compressed / fixed / dynamic huffman
// - supports all filters: none / sub / up / average / paeth
@LingDong-
LingDong- / png_write1.js
Created October 10, 2022 20:05
a baseline somewhat-compressing PNG writer in 150 lines
// baseline somewhat-compressing PNG writer in 150 lines
//
// - supports gray, gray+A, RGB, RGBA, bit depth : 8
// - uses "fixed" huffman codes of DEFLATE spec
// - compresses well for graphics with large "flat" areas,
// basically pixel-wise run-length encoding translated to LZ77 style <len,dist>,
// always filtered with SUB filter
// - writes IHDR, IDAT, IEND -- optional chunks can be
// passed as function argument to be encoded (see example)
//
@LingDong-
LingDong- / bpmrw.h
Created April 19, 2023 15:58
tiny .BMP image reader/writer in C99
/*
bmprw.h
tiny .BMP image reader/writer in C99
with no memory allocations
reader supports:
- 1, 4, 8, 24, 32 bit depth, palette / no palette
- RLE4 / RLE8 compression / no compression
@LingDong-
LingDong- / bsl124.py
Created June 1, 2023 19:59
(minimal) TI MSP430 μC 1,2,4 Family BSL flasher (slau319af)
"""
bsl124.py
(minimal) TI MSP430 1,2,4 Family BSL flasher
using pyserial, works on windows, mac, and linux
- implementation of slau319af.pdf
- programming MSP430 chips via USB-TTL / FTDI / ...
- supports Intel HEX format
- supports F1xx, F2xx, F4xx, G2xx3 chips
@LingDong-
LingDong- / encode_anim_gif.js
Created September 15, 2023 00:27
minimal animated GIF encoder in 50 lines
// encode_anim_gif.js
// minimal code to make animated GIFs from arrays of pixels
// - no compression
// - supports 127 colors (127 shades of gray by default)
function encode_anim_gif(frames,w,h,delay=5){
let bytes = [];
bytes.push(0x47,0x49,0x46,0x38,0x39,0x61);
bytes.push(w&0xff);
bytes.push((w>>8)&0xff);