Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stb_image: fix gif decoding: only restore background when dispose is 2 or 3 #1702

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cocoa-xu
Copy link

@cocoa-xu cocoa-xu commented Oct 8, 2024

Hi, this PR should fix #1688. Background should be restored only when dispose is 2 or 3.

Minimal test code

gif-background.c

#ifdef __clang__
#define STBIDEF static inline
#endif

#define STB_IMAGE_STATIC
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

unsigned char* read_binary_file(const char* filename, size_t* size) {
    FILE* file = fopen(filename, "rb");
    if (file == NULL) {
        fprintf(stderr, "Error opening file: %s\n", filename);
        return NULL;
    }

    struct stat st;
    if (stat(filename, &st) != 0) {
        fprintf(stderr, "Error getting file size\n");
        fclose(file);
        return NULL;
    }

    *size = st.st_size;
    unsigned char* buffer = (unsigned char*)malloc(*size);
    if (buffer == NULL) {
        fprintf(stderr, "Error allocating memory\n");
        fclose(file);
        return NULL;
    }

    size_t bytes_read = fread(buffer, 1, *size, file);
    if (bytes_read != *size) {
        fprintf(stderr, "Error reading file: expected %zu bytes, got %zu bytes\n", *size, bytes_read);
        free(buffer);
        fclose(file);
        return NULL;
    }

    fclose(file);
    return buffer;
}

int main(int argc, const char *argv[]) {
    if (argc != 3) {
        printf("Usage: %s <gif file> <output dir>\n", argv[0]);
        exit(1);
    }

    size_t file_size;
    unsigned char* filedata = read_binary_file(argv[1], &file_size);

    int x, y, z, comp;
    int *delays = NULL;
    unsigned char *data = stbi_load_gif_from_memory(filedata, (int)file_size, &delays, &x, &y, &z, &comp, 0);

    if (data == NULL) {
        fprintf(stderr, "Error loading image\n");
        return 1;
    }

    unsigned char *start = data;
    size_t frame_size = x * y * sizeof(unsigned char) * 4;
    char * filename = (char *)malloc(1024);
    for (int i = 0; i < z; ++i) {
        bzero(filename, 1024);
        snprintf(filename, 1023, "%s/%d.png", argv[2], i);
        stbi_write_png(filename, x, y, 4, start, 0);
        start += frame_size;
    }

    free(filename);
    STBI_FREE((void *)data);
    STBI_FREE((void *)delays);
}

horse.gif

File from #1688:

horse

Test

Assuming gif-background.c and horse.gif has been downloaded to the project root directory.

$ cd stb
$ gcc -I$(pwd) gif-background.c -o gif-background
$ mkdir -p ./output
$ gif-background ./horse.gif ./output

Visual check on the 6th frame

Before

5

After

5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using stbi_load_gif_from_memory results in incorrect image data for each frame of the GIF
1 participant