The open source OpenXR runtime

external/stb: Update stb libraries from upstream

GitOrigin-RevId: 1ee679ca2ef753a528db5ba6801e1067b40481b8

+95 -37
+95 -37
src/external/stb/stb_image_write.h
··· 1 - /* stb_image_write - v1.14 - public domain - http://nothings.org/stb 1 + /* stb_image_write - v1.16 - public domain - http://nothings.org/stb 2 2 writes out PNG/BMP/TGA/JPEG/HDR images to C stdio - Sean Barrett 2010-2015 3 3 no warranty implied; use at your own risk 4 4 ··· 140 140 Ivan Tikhonov 141 141 github:ignotion 142 142 Adam Schackart 143 + Andrew Kensler 143 144 144 145 LICENSE 145 146 ··· 166 167 #endif 167 168 168 169 #ifndef STB_IMAGE_WRITE_STATIC // C++ forbids static forward declarations 169 - extern int stbi_write_tga_with_rle; 170 - extern int stbi_write_png_compression_level; 171 - extern int stbi_write_force_png_filter; 170 + STBIWDEF int stbi_write_tga_with_rle; 171 + STBIWDEF int stbi_write_png_compression_level; 172 + STBIWDEF int stbi_write_force_png_filter; 172 173 #endif 173 174 174 175 #ifndef STBI_WRITE_NO_STDIO ··· 178 179 STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data); 179 180 STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality); 180 181 181 - #ifdef STBI_WINDOWS_UTF8 182 + #ifdef STBIW_WINDOWS_UTF8 182 183 STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input); 183 184 #endif 184 185 #endif ··· 267 268 { 268 269 stbi_write_func *func; 269 270 void *context; 271 + unsigned char buffer[64]; 272 + int buf_used; 270 273 } stbi__write_context; 271 274 272 275 // initialize a callback-based context ··· 283 286 fwrite(data,1,size,(FILE*) context); 284 287 } 285 288 286 - #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8) 289 + #if defined(_WIN32) && defined(STBIW_WINDOWS_UTF8) 287 290 #ifdef __cplusplus 288 291 #define STBIW_EXTERN extern "C" 289 292 #else ··· 294 297 295 298 STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input) 296 299 { 297 - return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL); 300 + return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL); 298 301 } 299 302 #endif 300 303 301 304 static FILE *stbiw__fopen(char const *filename, char const *mode) 302 305 { 303 306 FILE *f; 304 - #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8) 307 + #if defined(_WIN32) && defined(STBIW_WINDOWS_UTF8) 305 308 wchar_t wMode[64]; 306 309 wchar_t wFilename[1024]; 307 - if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename))) 310 + if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)/sizeof(*wFilename))) 308 311 return 0; 309 312 310 - if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode))) 313 + if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)/sizeof(*wMode))) 311 314 return 0; 312 315 313 - #if _MSC_VER >= 1400 314 - if (0 != _wfopen_s(&f, wFilename, wMode)) 315 - f = 0; 316 + #if defined(_MSC_VER) && _MSC_VER >= 1400 317 + if (0 != _wfopen_s(&f, wFilename, wMode)) 318 + f = 0; 316 319 #else 317 320 f = _wfopen(wFilename, wMode); 318 321 #endif ··· 380 383 va_end(v); 381 384 } 382 385 386 + static void stbiw__write_flush(stbi__write_context *s) 387 + { 388 + if (s->buf_used) { 389 + s->func(s->context, &s->buffer, s->buf_used); 390 + s->buf_used = 0; 391 + } 392 + } 393 + 383 394 static void stbiw__putc(stbi__write_context *s, unsigned char c) 384 395 { 385 396 s->func(s->context, &c, 1); 386 397 } 387 398 399 + static void stbiw__write1(stbi__write_context *s, unsigned char a) 400 + { 401 + if ((size_t)s->buf_used + 1 > sizeof(s->buffer)) 402 + stbiw__write_flush(s); 403 + s->buffer[s->buf_used++] = a; 404 + } 405 + 388 406 static void stbiw__write3(stbi__write_context *s, unsigned char a, unsigned char b, unsigned char c) 389 407 { 390 - unsigned char arr[3]; 391 - arr[0] = a; arr[1] = b; arr[2] = c; 392 - s->func(s->context, arr, 3); 408 + int n; 409 + if ((size_t)s->buf_used + 3 > sizeof(s->buffer)) 410 + stbiw__write_flush(s); 411 + n = s->buf_used; 412 + s->buf_used = n+3; 413 + s->buffer[n+0] = a; 414 + s->buffer[n+1] = b; 415 + s->buffer[n+2] = c; 393 416 } 394 417 395 418 static void stbiw__write_pixel(stbi__write_context *s, int rgb_dir, int comp, int write_alpha, int expand_mono, unsigned char *d) ··· 398 421 int k; 399 422 400 423 if (write_alpha < 0) 401 - s->func(s->context, &d[comp - 1], 1); 424 + stbiw__write1(s, d[comp - 1]); 402 425 403 426 switch (comp) { 404 427 case 2: // 2 pixels = mono + alpha, alpha is written separately, so same as 1-channel case ··· 406 429 if (expand_mono) 407 430 stbiw__write3(s, d[0], d[0], d[0]); // monochrome bmp 408 431 else 409 - s->func(s->context, d, 1); // monochrome TGA 432 + stbiw__write1(s, d[0]); // monochrome TGA 410 433 break; 411 434 case 4: 412 435 if (!write_alpha) { ··· 422 445 break; 423 446 } 424 447 if (write_alpha > 0) 425 - s->func(s->context, &d[comp - 1], 1); 448 + stbiw__write1(s, d[comp - 1]); 426 449 } 427 450 428 451 static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, void *data, int write_alpha, int scanline_pad, int expand_mono) ··· 447 470 unsigned char *d = (unsigned char *) data + (j*x+i)*comp; 448 471 stbiw__write_pixel(s, rgb_dir, comp, write_alpha, expand_mono, d); 449 472 } 473 + stbiw__write_flush(s); 450 474 s->func(s->context, &zero, scanline_pad); 451 475 } 452 476 } ··· 467 491 468 492 static int stbi_write_bmp_core(stbi__write_context *s, int x, int y, int comp, const void *data) 469 493 { 470 - int pad = (-x*3) & 3; 471 - return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *) data,0,pad, 472 - "11 4 22 4" "4 44 22 444444", 473 - 'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40, // file header 474 - 40, x,y, 1,24, 0,0,0,0,0,0); // bitmap header 494 + if (comp != 4) { 495 + // write RGB bitmap 496 + int pad = (-x*3) & 3; 497 + return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *) data,0,pad, 498 + "11 4 22 4" "4 44 22 444444", 499 + 'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40, // file header 500 + 40, x,y, 1,24, 0,0,0,0,0,0); // bitmap header 501 + } else { 502 + // RGBA bitmaps need a v4 header 503 + // use BI_BITFIELDS mode with 32bpp and alpha mask 504 + // (straight BI_RGB with alpha mask doesn't work in most readers) 505 + return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *)data,1,0, 506 + "11 4 22 4" "4 44 22 444444 4444 4 444 444 444 444", 507 + 'B', 'M', 14+108+x*y*4, 0, 0, 14+108, // file header 508 + 108, x,y, 1,32, 3,0,0,0,0,0, 0xff0000,0xff00,0xff,0xff000000u, 0, 0,0,0, 0,0,0, 0,0,0, 0,0,0); // bitmap V4 header 509 + } 475 510 } 476 511 477 512 STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data) 478 513 { 479 - stbi__write_context s; 514 + stbi__write_context s = { 0 }; 480 515 stbi__start_write_callbacks(&s, func, context); 481 516 return stbi_write_bmp_core(&s, x, y, comp, data); 482 517 } ··· 484 519 #ifndef STBI_WRITE_NO_STDIO 485 520 STBIWDEF int stbi_write_bmp(char const *filename, int x, int y, int comp, const void *data) 486 521 { 487 - stbi__write_context s; 522 + stbi__write_context s = { 0 }; 488 523 if (stbi__start_write_file(&s,filename)) { 489 524 int r = stbi_write_bmp_core(&s, x, y, comp, data); 490 525 stbi__end_write_file(&s); ··· 557 592 558 593 if (diff) { 559 594 unsigned char header = STBIW_UCHAR(len - 1); 560 - s->func(s->context, &header, 1); 595 + stbiw__write1(s, header); 561 596 for (k = 0; k < len; ++k) { 562 597 stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin + k * comp); 563 598 } 564 599 } else { 565 600 unsigned char header = STBIW_UCHAR(len - 129); 566 - s->func(s->context, &header, 1); 601 + stbiw__write1(s, header); 567 602 stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin); 568 603 } 569 604 } 570 605 } 606 + stbiw__write_flush(s); 571 607 } 572 608 return 1; 573 609 } 574 610 575 611 STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data) 576 612 { 577 - stbi__write_context s; 613 + stbi__write_context s = { 0 }; 578 614 stbi__start_write_callbacks(&s, func, context); 579 615 return stbi_write_tga_core(&s, x, y, comp, (void *) data); 580 616 } ··· 582 618 #ifndef STBI_WRITE_NO_STDIO 583 619 STBIWDEF int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data) 584 620 { 585 - stbi__write_context s; 621 + stbi__write_context s = { 0 }; 586 622 if (stbi__start_write_file(&s,filename)) { 587 623 int r = stbi_write_tga_core(&s, x, y, comp, (void *) data); 588 624 stbi__end_write_file(&s); ··· 597 633 // by Baldur Karlsson 598 634 599 635 #define stbiw__max(a, b) ((a) > (b) ? (a) : (b)) 636 + 637 + #ifndef STBI_WRITE_NO_STDIO 600 638 601 639 static void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear) 602 640 { ··· 732 770 char header[] = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n"; 733 771 s->func(s->context, header, sizeof(header)-1); 734 772 735 - #ifdef __STDC_WANT_SECURE_LIB__ 773 + #ifdef __STDC_LIB_EXT1__ 736 774 len = sprintf_s(buffer, sizeof(buffer), "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x); 737 775 #else 738 776 len = sprintf(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x); ··· 748 786 749 787 STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const float *data) 750 788 { 751 - stbi__write_context s; 789 + stbi__write_context s = { 0 }; 752 790 stbi__start_write_callbacks(&s, func, context); 753 791 return stbi_write_hdr_core(&s, x, y, comp, (float *) data); 754 792 } 755 793 756 - #ifndef STBI_WRITE_NO_STDIO 757 794 STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data) 758 795 { 759 - stbi__write_context s; 796 + stbi__write_context s = { 0 }; 760 797 if (stbi__start_write_file(&s,filename)) { 761 798 int r = stbi_write_hdr_core(&s, x, y, comp, (float *) data); 762 799 stbi__end_write_file(&s); ··· 943 980 for (i=0; i < stbiw__ZHASH; ++i) 944 981 (void) stbiw__sbfree(hash_table[i]); 945 982 STBIW_FREE(hash_table); 983 + 984 + // store uncompressed instead if compression was worse 985 + if (stbiw__sbn(out) > data_len + 2 + ((data_len+32766)/32767)*5) { 986 + stbiw__sbn(out) = 2; // truncate to DEFLATE 32K window and FLEVEL = 1 987 + for (j = 0; j < data_len;) { 988 + int blocklen = data_len - j; 989 + if (blocklen > 32767) blocklen = 32767; 990 + stbiw__sbpush(out, data_len - j == blocklen); // BFINAL = ?, BTYPE = 0 -- no compression 991 + stbiw__sbpush(out, STBIW_UCHAR(blocklen)); // LEN 992 + stbiw__sbpush(out, STBIW_UCHAR(blocklen >> 8)); 993 + stbiw__sbpush(out, STBIW_UCHAR(~blocklen)); // NLEN 994 + stbiw__sbpush(out, STBIW_UCHAR(~blocklen >> 8)); 995 + memcpy(out+stbiw__sbn(out), data+j, blocklen); 996 + stbiw__sbn(out) += blocklen; 997 + j += blocklen; 998 + } 999 + } 946 1000 947 1001 { 948 1002 // compute adler32 on input ··· 1552 1606 1553 1607 STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality) 1554 1608 { 1555 - stbi__write_context s; 1609 + stbi__write_context s = { 0 }; 1556 1610 stbi__start_write_callbacks(&s, func, context); 1557 1611 return stbi_write_jpg_core(&s, x, y, comp, (void *) data, quality); 1558 1612 } ··· 1561 1615 #ifndef STBI_WRITE_NO_STDIO 1562 1616 STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality) 1563 1617 { 1564 - stbi__write_context s; 1618 + stbi__write_context s = { 0 }; 1565 1619 if (stbi__start_write_file(&s,filename)) { 1566 1620 int r = stbi_write_jpg_core(&s, x, y, comp, data, quality); 1567 1621 stbi__end_write_file(&s); ··· 1574 1628 #endif // STB_IMAGE_WRITE_IMPLEMENTATION 1575 1629 1576 1630 /* Revision history 1631 + 1.16 (2021-07-11) 1632 + make Deflate code emit uncompressed blocks when it would otherwise expand 1633 + support writing BMPs with alpha channel 1634 + 1.15 (2020-07-13) unknown 1577 1635 1.14 (2020-02-02) updated JPEG writer to downsample chroma channels 1578 1636 1.13 1579 1637 1.12 ··· 1611 1669 add HDR output 1612 1670 fix monochrome BMP 1613 1671 0.95 (2014-08-17) 1614 - add monochrome TGA output 1672 + add monochrome TGA output 1615 1673 0.94 (2014-05-31) 1616 1674 rename private functions to avoid conflicts with stb_image.h 1617 1675 0.93 (2014-05-27)