nothings / stb

stb single-file public domain libraries for C/C++
https://twitter.com/nothings
Other
26.68k stars 7.71k forks source link

stbir_resize_uint8 guarantee same result if same dimension? #836

Open zchrissirhcz opened 4 years ago

zchrissirhcz commented 4 years ago

when calling stbir_resize_uint8, with target image size the same as the source image size, does it guarantee generate the exactly same result as the source image pixels? Based on my test result, it is not the same pixels. So sad result.

main.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

//#define STB_IMAGE_WRITE_IMPLEMENTATION
//#include "stb_image_write.h"

#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb_image_resize.h"

#define bark printf("OK in line %d\n", __LINE__);

typedef struct MyImage {
    int h, w, c;
    unsigned char* data;
} MyImage;

int main() {
    // load an image
    const char* fname = "cat_224.bmp";
    int h, w, c;
    int channels = 3;
    unsigned char* data = stbi_load(fname, &w, &h, &c, channels);
    if (!data) {
        fprintf(stderr, "failed to load image\n");
        exit(1);
    }

    c = channels;

    MyImage im;
    im.h = h;
    im.w = w;
    im.c = c;
    im.data = data;

    // resize image
    MyImage res;
    res.h = h;
    res.w = w;
    res.c = c;
    res.data = (unsigned char*)malloc(h*w*c);

    bark;

    stbir_resize_uint8(im.data, im.w, im.h, 0,
            res.data, res.w, res.h, 0, c);

    int mass = h*w*c;
    for(int i=0; i<mass; i++) {
        if (im.data[i]!=res.data[i]) {
            fprintf(stderr, "Error! im.data[%d]!=res.data[%d] (%d vs %d)\n",
                    i, i, im.data[i], res.data[i]);
        }
    }

    // release memory
    free(res.data);
    free(data);

    return 0;
}

cat_224.bmp is 224*224*3 size:

gcc main.c -lm
./a.out > log.txt 2>&1
wc -l log.txt 

got result:

79478 log.txt

zchrissirhcz commented 4 years ago

Since .bmp files can't be uploaded here, the tested cat_224.bmp image is in the tar.gz file, please extract from it: cat_224.bmp.tar.gz

nothings commented 4 years ago

We have a basically total rewrite of this library about 90% complete, and this issue should be fixed by that rewrite. I don't know when it will be released, though.

zchrissirhcz commented 4 years ago

@nothings Hi, how is the rewriting going? We very like stb image's simple interface and would like to know the plan (e.g. load image, resize image, timeline, etc) if possible. Hope we can use new stb image soon.

nothings commented 4 years ago

I still don't know. The rewrite is being done by Jeff Roberts, who wrote stb_sprintf, and he has a full time job running RAD Game Tools, so I don't know what progress he's made lately.