Geant4 Cross Reference |
1 1 2 // G.Barrand: pure header version of fpng foun 3 4 // fpng.cpp 1.0.6 - Fast 24/32bpp .PNG image w 5 // PNG's generated by this code have been test 6 // 7 // Uses code from the simple PNG writer functi 8 // Some low-level Deflate/Huffman functions de 9 // Low-level Huffman code size function: publi 10 // 11 12 #include <assert.h> 13 #include <string.h> 14 #include <stdio.h> 15 16 namespace tools { 17 namespace fpng 18 { 19 static const int FPNG_FALSE = 0; 20 static const uint8_t FPNG_FDEC_VERSION = 0; 21 static const uint32_t FPNG_MAX_SUPPORTED_DIM 22 23 template <typename S> static inline S maximu 24 template <typename S> static inline S minimu 25 26 // A good compiler should be able to optimiz 27 static inline uint32_t READ_LE32(const void* 28 { 29 const uint8_t* pBytes = (const uint8_t*)p; 30 return ((uint32_t)pBytes[0]) | (((uint32_t 31 } 32 33 static inline uint32_t READ_BE32(const void* 34 { 35 const uint8_t* pBytes = (const uint8_t*)p; 36 return ((uint32_t)pBytes[3]) | (((uint32_t 37 } 38 39 static inline void WRITE_LE32(const void* p, 40 { 41 uint8_t* pBytes = (uint8_t*)p; 42 pBytes[0] = (uint8_t)(v); 43 pBytes[1] = (uint8_t)(v >> 8); 44 pBytes[2] = (uint8_t)(v >> 16); 45 pBytes[3] = (uint8_t)(v >> 24); 46 } 47 48 static inline void WRITE_LE64(const void* p, 49 { 50 uint8_t* pBytes = (uint8_t*)p; 51 pBytes[0] = (uint8_t)(v); 52 pBytes[1] = (uint8_t)(v >> 8); 53 pBytes[2] = (uint8_t)(v >> 16); 54 pBytes[3] = (uint8_t)(v >> 24); 55 pBytes[4] = (uint8_t)(v >> 32); 56 pBytes[5] = (uint8_t)(v >> 40); 57 pBytes[6] = (uint8_t)(v >> 48); 58 pBytes[7] = (uint8_t)(v >> 56); 59 } 60 61 // Customized the very common case of readin 62 static inline uint32_t READ_RGB_PIXEL(const 63 { 64 const uint8_t* pBytes = (const uint8_t*)p; 65 return ((uint32_t)pBytes[0]) | (((uint32_t 66 } 67 68 // See "Slicing by 4" CRC-32 algorithm here: 69 // https://create.stephan-brumme.com/crc32/ 70 71 // Precomputed 4KB of CRC-32 tables 72 static const uint32_t g_crc32_4[4][256] = { 73 {00, 016701630226, 035603460454, 02310225067 74 07333420310, 011432210136, 032530040744, 024 75 016667040620, 0166670406, 023064420274, 0357 76 011554460530, 07255250716, 024357000164, 032 77 035556101440, 023257731666, 0355561014, 0164 78 032665521750, 024164311576, 07066141304, 011 79 023331141260, 035430771046, 016532521634, 02 80 024002561170, 032703351356, 011601101524, 07 81 }, { 00,03106630501,06215461202,05313251703, 82 022541022242,021447612743,024754443040,02765 83 036036247405,035130477104,030223626607,03332 84 014577265647,017471455346,012762604445,01166 85 07340714113,04246124412,01155375311,02053545 86 025601736351,026707106650,023414357153,02051 87 031376553516,032270363017,037163132714,03406 88 013637571754,010731341255,015422110556,01652 89 }, { 00,0160465067,0341152156,0221537131,070 90 07023243340,07143626327,07362311216,07202774 91 016046506700,016126163767,016307454656,01626 92 011065745440,011105320427,011324617516,01124 93 034115215600,034075670667,034254347756,03433 94 033136056540,033056433527,033277104416,03331 95 022153713100,022033376167,022212641056,02237 96 025170550240,025010135227,025231402316,02535 97 }, { 00,027057063545,025202344213,0225532775 98 024014527476,03043544133,01216663665,0262416 99 023305054075,04352037530,06107310266,0211503 100 07311573403,020346510146,022113637610,051446 101 035526333073,012571350536,010724077260,03777 102 011532614405,036565677140,034730550616,01376 103 016623367006,031674304543,033421023215,01447 104 032637640470,015660623135,017435504663,03046 105 } }; 106 107 static uint32_t crc32_slice_by_4(const void* 108 { 109 uint32_t crc = ~cur_crc32; 110 const uint32_t* pData32 = static_cast<cons 111 112 for (; data_len >= sizeof(uint32_t); ++pDa 113 { 114 uint32_t v = READ_LE32(pData32) ^ crc; 115 crc = g_crc32_4[0][v >> 24] ^ g_crc32_4[ 116 } 117 118 for (const uint8_t* pData8 = reinterpret_c 119 crc = (crc >> 8) ^ g_crc32_4[0][(crc & 0 120 121 return ~crc; 122 } 123 124 inline uint32_t fpng_crc32(const void* pData 125 { 126 return crc32_slice_by_4(pData, size, prev_ 127 } 128 129 static uint32_t fpng_adler32_scalar(const ui 130 { 131 uint32_t i, s1 = (uint32_t)(adler & 0xffff 132 if (!ptr) return FPNG_ADLER32_INIT; 133 while (buf_len) { 134 for (i = 0; i + 7 < block_len; i += 8, p 135 s1 += ptr[0], s2 += s1; s1 += ptr[1], 136 s1 += ptr[4], s2 += s1; s1 += ptr[5], 137 } 138 for (; i < block_len; ++i) s1 += *ptr++, 139 s1 %= 65521U, s2 %= 65521U; buf_len -= b 140 } 141 return (s2 << 16) + s1; 142 } 143 144 inline uint32_t fpng_adler32(const void* pDa 145 { 146 return fpng_adler32_scalar((const uint8_t* 147 } 148 149 // Ensure we've been configured for endianne 150 static inline bool endian_check() 151 { 152 uint32_t endian_check = 0; 153 WRITE_LE32(&endian_check, 0x1234ABCD); 154 const uint32_t first_byte = reinterpret_ca 155 return first_byte == 0xCD; 156 } 157 158 static const uint16_t g_defl_len_sym[256] = 159 257,258,259,260,261,262,263,264,265,265,26 160 273,273,273,273,273,273,273,273,274,274,27 161 277,277,277,277,277,277,277,277,277,277,27 162 279,279,279,279,279,279,279,279,279,279,27 163 281,281,281,281,281,281,281,281,281,281,28 164 282,282,282,282,282,282,282,282,282,282,28 165 283,283,283,283,283,283,283,283,283,283,28 166 284,284,284,284,284,284,284,284,284,284,28 167 168 static const uint8_t g_defl_len_extra[256] = 169 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2, 170 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, 171 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 172 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 173 174 static const uint8_t g_defl_small_dist_sym[5 175 0,1,2,3,4,4,5,5,6,6,6,6,7,7,7,7,8,8,8,8,8, 176 11,11,11,11,11,11,11,11,11,11,12,12,12,12, 177 13,13,13,13,13,13,13,13,13,13,13,13,13,13, 178 14,14,14,14,14,14,14,14,14,14,14,14,14,14, 179 14,14,14,14,14,14,14,14,14,15,15,15,15,15, 180 15,15,15,15,15,15,15,15,15,15,15,15,15,15, 181 16,16,16,16,16,16,16,16,16,16,16,16,16,16, 182 16,16,16,16,16,16,16,16,16,16,16,16,16,16, 183 16,16,16,16,16,16,16,16,16,16,16,16,16,16, 184 17,17,17,17,17,17,17,17,17,17,17,17,17,17, 185 17,17,17,17,17,17,17,17,17,17,17,17,17,17, 186 17,17,17,17,17,17,17,17,17,17,17,17,17,17, 187 188 static const uint32_t g_bitmasks[17] = { 0x0 189 190 // Huffman tables generated by fpng_test -t 191 // Feel free to retrain the encoder on your 192 static const uint8_t g_dyn_huff_3[] = { 193 120, 1, 237, 195, 3, 176, 110, 89, 122, 128, 194 109, 219, 201, 36, 147, 153, 105, 235, 246, 195 const uint32_t DYN_HUFF_3_BITBUF = 30, DYN_H 196 static const struct { uint8_t m_code_size; u 197 {2,0},{4,2},{4,10},{5,14},{5,30},{6,25},{6,5 198 {9,359},{10,695},{10,439},{10,951},{10,119}, 199 {12,2159},{12,1135},{12,3183},{12,623},{12,2 200 {12,2079},{12,1055},{12,3103},{12,543},{12,2 201 {12,2143},{12,1119},{12,3167},{12,607},{12,2 202 {12,2111},{12,1087},{12,3135},{12,575},{12,2 203 {12,2175},{12,1151},{12,3199},{12,639},{12,2 204 {9,231},{10,335},{9,487},{9,23},{9,279},{9,1 205 {12,2303},{6,29},{0,0},{0,0},{8,251},{0,0},{ 206 }; 207 208 static const uint8_t g_dyn_huff_4[] = { 209 120, 1, 229, 196, 99, 180, 37, 103, 218, 128 210 17, 140, 98, 219, 102, 219, 60, 125, 172, 20 211 const uint32_t DYN_HUFF_4_BITBUF = 1, DYN_HU 212 static const struct { uint8_t m_code_size; u 213 {2,0},{4,2},{5,6},{6,30},{6,62},{6,1},{7,41} 214 {9,307},{9,179},{9,435},{9,115},{9,371},{9,2 215 {10,967},{10,39},{10,551},{10,295},{10,807}, 216 {11,431},{11,1455},{11,943},{11,1967},{11,11 217 {11,415},{11,1439},{11,927},{11,1951},{11,95 218 {11,447},{11,1471},{11,959},{11,1983},{11,12 219 {10,567},{10,311},{10,823},{10,183},{10,695} 220 {9,91},{9,347},{9,219},{9,475},{9,59},{9,315 221 {12,2047},{0,0},{6,9},{0,0},{0,0},{0,0},{8,1 222 }; 223 224 #define TOOLS_FPNG_PUT_BITS(bb, ll) do { uint3 225 #define TOOLS_FPNG_PUT_BITS_CZ(bb, ll) do { ui 226 227 #define TOOLS_FPNG_PUT_BITS_FLUSH do { \ 228 if ((dst_ofs + 8) > dst_buf_size) \ 229 return 0; \ 230 WRITE_LE64(pDst + dst_ofs, bit_buf); \ 231 uint32_t bits_to_shift = bit_buf_size & ~7; 232 dst_ofs += (bits_to_shift >> 3); \ 233 assert(bits_to_shift < 64); \ 234 bit_buf = bit_buf >> bits_to_shift; \ 235 bit_buf_size -= bits_to_shift; \ 236 } while(0) 237 238 #define TOOLS_FPNG_PUT_BITS_FORCE_FLUSH do { \ 239 while (bit_buf_size > 0) \ 240 { \ 241 if ((dst_ofs + 1) > dst_buf_size) \ 242 return 0; \ 243 *(uint8_t*)(pDst + dst_ofs) = (uint8_t)bit 244 dst_ofs++; \ 245 bit_buf >>= 8; \ 246 bit_buf_size -= 8; \ 247 } \ 248 } while(0) 249 250 enum 251 { 252 DEFL_MAX_HUFF_TABLES = 3, 253 DEFL_MAX_HUFF_SYMBOLS = 288, 254 DEFL_MAX_HUFF_SYMBOLS_0 = 288, 255 DEFL_MAX_HUFF_SYMBOLS_1 = 32, 256 DEFL_MAX_HUFF_SYMBOLS_2 = 19, 257 DEFL_LZ_DICT_SIZE = 32768, 258 DEFL_LZ_DICT_SIZE_MASK = DEFL_LZ_DICT_SIZE 259 DEFL_MIN_MATCH_LEN = 3, 260 DEFL_MAX_MATCH_LEN = 258 261 }; 262 263 struct defl_huff 264 { 265 uint16_t m_huff_count[DEFL_MAX_HUFF_TABLES 266 uint16_t m_huff_codes[DEFL_MAX_HUFF_TABLES 267 uint8_t m_huff_code_sizes[DEFL_MAX_HUFF_TA 268 }; 269 270 struct defl_sym_freq 271 { 272 uint16_t m_key; 273 uint16_t m_sym_index; 274 }; 275 276 #define TOOLS_FPNG_DEFL_CLEAR_OBJ(obj) memset( 277 278 static defl_sym_freq* defl_radix_sort_syms(u 279 { 280 uint32_t total_passes = 2, pass_shift, pas 281 for (i = 0; i < num_syms; i++) { uint32_t 282 while ((total_passes > 1) && (num_syms == 283 for (pass_shift = 0, pass = 0; pass < tota 284 { 285 const uint32_t* pHist = &hist[pass << 8] 286 uint32_t offsets[256], cur_ofs = 0; 287 for (i = 0; i < 256; i++) { offsets[i] = 288 for (i = 0; i < num_syms; i++) pNew_syms 289 { defl_sym_freq* t = pCur_syms; pCur_sym 290 } 291 return pCur_syms; 292 } 293 294 // defl_calculate_minimum_redundancy() origi 295 static void defl_calculate_minimum_redundanc 296 { 297 int root, leaf, next, avbl, used, dpth; 298 if (n == 0) return; else if (n == 1) { A[0 299 A[0].m_key += A[1].m_key; root = 0; leaf = 300 for (next = 1; next < n - 1; next++) 301 { 302 if (leaf >= n || A[root].m_key < A[leaf] 303 else A[next].m_key = A[leaf++].m_key; 304 if (leaf >= n || (root < next && A[root] 305 else A[next].m_key = (uint16_t)(A[next]. 306 } 307 A[n - 2].m_key = 0; for (next = n - 3; nex 308 avbl = 1; used = dpth = 0; root = n - 2; n 309 while (avbl > 0) 310 { 311 while (root >= 0 && (int)A[root].m_key = 312 while (avbl > used) { A[next--].m_key = 313 avbl = 2 * used; dpth++; used = 0; 314 } 315 } 316 317 // Limits canonical Huffman code table's max 318 enum { DEFL_MAX_SUPPORTED_HUFF_CODESIZE = 32 319 static void defl_huffman_enforce_max_code_si 320 { 321 int i; uint32_t total = 0; if (code_list_l 322 for (i = max_code_size + 1; i <= DEFL_MAX_ 323 for (i = max_code_size; i > 0; i--) total 324 while (total != (1UL << max_code_size)) 325 { 326 pNum_codes[max_code_size]--; 327 for (i = max_code_size - 1; i > 0; i--) 328 total--; 329 } 330 } 331 332 static void defl_optimize_huffman_table(defl 333 { 334 int i, j, l, num_codes[1 + DEFL_MAX_SUPPOR 335 if (static_table) 336 { 337 for (i = 0; i < table_len; i++) num_code 338 } 339 else 340 { 341 defl_sym_freq syms0[DEFL_MAX_HUFF_SYMBOL 342 int num_used_syms = 0; 343 const uint16_t* pSym_count = &d->m_huff_ 344 for (i = 0; i < table_len; i++) if (pSym 345 346 pSyms = defl_radix_sort_syms(num_used_sy 347 348 for (i = 0; i < num_used_syms; i++) num_ 349 350 defl_huffman_enforce_max_code_size(num_c 351 352 TOOLS_FPNG_DEFL_CLEAR_OBJ(d->m_huff_code 353 for (i = 1, j = num_used_syms; i <= code 354 for (l = num_codes[i]; l > 0; l--) d-> 355 } 356 357 next_code[1] = 0; for (j = 0, i = 2; i <= 358 359 for (i = 0; i < table_len; i++) 360 { 361 uint32_t rev_code = 0, code, code_size; 362 code = next_code[code_size]++; for (l = 363 d->m_huff_codes[table_num][i] = (uint16_ 364 } 365 } 366 367 #define TOOLS_FPNG_DEFL_RLE_PREV_CODE_SIZE() { 368 if (rle_repeat_count < 3) { \ 369 d->m_huff_count[2][prev_code_size] = (uint 370 while (rle_repeat_count--) packed_code_siz 371 } else { \ 372 d->m_huff_count[2][16] = (uint16_t)(d->m_h 373 } rle_repeat_count = 0; } } 374 375 #define TOOLS_FPNG_DEFL_RLE_ZERO_CODE_SIZE() { 376 if (rle_z_count < 3) { \ 377 d->m_huff_count[2][0] = (uint16_t)(d->m_hu 378 } else if (rle_z_count <= 10) { \ 379 d->m_huff_count[2][17] = (uint16_t)(d->m_h 380 } else { \ 381 d->m_huff_count[2][18] = (uint16_t)(d->m_h 382 } rle_z_count = 0; } } 383 384 //G.Barrand: const in the below: 385 static const uint8_t g_defl_packed_code_size 386 387 #define TOOLS_FPNG_DEFL_DYN_PUT_BITS(bb, ll) \ 388 do { \ 389 uint32_t b = (bb), l = (ll); \ 390 assert((l) >= 1 && (l) <= 16); assert((b) < 391 bit_buf |= (((uint64_t)(b)) << bit_buf_size) 392 while (bit_buf_size >= 8) \ 393 { \ 394 if ((dst_ofs + 1) > dst_buf_size) \ 395 return false; \ 396 *(uint8_t*)(pDst + dst_ofs) = (uint8_t)bit 397 dst_ofs++; \ 398 bit_buf >>= 8; \ 399 bit_buf_size -= 8; \ 400 } \ 401 } while(0) 402 403 static bool defl_start_dynamic_block(defl_hu 404 { 405 int num_lit_codes, num_dist_codes, num_bit 406 uint8_t code_sizes_to_pack[DEFL_MAX_HUFF_S 407 408 d->m_huff_count[0][256] = 1; 409 410 defl_optimize_huffman_table(d, 0, DEFL_MAX 411 defl_optimize_huffman_table(d, 1, DEFL_MAX 412 413 for (num_lit_codes = 286; num_lit_codes > 414 for (num_dist_codes = 30; num_dist_codes > 415 416 memcpy(code_sizes_to_pack, &d->m_huff_code 417 memcpy(code_sizes_to_pack + num_lit_codes, 418 total_code_sizes_to_pack = num_lit_codes + 419 420 memset(&d->m_huff_count[2][0], 0, sizeof(d 421 for (i = 0; i < total_code_sizes_to_pack; 422 { 423 uint8_t code_size = code_sizes_to_pack[i 424 if (!code_size) 425 { 426 TOOLS_FPNG_DEFL_RLE_PREV_CODE_SIZE(); 427 if (++rle_z_count == 138) { TOOLS_FPNG 428 } 429 else 430 { 431 TOOLS_FPNG_DEFL_RLE_ZERO_CODE_SIZE(); 432 if (code_size != prev_code_size) 433 { 434 TOOLS_FPNG_DEFL_RLE_PREV_CODE_SIZE() 435 d->m_huff_count[2][code_size] = (uin 436 } 437 else if (++rle_repeat_count == 6) 438 { 439 TOOLS_FPNG_DEFL_RLE_PREV_CODE_SIZE() 440 } 441 } 442 prev_code_size = code_size; 443 } 444 if (rle_repeat_count) { TOOLS_FPNG_DEFL_RL 445 else { TOOLS_FPNG_DEFL_RLE_ZERO_CODE_SIZE( 446 447 defl_optimize_huffman_table(d, 2, DEFL_MAX 448 449 // max of 2+5+5+4+18*3+(288+32)*7=2310 bit 450 TOOLS_FPNG_DEFL_DYN_PUT_BITS(2, 2); 451 452 TOOLS_FPNG_DEFL_DYN_PUT_BITS(num_lit_codes 453 TOOLS_FPNG_DEFL_DYN_PUT_BITS(num_dist_code 454 455 for (num_bit_lengths = 18; num_bit_lengths 456 num_bit_lengths = maximum<int>(4, (num_bit 457 for (i = 0; (int)i < num_bit_lengths; i++) 458 459 for (packed_code_sizes_index = 0; packed_c 460 { 461 uint32_t code = packed_code_sizes[packed 462 TOOLS_FPNG_DEFL_DYN_PUT_BITS(d->m_huff_c 463 if (code >= 16) TOOLS_FPNG_DEFL_DYN_PUT_ 464 } 465 466 return true; 467 } 468 469 static uint32_t write_raw_block(const uint8_ 470 { 471 if (dst_buf_size < 2) 472 return 0; 473 474 pDst[0] = 0x78; 475 pDst[1] = 0x01; 476 477 uint32_t dst_ofs = 2; 478 479 uint32_t src_ofs = 0; 480 while (src_ofs < src_len) 481 { 482 const uint32_t src_remaining = src_len - 483 const uint32_t block_size = minimum<uint 484 const bool final_block = (block_size == 485 486 if ((dst_ofs + 5 + block_size) > dst_buf 487 return 0; 488 489 pDst[dst_ofs + 0] = final_block ? 1 : 0; 490 491 pDst[dst_ofs + 1] = block_size & 0xFF; 492 pDst[dst_ofs + 2] = (block_size >> 8) & 493 494 pDst[dst_ofs + 3] = (~block_size) & 0xFF 495 pDst[dst_ofs + 4] = ((~block_size) >> 8) 496 497 memcpy(pDst + dst_ofs + 5, pSrc + src_of 498 499 src_ofs += block_size; 500 dst_ofs += 5 + block_size; 501 } 502 503 uint32_t src_adler32 = fpng_adler32(pSrc, 504 505 for (uint32_t i = 0; i < 4; i++) 506 { 507 if (dst_ofs + 1 > dst_buf_size) 508 return 0; 509 510 pDst[dst_ofs] = (uint8_t)(src_adler32 >> 511 dst_ofs++; 512 513 src_adler32 <<= 8; 514 } 515 516 return dst_ofs; 517 } 518 519 static void adjust_freq32(uint32_t num_freq, 520 { 521 uint32_t total_freq = 0; 522 for (uint32_t i = 0; i < num_freq; i++) 523 total_freq += pFreq[i]; 524 525 if (!total_freq) 526 { 527 memset(pFreq16, 0, num_freq * sizeof(uin 528 return; 529 } 530 531 uint32_t total_freq16 = 0; 532 for (uint32_t i = 0; i < num_freq; i++) 533 { 534 uint64_t f = pFreq[i]; 535 if (!f) 536 { 537 pFreq16[i] = 0; 538 continue; 539 } 540 541 pFreq16[i] = (uint16_t)maximum<uint32_t> 542 543 total_freq16 += pFreq16[i]; 544 } 545 546 while (total_freq16 > UINT16_MAX) 547 { 548 total_freq16 = 0; 549 for (uint32_t i = 0; i < num_freq; i++) 550 { 551 if (pFreq[i]) 552 { 553 pFreq[i] = maximum<uint32_t>(1, pFre 554 total_freq16 += pFreq[i]; 555 } 556 } 557 } 558 } 559 560 static uint32_t pixel_deflate_dyn_3_rle( 561 const uint8_t* pImg, uint32_t w, uint32_t 562 uint8_t* pDst, uint32_t dst_buf_size) 563 { 564 const uint32_t bpl = 1 + w * 3; 565 566 uint64_t bit_buf = 0; 567 int bit_buf_size = 0; 568 569 uint32_t dst_ofs = 0; 570 571 // zlib header 572 TOOLS_FPNG_PUT_BITS(0x78, 8); 573 TOOLS_FPNG_PUT_BITS(0x01, 8); 574 575 // write BFINAL bit 576 TOOLS_FPNG_PUT_BITS(1, 1); 577 578 std::vector<uint32_t> codes((w + 1) * h); 579 uint32_t* pDst_codes = codes.data(); 580 581 uint32_t lit_freq[DEFL_MAX_HUFF_SYMBOLS_0] 582 memset(lit_freq, 0, sizeof(lit_freq)); 583 584 const uint8_t* pSrc = pImg; 585 uint32_t src_ofs = 0; 586 587 uint32_t src_adler32 = fpng_adler32(pImg, 588 589 const uint32_t dist_sym = g_defl_small_dis 590 591 for (uint32_t y = 0; y < h; y++) 592 { 593 const uint32_t end_src_ofs = src_ofs + b 594 595 const uint32_t filter_lit = pSrc[src_ofs 596 *pDst_codes++ = 1 | (filter_lit << 8); 597 lit_freq[filter_lit]++; 598 599 uint32_t prev_lits; 600 601 { 602 uint32_t lits = READ_RGB_PIXEL(pSrc + 603 604 *pDst_codes++ = lits << 8; 605 606 lit_freq[lits & 0xFF]++; 607 lit_freq[(lits >> 8) & 0xFF]++; 608 lit_freq[lits >> 16]++; 609 610 src_ofs += 3; 611 612 prev_lits = lits; 613 } 614 615 while (src_ofs < end_src_ofs) 616 { 617 uint32_t lits = READ_RGB_PIXEL(pSrc + 618 619 if (lits == prev_lits) 620 { 621 uint32_t match_len = 3; 622 uint32_t max_match_len = minimum<int 623 624 while (match_len < max_match_len) 625 { 626 if (READ_RGB_PIXEL(pSrc + src_ofs 627 break; 628 match_len += 3; 629 } 630 631 *pDst_codes++ = match_len - 1; 632 633 uint32_t adj_match_len = match_len - 634 635 lit_freq[g_defl_len_sym[adj_match_le 636 637 src_ofs += match_len; 638 } 639 else 640 { 641 *pDst_codes++ = lits << 8; 642 643 lit_freq[lits & 0xFF]++; 644 lit_freq[(lits >> 8) & 0xFF]++; 645 lit_freq[lits >> 16]++; 646 647 prev_lits = lits; 648 649 src_ofs += 3; 650 } 651 652 } // while (src_ofs < end_src_ofs) 653 654 } // y 655 656 assert(src_ofs == h * bpl); 657 const uint32_t total_codes = (uint32_t)(pD 658 assert(total_codes <= codes.size()); 659 660 defl_huff dh; 661 662 lit_freq[256] = 1; 663 664 adjust_freq32(DEFL_MAX_HUFF_SYMBOLS_0, lit 665 666 memset(&dh.m_huff_count[1][0], 0, sizeof(d 667 dh.m_huff_count[1][dist_sym] = 1; 668 dh.m_huff_count[1][dist_sym + 1] = 1; // t 669 670 if (!defl_start_dynamic_block(&dh, pDst, d 671 return 0; 672 673 assert(bit_buf_size <= 7); 674 assert(dh.m_huff_codes[1][dist_sym] == 0 & 675 676 for (uint32_t i = 0; i < total_codes; i++) 677 { 678 uint32_t c = codes[i]; 679 680 uint32_t c_type = c & 0xFF; 681 if (c_type == 0) 682 { 683 uint32_t lits = c >> 8; 684 685 TOOLS_FPNG_PUT_BITS_CZ(dh.m_huff_codes 686 lits >>= 8; 687 688 TOOLS_FPNG_PUT_BITS_CZ(dh.m_huff_codes 689 lits >>= 8; 690 691 TOOLS_FPNG_PUT_BITS_CZ(dh.m_huff_codes 692 } 693 else if (c_type == 1) 694 { 695 uint32_t lit = c >> 8; 696 TOOLS_FPNG_PUT_BITS_CZ(dh.m_huff_codes 697 } 698 else 699 { 700 uint32_t match_len = c_type + 1; 701 702 uint32_t adj_match_len = match_len - 3 703 704 TOOLS_FPNG_PUT_BITS_CZ(dh.m_huff_codes 705 TOOLS_FPNG_PUT_BITS(adj_match_len & g_ 706 707 // no need to write the distance code, 708 //TOOLS_FPNG_PUT_BITS_CZ(dh.m_huff_cod 709 } 710 711 // up to 55 bits 712 TOOLS_FPNG_PUT_BITS_FLUSH; 713 } 714 715 TOOLS_FPNG_PUT_BITS_CZ(dh.m_huff_codes[0][ 716 717 TOOLS_FPNG_PUT_BITS_FORCE_FLUSH; 718 719 // Write zlib adler32 720 for (uint32_t i = 0; i < 4; i++) 721 { 722 if ((dst_ofs + 1) > dst_buf_size) 723 return 0; 724 *(uint8_t*)(pDst + dst_ofs) = (uint8_t)( 725 dst_ofs++; 726 727 src_adler32 <<= 8; 728 } 729 730 return dst_ofs; 731 } 732 733 static uint32_t pixel_deflate_dyn_3_rle_one_ 734 const uint8_t* pImg, uint32_t w, uint32_t 735 uint8_t* pDst, uint32_t dst_buf_size) 736 { 737 const uint32_t bpl = 1 + w * 3; 738 739 if (dst_buf_size < sizeof(g_dyn_huff_3)) 740 return false; 741 memcpy(pDst, g_dyn_huff_3, sizeof(g_dyn_hu 742 uint32_t dst_ofs = sizeof(g_dyn_huff_3); 743 744 uint64_t bit_buf = DYN_HUFF_3_BITBUF; 745 int bit_buf_size = DYN_HUFF_3_BITBUF_SIZE; 746 747 const uint8_t* pSrc = pImg; 748 uint32_t src_ofs = 0; 749 750 uint32_t src_adler32 = fpng_adler32(pImg, 751 752 for (uint32_t y = 0; y < h; y++) 753 { 754 const uint32_t end_src_ofs = src_ofs + b 755 756 const uint32_t filter_lit = pSrc[src_ofs 757 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_3_code 758 759 uint32_t prev_lits; 760 761 { 762 uint32_t lits = READ_RGB_PIXEL(pSrc + 763 764 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_3_co 765 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_3_co 766 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_3_co 767 768 src_ofs += 3; 769 770 prev_lits = lits; 771 } 772 773 TOOLS_FPNG_PUT_BITS_FLUSH; 774 775 while (src_ofs < end_src_ofs) 776 { 777 uint32_t lits = READ_RGB_PIXEL(pSrc + 778 779 if (lits == prev_lits) 780 { 781 uint32_t match_len = 3; 782 uint32_t max_match_len = minimum<int 783 784 while (match_len < max_match_len) 785 { 786 if (READ_RGB_PIXEL(pSrc + src_ofs 787 break; 788 match_len += 3; 789 } 790 791 uint32_t adj_match_len = match_len - 792 793 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_3_ 794 TOOLS_FPNG_PUT_BITS(adj_match_len & 795 796 src_ofs += match_len; 797 } 798 else 799 { 800 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_3_ 801 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_3_ 802 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_3_ 803 804 prev_lits = lits; 805 806 src_ofs += 3; 807 } 808 809 TOOLS_FPNG_PUT_BITS_FLUSH; 810 811 } // while (src_ofs < end_src_ofs) 812 813 } // y 814 815 assert(src_ofs == h * bpl); 816 817 assert(bit_buf_size <= 7); 818 819 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_3_codes[ 820 821 TOOLS_FPNG_PUT_BITS_FORCE_FLUSH; 822 823 // Write zlib adler32 824 for (uint32_t i = 0; i < 4; i++) 825 { 826 if ((dst_ofs + 1) > dst_buf_size) 827 return 0; 828 *(uint8_t*)(pDst + dst_ofs) = (uint8_t)( 829 dst_ofs++; 830 831 src_adler32 <<= 8; 832 } 833 834 return dst_ofs; 835 } 836 837 static uint32_t pixel_deflate_dyn_4_rle( 838 const uint8_t* pImg, uint32_t w, uint32_t 839 uint8_t* pDst, uint32_t dst_buf_size) 840 { 841 const uint32_t bpl = 1 + w * 4; 842 843 uint64_t bit_buf = 0; 844 int bit_buf_size = 0; 845 846 uint32_t dst_ofs = 0; 847 848 // zlib header 849 TOOLS_FPNG_PUT_BITS(0x78, 8); 850 TOOLS_FPNG_PUT_BITS(0x01, 8); 851 852 // write BFINAL bit 853 TOOLS_FPNG_PUT_BITS(1, 1); 854 855 std::vector<uint64_t> codes; 856 codes.resize((w + 1) * h); 857 uint64_t* pDst_codes = codes.data(); 858 859 uint32_t lit_freq[DEFL_MAX_HUFF_SYMBOLS_0] 860 memset(lit_freq, 0, sizeof(lit_freq)); 861 862 const uint8_t* pSrc = pImg; 863 uint32_t src_ofs = 0; 864 865 uint32_t src_adler32 = fpng_adler32(pImg, 866 867 const uint32_t dist_sym = g_defl_small_dis 868 869 for (uint32_t y = 0; y < h; y++) 870 { 871 const uint32_t end_src_ofs = src_ofs + b 872 873 const uint32_t filter_lit = pSrc[src_ofs 874 *pDst_codes++ = 1 | (filter_lit << 8); 875 lit_freq[filter_lit]++; 876 877 uint32_t prev_lits; 878 { 879 uint32_t lits = READ_LE32(pSrc + src_o 880 881 *pDst_codes++ = (uint64_t)lits << 8; 882 883 lit_freq[lits & 0xFF]++; 884 lit_freq[(lits >> 8) & 0xFF]++; 885 lit_freq[(lits >> 16) & 0xFF]++; 886 lit_freq[lits >> 24]++; 887 888 src_ofs += 4; 889 890 prev_lits = lits; 891 } 892 893 while (src_ofs < end_src_ofs) 894 { 895 uint32_t lits = READ_LE32(pSrc + src_o 896 897 if (lits == prev_lits) 898 { 899 uint32_t match_len = 4; 900 uint32_t max_match_len = minimum<int 901 902 while (match_len < max_match_len) 903 { 904 if (READ_LE32(pSrc + src_ofs + mat 905 break; 906 match_len += 4; 907 } 908 909 *pDst_codes++ = match_len - 1; 910 911 uint32_t adj_match_len = match_len - 912 913 lit_freq[g_defl_len_sym[adj_match_le 914 915 src_ofs += match_len; 916 } 917 else 918 { 919 *pDst_codes++ = (uint64_t)lits << 8; 920 921 lit_freq[lits & 0xFF]++; 922 lit_freq[(lits >> 8) & 0xFF]++; 923 lit_freq[(lits >> 16) & 0xFF]++; 924 lit_freq[lits >> 24]++; 925 926 prev_lits = lits; 927 928 src_ofs += 4; 929 } 930 931 } // while (src_ofs < end_src_ofs) 932 933 } // y 934 935 assert(src_ofs == h * bpl); 936 const uint32_t total_codes = (uint32_t)(pD 937 assert(total_codes <= codes.size()); 938 939 defl_huff dh; 940 941 lit_freq[256] = 1; 942 943 adjust_freq32(DEFL_MAX_HUFF_SYMBOLS_0, lit 944 945 memset(&dh.m_huff_count[1][0], 0, sizeof(d 946 dh.m_huff_count[1][dist_sym] = 1; 947 dh.m_huff_count[1][dist_sym + 1] = 1; // t 948 949 if (!defl_start_dynamic_block(&dh, pDst, d 950 return 0; 951 952 assert(bit_buf_size <= 7); 953 assert(dh.m_huff_codes[1][dist_sym] == 0 & 954 955 for (uint32_t i = 0; i < total_codes; i++) 956 { 957 uint64_t c = codes[i]; 958 959 uint32_t c_type = (uint32_t)(c & 0xFF); 960 if (c_type == 0) 961 { 962 uint32_t lits = (uint32_t)(c >> 8); 963 964 TOOLS_FPNG_PUT_BITS_CZ(dh.m_huff_codes 965 lits >>= 8; 966 967 TOOLS_FPNG_PUT_BITS_CZ(dh.m_huff_codes 968 lits >>= 8; 969 970 TOOLS_FPNG_PUT_BITS_CZ(dh.m_huff_codes 971 lits >>= 8; 972 973 if (bit_buf_size >= 49) 974 { 975 TOOLS_FPNG_PUT_BITS_FLUSH; 976 } 977 978 TOOLS_FPNG_PUT_BITS_CZ(dh.m_huff_codes 979 } 980 else if (c_type == 1) 981 { 982 uint32_t lit = (uint32_t)(c >> 8); 983 TOOLS_FPNG_PUT_BITS_CZ(dh.m_huff_codes 984 } 985 else 986 { 987 uint32_t match_len = c_type + 1; 988 989 uint32_t adj_match_len = match_len - 3 990 991 TOOLS_FPNG_PUT_BITS_CZ(dh.m_huff_codes 992 TOOLS_FPNG_PUT_BITS(adj_match_len & g_ 993 994 // no need to write the distance code, 995 } 996 997 // up to 55 bits 998 TOOLS_FPNG_PUT_BITS_FLUSH; 999 } 1000 1001 TOOLS_FPNG_PUT_BITS_CZ(dh.m_huff_codes[0] 1002 1003 TOOLS_FPNG_PUT_BITS_FORCE_FLUSH; 1004 1005 // Write zlib adler32 1006 for (uint32_t i = 0; i < 4; i++) 1007 { 1008 if ((dst_ofs + 1) > dst_buf_size) 1009 return 0; 1010 *(uint8_t*)(pDst + dst_ofs) = (uint8_t) 1011 dst_ofs++; 1012 1013 src_adler32 <<= 8; 1014 } 1015 1016 return dst_ofs; 1017 } 1018 1019 static uint32_t pixel_deflate_dyn_4_rle_one 1020 const uint8_t* pImg, uint32_t w, uint32_t 1021 uint8_t* pDst, uint32_t dst_buf_size) 1022 { 1023 const uint32_t bpl = 1 + w * 4; 1024 1025 if (dst_buf_size < sizeof(g_dyn_huff_4)) 1026 return false; 1027 memcpy(pDst, g_dyn_huff_4, sizeof(g_dyn_h 1028 uint32_t dst_ofs = sizeof(g_dyn_huff_4); 1029 1030 uint64_t bit_buf = DYN_HUFF_4_BITBUF; 1031 int bit_buf_size = DYN_HUFF_4_BITBUF_SIZE 1032 1033 const uint8_t* pSrc = pImg; 1034 uint32_t src_ofs = 0; 1035 1036 uint32_t src_adler32 = fpng_adler32(pImg, 1037 1038 for (uint32_t y = 0; y < h; y++) 1039 { 1040 const uint32_t end_src_ofs = src_ofs + 1041 1042 const uint32_t filter_lit = pSrc[src_of 1043 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_4_cod 1044 1045 TOOLS_FPNG_PUT_BITS_FLUSH; 1046 1047 uint32_t prev_lits; 1048 { 1049 uint32_t lits = READ_LE32(pSrc + src_ 1050 1051 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_4_c 1052 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_4_c 1053 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_4_c 1054 1055 if (bit_buf_size >= 49) 1056 { 1057 TOOLS_FPNG_PUT_BITS_FLUSH; 1058 } 1059 1060 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_4_c 1061 1062 src_ofs += 4; 1063 1064 prev_lits = lits; 1065 } 1066 1067 TOOLS_FPNG_PUT_BITS_FLUSH; 1068 1069 while (src_ofs < end_src_ofs) 1070 { 1071 uint32_t lits = READ_LE32(pSrc + src_ 1072 1073 if (lits == prev_lits) 1074 { 1075 uint32_t match_len = 4; 1076 uint32_t max_match_len = minimum<in 1077 1078 while (match_len < max_match_len) 1079 { 1080 if (READ_LE32(pSrc + src_ofs + ma 1081 break; 1082 match_len += 4; 1083 } 1084 1085 uint32_t adj_match_len = match_len 1086 1087 const uint32_t match_code_bits = g_ 1088 const uint32_t len_extra_bits = g_d 1089 1090 if (match_len == 4) 1091 { 1092 // This check is optional - see i 1093 uint32_t lit_bits = g_dyn_huff_4_ 1094 g_dyn_huff_4_codes[(lits >> 16) 1095 1096 if ((match_code_bits + len_extra_ 1097 goto do_literals; 1098 } 1099 1100 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_4 1101 TOOLS_FPNG_PUT_BITS(adj_match_len & 1102 1103 src_ofs += match_len; 1104 } 1105 else 1106 { 1107 do_literals: 1108 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_4 1109 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_4 1110 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_4 1111 1112 if (bit_buf_size >= 49) 1113 { 1114 TOOLS_FPNG_PUT_BITS_FLUSH; 1115 } 1116 1117 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_4 1118 1119 src_ofs += 4; 1120 1121 prev_lits = lits; 1122 } 1123 1124 TOOLS_FPNG_PUT_BITS_FLUSH; 1125 1126 } // while (src_ofs < end_src_ofs) 1127 1128 } // y 1129 1130 assert(src_ofs == h * bpl); 1131 1132 assert(bit_buf_size <= 7); 1133 1134 TOOLS_FPNG_PUT_BITS_CZ(g_dyn_huff_4_codes 1135 1136 TOOLS_FPNG_PUT_BITS_FORCE_FLUSH; 1137 1138 // Write zlib adler32 1139 for (uint32_t i = 0; i < 4; i++) 1140 { 1141 if ((dst_ofs + 1) > dst_buf_size) 1142 return 0; 1143 *(uint8_t*)(pDst + dst_ofs) = (uint8_t) 1144 dst_ofs++; 1145 1146 src_adler32 <<= 8; 1147 } 1148 1149 return dst_ofs; 1150 } 1151 1152 static void vector_append(std::vector<uint8 1153 { 1154 if (len) 1155 { 1156 size_t l = buf.size(); 1157 buf.resize(l + len); 1158 memcpy(buf.data() + l, pData, len); 1159 } 1160 } 1161 1162 static void apply_filter(uint32_t filter, i 1163 { 1164 (void)h; 1165 1166 switch (filter) 1167 { 1168 case 0: 1169 { 1170 *pDst++ = 0; 1171 1172 memcpy(pDst, pSrc, bpl); 1173 break; 1174 } 1175 case 2: 1176 { 1177 assert(pPrev_src); 1178 1179 // Previous scanline 1180 *pDst++ = 2; 1181 1182 { 1183 if (num_chans == 3) 1184 { 1185 for (uint32_t x = 0; x < (uint32_t) 1186 { 1187 pDst[0] = (uint8_t)(pSrc[0] - pPr 1188 pDst[1] = (uint8_t)(pSrc[1] - pPr 1189 pDst[2] = (uint8_t)(pSrc[2] - pPr 1190 1191 pSrc += 3; 1192 pPrev_src += 3; 1193 pDst += 3; 1194 } 1195 } 1196 else 1197 { 1198 for (uint32_t x = 0; x < (uint32_t) 1199 { 1200 pDst[0] = (uint8_t)(pSrc[0] - pPr 1201 pDst[1] = (uint8_t)(pSrc[1] - pPr 1202 pDst[2] = (uint8_t)(pSrc[2] - pPr 1203 pDst[3] = (uint8_t)(pSrc[3] - pPr 1204 1205 pSrc += 4; 1206 pPrev_src += 4; 1207 pDst += 4; 1208 } 1209 } 1210 } 1211 1212 break; 1213 } 1214 default: 1215 assert(0); 1216 break; 1217 } 1218 } 1219 1220 inline bool fpng_encode_image_to_memory(con 1221 { 1222 if (!endian_check()) 1223 { 1224 assert(0); 1225 return false; 1226 } 1227 1228 if ((w < 1) || (h < 1) || (w * (uint64_t) 1229 { 1230 assert(0); 1231 return false; 1232 } 1233 1234 if ((num_chans != 3) && (num_chans != 4)) 1235 { 1236 assert(0); 1237 return false; 1238 } 1239 1240 int i, bpl = w * num_chans; 1241 uint32_t y; 1242 1243 std::vector<uint8_t> temp_buf; 1244 temp_buf.resize((bpl + 1) * h + 7); 1245 uint32_t temp_buf_ofs = 0; 1246 1247 for (y = 0; y < h; ++y) 1248 { 1249 const uint8_t* pSrc = (uint8_t*)pImage 1250 const uint8_t* pPrev_src = y ? ((uint8_ 1251 1252 uint8_t* pDst = &temp_buf[temp_buf_ofs] 1253 1254 apply_filter(y ? 2 : 0, w, h, num_chans 1255 1256 temp_buf_ofs += 1 + bpl; 1257 } 1258 1259 const uint32_t PNG_HEADER_SIZE = 58; 1260 1261 uint32_t out_ofs = PNG_HEADER_SIZE; 1262 1263 out_buf.resize((out_ofs + (bpl + 1) * h + 1264 1265 uint32_t defl_size = 0; 1266 if ((flags & FPNG_FORCE_UNCOMPRESSED) == 1267 { 1268 if (num_chans == 3) 1269 { 1270 if (flags & FPNG_ENCODE_SLOWER) 1271 defl_size = pixel_deflate_dyn_3_rle 1272 else 1273 defl_size = pixel_deflate_dyn_3_rle 1274 } 1275 else 1276 { 1277 if (flags & FPNG_ENCODE_SLOWER) 1278 defl_size = pixel_deflate_dyn_4_rle 1279 else 1280 defl_size = pixel_deflate_dyn_4_rle 1281 } 1282 } 1283 1284 uint32_t zlib_size = defl_size; 1285 1286 if (!defl_size) 1287 { 1288 // Dynamic block failed to compress - f 1289 1290 temp_buf_ofs = 0; 1291 1292 for (y = 0; y < h; ++y) 1293 { 1294 const uint8_t* pSrc = (uint8_t*)pImag 1295 1296 uint8_t* pDst = &temp_buf[temp_buf_of 1297 1298 apply_filter(0, w, h, num_chans, bpl, 1299 1300 temp_buf_ofs += 1 + bpl; 1301 } 1302 1303 assert(temp_buf_ofs <= temp_buf.size()) 1304 1305 out_buf.resize(out_ofs + 6 + temp_buf_o 1306 1307 uint32_t raw_size = write_raw_block(tem 1308 if (!raw_size) 1309 { 1310 // Somehow we miscomputed the size of 1311 assert(0); 1312 return false; 1313 } 1314 1315 zlib_size = raw_size; 1316 } 1317 1318 assert((out_ofs + zlib_size) <= out_buf.s 1319 1320 out_buf.resize(out_ofs + zlib_size); 1321 1322 const uint32_t idat_len = (uint32_t)out_b 1323 1324 // Write real PNG header, fdEC chunk, and 1325 { 1326 static const uint8_t s_color_type[] = { 1327 1328 uint8_t pnghdr[58] = { 1329 0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x 1330 0x00,0x00,0x00,0x0d, 'I','H','D','R', 1331 0,0,(uint8_t)(w >> 8),(uint8_t)w, / 1332 0,0,(uint8_t)(h >> 8),(uint8_t)h, // 1333 8, //bit_depth 1334 s_color_type[num_chans], // color_typ 1335 0, // compression 1336 0, // filter 1337 0, // interlace 1338 0, 0, 0, 0, // IHDR crc32 1339 0, 0, 0, 5, 'f', 'd', 'E', 'C', 82, 3 1340 (uint8_t)(idat_len >> 24),(uint8_t)(i 1341 }; 1342 1343 // Compute IHDR CRC32 1344 uint32_t c = (uint32_t)fpng_crc32(pnghd 1345 for (i = 0; i < 4; ++i, c <<= 8) 1346 ((uint8_t*)(pnghdr + 29))[i] = (uint8 1347 1348 memcpy(out_buf.data(), pnghdr, PNG_HEAD 1349 } 1350 1351 // Write IDAT chunk's CRC32 and a 0 lengt 1352 vector_append(out_buf, "\0\0\0\0\0\0\0\0\ 1353 1354 // Compute IDAT crc32 1355 uint32_t c = (uint32_t)fpng_crc32(out_buf 1356 1357 for (i = 0; i < 4; ++i, c <<= 8) 1358 (out_buf.data() + out_buf.size() - 16)[ 1359 1360 return true; 1361 } 1362 1363 inline bool fpng_encode_image_to_file(const 1364 { 1365 std::vector<uint8_t> out_buf; 1366 if (!fpng_encode_image_to_memory(pImage, 1367 return false; 1368 1369 FILE* pFile = fopen(pFilename 1370 if (!pFile) return false; 1371 1372 if (fwrite(out_buf.data(), 1, out_buf.siz 1373 { 1374 fclose(pFile); 1375 return false; 1376 } 1377 1378 return (fclose(pFile) != EOF); 1379 } 1380 1381 // Decompression 1382 1383 const uint32_t FPNG_DECODER_TABLE_BITS = 12 1384 const uint32_t FPNG_DECODER_TABLE_SIZE = 1 1385 1386 static bool build_decoder_table(uint32_t nu 1387 { 1388 uint32_t num_codes[16]; 1389 1390 memset(num_codes, 0, sizeof(num_codes)); 1391 for (uint32_t i = 0; i < num_syms; i++) 1392 { 1393 assert(uint32_t(pCode_sizes[i]) < 1394 num_codes[pCode_sizes[i]]++; 1395 } 1396 1397 uint32_t next_code[17]; 1398 next_code[0] = next_code[1] = 0; 1399 uint32_t total = 0; 1400 for (uint32_t i = 1; i <= 15; i++) 1401 next_code[i + 1] = (uint32_t)(total = ( 1402 1403 if (total != 0x10000) 1404 { 1405 uint32_t j = 0; 1406 1407 for (uint32_t i = 15; i != 0; i--) 1408 if ((j += num_codes[i]) > 1) 1409 return false; 1410 1411 if (j != 1) 1412 return false; 1413 } 1414 1415 uint32_t rev_codes[DEFL_MAX_HUFF_SYMBOLS] 1416 1417 for (uint32_t i = 0; i < num_syms; i++) 1418 rev_codes[i] = next_code[pCode_sizes[i] 1419 1420 memset(pTable, 0, sizeof(uint32_t) * FPNG 1421 1422 for (uint32_t i = 0; i < num_syms; i++) 1423 { 1424 const uint32_t code_size = pCode_sizes[ 1425 if (!code_size) 1426 continue; 1427 1428 uint32_t old_code = rev_codes[i], new_c 1429 for (uint32_t j = code_size; j != 0; j- 1430 { 1431 new_code = (new_code << 1) | (old_cod 1432 old_code >>= 1; 1433 } 1434 1435 uint32_t j = 1 << code_size; 1436 1437 while (new_code < FPNG_DECODER_TABLE_SI 1438 { 1439 pTable[new_code] = i | (code_size << 1440 new_code += j; 1441 } 1442 } 1443 1444 return true; 1445 } 1446 1447 static const uint16_t g_run_len3_to_4[259] 1448 { 1449 0, 1450 0, 0, 4, 0, 0, 8, 0, 0, 12, 0, 0, 16, 0, 1451 32, 0, 0, 36, 0, 0, 40, 0, 0, 44, 0, 0, 4 1452 60, 0, 0, 64, 0, 0, 68, 0, 0, 72, 0, 0, 7 1453 88, 0, 0, 92, 0, 0, 96, 0, 0, 100, 0, 0, 1454 116, 0, 0, 120, 0, 0, 124, 0, 0, 128, 0, 1455 144, 0, 0, 148, 0, 0, 152, 0, 0, 156, 0, 1456 172, 0, 0, 176, 0, 0, 180, 0, 0, 184, 0, 1457 200, 0, 0, 204, 0, 0, 208, 0, 0, 212, 0, 1458 228, 0, 0, 232, 0, 0, 236, 0, 0, 240, 0, 1459 256, 0, 0, 260, 0, 0, 264, 0, 0, 268, 0, 1460 284, 0, 0, 288, 0, 0, 292, 0, 0, 296, 0, 1461 312, 0, 0, 316, 0, 0, 320, 0, 0, 324, 0, 1462 340, 0, 0, 1463 344, 1464 }; 1465 1466 static const int s_length_extra[] = { 0,0,0 1467 static const int s_length_range[] = { 3,4,5 1468 1469 #define TOOLS_FPNG_ENSURE_32BITS() do { \ 1470 if (bit_buf_size < 32) { \ 1471 if ((src_ofs + 4) > src_len) return false 1472 bit_buf |= ((uint64_t)READ_LE32(pSrc + sr 1473 src_ofs += 4; bit_buf_size += 32; } \ 1474 } while(0) 1475 1476 #define TOOLS_FPNG_GET_BITS(b, ll) do { \ 1477 uint32_t l = ll; assert(l && (l <= 32)); \ 1478 b = (uint32_t)(bit_buf & g_bitmasks[l]); \ 1479 bit_buf >>= l; \ 1480 bit_buf_size -= l; \ 1481 TOOLS_FPNG_ENSURE_32BITS(); \ 1482 } while(0) 1483 1484 #define TOOLS_FPNG_SKIP_BITS(ll) do { \ 1485 uint32_t l = ll; assert(l <= 32); \ 1486 bit_buf >>= l; \ 1487 bit_buf_size -= l; \ 1488 TOOLS_FPNG_ENSURE_32BITS(); \ 1489 } while(0) 1490 1491 #define TOOLS_FPNG_GET_BITS_NE(b, ll) do { \ 1492 uint32_t l = ll; assert(l && (l <= 32) && ( 1493 b = (uint32_t)(bit_buf & g_bitmasks[l]); \ 1494 bit_buf >>= l; \ 1495 bit_buf_size -= l; \ 1496 } while(0) 1497 1498 #define TOOLS_FPNG_SKIP_BITS_NE(ll) do { \ 1499 uint32_t l = ll; assert(l <= 32 && (bit_buf 1500 bit_buf >>= l; \ 1501 bit_buf_size -= l; \ 1502 } while(0) 1503 1504 static bool prepare_dynamic_block( 1505 const uint8_t* pSrc, uint32_t src_len, ui 1506 uint32_t& bit_buf_size, uint64_t& bit_buf 1507 uint32_t* pLit_table, uint32_t num_chans) 1508 { 1509 static const uint8_t s_bit_length_order[] 1510 1511 uint32_t num_lit_codes, num_dist_codes, n 1512 1513 TOOLS_FPNG_GET_BITS(num_lit_codes, 5); 1514 num_lit_codes += 257; 1515 1516 TOOLS_FPNG_GET_BITS(num_dist_codes, 5); 1517 num_dist_codes += 1; 1518 1519 uint32_t total_codes = num_lit_codes + nu 1520 if (total_codes > (DEFL_MAX_HUFF_SYMBOLS_ 1521 return false; 1522 1523 uint8_t code_sizes[DEFL_MAX_HUFF_SYMBOLS_ 1524 memset(code_sizes, 0, sizeof(code_sizes)) 1525 1526 TOOLS_FPNG_GET_BITS(num_clen_codes, 4); 1527 num_clen_codes += 4; 1528 1529 uint8_t clen_codesizes[DEFL_MAX_HUFF_SYMB 1530 memset(clen_codesizes, 0, sizeof(clen_cod 1531 1532 for (uint32_t i = 0; i < num_clen_codes; 1533 { 1534 uint32_t len = 0; 1535 TOOLS_FPNG_GET_BITS(len, 3); 1536 clen_codesizes[s_bit_length_order[i]] = 1537 } 1538 1539 uint32_t clen_table[FPNG_DECODER_TABLE_SI 1540 if (!build_decoder_table(DEFL_MAX_HUFF_SY 1541 return false; 1542 1543 uint32_t min_code_size = 15; 1544 1545 for (uint32_t cur_code = 0; cur_code < to 1546 { 1547 uint32_t sym = clen_table[bit_buf & (FP 1548 uint32_t sym_len = sym >> 9; 1549 if (!sym_len) 1550 return false; 1551 TOOLS_FPNG_SKIP_BITS(sym_len); 1552 sym &= 511; 1553 1554 if (sym <= 15) 1555 { 1556 // Can't be a fpng Huffman table 1557 if (sym > FPNG_DECODER_TABLE_BITS) 1558 return false; 1559 1560 if (sym) 1561 min_code_size = minimum(min_code_si 1562 1563 code_sizes[cur_code++] = (uint8_t)sym 1564 continue; 1565 } 1566 1567 uint32_t rep_len = 0, rep_code_size = 0 1568 1569 switch (sym) 1570 { 1571 case 16: 1572 { 1573 TOOLS_FPNG_GET_BITS(rep_len, 2); 1574 rep_len += 3; 1575 if (!cur_code) 1576 return false; 1577 rep_code_size = code_sizes[cur_code - 1578 break; 1579 } 1580 case 17: 1581 { 1582 TOOLS_FPNG_GET_BITS(rep_len, 3); 1583 rep_len += 3; 1584 rep_code_size = 0; 1585 break; 1586 } 1587 case 18: 1588 { 1589 TOOLS_FPNG_GET_BITS(rep_len, 7); 1590 rep_len += 11; 1591 rep_code_size = 0; 1592 break; 1593 } 1594 } 1595 1596 if ((cur_code + rep_len) > total_codes) 1597 return false; 1598 1599 for (; rep_len; rep_len--) 1600 code_sizes[cur_code++] = (uint8_t)rep 1601 } 1602 1603 uint8_t lit_codesizes[DEFL_MAX_HUFF_SYMBO 1604 1605 memcpy(lit_codesizes, code_sizes, num_lit 1606 memset(lit_codesizes + num_lit_codes, 0, 1607 1608 uint32_t total_valid_distcodes = 0; 1609 for (uint32_t i = 0; i < num_dist_codes; 1610 total_valid_distcodes += (code_sizes[nu 1611 1612 // 1 or 2 because the first version of FP 1613 if ((total_valid_distcodes < 1) || (total 1614 return false; 1615 1616 if (code_sizes[num_lit_codes + (num_chans 1617 return false; 1618 1619 if (total_valid_distcodes == 2) 1620 { 1621 // If there are two valid distance code 1622 if (code_sizes[num_lit_codes + num_chan 1623 return false; 1624 } 1625 1626 if (!build_decoder_table(num_lit_codes, l 1627 return false; 1628 1629 // Add next symbol to decoder table, when 1630 for (uint32_t i = 0; i < FPNG_DECODER_TAB 1631 { 1632 uint32_t sym = pLit_table[i] & 511; 1633 if (sym >= 256) 1634 continue; 1635 1636 uint32_t sym_bits = (pLit_table[i] >> 9 1637 if (!sym_bits) 1638 continue; 1639 assert(sym_bits <= FPNG_DECODER_TABLE_B 1640 1641 uint32_t bits_left = FPNG_DECODER_TABLE 1642 if (bits_left < min_code_size) 1643 continue; 1644 1645 uint32_t next_bits = i >> sym_bits; 1646 uint32_t next_sym = pLit_table[next_bit 1647 uint32_t next_sym_bits = (pLit_table[ne 1648 if ((!next_sym_bits) || (bits_left < ne 1649 continue; 1650 1651 pLit_table[i] |= (next_sym << 16) | (ne 1652 } 1653 1654 return true; 1655 } 1656 1657 static bool fpng_pixel_zlib_raw_decompress( 1658 const uint8_t* pSrc, uint32_t src_len, ui 1659 uint8_t* pDst, uint32_t w, uint32_t h, 1660 uint32_t src_chans, uint32_t dst_chans) 1661 { 1662 assert((src_chans == 3) || (src_chans == 1663 assert((dst_chans == 3) || (dst_chans == 1664 1665 const uint32_t src_bpl = w * src_chans; 1666 const uint32_t dst_bpl = w * dst_chans; 1667 const uint32_t dst_len = dst_bpl * h; 1668 1669 uint32_t src_ofs = 2; 1670 uint32_t dst_ofs = 0; 1671 uint32_t raster_ofs = 0; 1672 uint32_t comp_ofs = 0; 1673 1674 for (; ; ) 1675 { 1676 if ((src_ofs + 1) > src_len) 1677 return false; 1678 1679 const bool bfinal = (pSrc[src_ofs] & 1) 1680 const uint32_t btype = (pSrc[src_ofs] > 1681 if (btype != 0) 1682 return false; 1683 1684 src_ofs++; 1685 1686 if ((src_ofs + 4) > src_len) 1687 return false; 1688 uint32_t len = pSrc[src_ofs + 0] | (pSr 1689 uint32_t nlen = pSrc[src_ofs + 2] | (pS 1690 src_ofs += 4; 1691 1692 if (len != (~nlen & 0xFFFF)) 1693 return false; 1694 1695 if ((src_ofs + len) > src_len) 1696 return false; 1697 1698 // Raw blocks are a relatively uncommon 1699 // Supports 3->4 and 4->3 byte/pixel co 1700 for (uint32_t i = 0; i < len; i++) 1701 { 1702 uint32_t c = pSrc[src_ofs + i]; 1703 1704 if (!raster_ofs) 1705 { 1706 // Check filter type 1707 if (c != 0) 1708 return false; 1709 1710 assert(!comp_ofs); 1711 } 1712 else 1713 { 1714 if (comp_ofs < dst_chans) 1715 { 1716 if (dst_ofs == dst_len) 1717 return false; 1718 1719 pDst[dst_ofs++] = (uint8_t)c; 1720 } 1721 1722 if (++comp_ofs == src_chans) 1723 { 1724 if (dst_chans > src_chans) 1725 { 1726 if (dst_ofs == dst_len) 1727 return false; 1728 1729 pDst[dst_ofs++] = (uint8_t)0xFF 1730 } 1731 1732 comp_ofs = 0; 1733 } 1734 } 1735 1736 if (++raster_ofs == (src_bpl + 1)) 1737 { 1738 assert(!comp_ofs); 1739 raster_ofs = 0; 1740 } 1741 } 1742 1743 src_ofs += len; 1744 1745 if (bfinal) 1746 break; 1747 } 1748 1749 if (comp_ofs != 0) 1750 return false; 1751 1752 // Check for zlib adler32 1753 if ((src_ofs + 4) != zlib_len) 1754 return false; 1755 1756 return (dst_ofs == dst_len); 1757 } 1758 1759 template<uint32_t dst_comps> 1760 static bool fpng_pixel_zlib_decompress_3( 1761 const uint8_t* pSrc, uint32_t src_len, ui 1762 uint8_t* pDst, uint32_t w, uint32_t h) 1763 { 1764 assert(src_len >= (zlib_len + 4)); 1765 1766 const uint32_t dst_bpl = w * dst_comps; 1767 //const uint32_t dst_len = dst_bpl * h; 1768 1769 if (zlib_len < 7) 1770 return false; 1771 1772 // check zlib header 1773 if ((pSrc[0] != 0x78) || (pSrc[1] != 0x01 1774 return false; 1775 1776 uint32_t src_ofs = 2; 1777 1778 if ((pSrc[src_ofs] & 6) == 0) 1779 return fpng_pixel_zlib_raw_decompress(p 1780 1781 if ((src_ofs + 4) > src_len) 1782 return false; 1783 uint64_t bit_buf = READ_LE32(pSrc + src_o 1784 src_ofs += 4; 1785 1786 uint32_t bit_buf_size = 32; 1787 1788 uint32_t bfinal, btype; 1789 TOOLS_FPNG_GET_BITS(bfinal, 1); 1790 TOOLS_FPNG_GET_BITS(btype, 2); 1791 1792 // Must be the final block or it's not va 1793 if ((bfinal != 1) || (btype != 2)) 1794 return false; 1795 1796 uint32_t lit_table[FPNG_DECODER_TABLE_SIZ 1797 if (!prepare_dynamic_block(pSrc, src_len, 1798 return false; 1799 1800 const uint8_t* pPrev_scanline = NULL/*nul 1801 uint8_t* pCur_scanline = pDst; 1802 1803 for (uint32_t y = 0; y < h; y++) 1804 { 1805 // At start of PNG scanline, so read th 1806 assert(bit_buf_size >= FPNG_DECODER_TAB 1807 uint32_t filter = lit_table[bit_buf & ( 1808 uint32_t filter_len = (filter >> 9) & 1 1809 if (!filter_len) 1810 return false; 1811 TOOLS_FPNG_SKIP_BITS(filter_len); 1812 filter &= 511; 1813 1814 uint32_t expected_filter = (y ? 2 : 0); 1815 if (filter != expected_filter) 1816 return false; 1817 1818 uint32_t x_ofs = 0; 1819 uint8_t prev_delta_r = 0, prev_delta_g 1820 do 1821 { 1822 assert(bit_buf_size >= FPNG_DECODER_T 1823 uint32_t lit0_tab = lit_table[bit_buf 1824 1825 uint32_t lit0 = lit0_tab; 1826 uint32_t lit0_len = (lit0_tab >> 9) & 1827 if (!lit0_len) 1828 return false; 1829 TOOLS_FPNG_SKIP_BITS(lit0_len); 1830 1831 if (lit0 & 256) 1832 { 1833 lit0 &= 511; 1834 1835 // Can't be EOB - we still have mor 1836 if (lit0 == 256) 1837 return false; 1838 1839 // Must be an RLE match against the 1840 uint32_t run_len = s_length_range[l 1841 if (lit0 >= 265) 1842 { 1843 uint32_t e; 1844 TOOLS_FPNG_GET_BITS_NE(e, s_lengt 1845 1846 run_len += e; 1847 } 1848 1849 // Skip match distance - it's alway 1850 TOOLS_FPNG_SKIP_BITS_NE(1); 1851 1852 // Matches must always be a multipl 1853 assert((run_len % 3) == 0); 1854 1855 if (dst_comps == 4) 1856 { 1857 const uint32_t x_ofs_end = x_ofs 1858 1859 // Check for valid run lengths 1860 if (x_ofs == x_ofs_end) 1861 return false; 1862 1863 // Matches cannot cross scanlines 1864 if (x_ofs_end > dst_bpl) 1865 return false; 1866 1867 if (pPrev_scanline) 1868 { 1869 if ((prev_delta_r | prev_delta_ 1870 { 1871 memcpy(pCur_scanline + x_ofs, 1872 x_ofs = x_ofs_end; 1873 } 1874 else 1875 { 1876 do 1877 { 1878 pCur_scanline[x_ofs] = (uin 1879 pCur_scanline[x_ofs + 1] = 1880 pCur_scanline[x_ofs + 2] = 1881 pCur_scanline[x_ofs + 3] = 1882 x_ofs += 4; 1883 } while (x_ofs < x_ofs_end); 1884 } 1885 } 1886 else 1887 { 1888 do 1889 { 1890 pCur_scanline[x_ofs] = prev_d 1891 pCur_scanline[x_ofs + 1] = pr 1892 pCur_scanline[x_ofs + 2] = pr 1893 pCur_scanline[x_ofs + 3] = 0x 1894 x_ofs += 4; 1895 } while (x_ofs < x_ofs_end); 1896 } 1897 } 1898 else 1899 { 1900 // Check for valid run lengths 1901 if (!g_run_len3_to_4[run_len]) 1902 return false; 1903 1904 const uint32_t x_ofs_end = x_ofs 1905 1906 // Matches cannot cross scanlines 1907 if (x_ofs_end > dst_bpl) 1908 return false; 1909 1910 if (pPrev_scanline) 1911 { 1912 if ((prev_delta_r | prev_delta_ 1913 { 1914 memcpy(pCur_scanline + x_ofs, 1915 x_ofs = x_ofs_end; 1916 } 1917 else 1918 { 1919 do 1920 { 1921 pCur_scanline[x_ofs] = (uin 1922 pCur_scanline[x_ofs + 1] = 1923 pCur_scanline[x_ofs + 2] = 1924 x_ofs += 3; 1925 } while (x_ofs < x_ofs_end); 1926 } 1927 } 1928 else 1929 { 1930 do 1931 { 1932 pCur_scanline[x_ofs] = prev_d 1933 pCur_scanline[x_ofs + 1] = pr 1934 pCur_scanline[x_ofs + 2] = pr 1935 x_ofs += 3; 1936 } while (x_ofs < x_ofs_end); 1937 } 1938 } 1939 } 1940 else 1941 { 1942 uint32_t lit1, lit2; 1943 1944 uint32_t lit1_spec_len = (lit0_tab 1945 uint32_t lit2_len; 1946 if (lit1_spec_len) 1947 { 1948 lit1 = (lit0_tab >> 16) & 511; 1949 TOOLS_FPNG_SKIP_BITS_NE(lit1_spec 1950 1951 assert(bit_buf_size >= FPNG_DECOD 1952 lit2 = lit_table[bit_buf & (FPNG_ 1953 lit2_len = (lit2 >> 9) & 15; 1954 if (!lit2_len) 1955 return false; 1956 } 1957 else 1958 { 1959 assert(bit_buf_size >= FPNG_DECOD 1960 lit1 = lit_table[bit_buf & (FPNG_ 1961 uint32_t lit1_len = (lit1 >> 9) & 1962 if (!lit1_len) 1963 return false; 1964 TOOLS_FPNG_SKIP_BITS_NE(lit1_len) 1965 1966 lit2_len = (lit1 >> (16 + 9)); 1967 if (lit2_len) 1968 lit2 = lit1 >> 16; 1969 else 1970 { 1971 assert(bit_buf_size >= FPNG_DEC 1972 lit2 = lit_table[bit_buf & (FPN 1973 lit2_len = (lit2 >> 9) & 15; 1974 if (!lit2_len) 1975 return false; 1976 } 1977 } 1978 1979 TOOLS_FPNG_SKIP_BITS(lit2_len); 1980 1981 // Check for matches 1982 if ((lit1 | lit2) & 256) 1983 return false; 1984 1985 if (dst_comps == 4) 1986 { 1987 if (pPrev_scanline) 1988 { 1989 pCur_scanline[x_ofs] = (uint8_t 1990 pCur_scanline[x_ofs + 1] = (uin 1991 pCur_scanline[x_ofs + 2] = (uin 1992 pCur_scanline[x_ofs + 3] = 0xFF 1993 } 1994 else 1995 { 1996 pCur_scanline[x_ofs] = (uint8_t 1997 pCur_scanline[x_ofs + 1] = (uin 1998 pCur_scanline[x_ofs + 2] = (uin 1999 pCur_scanline[x_ofs + 3] = 0xFF 2000 } 2001 x_ofs += 4; 2002 } 2003 else 2004 { 2005 if (pPrev_scanline) 2006 { 2007 pCur_scanline[x_ofs] = (uint8_t 2008 pCur_scanline[x_ofs + 1] = (uin 2009 pCur_scanline[x_ofs + 2] = (uin 2010 } 2011 else 2012 { 2013 pCur_scanline[x_ofs] = (uint8_t 2014 pCur_scanline[x_ofs + 1] = (uin 2015 pCur_scanline[x_ofs + 2] = (uin 2016 } 2017 x_ofs += 3; 2018 } 2019 2020 prev_delta_r = (uint8_t)lit0; 2021 prev_delta_g = (uint8_t)lit1; 2022 prev_delta_b = (uint8_t)lit2; 2023 2024 // See if we can decode one more pi 2025 uint32_t spec_next_len0_len = lit2 2026 if ((spec_next_len0_len) && (x_ofs 2027 { 2028 lit0 = (lit2 >> 16) & 511; 2029 if (lit0 < 256) 2030 { 2031 TOOLS_FPNG_SKIP_BITS_NE(spec_ne 2032 2033 assert(bit_buf_size >= FPNG_DEC 2034 lit1 = lit_table[bit_buf & (FPN 2035 uint32_t lit1_len = (lit1 >> 9) 2036 if (!lit1_len) 2037 return false; 2038 TOOLS_FPNG_SKIP_BITS(lit1_len); 2039 2040 lit2_len = (lit1 >> (16 + 9)); 2041 if (lit2_len) 2042 lit2 = lit1 >> 16; 2043 else 2044 { 2045 assert(bit_buf_size >= FPNG_D 2046 lit2 = lit_table[bit_buf & (F 2047 lit2_len = (lit2 >> 9) & 15; 2048 if (!lit2_len) 2049 return false; 2050 } 2051 2052 TOOLS_FPNG_SKIP_BITS_NE(lit2_le 2053 2054 // Check for matches 2055 if ((lit1 | lit2) & 256) 2056 return false; 2057 2058 if (dst_comps == 4) 2059 { 2060 if (pPrev_scanline) 2061 { 2062 pCur_scanline[x_ofs] = (uin 2063 pCur_scanline[x_ofs + 1] = 2064 pCur_scanline[x_ofs + 2] = 2065 pCur_scanline[x_ofs + 3] = 2066 } 2067 else 2068 { 2069 pCur_scanline[x_ofs] = (uin 2070 pCur_scanline[x_ofs + 1] = 2071 pCur_scanline[x_ofs + 2] = 2072 pCur_scanline[x_ofs + 3] = 2073 } 2074 x_ofs += 4; 2075 } 2076 else 2077 { 2078 if (pPrev_scanline) 2079 { 2080 pCur_scanline[x_ofs] = (uin 2081 pCur_scanline[x_ofs + 1] = 2082 pCur_scanline[x_ofs + 2] = 2083 } 2084 else 2085 { 2086 pCur_scanline[x_ofs] = (uin 2087 pCur_scanline[x_ofs + 1] = 2088 pCur_scanline[x_ofs + 2] = 2089 } 2090 x_ofs += 3; 2091 } 2092 2093 prev_delta_r = (uint8_t)lit0; 2094 prev_delta_g = (uint8_t)lit1; 2095 prev_delta_b = (uint8_t)lit2; 2096 2097 } // if (lit0 < 256) 2098 2099 } // if ((spec_next_len0_len) && (x 2100 } 2101 2102 } while (x_ofs < dst_bpl); 2103 2104 pPrev_scanline = pCur_scanline; 2105 pCur_scanline += dst_bpl; 2106 2107 } // y 2108 2109 // The last symbol should be EOB 2110 assert(bit_buf_size >= FPNG_DECODER_TABLE 2111 uint32_t lit0 = lit_table[bit_buf & (FPNG 2112 uint32_t lit0_len = (lit0 >> 9) & 15; 2113 if (!lit0_len) 2114 return false; 2115 lit0 &= 511; 2116 if (lit0 != 256) 2117 return false; 2118 2119 bit_buf_size -= lit0_len; 2120 bit_buf >>= lit0_len; 2121 2122 uint32_t align_bits = bit_buf_size & 7; 2123 bit_buf_size -= align_bits; 2124 bit_buf >>= align_bits; 2125 2126 if (src_ofs < (bit_buf_size >> 3)) 2127 return false; 2128 src_ofs -= (bit_buf_size >> 3); 2129 2130 // We should be at the very end, because 2131 if ((src_ofs + 4) != zlib_len) 2132 return false; 2133 2134 return true; 2135 } 2136 2137 template<uint32_t dst_comps> 2138 static bool fpng_pixel_zlib_decompress_4( 2139 const uint8_t* pSrc, uint32_t src_len, ui 2140 uint8_t* pDst, uint32_t w, uint32_t h) 2141 { 2142 assert(src_len >= (zlib_len + 4)); 2143 2144 const uint32_t dst_bpl = w * dst_comps; 2145 //const uint32_t dst_len = dst_bpl * h; 2146 2147 if (zlib_len < 7) 2148 return false; 2149 2150 // check zlib header 2151 if ((pSrc[0] != 0x78) || (pSrc[1] != 0x01 2152 return false; 2153 2154 uint32_t src_ofs = 2; 2155 2156 if ((pSrc[src_ofs] & 6) == 0) 2157 return fpng_pixel_zlib_raw_decompress(p 2158 2159 if ((src_ofs + 4) > src_len) 2160 return false; 2161 uint64_t bit_buf = READ_LE32(pSrc + src_o 2162 src_ofs += 4; 2163 2164 uint32_t bit_buf_size = 32; 2165 2166 uint32_t bfinal, btype; 2167 TOOLS_FPNG_GET_BITS(bfinal, 1); 2168 TOOLS_FPNG_GET_BITS(btype, 2); 2169 2170 // Must be the final block or it's not va 2171 if ((bfinal != 1) || (btype != 2)) 2172 return false; 2173 2174 uint32_t lit_table[FPNG_DECODER_TABLE_SIZ 2175 if (!prepare_dynamic_block(pSrc, src_len, 2176 return false; 2177 2178 const uint8_t* pPrev_scanline = NULL/*nul 2179 uint8_t* pCur_scanline = pDst; 2180 2181 for (uint32_t y = 0; y < h; y++) 2182 { 2183 // At start of PNG scanline, so read th 2184 assert(bit_buf_size >= FPNG_DECODER_TAB 2185 uint32_t filter = lit_table[bit_buf & ( 2186 uint32_t filter_len = (filter >> 9) & 1 2187 if (!filter_len) 2188 return false; 2189 TOOLS_FPNG_SKIP_BITS(filter_len); 2190 filter &= 511; 2191 2192 uint32_t expected_filter = (y ? 2 : 0); 2193 if (filter != expected_filter) 2194 return false; 2195 2196 uint32_t x_ofs = 0; 2197 uint8_t prev_delta_r = 0, prev_delta_g 2198 do 2199 { 2200 assert(bit_buf_size >= FPNG_DECODER_T 2201 uint32_t lit0_tab = lit_table[bit_buf 2202 2203 uint32_t lit0 = lit0_tab; 2204 uint32_t lit0_len = (lit0_tab >> 9) & 2205 if (!lit0_len) 2206 return false; 2207 TOOLS_FPNG_SKIP_BITS(lit0_len); 2208 2209 if (lit0 & 256) 2210 { 2211 lit0 &= 511; 2212 2213 // Can't be EOB - we still have mor 2214 if (lit0 == 256) 2215 return false; 2216 2217 // Must be an RLE match against the 2218 uint32_t run_len = s_length_range[l 2219 if (lit0 >= 265) 2220 { 2221 uint32_t e; 2222 TOOLS_FPNG_GET_BITS_NE(e, s_lengt 2223 2224 run_len += e; 2225 } 2226 2227 // Skip match distance - it's alway 2228 TOOLS_FPNG_SKIP_BITS_NE(1); 2229 2230 // Matches must always be a multipl 2231 if (run_len & 3) 2232 return false; 2233 2234 if (dst_comps == 3) 2235 { 2236 const uint32_t run_len3 = (run_le 2237 const uint32_t x_ofs_end = x_ofs 2238 2239 // Matches cannot cross scanlines 2240 if (x_ofs_end > dst_bpl) 2241 return false; 2242 2243 if (pPrev_scanline) 2244 { 2245 if ((prev_delta_r | prev_delta_ 2246 { 2247 memcpy(pCur_scanline + x_ofs, 2248 x_ofs = x_ofs_end; 2249 } 2250 else 2251 { 2252 do 2253 { 2254 pCur_scanline[x_ofs] = (uin 2255 pCur_scanline[x_ofs + 1] = 2256 pCur_scanline[x_ofs + 2] = 2257 x_ofs += 3; 2258 } while (x_ofs < x_ofs_end); 2259 } 2260 } 2261 else 2262 { 2263 do 2264 { 2265 pCur_scanline[x_ofs] = prev_d 2266 pCur_scanline[x_ofs + 1] = pr 2267 pCur_scanline[x_ofs + 2] = pr 2268 x_ofs += 3; 2269 } while (x_ofs < x_ofs_end); 2270 } 2271 } 2272 else 2273 { 2274 const uint32_t x_ofs_end = x_ofs 2275 2276 // Matches cannot cross scanlines 2277 if (x_ofs_end > dst_bpl) 2278 return false; 2279 2280 if (pPrev_scanline) 2281 { 2282 if ((prev_delta_r | prev_delta_ 2283 { 2284 memcpy(pCur_scanline + x_ofs, 2285 x_ofs = x_ofs_end; 2286 } 2287 else 2288 { 2289 do 2290 { 2291 pCur_scanline[x_ofs] = (uin 2292 pCur_scanline[x_ofs + 1] = 2293 pCur_scanline[x_ofs + 2] = 2294 pCur_scanline[x_ofs + 3] = 2295 x_ofs += 4; 2296 } while (x_ofs < x_ofs_end); 2297 } 2298 } 2299 else 2300 { 2301 do 2302 { 2303 pCur_scanline[x_ofs] = prev_d 2304 pCur_scanline[x_ofs + 1] = pr 2305 pCur_scanline[x_ofs + 2] = pr 2306 pCur_scanline[x_ofs + 3] = pr 2307 x_ofs += 4; 2308 } while (x_ofs < x_ofs_end); 2309 } 2310 } 2311 } 2312 else 2313 { 2314 uint32_t lit1, lit2; 2315 2316 uint32_t lit1_spec_len = (lit0_tab 2317 uint32_t lit2_len; 2318 if (lit1_spec_len) 2319 { 2320 lit1 = (lit0_tab >> 16) & 511; 2321 TOOLS_FPNG_SKIP_BITS_NE(lit1_spec 2322 2323 assert(bit_buf_size >= FPNG_DECOD 2324 lit2 = lit_table[bit_buf & (FPNG_ 2325 lit2_len = (lit2 >> 9) & 15; 2326 if (!lit2_len) 2327 return false; 2328 } 2329 else 2330 { 2331 assert(bit_buf_size >= FPNG_DECOD 2332 lit1 = lit_table[bit_buf & (FPNG_ 2333 uint32_t lit1_len = (lit1 >> 9) & 2334 if (!lit1_len) 2335 return false; 2336 TOOLS_FPNG_SKIP_BITS_NE(lit1_len) 2337 2338 lit2_len = (lit1 >> (16 + 9)); 2339 if (lit2_len) 2340 lit2 = lit1 >> 16; 2341 else 2342 { 2343 assert(bit_buf_size >= FPNG_DEC 2344 lit2 = lit_table[bit_buf & (FPN 2345 lit2_len = (lit2 >> 9) & 15; 2346 if (!lit2_len) 2347 return false; 2348 } 2349 } 2350 2351 uint32_t lit3; 2352 uint32_t lit3_len = lit2 >> (16 + 9 2353 2354 if (lit3_len) 2355 { 2356 lit3 = (lit2 >> 16); 2357 TOOLS_FPNG_SKIP_BITS(lit2_len + l 2358 } 2359 else 2360 { 2361 TOOLS_FPNG_SKIP_BITS(lit2_len); 2362 2363 assert(bit_buf_size >= FPNG_DECOD 2364 lit3 = lit_table[bit_buf & (FPNG_ 2365 lit3_len = (lit3 >> 9) & 15; 2366 if (!lit3_len) 2367 return false; 2368 2369 TOOLS_FPNG_SKIP_BITS_NE(lit3_len) 2370 } 2371 2372 // Check for matches 2373 if ((lit1 | lit2 | lit3) & 256) 2374 return false; 2375 2376 if (dst_comps == 3) 2377 { 2378 if (pPrev_scanline) 2379 { 2380 pCur_scanline[x_ofs] = (uint8_t 2381 pCur_scanline[x_ofs + 1] = (uin 2382 pCur_scanline[x_ofs + 2] = (uin 2383 } 2384 else 2385 { 2386 pCur_scanline[x_ofs] = (uint8_t 2387 pCur_scanline[x_ofs + 1] = (uin 2388 pCur_scanline[x_ofs + 2] = (uin 2389 } 2390 2391 x_ofs += 3; 2392 } 2393 else 2394 { 2395 if (pPrev_scanline) 2396 { 2397 pCur_scanline[x_ofs] = (uint8_t 2398 pCur_scanline[x_ofs + 1] = (uin 2399 pCur_scanline[x_ofs + 2] = (uin 2400 pCur_scanline[x_ofs + 3] = (uin 2401 } 2402 else 2403 { 2404 pCur_scanline[x_ofs] = (uint8_t 2405 pCur_scanline[x_ofs + 1] = (uin 2406 pCur_scanline[x_ofs + 2] = (uin 2407 pCur_scanline[x_ofs + 3] = (uin 2408 } 2409 2410 x_ofs += 4; 2411 } 2412 2413 prev_delta_r = (uint8_t)lit0; 2414 prev_delta_g = (uint8_t)lit1; 2415 prev_delta_b = (uint8_t)lit2; 2416 prev_delta_a = (uint8_t)lit3; 2417 } 2418 2419 } while (x_ofs < dst_bpl); 2420 2421 pPrev_scanline = pCur_scanline; 2422 pCur_scanline += dst_bpl; 2423 } // y 2424 2425 // The last symbol should be EOB 2426 assert(bit_buf_size >= FPNG_DECODER_TABLE 2427 uint32_t lit0 = lit_table[bit_buf & (FPNG 2428 uint32_t lit0_len = (lit0 >> 9) & 15; 2429 if (!lit0_len) 2430 return false; 2431 lit0 &= 511; 2432 if (lit0 != 256) 2433 return false; 2434 2435 bit_buf_size -= lit0_len; 2436 bit_buf >>= lit0_len; 2437 2438 uint32_t align_bits = bit_buf_size & 7; 2439 bit_buf_size -= align_bits; 2440 bit_buf >>= align_bits; 2441 2442 if (src_ofs < (bit_buf_size >> 3)) 2443 return false; 2444 src_ofs -= (bit_buf_size >> 3); 2445 2446 // We should be at the very end, because 2447 if ((src_ofs + 4) != zlib_len) 2448 return false; 2449 2450 return true; 2451 } 2452 2453 #pragma pack(push) 2454 #pragma pack(1) 2455 struct png_chunk_prefix 2456 { 2457 uint32_t m_length; 2458 uint8_t m_type[4]; 2459 }; 2460 struct png_ihdr 2461 { 2462 png_chunk_prefix m_prefix; 2463 uint32_t m_width; 2464 uint32_t m_height; 2465 uint8_t m_bitdepth; 2466 uint8_t m_color_type; 2467 uint8_t m_comp_method; 2468 uint8_t m_filter_method; 2469 uint8_t m_interlace_method; 2470 uint32_t m_crc32; 2471 }; 2472 const uint32_t IHDR_EXPECTED_LENGTH = 13; 2473 struct png_iend 2474 { 2475 png_chunk_prefix m_prefix; 2476 uint32_t m_crc32; 2477 }; 2478 #pragma pack(pop) 2479 2480 static int fpng_get_info_internal(const voi 2481 { 2482 static const uint8_t s_png_sig[8] = { 137 2483 2484 if (!endian_check()) 2485 { 2486 assert(0); 2487 return false; 2488 } 2489 2490 width = 0; 2491 height = 0; 2492 channels_in_file = 0; 2493 idat_ofs = 0, idat_len = 0; 2494 2495 // Ensure the file has at least a minimum 2496 if (image_size < (sizeof(s_png_sig) + siz 2497 return FPNG_DECODE_FAILED_NOT_PNG; 2498 2499 if (memcmp(pImage, s_png_sig, 8) != 0) 2500 return FPNG_DECODE_FAILED_NOT_PNG; 2501 2502 const uint8_t* pImage_u8 = static_cast<co 2503 2504 const png_ihdr& ihdr = *reinterpret_cast< 2505 pImage_u8 += sizeof(png_ihdr); 2506 2507 if (READ_BE32(&ihdr.m_prefix.m_length) != 2508 return FPNG_DECODE_FAILED_NOT_PNG; 2509 2510 if (fpng_crc32(ihdr.m_prefix.m_type, 4 + 2511 return FPNG_DECODE_FAILED_HEADER_CRC32; 2512 2513 width = READ_BE32(&ihdr.m_width); 2514 height = READ_BE32(&ihdr.m_height); 2515 2516 if (!width || !height || (width > FPNG_MA 2517 return FPNG_DECODE_FAILED_INVALID_DIMEN 2518 2519 uint64_t total_pixels = (uint64_t)width * 2520 if (total_pixels > (1 << 30)) 2521 return FPNG_DECODE_FAILED_INVALID_DIMEN 2522 2523 if ((ihdr.m_comp_method) || (ihdr.m_filte 2524 return FPNG_DECODE_NOT_FPNG; 2525 2526 if (ihdr.m_color_type == 2) 2527 channels_in_file = 3; 2528 else if (ihdr.m_color_type == 6) 2529 channels_in_file = 4; 2530 2531 if (!channels_in_file) 2532 return FPNG_DECODE_NOT_FPNG; 2533 2534 // Scan all the chunks. Look for one IDAT 2535 bool found_fdec_chunk = false; 2536 2537 for (; ; ) 2538 { 2539 const size_t src_ofs = pImage_u8 - stat 2540 if (src_ofs >= image_size) 2541 return FPNG_DECODE_FAILED_CHUNK_PARSI 2542 2543 const uint32_t bytes_remaining = image_ 2544 if (bytes_remaining < sizeof(uint32_t) 2545 return FPNG_DECODE_FAILED_CHUNK_PARSI 2546 2547 const png_chunk_prefix* pChunk = reinte 2548 2549 const uint32_t chunk_len = READ_BE32(&p 2550 if ((src_ofs + sizeof(uint32_t) + chunk 2551 return FPNG_DECODE_FAILED_CHUNK_PARSI 2552 2553 for (uint32_t i = 0; i < 4; i++) 2554 { 2555 const uint8_t c = pChunk->m_type[i]; 2556 const bool is_upper = (c >= 65) && (c 2557 if ((!is_upper) && (!is_lower)) 2558 return FPNG_DECODE_FAILED_CHUNK_PAR 2559 } 2560 2561 char chunk_type[5] = { (char)pChunk->m_ 2562 const bool is_idat = strcmp(chunk_type, 2563 2564 const uint8_t* pChunk_data = pImage_u8 2565 2566 if (strcmp(chunk_type, "IEND") == 0) 2567 break; 2568 else if (is_idat) 2569 { 2570 // If there were multiple IDAT's, or 2571 if ((idat_ofs) || (!found_fdec_chunk) 2572 return FPNG_DECODE_NOT_FPNG; 2573 2574 idat_ofs = (uint32_t)src_ofs; 2575 idat_len = chunk_len; 2576 2577 // Sanity check the IDAT chunk length 2578 if (idat_len < 7) 2579 return FPNG_DECODE_FAILED_INVALID_I 2580 } 2581 else if (strcmp(chunk_type, "fdEC") == 2582 { 2583 if (found_fdec_chunk) 2584 return FPNG_DECODE_NOT_FPNG; 2585 2586 // We've got our fdEC chunk. Now make 2587 if (chunk_len != 5) 2588 return FPNG_DECODE_NOT_FPNG; 2589 2590 // Check fdEC chunk sig 2591 if ((pChunk_data[0] != 82) || (pChunk 2592 return FPNG_DECODE_NOT_FPNG; 2593 2594 // Check fdEC version 2595 if (pChunk_data[4] != FPNG_FDEC_VERSI 2596 return FPNG_DECODE_NOT_FPNG; 2597 2598 found_fdec_chunk = true; 2599 } 2600 else 2601 { 2602 // Bail if it's a critical chunk - ca 2603 if ((chunk_type[0] & 32) == 0) 2604 return FPNG_DECODE_NOT_FPNG; 2605 2606 // ancillary chunk - skip it 2607 } 2608 2609 pImage_u8 += sizeof(png_chunk_prefix) + 2610 } 2611 2612 if ((!found_fdec_chunk) || (!idat_ofs)) 2613 return FPNG_DECODE_NOT_FPNG; 2614 2615 return FPNG_DECODE_SUCCESS; 2616 } 2617 2618 inline int fpng_get_info(const void* pImage 2619 { 2620 uint32_t idat_ofs = 0, idat_len = 0; 2621 return fpng_get_info_internal(pImage, ima 2622 } 2623 2624 inline int fpng_decode_memory(const void *p 2625 { 2626 out.resize(0); 2627 width = 0; 2628 height = 0; 2629 channels_in_file = 0; 2630 2631 if ((!pImage) || (!image_size) || ((desir 2632 { 2633 assert(0); 2634 return FPNG_DECODE_INVALID_ARG; 2635 } 2636 2637 uint32_t idat_ofs = 0, idat_len = 0; 2638 int status = fpng_get_info_internal(pImag 2639 if (status) 2640 return status; 2641 2642 const uint64_t mem_needed = (uint64_t)wid 2643 if (mem_needed > UINT32_MAX) 2644 return FPNG_DECODE_FAILED_DIMENSIONS_TO 2645 2646 // On 32-bit systems do a quick sanity ch 2647 if ((sizeof(size_t) == sizeof(uint32_t)) 2648 return FPNG_DECODE_FAILED_DIMENSIONS_TO 2649 2650 out.resize(mem_needed); 2651 2652 const uint8_t* pIDAT_data = static_cast<c 2653 const uint32_t src_len = image_size - (id 2654 2655 bool decomp_status; 2656 if (desired_channels == 3) 2657 { 2658 if (channels_in_file == 3) 2659 decomp_status = fpng_pixel_zlib_decom 2660 else 2661 decomp_status = fpng_pixel_zlib_decom 2662 } 2663 else 2664 { 2665 if (channels_in_file == 3) 2666 decomp_status = fpng_pixel_zlib_decom 2667 else 2668 decomp_status = fpng_pixel_zlib_decom 2669 } 2670 if (!decomp_status) 2671 { 2672 // Something went wrong. Either the fil 2673 // The conservative thing to do is indi 2674 return FPNG_DECODE_NOT_FPNG; 2675 } 2676 2677 return FPNG_DECODE_SUCCESS; 2678 } 2679 2680 inline int fpng_decode_file(const char* pFi 2681 { 2682 FILE* pFile = fopen(pFilename 2683 if (!pFile) return FPNG_DECOD 2684 2685 if (fseek(pFile, 0, SEEK_END) != 0) 2686 { 2687 fclose(pFile); 2688 return FPNG_DECODE_FILE_SEEK_FAILED; 2689 } 2690 2691 #ifdef _MSC_VER 2692 int64_t filesize = _ftelli64(pFile); 2693 #else 2694 int64_t filesize = ftello(pFile); 2695 #endif 2696 2697 if (fseek(pFile, 0, SEEK_SET) != 0) 2698 { 2699 fclose(pFile); 2700 return FPNG_DECODE_FILE_SEEK_FAILED; 2701 } 2702 2703 if ( (filesize < 0) || (filesize > UINT32 2704 { 2705 fclose(pFile); 2706 return FPNG_DECODE_FILE_TOO_LARGE; 2707 } 2708 2709 std::vector<uint8_t> buf((size_t)filesize 2710 if (fread(buf.data(), 1, buf.size(), pFil 2711 { 2712 fclose(pFile); 2713 return FPNG_DECODE_FILE_READ_FAILED; 2714 } 2715 2716 fclose(pFile); 2717 2718 return fpng_decode_memory(buf.data(), (ui 2719 } 2720 2721 #undef TOOLS_FPNG_PUT_BITS 2722 #undef TOOLS_FPNG_PUT_BITS_CZ 2723 #undef TOOLS_FPNG_PUT_BITS_FLUSH 2724 #undef TOOLS_FPNG_PUT_BITS_FORCE_FLUSH 2725 #undef TOOLS_FPNG_DEFL_CLEAR_OBJ 2726 #undef TOOLS_FPNG_DEFL_RLE_PREV_CODE_SIZE 2727 #undef TOOLS_FPNG_DEFL_RLE_ZERO_CODE_SIZE 2728 #undef TOOLS_FPNG_DEFL_DYN_PUT_BITS 2729 #undef TOOLS_FPNG_ENSURE_32BITS 2730 #undef TOOLS_FPNG_GET_BITS 2731 #undef TOOLS_FPNG_SKIP_BITS 2732 #undef TOOLS_FPNG_GET_BITS_NE 2733 #undef TOOLS_FPNG_SKIP_BITS_NE 2734 2735 } // namespace fpng 2736 } // namespace tools 2737 2738 /* 2739 This is free and unencumbered software rele 2740 2741 Anyone is free to copy, modify, publish, us 2742 distribute this software, either in source 2743 binary, for any purpose, commercial or non- 2744 means. 2745 2746 In jurisdictions that recognize copyright l 2747 of this software dedicate any and all copyr 2748 software to the public domain. We make this 2749 of the public at large and to the detriment 2750 successors. We intend this dedication to be 2751 relinquishment in perpetuity of all present 2752 software under copyright law. 2753 2754 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT W 2755 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMIT 2756 MERCHANTABILITY, FITNESS FOR A PARTICULAR P 2757 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR 2758 OTHER LIABILITY, WHETHER IN AN ACTION OF CO 2759 ARISING FROM, OUT OF OR IN CONNECTION WITH 2760 OTHER DEALINGS IN THE SOFTWARE. 2761 2762 For more information, please refer to <http 2763 2764 Richard Geldreich, Jr. 2765 12/30/2021 2766 */ 2767