rewrite HDR rle logic
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parent 80d5c4b · Browse files · 2 files·+54 added−45 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+54 added−45 removed
- tests:+14 added−0 removed
- other:+40 added−45 removed
Provenance
- Commit
22fa9a4- Tree
84fe8c6- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent
80d5c4b
2 files changed+54 added−45 removed
modified:tests/image_test.c+14 added
49 unmodified lines50 printf("Final: %d (%d big) of %d\n", count, bigcount, total);51 }52 #endif53 54+float hdr_data[200][200][3];55+56 int main(int argc, char **argv)57 {58 int w,h;59 //test_ycbcr();60+61+ #if 062+ // test hdr asserts63+ for (h=0; h < 100; h += 2)64+ for (w=0; w < 200; ++w)65+ hdr_data[h][w][0] = (float) rand(),66+ hdr_data[h][w][1] = (float) rand(),67+ hdr_data[h][w][2] = (float) rand();68+69+ stbi_write_hdr("output/test.hdr", 200,200,3,hdr_data[0][0]);70+ #endif71+72 if (argc > 1) {73 int i, n;74 75 for (i=1; i < argc; ++i) {74 unmodified linesmodified:stb_image_write.h+40 added-45 removed
1-/* stb_image_write - v0.96 - public domain - http://nothings.org/stb/stb_image_write.h1+/* stb_image_write - v0.97 - public domain - http://nothings.org/stb/stb_image_write.h2 writes out PNG/BMP/TGA images to C stdio - Sean Barrett 20103 no warranty implied; use at your own risk4 5 222 unmodified lines228 rgbe[3] = (unsigned char)(exponent + 128);229 }230 }231 232-void stbiw__write_rle_data(FILE *f, int length, unsigned char databyte)232+void stbiw__write_run_data(FILE *f, int length, unsigned char databyte)233 {234- unsigned char lengthbyte = 0x80 | (unsigned char)(length & 0x7f);235- assert(length <= 0x7f);234+ unsigned char lengthbyte = (unsigned char) (length+128);235+ assert(length+128 <= 255);236 fwrite(&lengthbyte, 1, 1, f);237 fwrite(&databyte, 1, 1, f);238 }239 240-void stbiw__write_nonrle_data(FILE *f, int length, unsigned char *data)240+void stbiw__write_dump_data(FILE *f, int length, unsigned char *data)241 {242 unsigned char lengthbyte = (unsigned char )(length & 0xff);243- assert(length <= 0x7f);243+ assert(length <= 128); // inconsistent with spec but consistent with official code244 fwrite(&lengthbyte, 1, 1, f);245 fwrite(data, length, 1, f);246 }247 23 unmodified lines271 stbiw__linear_to_rgbe(rgbe, linear);272 fwrite(rgbe, 4, 1, f);273 }274 } else {275+ int c,r;276 /* encode into scratch buffer */277 for (x=0; x < width; x++) {278 switch(comp) {279 case 4: /* fallthrough */14 unmodified lines294 295 fwrite(scanlineheader, 4, 1, f);296 297 /* RLE each component separately */297- for (x=0; x < 4; x++) {298- unsigned char *comp = &scratch[width*x];298+ for (c=0; c < 4; c++) {299+ unsigned char *comp = &scratch[width*c];300 int runstart = 0, head = 0, rlerun = 0;301 301- while (head < width) {302- head++;303-304- if (head - runstart == 127 && rlerun == 1) {305- // max length RLE run306- stbiw__write_rle_data(f, head - runstart, comp[runstart]);307- rlerun = 0;308- runstart = head;309- } else if (head - runstart == 128 && rlerun == 0) {310- // max length non-RLE run311- stbiw__write_nonrle_data(f, head - runstart, comp+runstart);312- rlerun = 0;313- runstart = head;314- } else if (comp[head] != comp[head-1] && rlerun == 1) {315- // end of RLE run316- stbiw__write_rle_data(f, head - runstart, comp[runstart]);317- rlerun = 0;318- runstart = head;319- } else {320- // continue accumulating RLE run321- if (rlerun == 1) continue;322-323- // see if we can start an RLE run, at least 3 bytes same324- if (rlerun == 0 && head - runstart >= 2325- && comp[head] == comp[head-1]326- && comp[head] == comp[head-2]) {327- // found a run. Flush non-run (if there is anything) and then start an RLE run328- if (head - runstart > 2) {329- stbiw__write_nonrle_data(f, head-2 - runstart, comp+runstart);330- }331-332- rlerun = 1;333- runstart = head-2;302+ x = 0;303+ while (x < width) {304+ // find first run305+ r = x;306+ while (r+2 < width) {307+ if (comp[r] == comp[r+1] && comp[r] == comp[r+2])308+ break;309+ ++r;310+ }311+ if (r+2 >= width)312+ r = width;313+ // dump up to first run314+ while (x < r) {315+ int len = r-x;316+ if (len > 128) len = 128;317+ stbiw__write_dump_data(f, len, &comp[x]);318+ x += len;319+ }320+ // if there's a run, output it321+ if (r+2 < width) { // same test as what we break out of in search loop, so only true if we break'd322+ // find next byte after run323+ while (r < width && comp[r] == comp[x])324+ ++r; 325+ // output run up to r326+ while (x < r) {327+ int len = r-x;328+ if (len > 127) len = 127;329+ stbiw__write_run_data(f, len, comp[x]);330+ x += len;331 }332 }333 }337-338- // flush remaining sequence (if any)339- if (rlerun == 1) stbiw__write_rle_data(f, head - runstart, comp[runstart]);340- else if (head - runstart > 0) stbiw__write_nonrle_data(f, head - runstart, comp+runstart);334 }335 }336 }337 340 unmodified lines678 #endif // STB_IMAGE_WRITE_IMPLEMENTATION679 680 /* Revision history681 682+ 0.97 (2015-01-18)683+ fixed HDR asserts, rewrote HDR rle logic684 0.96 (2015-01-17)685 add HDR output686 fix monochrome BMP687 0.95 (2014-08-17)11 unmodified lines