Hung Truong
Hung Truong

Reputation: 451

stbi_load cannot load image

I'm trying to load a texture using stbi_load(). Here's my code:

int width, height, numComponents;

unsigned char* imgData = stbi_load(fileName.c_str(), &width, &height, &numComponents, 4);

if (imgData == NULL)
    cout << "Cannot load texture" << endl;

//some code

//free

stbi_image_free(imgData);

And when I run the program, it says Cannot load texture. I don't know what's wrong. I am sure that the filename is a valid path, because when I write:

std::ifstream infile(fileName);

if (infile.good())
{
    cout << "Good" << endl;
}
else
{
    cout << "Not good" << endl;
}

It produces Good. Can someone tell me what is wrong with this code that causes imgData to be NULL (The image is a *.jpg file, if anybody was wondering). Any help would be highly appreciated!

Edit : I ran stbi_failure_reason() and it returned progressive jpeg. What does this mean ?

Upvotes: 9

Views: 24199

Answers (2)

Wang Wei
Wang Wei

Reputation: 69

As stated in the stbi_image source code(https://gist.github.com/1271/970f5fe47cf721c6ce5d8b75d2840c46#file-stb_image-c):

There're such Limitations:

- no jpeg progressive support
- non-HDR formats support 8-bit samples only (jpeg, png)
- no delayed line count (jpeg) -- IJG doesn't support either
- no 1-bit BMP
- GIF always returns *comp=4

As for the progressive jpeg:

compared to standard jpeg, the main difference lies in how the image is loaded.

  • The standard JPEG format loads images one line at a time, from top to bottom, and each line is already pixel perfect.
  • while for the progressive jpeg, the image appears all at once as a whole, but it’ll be blurry and pixelated a first. Gradually, you will see a clear and fully loaded image.

How to check if a jpeg file is progressive or not:

you could follow this answer's instruction:https://superuser.com/a/1617350/1319167

Upvotes: 3

Hung Truong
Hung Truong

Reputation: 451

So it turns out the answer was really easy. If you read from this website : https://gist.github.com/roxlu/3077861

You'll see that stb_image doesn't support progressive JPEG image format (which I didn't even knew existed)

Upvotes: 6

Related Questions