okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e4309aaf3
main
1// src/image/png/png_write.cpp - see include/strata/image/png_write.hpp. 2#include "strata/image/png_write.hpp" 3 4#include <algorithm> 5#include <array> 6#include <cstddef> 7#include <cstdio> 8#include <cstring> 9 10namespace strata::image { 11namespace { 12 13const std::array < uint32_t ,256 >& crc_table () { 14static const std::array < uint32_t ,256 > t = [] { 15 std::array < uint32_t ,256 > r {}; 16for (uint32_t i = 0 ;i < 256 ;++ i ) { 17uint32_t c = i ; 18for (int k = 0 ;k < 8 ;++ k )c = (c & 1u ) ?0xEDB88320u ^ (c >>1 ) :c >>1 ; 19r [i ]= c ; 20 } 21return r ; 22 }(); 23return t ; 24} 25 26void put32 (std::vector < uint8_t >& o ,uint32_t v ) { 27o .push_back ((uint8_t ) (v >>24 )); 28o .push_back ((uint8_t ) (v >>16 )); 29o .push_back ((uint8_t ) (v >>8 )); 30o .push_back ((uint8_t )v ); 31} 32 33// one chunk: length, type, data, CRC over type + data 34void chunk (std::vector < uint8_t >& o ,const char type [4 ],const uint8_t * data ,size_t n ) { 35put32 (o , (uint32_t )n ); 36const size_t at = o .size (); 37o .insert (o .end (),type ,type + 4 ); 38if (n )o .insert (o .end (),data ,data + n ); 39put32 (o ,png_crc32 (o .data ()+ at ,n + 4 )); 40} 41 42}// namespace 43 44uint32_t png_crc32 (const uint8_t * p ,size_t n ,uint32_t crc ) { 45const auto & t = crc_table (); 46uint32_t c = crc ^0xFFFFFFFFu ; 47for (size_t i = 0 ;i < n ;++ i )c = t [(c ^p [i ])& 0xFFu ] ^ (c >>8 ); 48return c ^0xFFFFFFFFu ; 49} 50 51uint32_t png_adler32 (const uint8_t * p ,size_t n ,uint32_t a ) { 52uint32_t s1 = a & 0xFFFFu ,s2 = a >>16 ; 53while (n > 0 ) { 54const size_t k = n < 5552 ?n :5552 ;// the largest run before the sums can overflow 32 bits 55for (size_t i = 0 ;i < k ;++ i ) { 56s1 += p [i ]; 57s2 += s1 ; 58 } 59s1 %=65521u ; 60s2 %=65521u ; 61p += k ; 62n -= k ; 63 } 64return (s2 <<16 ) |s1 ; 65} 66 67std::vector < uint8_t > png_encode (const uint8_t * pixels ,int w ,int h ,int channels ,const std::string & parameters ) { 68if (w <=0 || h <=0 || (channels != 1 && channels != 3 && channels != 4 ))return {}; 69// the raw stream: per row the filter byte 0, then the row's bytes 70const size_t row = (size_t )w * channels ; 71 std::vector < uint8_t > raw ; 72raw .reserve ((row + 1 )* h ); 73for (int y = 0 ;y < h ;++ y ) { 74raw .push_back (0 ); 75raw .insert (raw .end (),pixels + (size_t )y * row ,pixels + (size_t ) (y + 1 )* row ); 76 } 77// zlib: CMF 0x78 (deflate, 32 KB window), FLG 0x01 (fastest; 0x7801 % 31 == 0), stored blocks of <= 65535 bytes, 78// Adler-32 of the raw stream, big-endian 79 std::vector < uint8_t > z {0x78 ,0x01 }; 80for (size_t at = 0 ;;) {// raw holds at least one row 81const size_t len = std::min < size_t > (65535 ,raw .size ()- at ); 82const bool last = at + len == raw .size (); 83z .push_back (last ?1 :0 );// BFINAL, BTYPE 00 (stored); the rest of the byte is padding 84z .push_back ((uint8_t )len ); 85z .push_back ((uint8_t ) (len >>8 )); 86z .push_back ((uint8_t ) ~len ); 87z .push_back ((uint8_t ) (~len >>8 )); 88z .insert (z .end (),raw .begin ()+ (ptrdiff_t )at ,raw .begin ()+ (ptrdiff_t ) (at + len )); 89at += len ; 90if (last )break ; 91 } 92const uint32_t ad = png_adler32 (raw .data (),raw .size ()); 93put32 (z ,ad ); 94 95static const uint8_t sig [8 ]= {0x89 ,'P' ,'N' ,'G' ,'\r' ,'\n' ,0x1A ,'\n' }; 96static const uint8_t ctype [5 ]= {0 ,0 ,4 ,2 ,6 };// by channel count: grey, grey+alpha, RGB, RGBA 97 std::vector < uint8_t > o (sig ,sig + 8 ); 98uint8_t ihdr [13 ]; 99for (int i = 0 ;i < 4 ;++ i ) { 100ihdr [i ]= (uint8_t ) ((uint32_t )w >> (24 - 8 * i )); 101ihdr [4 + i ]= (uint8_t ) ((uint32_t )h >> (24 - 8 * i )); 102 } 103ihdr [8 ]= 8 ;// bit depth 104ihdr [9 ]= ctype [channels ]; 105ihdr [10 ]= ihdr [11 ]= ihdr [12 ]= 0 ;// deflate, adaptive filtering (type 0 per row), no interlace 106chunk (o ,"IHDR" ,ihdr ,13 ); 107if (!parameters .empty ()) {// as stb_image_write in sd.cpp: keyword "parameters", a NUL, the text 108 std::vector < uint8_t > t ((const uint8_t * )"parameters" , (const uint8_t * )"parameters" + 10 ); 109t .push_back (0 ); 110t .insert (t .end (),parameters .begin (),parameters .end ()); 111chunk (o ,"tEXt" ,t .data (),t .size ()); 112 } 113chunk (o ,"IDAT" ,z .data (),z .size ()); 114chunk (o ,"IEND" ,nullptr ,0 ); 115return o ; 116} 117 118std::string png_write_file (const std::string & path ,const uint8_t * pixels ,int w ,int h ,int channels , 119const std::string & parameters ) { 120const std::vector < uint8_t > png = png_encode (pixels ,w ,h ,channels ,parameters ); 121if (png .empty ())return "png_write: bad image shape" ; 122FILE * f = std::fopen (path .c_str (),"wb" ); 123if (!f )return "png_write: cannot create " + path ; 124const bool ok = std::fwrite (png .data (),1 ,png .size (),f )== png .size (); 125return std::fclose (f )== 0 && ok ? std::string () :"png_write: cannot write " + path ; 126} 127 128}// namespace strata::image