//- png writer (no zlib): per row filter pick + LZ77 with fixed huffman codes, one deflate block // NOTE: diagrams are mostly flat color, so even fixed codes compress them 20-50x. good enough for // export, and zero dependencies. typedef struct PngBits PngBits; struct PngBits { u8 *data; u64 size; u64 cap; u32 acc; u32 count; }; internal void png_bits_put(PngBits *b, u32 value, u32 bits) { b->acc |= value << b->count; b->count += bits; while(b->count >= 8) { if(b->size < b->cap) b->data[b->size++] = (u8)b->acc; b->acc >>= 8; b->count -= 8; } } internal void png_bits_flush(PngBits *b) { if(b->count > 0 && b->size < b->cap) b->data[b->size++] = (u8)b->acc; b->acc = b->count = 0; } // NOTE: huffman codes go most significant bit first, the bit stream is lsb first internal void png_put_code(PngBits *b, u32 code, u32 bits) { u32 rev = 0; for(u32 i = 0; i < bits; ++i) rev |= ((code >> i) & 1u) << (bits - 1 - i); png_bits_put(b, rev, bits); } internal void png_put_literal(PngBits *b, u32 sym) { if(sym <= 143) png_put_code(b, 0x30 + sym, 8); else if(sym <= 255) png_put_code(b, 0x190 + (sym - 144), 9); else if(sym <= 279) png_put_code(b, sym - 256, 7); else png_put_code(b, 0xC0 + (sym - 280), 8); } global_variable const u16 png_len_base[29] = {3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258}; global_variable const u8 png_len_extra[29] = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0}; global_variable const u16 png_dist_base[30] = {1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577}; global_variable const u8 png_dist_extra[30]= {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13}; internal void png_put_match(PngBits *b, u32 length, u32 distance) { u32 li = 28; while(li > 0 && png_len_base[li] > length) --li; png_put_literal(b, 257 + li); if(png_len_extra[li]) png_bits_put(b, length - png_len_base[li], png_len_extra[li]); u32 di = 29; while(di > 0 && png_dist_base[di] > distance) --di; png_put_code(b, di, 5); if(png_dist_extra[di]) png_bits_put(b, distance - png_dist_base[di], png_dist_extra[di]); } // NOTE: zlib stream (header, one fixed huffman block, adler32) of in into out. returns the size internal u64 png_deflate(const u8 *in, u64 size, u8 *out, u64 cap) { PngBits b = {out, 0, cap, 0, 0}; png_bits_put(&b, 0x78, 8); png_bits_put(&b, 0x01, 8); png_bits_put(&b, 1, 1); // BFINAL png_bits_put(&b, 1, 2); // BTYPE = fixed huffman enum { HASH_BITS = 15, WINDOW = 32768, MAX_CHAIN = 24 }; s32 *head = (s32 *)malloc(sizeof(s32) * (1u << HASH_BITS)); s32 *prev = (s32 *)malloc(sizeof(s32) * WINDOW); if(!head || !prev) { free(head); free(prev); return 0; } for(u32 i = 0; i < (1u << HASH_BITS); ++i) head[i] = -1; u64 i = 0; while(i < size) { u32 best_len = 0, best_dist = 0; if(i + 3 <= size) { u32 h = ((u32)in[i] << 10 ^ (u32)in[i + 1] << 5 ^ (u32)in[i + 2]) & ((1u << HASH_BITS) - 1); s32 cand = head[h]; u32 chain = 0; u64 max_len = Min((u64)258, size - i); while(cand >= 0 && i - (u64)cand <= WINDOW - 1 && chain++ < MAX_CHAIN) { const u8 *a = in + cand, *c = in + i; u32 len = 0; while(len < max_len && a[len] == c[len]) ++len; if(len > best_len) { best_len = len; best_dist = (u32)(i - (u64)cand); if(len == max_len) break; } s32 next = prev[cand % WINDOW]; if(next >= cand) break; cand = next; } prev[i % WINDOW] = head[h]; head[h] = (s32)i; } if(best_len >= 3) { png_put_match(&b, best_len, best_dist); // hash the skipped positions too (keeps long runs findable) for(u64 k = i + 1; k < i + best_len && k + 3 <= size; ++k) { u32 h = ((u32)in[k] << 10 ^ (u32)in[k + 1] << 5 ^ (u32)in[k + 2]) & ((1u << HASH_BITS) - 1); prev[k % WINDOW] = head[h]; head[h] = (s32)k; } i += best_len; } else { png_put_literal(&b, in[i]); ++i; } } png_put_literal(&b, 256); png_bits_flush(&b); free(head); free(prev); u32 s1 = 1, s2 = 0; for(u64 k = 0; k < size; ++k) { s1 = (s1 + in[k]) % 65521u; s2 = (s2 + s1) % 65521u; } u32 adler = (s2 << 16) | s1; if(b.size + 4 <= cap) { out[b.size++] = (u8)(adler >> 24); out[b.size++] = (u8)(adler >> 16); out[b.size++] = (u8)(adler >> 8); out[b.size++] = (u8)adler; } return (b.size <= cap) ? b.size : 0; } internal u32 png_crc(u32 crc, const u8 *data, u64 size) { global_variable u32 table[256]; global_variable b32 ready = false; if(!ready) { for(u32 n = 0; n < 256; ++n) { u32 c = n; for(u32 k = 0; k < 8; ++k) c = (c & 1) ? 0xEDB88320u ^ (c >> 1) : c >> 1; table[n] = c; } ready = true; } crc = ~crc; for(u64 i = 0; i < size; ++i) crc = table[(crc ^ data[i]) & 0xFF] ^ (crc >> 8); return ~crc; } internal void png_chunk(FILE *f, const char *type, const u8 *data, u32 size) { u8 len[4] = {(u8)(size >> 24), (u8)(size >> 16), (u8)(size >> 8), (u8)size}; fwrite(len, 1, 4, f); fwrite(type, 1, 4, f); if(size) fwrite(data, 1, size, f); u32 crc = png_crc(0, (const u8 *)type, 4); crc = png_crc(crc, data, size); u8 c[4] = {(u8)(crc >> 24), (u8)(crc >> 16), (u8)(crc >> 8), (u8)crc}; fwrite(c, 1, 4, f); } // NOTE: rgb is 3 bytes per pixel, top row first. ppi goes into pHYs so viewers show the size right internal b32 png_write_rgb(const char *path, const u8 *rgb, u32 width, u32 height, u32 ppi) { if(!width || !height) return false; u64 stride = (u64)width * 3; u64 raw_size = (stride + 1) * height; u8 *raw = (u8 *)malloc(raw_size); u8 *zout = (u8 *)malloc(raw_size + raw_size / 8 + 1024); if(!raw || !zout) { free(raw); free(zout); return false; } // per row: the filter with the smallest sum of absolute values (the usual heuristic) u8 *cand = (u8 *)malloc(stride * 5); for(u32 y = 0; y < height; ++y) { const u8 *row = rgb + (u64)y * stride; const u8 *up = y ? rgb + (u64)(y - 1) * stride : NULL; u64 best_sum = ~0ull; u32 best = 0; for(u32 ft = 0; ft < 5; ++ft) { u8 *o = cand + ft * stride; u64 sum = 0; for(u64 x = 0; x < stride; ++x) { s32 a = (x >= 3) ? row[x - 3] : 0; s32 b = up ? up[x] : 0; s32 c = (up && x >= 3) ? up[x - 3] : 0; s32 pred = 0; if(ft == 1) pred = a; else if(ft == 2) pred = b; else if(ft == 3) pred = (a + b) / 2; else if(ft == 4) { s32 p = a + b - c, pa = abs(p - a), pb = abs(p - b), pc = abs(p - c); pred = (pa <= pb && pa <= pc) ? a : (pb <= pc ? b : c); } u8 v = (u8)(row[x] - pred); o[x] = v; sum += (v < 128) ? v : 256 - v; } if(sum < best_sum) { best_sum = sum; best = ft; } } u8 *dst = raw + (u64)y * (stride + 1); dst[0] = (u8)best; MemCpy(dst + 1, cand + best * stride, stride); } free(cand); u64 zsize = png_deflate(raw, raw_size, zout, raw_size + raw_size / 8 + 1024); free(raw); if(!zsize) { free(zout); return false; } FILE *f = fopen(path, "wb"); if(!f) { free(zout); return false; } static const u8 sig[8] = {137, 80, 78, 71, 13, 10, 26, 10}; fwrite(sig, 1, 8, f); u8 ihdr[13] = {(u8)(width >> 24), (u8)(width >> 16), (u8)(width >> 8), (u8)width, (u8)(height >> 24), (u8)(height >> 16), (u8)(height >> 8), (u8)height, 8, 2, 0, 0, 0}; png_chunk(f, "IHDR", ihdr, 13); if(ppi) { u32 ppm = (u32)((f64)ppi / 0.0254 + 0.5); u8 phys[9] = {(u8)(ppm >> 24), (u8)(ppm >> 16), (u8)(ppm >> 8), (u8)ppm, (u8)(ppm >> 24), (u8)(ppm >> 16), (u8)(ppm >> 8), (u8)ppm, 1}; png_chunk(f, "pHYs", phys, 9); } // NOTE: IDAT chunks of at most 1 MiB, some readers dislike huge chunks for(u64 off = 0; off < zsize; off += MiB(1)) { u32 n = (u32)Min((u64)MiB(1), zsize - off); png_chunk(f, "IDAT", zout + off, n); } png_chunk(f, "IEND", NULL, 0); b32 ok = ferror(f) == 0; fclose(f); free(zout); return ok; }