Skip to content

STBI not loading image despite giving the right directory for absolutely no reason #1833

Closed

Description

@MyoskSoftwareCompany

I tried loading an image but it just doesn't want to load, despite me giving the right path.
main.cpp
GLuint menuBG = render3d->loadTexture("./assets/title.png");
gl2.h
#include <stb_image.h> ... GLuint loadTexture(const char* filename);
stb_image.cpp
#define STB_IMAGE_IMPLEMENTATION #include <stb_image.h>
gl2.cpp
GLuint Renderer::loadTexture(const char* filename) { int w, h, channels; unsigned char* textureData = stbi_load(filename, &w, &h, &channels, 0); if (!textureData) { printf("%s\n", stbi_failure_reason()); return 0; } ...

Activity

  1. yixy-only commented on Sep 1, 2025

    @yixy-only

    @MyoskSoftwareCompany Could you upload the PNG image you are using here and I'll verify whether the image format is supported by stb_image. It would be nice if you could create a short C++ project that reproduces the problem.

  2. MyoskSoftwareCompany commented on Sep 1, 2025

    @MyoskSoftwareCompany
    Author

    Image btw the issue just comes whenever i load an image

  3. nothings commented on Sep 1, 2025

    @nothings
    Owner

    What does the stbi_failure_reason() printf show?

  4. yixy-only commented on Sep 1, 2025

    @yixy-only

    @MyoskSoftwareCompany I downloaded the image you uploaded. After testing, the image is fine and it can be read correctly by stb_image.

    • If it is not readable in your project, please change the path to an absolute path instead of a relative path and retest again. Because the current directory may not be what you expected.
    • This PNG image is stored in RGB24 format, which means that one pixel takes up 3 bytes. Because you passed 0 to the desired_channels parameter of the stbi_load() function, you get a pixel buffer where a pixel is also stored 3 bytes next to each other. If a pixel takes up 4 bytes in your frame buffer, then you need to copy it pixel by pixel.
    • If you want each pixel of the loaded image data to take up 4 bytes instead of 3, pass STBI_rgb_alpha in the desired_channels parameter of the stbi_load() function, which means that each pixel of the load is stored in the RGBA32 format, taking up 4 bytes per pixel, which makes it easier to reading.
  5. MyoskSoftwareCompany commented on Sep 1, 2025

    @MyoskSoftwareCompany
    Author

    the issue still persists

    Image
  6. yixy-only commented on Sep 1, 2025

    @yixy-only

    the issue still persists

    @MyoskSoftwareCompany Yes!

    STBIDEF stbi_uc *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)
    {
       FILE *f = stbi__fopen(filename, "rb");
       unsigned char *result;
       if (!f) return stbi__errpuc("can't fopen", "Unable to open file");
       result = stbi_load_from_file(f,x,y,comp,req_comp);
       fclose(f);
       return result;
    }

    As you can see from the output message and the source code, the reason is that it is not possible to open this file, which may not be able to find the file based on the path. Please check the file path and test using the full path (absolute path) of the file.

    You run the program at C:\Users\Admin\Documents\dm. in fact, the current path is at C:\Users\Admin\Documents\dm. If you use relative paths, are you sure the path of the picture is C:\Users\Admin\Documents\dm\assets\title.png?

  7. Repository owner locked and limited conversation to collaborators on Sep 1, 2025
  8. converted this issue into a discussion #1835 on Sep 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions