Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

same jpeg image decode incorrect #695

Closed
stanley0614 opened this issue Jan 16, 2019 · 2 comments
Closed

same jpeg image decode incorrect #695

stanley0614 opened this issue Jan 16, 2019 · 2 comments

Comments

@stanley0614
Copy link

I am a new in using stb_image. I load a jpeg picture to opengl texture by using stb_image. and I find the picture not correct. but when I use ImageMagick to load the picture it is good! I do not know why, Is this a bug in stb_image or this kind of jpeg is not supported ? anyone can help me, Thank you very much.
original image:

8

stb_image load to texture:
bad_texture

@zao
Copy link

zao commented Jan 16, 2019

TL;DR - probably user application error.

The image as presented here round-trips correctly through stbi_load -> stbi_write_png for me with the following test program:

#define STB_IMAGE_IMPLEMENTATION 1
#define STB_IMAGE_WRITE_IMPLEMENTATION 1
#include "stb_image.h"
#include "stb_image_write.h"

#define IN_FILENAME "/mnt/y/Downloads/51249360-fcb19f80-19cd-11e9-86eb-787bb44422ac.jpeg"
#define OUT_FILENAME "/mnt/f/Temp/car.png"

int main() {
        int x, y, n;
        unsigned char* data = stbi_load(IN_FILENAME, &x, &y, &n, 0);
        fprintf(stderr, "x: %d, y: %d, n: %d\n", x, y, n);
        stbi_write_png(OUT_FILENAME, x, y, n, data, 0);
        free(data);
        return 0;
}

My psychic debugging hat tells me that you are uploading the image to OpenGL without setting GL_UNPACK_ALIGNMENT to 1. The default is 4, which means that whenever you upload pixel data to OpenGL, it'll pad out it's expectation of row stride to a round four bytes. As a row of your image is 529 * 3 (1587) bytes which is not divisible by four, you're losing one byte per row, resulting in the sloped weirdness you observe, with a region of undefined memory up top.

@stanley0614
Copy link
Author

@zao Thank you very much. It is GL_UNPACK_ALIGNMENT reason. 再次感谢你!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants