Moritz Seppelt
Moritz Seppelt

Reputation: 61

stbi_load doesn't return any value

I'm trying to load some images with stb_image to an unsigned char*. The code goes like this:

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

namespace rhino {

    Texture::Texture(const std::string& file) {
        stbi_set_flip_vertically_on_load(1);

        buffer = stbi_load(file.c_str(), &width, &height, &bpp, 4);
         ...

The buffer, bpp, width and height variables are declared in the Textureclass. After the stbi_load call, width, height and bpp are 0. I am certain that the filepath is correct but even if I enter a wrong path, the same resuts are occuring. I don't know if stbi_image is supposed to print something to the console if an error occurs but no text is printed there.

[EDIT] Does somebody actually know what is supposed to happen if stb_image cannot read a image?

Upvotes: 2

Views: 6706

Answers (2)

Rathnavel
Rathnavel

Reputation: 75

You can always use the stbi_failure_reason() to get a hint of what is going wrong. If the stbi_load() works fine, stbi_failure_reason() returns null. If not, it returns the actual error.

 buffer = stbi_load(file.c_str(), &width, &height, &bpp, 4);

  if(stbi_failure_reason())
    std::cout << stbi_failure_reason();

In your case, where you were using the relative path instead of an absolute path, it would have returned a message, "Can't fopen". That way you could have guessed its pointing to the wrong folder. If the image type that you were using is not supported by STBI, you would have received a message "unknown image type" and so on.

Upvotes: 2

MivVG
MivVG

Reputation: 699

I guess you have to use an absolute path because your stb_image.h is somewhere outside your project directory. This way, it will look for the relative path of your image from the directory the header is in. and not in your project directory.

You have two options to fix this:

  • Use absolute paths

  • Move stb_image.h to your project directory, preferably at the root of it

Upvotes: 1

Related Questions