展示 HN:TurboLibBMP 0.1.1 – 单头文件 BMP 库,使用 C 语言(无内存分配,嵌入式)

1作者: DenisDolya大约 20 小时前原帖
嗨,HN, 我开始这个项目是因为厌倦了那些隐藏内存分配、引入大量 libc 或只解决问题一小部分的 BMP 库。虽然有很多类似 libbmp 的库,但功能分散:一个负责解码,另一个负责编码,还有一个专注于嵌入式,另一个则是单头文件。在实际项目中,这通常意味着需要依赖 5 到 10 个小库才能加载和显示图像。 我希望有一个可预测的库:没有内存分配、只有一个头文件、对缓冲区的完全控制,并且可以在桌面和微控制器上使用。这样你总是知道内存来自哪里以及去向何处。 随着时间的推移,我越来越深入这个项目,并开始围绕我称之为 ESS 的理念进行优化:能效、速度、小二进制。我为常见的 24/32bpp BMP 添加了快速路径,消除了热循环中的不必要分支,并专注于可预测的性能。对于嵌入式系统,我添加了一个流式解码器,可以通过回调直接写入 LCD/DMA,甚至可以只解码一个裁剪的矩形,以节省 CPU 时间和功耗。 最终的结果是 TurboLibBMP:一个单头文件、类似 stb 风格的 BMP 解码器和编码器,使用 C 编写,没有内存分配、没有标准输入输出、没有隐藏缓冲区,并且用户可以完全控制内存和行为。它遵循 GPL-3.0 许可,并且可以在 C 和 C++ 中使用。 我主要是为我自己的低级和嵌入式项目构建这个库,但我分享出来是希望其他人也能找到它的用处。我非常希望能收到关于 API 设计、边缘情况以及这种方法在实际项目中是否合理的反馈。 仓库链接:<a href="https://github.com/Ferki-git-creator/turbo-lib-bmp" rel="nofollow">https://github.com/Ferki-git-creator/turbo-lib-bmp</a>
查看原文
Hi HN, I started this project because I was tired of BMP libraries that hide allocations, pull half of libc, or solve only one small part of the problem. There are many libbmp-style repos, but the functionality feels scattered: one does decoding, another does encoding, another works on embedded, another is single-header. In real projects this often means depending on 5–10 small libraries just to load and display images. I wanted one predictable library instead: no allocations, one header, full control over buffers, and usable both on desktop and on microcontrollers. Something where you always know where memory comes from and where it goes. Over time, I got more and more into it and started optimizing around what I call ESS: Energy, Speed, Small binary. I added fast paths for common 24&#x2F;32bpp BMPs, removed unnecessary branching in hot loops, and focused on predictable performance. For embedded systems, I added a streaming decoder that can write directly into LCD&#x2F;DMA via callbacks, and even decode only a clipped rectangle to save CPU time and power. The result is TurboLibBMP: a single-header, stb-style BMP decoder and encoder in C, with no allocations, no stdio, no hidden buffers, and full user control over memory and behavior. It is GPL-3.0 and works in both C and C++. I built this mainly for my own low-level and embedded projects, but I’m sharing it in case others find it useful. I would really appreciate feedback on the API design, edge cases, and whether this approach makes sense in real projects. Repo: <a href="https:&#x2F;&#x2F;github.com&#x2F;Ferki-git-creator&#x2F;turbo-lib-bmp" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Ferki-git-creator&#x2F;turbo-lib-bmp</a>