Fix handling of iphone-procesed image to not accidentally appear corrupt (instead they just load wrong).
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add a proper testing path to image test
authored by Sean Barrett · committed by Sean Barrett · parent 687985d · Browse files · 3 files·+43 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+43 added−14 removed
- tests:+34 added−8 removed
- other:+9 added−6 removed
Provenance
- Commit
334cec8- Tree
dca7103- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent
687985d
3 files changed+43 added−14 removed
modified:tests/image_test/image_test.dsp+1 added-5 removed
39 unmodified lines40 # PROP Output_Dir "Release"41 # PROP Intermediate_Dir "Release"42 # PROP Target_Dir ""43 # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c44-# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c44+# ADD CPP /nologo /W3 /GX /O2 /I "..\.." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c45 # ADD BASE RSC /l 0x409 /d "NDEBUG"46 # ADD RSC /l 0x409 /d "NDEBUG"47 BSC32=bscmake.exe48 # ADD BASE BSC32 /nologo34 unmodified lines83 # Begin Source File84 85 SOURCE=..\image_test.c86 # End Source File87-# Begin Source File88-89-SOURCE=..\..\stb_image_write.h90-# End Source File87 # End Target88 # End Projectmodified:tests/image_test.c+33 added-3 removed
2 unmodified lines3 4 #define STB_IMAGE_IMPLEMENTATION5 #include "stb_image.h"6 7+#define STB_DEFINE8+#include "stb.h"9 10 int main(int argc, char **argv)11 {12 int w,h;11- unsigned char *data = stbi_load(argv[1], &w, &h, 0, 4);12- if (data)13- stbi_write_png("c:/x/result.png", w, h, 4, data, w*4);13+ if (argc > 1) {14+ int i;15+ for (i=1; i < argc; ++i) {16+ unsigned char *data;17+ printf("%s\n", argv[i]);18+ data = stbi_load(argv[i], &w, &h, 0, 4);19+ assert(data);20+ if (data) {21+ char fname[512];22+ stb_splitpath(fname, argv[i], STB_FILE);23+ stbi_write_png(stb_sprintf("output/%s.png", fname), w, h, 4, data, w*4);24+ free(data);25+ }26+ }27+ } else {28+ int i;29+ char **files = stb_readdir_files("images");30+ for (i=0; i < stb_arr_len(files); ++i) {31+ unsigned char *data;32+ printf("%s\n", files[i]);33+ data = stbi_load(files[i], &w, &h, 0, 4);34+ //assert(data);35+ if (data) {36+ char fname[512];37+ stb_splitpath(fname, files[i], STB_FILE);38+ stbi_write_png(stb_sprintf("output/%s.png", fname), w, h, 4, data, w*4);39+ free(data);40+ } else41+ printf("FAILED\n");42+ }43+ }44 return 0;45 }modified:stb_image.h+9 added-6 removed
24 unmodified lines25 - stbi__jpeg_huff_decode from arbitrary I/O callbacks26 - overridable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD)27 28 Latest revisions:29- 1.36 (2014-05-30) converted to header file29+ 1.36 (2014-06-03) converted to header file, allow reading incorrect iphoned-images without iphone flag30 1.35 (2014-05-27) warnings, bugfixes, TGA optimization, etc31 1.34 (unknown ) warning fix32 1.33 (2011-07-14) minor fixes suggested by Dave Moore33 1.32 (2011-07-13) info support for all filetypes (SpartanJ)2663 unmodified lines2697 {2698 stbi_uc palette[1024], pal_img_n=0;2699 stbi_uc has_trans=0, tc[3];2700 stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0;2701- int first=1,k,interlace=0, iphone=0;2701+ int first=1,k,interlace=0, is_iphone=0;2702 stbi__context *s = z->s;2703 2704 z->expanded = NULL;2705 z->idata = NULL;6 unmodified lines2712 for (;;) {2713 stbi__pngchunk c = stbi__get_chunk_header(s);2714 switch (c.type) {2715 case PNG_TYPE('C','g','B','I'):2716- iphone = stbi__de_iphone_flag;2716+ is_iphone = 1;2717 stbi__skip(s, c.length);2718 break;2719 case PNG_TYPE('I','H','D','R'): {2720 int depth,color,comp,filter;78 unmodified lines2799 stbi__uint32 raw_len;2800 if (first) return stbi__err("first not IHDR", "Corrupt PNG");2801 if (scan != SCAN_load) return 1;2802 if (z->idata == NULL) return stbi__err("no IDAT","Corrupt PNG");2803- z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, 16384, (int *) &raw_len, !iphone);2803+ z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, 16384, (int *) &raw_len, !is_iphone);2804 if (z->expanded == NULL) return 0; // zlib should set error2805 free(z->idata); z->idata = NULL;2806 if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)2807 s->img_out_n = s->img_n+1;1 unmodified line2809 s->img_out_n = s->img_n;2810 if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, interlace)) return 0;2811 if (has_trans)2812 if (!stbi__compute_transparency(z, tc, s->img_out_n)) return 0;2813- if (iphone && s->img_out_n > 2)2813+ if (is_iphone && stbi__de_iphone_flag && s->img_out_n > 2)2814 stbi__de_iphone(z);2815 if (pal_img_n) {2816 // pal_img_n == 3 or 42817 s->img_n = pal_img_n; // record the actual colors we had1431 unmodified lines4249 4250 *x = width;4251 *y = height;4252 4253- *comp = 3;4253+ if (comp) *comp = 3;4254 if (req_comp == 0) req_comp = 3;4255 4256 // Read data4257 hdr_data = (float *) malloc(height * width * req_comp * sizeof(float));276 unmodified lines4534 4535 4536 /*4537 revision history:4538+ 1.36 (2014-06-03)4539+ convert to header file single-file library4540+ if de-iphone isn't set, load iphone images color-swapped instead of returning NULL4541 1.35 (2014-05-27)4542 various warnings4543 fix broken STBI_SIMD path4544 fix bug where stbi_load_from_file no longer left file pointer in correct place88 unmodified lines