okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e4309aaf3
main
1// src/image/png/png_write_main.cpp - test program strata-png-write (not shipped) for include/strata/image/png_write.hpp. 2// 3// strata-png-write --vectors CRC-32 / Adler-32 against published check values 4// strata-png-write --pattern W H C OUT.png [PARAMETERS] a fixed test pattern (byte = (7x + 13y + 29c) mod 256) 5// strata-png-write --raw IN W H C OUT.png [PARAMETERS] IN: W*H*C bytes, row-major 6#include "strata/image/png_write.hpp" 7 8#include <cstdio> 9#include <cstdlib> 10#include <cstring> 11#include <fstream> 12#include <string> 13#include <vector> 14 15using namespace strata::image ; 16 17int main (int argc ,char ** argv ) { 18if (argc >=2 && std::strcmp (argv [1 ],"--vectors" )== 0 ) { 19struct V {const char * s ;uint32_t crc ,adler ; }; 20// CRC-32/ISO-HDLC check value (the CRC catalogue), Adler-32 of "Wikipedia" (RFC 1950's algorithm), empty input 21const V vs []= {{"123456789" ,0xCBF43926u ,0x091E01DEu }, {"Wikipedia" ,0xADAAC02Eu ,0x11E60398u }, {"" ,0u ,1u }, 22 {"The quick brown fox jumps over the lazy dog" ,0x414FA339u ,0x5BDC0FDAu }}; 23int bad = 0 ; 24for (const V & v :vs ) { 25const uint32_t c = png_crc32 ((const uint8_t * )v .s , std::strlen (v .s )); 26const uint32_t a = png_adler32 ((const uint8_t * )v .s , std::strlen (v .s )); 27 std::printf ("\"%s\": crc32 %08x (%s), adler32 %08x (%s)\n" ,v .s ,c ,c == v .crc ?"ok" :"WRONG" ,a , 28a == v .adler ?"ok" :"WRONG" ); 29bad += c != v .crc || a != v .adler ; 30 } 31// continuing over two parts gives the whole 32const char * s = "123456789" ; 33const uint32_t c2 = png_crc32 ((const uint8_t * )s + 4 ,5 ,png_crc32 ((const uint8_t * )s ,4 )); 34const uint32_t a2 = png_adler32 ((const uint8_t * )s + 4 ,5 ,png_adler32 ((const uint8_t * )s ,4 )); 35 std::printf ("split: crc32 %s, adler32 %s\n" ,c2 == 0xCBF43926u ?"ok" :"WRONG" ,a2 == 0x091E01DEu ?"ok" :"WRONG" ); 36bad += c2 != 0xCBF43926u || a2 != 0x091E01DEu ; 37// Adler-32 over 1 MB of 0xFF (the sums' modulo path) 38 std::vector < uint8_t > ff (1 <<20 ,0xFF ); 39 std::printf ("adler32 of 1 MiB of 0xff: %08x\n" ,png_adler32 (ff .data (),ff .size ())); 40 std::printf ("VECTORS %s\n" ,bad ?"FAIL" :"PASS" ); 41return bad ?1 :0 ; 42 } 43if (argc >=6 && std::strcmp (argv [1 ],"--pattern" )== 0 ) { 44const int w = std::atoi (argv [2 ]),h = std::atoi (argv [3 ]),c = std::atoi (argv [4 ]); 45 std::vector < uint8_t > px ((size_t )w * h * c ); 46for (int y = 0 ;y < h ;++ y ) 47for (int x = 0 ;x < w ;++ x ) 48for (int k = 0 ;k < c ;++ k )px [((size_t )y * w + x )* c + k ]= (uint8_t ) ((7 * x + 13 * y + 29 * k )& 255 ); 49const std::string e = png_write_file (argv [5 ],px .data (),w ,h ,c ,argc > 6 ?argv [6 ] :"" ); 50if (!e .empty ()) std::fprintf (stderr ,"%s\n" ,e .c_str ()); 51return e .empty () ?0 :1 ; 52 } 53if (argc >=7 && std::strcmp (argv [1 ],"--raw" )== 0 ) { 54const int w = std::atoi (argv [3 ]),h = std::atoi (argv [4 ]),c = std::atoi (argv [5 ]); 55 std::ifstream f (argv [2 ], std::ios::binary ); 56 std::vector < uint8_t > px ((std::istreambuf_iterator < char > (f )), {}); 57if (px .size ()!= (size_t )w * h * c ) { 58 std::fprintf (stderr ,"raw size %zu != %d x %d x %d\n" ,px .size (),w ,h ,c ); 59return 1 ; 60 } 61const std::string e = png_write_file (argv [6 ],px .data (),w ,h ,c ,argc > 7 ?argv [7 ] :"" ); 62if (!e .empty ()) std::fprintf (stderr ,"%s\n" ,e .c_str ()); 63return e .empty () ?0 :1 ; 64 } 65 std::fprintf (stderr ,"usage: strata-png-write --vectors | --pattern W H C OUT [PARAMS] | --raw IN W H C OUT [PARAMS]\n" ); 66return 2 ; 67}