Skip to content
Myr

fix incorrect img_n variable for interlaced files, caused files to be totally incorrect if forcing channel count

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 ff2d547 · Browse files · 2 files+32 added−20 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+32 added−20 removed

  • tests:+17 added−9 removed
  • other:+15 added−11 removed

2 files+32 added−20 removed

modified:tests/image_test.c+17 added-9 removed
30 unmodified lines
31 printf("FAILED 4\n");32 }33 } else {34 int i;35 char **files = stb_readdir_files("images");35 char **files = stb_readdir_files("pngsuite/part1");36 for (i=0; i < stb_arr_len(files); ++i) {37 int n;38 char **failed = NULL;39 unsigned char *data;39 printf("%s\n", files[i]);40 data = stbi_load(files[i], &w, &h, &n, 4); if (data) free(data); else printf("Failed &n\n");41 data = stbi_load(files[i], &w, &h, 0, 1); if (data) free(data); else printf("Failed 1\n");42 data = stbi_load(files[i], &w, &h, 0, 2); if (data) free(data); else printf("Failed 2\n");43 data = stbi_load(files[i], &w, &h, 0, 3); if (data) free(data); else printf("Failed 3\n");44 data = stbi_load(files[i], &w, &h, 0, 4);40 //printf("%s\n", files[i]);41 data = stbi_load(files[i], &w, &h, &n, 0); if (data) free(data); else stb_arr_push(failed, "&n");42 data = stbi_load(files[i], &w, &h, 0, 1); if (data) free(data); else stb_arr_push(failed, "1");43 data = stbi_load(files[i], &w, &h, 0, 2); if (data) free(data); else stb_arr_push(failed, "2");44 data = stbi_load(files[i], &w, &h, 0, 3); if (data) free(data); else stb_arr_push(failed, "3");45 data = stbi_load(files[i], &w, &h, 0, 4); if (data) ; else stb_arr_push(failed, "4");46 if (data) {47 char fname[512];48 stb_splitpath(fname, files[i], STB_FILE);49 stbi_write_png(stb_sprintf("output/%s.png", fname), w, h, 4, data, w*4);50 free(data);50 } else51 printf("FAILED\n");51 }52 if (failed) {53 int j;54 printf("FAILED: ");55 for (j=0; j < stb_arr_len(failed); ++j)56 printf("%s ", failed[j]);57 printf(" -- %s\n", files[i]);58 }59 }60 printf("Tested %d files\n", i);61 }62 return 0;63}
modified:stb_image.h+15 added-11 removed
2528 unmodified lines
2529 }2530 else {2531 // unpack 1/2/4-bit into a 8-bit buffer. allows us to keep the common 8-bit path optimal at minimal cost for 1/2/4-bit2532 // png guarante byte alignment, if width is not multiple of 8/4/2 we'll decode dummy trailing data that will be skipped in the later loop2533 in = line8;2534 stbi_uc* decode_out = line8;2533 stbi_uc * decode_out = line8;2534 stbi_uc scale = (color == 0) ? 0xFF/((1<<depth)-1) : 1; // scale grayscale values to 0..255 range2535 in = line8;2536 if (depth == 4) {2537 for (k=x*img_n; k >= 1; k-=2, raw++) {2538 *decode_out++ = scale * ((*raw >> 4) );2539 *decode_out++ = scale * ((*raw ) & 0x0f);
76 unmodified lines
2616 if (depth != 8) free(line8);2617 return 1;2618}26192620static int stbi__create_png_image(stbi__png *a, stbi_uc *raw, stbi__uint32 raw_len, int out_n, int depth, int color, int interlaced)2620static int stbi__create_png_image(stbi__png *a, stbi_uc *image_data, stbi__uint32 image_data_len, int out_n, int depth, int color, int interlaced)2621{2622 stbi_uc *final;2623 int p;2624 if (!interlaced)2625 return stbi__create_png_image_raw(a, raw, raw_len, out_n, a->s->img_x, a->s->img_y, depth, color);2625 return stbi__create_png_image_raw(a, image_data, image_data_len, out_n, a->s->img_x, a->s->img_y, depth, color);26262627 // de-interlacing2628 final = (stbi_uc *) stbi__malloc(a->s->img_x * a->s->img_y * out_n);2629 for (p=0; p < 7; ++p) {
5 unmodified lines
2635 // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 12636 x = (a->s->img_x - xorig[p] + xspc[p]-1) / xspc[p];2637 y = (a->s->img_y - yorig[p] + yspc[p]-1) / yspc[p];2638 if (x && y) {2639 stbi__uint32 img_len = ((((out_n * x * depth) + 7) >> 3) + 1) * y;2640 if (!stbi__create_png_image_raw(a, raw, raw_len, out_n, x, y, depth, color)) {2639 stbi__uint32 img_len = ((((a->s->img_n * x * depth) + 7) >> 3) + 1) * y;2640 if (!stbi__create_png_image_raw(a, image_data, image_data_len, out_n, x, y, depth, color)) {2641 free(final);2642 return 0;2643 }2644 for (j=0; j < y; ++j)2645 for (i=0; i < x; ++i)2646 memcpy(final + (j*yspc[p]+yorig[p])*a->s->img_x*out_n + (i*xspc[p]+xorig[p])*out_n,2644 for (j=0; j < y; ++j) {2645 for (i=0; i < x; ++i) {2646 int out_y = j*yspc[p]+yorig[p];2647 int out_x = i*xspc[p]+xorig[p];2648 memcpy(final + out_y*a->s->img_x*out_n + out_x*out_n,2649 a->out + (j*x+i)*out_n, out_n);2650 }2651 }2652 free(a->out);2649 raw += img_len;2650 raw_len -= img_len;2653 image_data += img_len;2654 image_data_len -= img_len;2655 }2656 }2657 a->out = final;2658
2090 unmodified lines