Wingblade
Wingblade

Reputation: 10093

stbi_load_from_memory not reading image dimensions correctly

I'm using ld -r -b binary -o res.o * in order to embed a folder full of several png images into my C program, among them one called font.png.

In my code, I am using this code to load the embedded font.png with stb_image:

char* img;
int w, h, n;
extern char* _binary_font_png_start;
extern char* _binary_font_png_end;
img = (char*)stbi_load_from_memory((unsigned char*)_binary_font_png_start, _binary_font_png_end - _binary_font_png_start, &w, &h, &n, 4);

The actual image dimensions are 256x128, but stbi gives me a value of 0 for w (width) and 2293200 for h (height). What went wrong?

Upvotes: 1

Views: 8628

Answers (1)

Wingblade
Wingblade

Reputation: 10093

Finally managed to make it work by changing my code as follows:

char* img;
int w, h, n;
extern char _binary_font_png_start, _binary_font_png_end;
img = (char*)stbi_load_from_memory((unsigned char*)&_binary_font_png_start, &_binary_font_png_end - &_binary_font_png_start, &w, &h, &n, 4);

Kinda weird, but it works so I guess it's correct? Haven't been able to find any docs on the "intended" way of these symbols being linked into c code, but it seems they represent the char at the end/start of the binary data, rather than a pointer to that char as I'd expected.

Upvotes: 4

Related Questions