Skip to content

Conversation

@cmb69
Copy link
Member

@cmb69 cmb69 commented Dec 25, 2024

As is, we write palette images as 8bpp BMP images. Besides that this is wasteful, some readers may not even be able to read 8bpp BMP images. Thus, we write palette images with at most two colors as 1bpp BMPs, unless compression is requested, because BMPs do not support compressed encoding of 1bpp images; we write palette images with at most 16 colors as 4bpp BMPs, and apply BMP_BI_RLE4 compression if requested.

We piggy-back on the existing implementation for BMP_BI_RLE8; that keeps it simple, and although it might not yield the best compression, it ought to be good enough.

References:
https://en.wikipedia.org/wiki/BMP_file_format
https://learn.microsoft.com/en-us/windows/win32/gdi/bitmap-compression

Closes #911.


While implementing this, I've noticed that the RLE compression is not quite as good as it could be. While there is room for improvement, it might get pretty tricky, and it may not be worth spending further time on that, given that RLE encoding only yields somewhat good compression for images with few details, anyway.

As is, we write palette images as 8bpp BMP images.  Besides that this
is wasteful, some readers may not even be able to read 8bpp BMP images.
Thus, we write palette images with at most two colors as 1bpp BMPs,
unless compression is requested, because BMPs do not support compressed
encoding of 1bpp images; we write palette images with at most 16 colors
as 4bpp BMPs, and apply `BMP_BI_RLE4` compression if requested.

We piggy-back on the existing implementation for `BMP_BI_RLE8`; that
keeps it simple, and although it might not yield the best compression,
it ought to be good enough.

References:
<https://en.wikipedia.org/wiki/BMP_file_format>
<https://learn.microsoft.com/en-us/windows/win32/gdi/bitmap-compression>

Closes libgd#911.
@cmb69
Copy link
Member Author

cmb69 commented Dec 25, 2024

CI failures are unrelated.

Copy link
Member

@vapier vapier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nits

@cmb69 cmb69 requested a review from vapier January 9, 2025 16:44
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.

BMP output support for specific color depths

2 participants