nothings / stb

stb single-file public domain libraries for C/C++
https://twitter.com/nothings
Other
25.83k stars 7.66k forks source link

A way to reuse a buffer when reading `.gif` files #1568

Open sam20908 opened 8 months ago

sam20908 commented 8 months ago

Is your feature request related to a problem? Please describe. When stb_image loads .gif files, it creates a buffer for the entire file. This can get troublesome for big files, as the memory allocated grows very large. This is very noticeable as in some cases it can take up to a few seconds just to allocate the memory.

Describe the solution you'd like I suggest adding an alternative method to load the files and show how it can be used. the library could take a buffer that it will use to fill the data for the current frame. This way, we only need to allocate a buffer for one frame, thus reducing memory allocations.

Pseudocode of the API:

gif = stbi_load_gif_v2("filename.gif", ...);
unsigned char buffer[...];
while (stbi_gif_has_frame(gif)) {
    error =  stbi_gif_decode_frame(gif, buffer);
    // do something with buffer if no error occurred
    stbi_gif_advance_frame(gif);
}

Let me know what you think!

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the feature request here.

DavinPride commented 8 months ago

gif = stbi_load_gif_v2("filename.gif", ...); unsigned char buffer[...]; while (stbi_gif_has_frame(gif)) { error = stbi_gif_decode_frame(gif, buffer); // do something with buffer if no error occurred stbi_gif_advance_frame(gif); }

nothings commented 8 months ago

The plan is to remove all support for animated gifs from stb_image and fork them into a separate library (which I personally will not be maintaing), because this has just been a source of headaches and it's really outside the original intent of stb_image.