展示 HN:giflib 的一个简单替代方案 – C 语言的仅头文件解码器
Show HN: Easy alternative to giflib – header-only decoder in C

原始链接: https://github.com/Ferki-git-creator/TurboStitchGIF-HeaderOnly-Fast-ZeroAllocation-PlatformIndependent-Embedded-C-GIF-Decoder

## TurboStitchGIF:轻量级GIF解码器 TurboStitchGIF是一个仅包含头文件的C库,专为高效的GIF解码而设计,尤其适用于嵌入式系统和对性能要求严格的应用。它具有最小的占用空间,**无需动态内存分配**,而是依赖于用户提供的缓冲区。 该库支持静态和动态GIF,提供**安全模式**以最小化内存使用,以及**Turbo模式**以优化速度。它与平台无关,仅使用标准C99,并提供初始化、获取GIF信息(尺寸)、解码带有延迟时间的帧以及清理等函数。 主要特性包括完整的动画支持(帧、延迟、透明度、循环)、画布大小和颜色调色板的可配置限制,以及强大的错误处理机制。可以通过预处理器定义进行定制。操作需要一个计算大小的暂存缓冲区。 TurboStitchGIF非常适合资源受限的环境,例如物联网设备、游戏开发和嵌入式显示,为GIF处理提供了一个简单、高效且无依赖的解决方案。

## TurboStitchGIF:轻量级GIF解码器 一位名为FerkiHN的开发者在Hacker News上分享了一个新的、仅包含头文件的C语言GIF解码器,名为TurboStitchGIF。它受到stb等“即插即用”风格库的启发,专为嵌入式系统和低资源环境设计。 主要特性包括:零动态内存分配(要求用户提供缓冲区)、更快速的LZW解码(“turbo模式”)、以及旨在兼容C89标准——尽管开发者承认可能存在一些现代扩展,并欢迎对此的反馈。 用户建议提供基于ARM系统的编译代码大小示例,以展示其轻量级特性。该项目旨在成为giflib的一个更简单的替代方案,从而产生更小的二进制文件。开发者欢迎建议,并积极采纳社区的反馈。
相关文章

原文

Header-only Platform Independent Zero Allocations MIT License

TurboStitchGIF is a lightweight, header-only C library for decoding GIF images with a focus on efficiency and minimal resource usage. Designed for embedded systems and performance-critical applications, it provides a simple API for decoding both static and animated GIFs while maintaining a tiny footprint.

  • Single-header implementation - Just include gif.h in your project
  • Zero dynamic allocations - Works with user-provided memory buffers
  • Platform independent - Pure C99 with no OS dependencies
  • Dual decoding modes:
    • Safe mode: Minimal memory usage (default)
    • Turbo mode: Optimized for speed (define GIF_MODE_TURBO)
  • Low memory footprint - Ideal for embedded systems and microcontrollers
  • Full animation support - Handles frames, delays, transparency, and looping
  • Configurable limits - Set canvas size and color limits via preprocessor defines
#define GIF_IMPLEMENTATION
#include "gif.h"

// Define scratch buffer size (calculate using GIF_SCRATCH_BUFFER_REQUIRED_SIZE)
uint8_t scratch_buffer[SCRATCH_BUFFER_SIZE]; 

void process_gif(const uint8_t* gif_data, size_t gif_size) {
    GIF_Context ctx;
    if(gif_init(&ctx, gif_data, gif_size, 
                scratch_buffer, sizeof(scratch_buffer)) != GIF_SUCCESS) {
        // Handle error
        return;
    }

    int width, height;
    gif_get_info(&ctx, &width, &height);
    
    uint8_t* frame_buffer = malloc(width * height * 3);
    int delay_ms;
    
    while(gif_next_frame(&ctx, frame_buffer, &delay_ms) > 0) {
        // Process frame
        // delay_ms contains frame duration
    }
    
    gif_close(&ctx);
    free(frame_buffer);
}

Customize the library by defining these before including gif.h:

#define GIF_MAX_WIDTH 800      // Max canvas width
#define GIF_MAX_HEIGHT 600     // Max canvas height
#define GIF_MAX_COLORS 256     // Max colors in palette
#define GIF_MODE_TURBO         // Enable faster decoding
#define GIF_MAX_CODE_SIZE 12   // LZW max code size (usually 12)
#include "gif.h"
Function Description
gif_init() Initialize decoder context
gif_get_info() Get GIF dimensions
gif_next_frame() Decode next animation frame
gif_rewind() Restart animation from beginning
gif_close() Clean up decoder context
gif_set_error_callback() Set custom error handler

The library requires a scratch buffer whose size depends on the selected mode:

// Safe mode (default)
#define GIF_SCRATCH_BUFFER_REQUIRED_SIZE ...

// Turbo mode (faster)
#define GIF_SCRATCH_BUFFER_REQUIRED_SIZE ...

Calculate the exact size needed using these macros in your code.

🌟 Why Choose This Library?

  • Ultra-lightweight - Perfect for resource-constrained environments
  • No external dependencies - Just the C standard library
  • Simple integration - Single header file simplifies project setup
  • Efficient decoding - Optimized LZW implementation with Turbo mode
  • Robust error handling - Detailed error codes and callback support
  • Transparency support - Handles alpha channel properly
  • Animation features - Full support for frame delays and looping

Ideal for:

  • Embedded systems displays
  • IoT device interfaces
  • Game development
  • Resource-constrained applications
  • Educational projects
  • Custom image viewers

If you enjoy using this library and find it useful, consider supporting my work with a coffee!

Buy Me a Coffee at ko-fi.com

Your support helps me continue maintaining and improving this project!

This project is licensed under the MIT License - see the LICENSE file for details.

联系我们 contact @ memedata.com