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

Fix BMP palette loading #386

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libvisual/libvisual/lv_video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,10 @@ namespace LV {
}
}

// Videos must have the same palette.
if (m_impl->palette != video->m_impl->palette)
return false;

return true;
}

Expand Down
21 changes: 17 additions & 4 deletions libvisual/libvisual/private/lv_video_bmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@
#include <istream>
#include <cstring>

#define BI_RGB 0
#define BI_RLE8 1
#define BI_RLE4 2
/* BMP compression methods */
#define BI_RGB 0
#define BI_RLE8 1
#define BI_RLE4 2
#define BI_BITFIELDS 3


namespace LV {

Expand Down Expand Up @@ -286,6 +289,8 @@ namespace LV {
fp.read (reinterpret_cast<char*> (&bf_bits), 4);
bf_bits = VISUAL_ENDIAN_LEI32 (bf_bits);

auto dib_header_pos = fp.tellg ();

/* Read the info structure size */
fp.read (reinterpret_cast<char*> (&bi_size), 4);
bi_size = VISUAL_ENDIAN_LEI32 (bi_size);
Expand Down Expand Up @@ -341,13 +346,19 @@ namespace LV {
return nullptr;
}

if (bi_compression > 3) {
if (bi_compression >= BI_BITFIELDS) {
visual_log (VISUAL_LOG_ERROR, "Bitmap uses an invalid or unsupported compression scheme");
fp.seekg (saved_stream_pos);
return nullptr;
}

/* Load the palette */

/* Skip past DIB header to color table */
/* BI_BITFIELDS and BI_ALPHABITFIELDS are unsupported, so there are
no bitmasks after the DIB header. */
fp.seekg (dib_header_pos + std::streampos {bi_size}, std::ios::beg);
Copy link
Member

@hartwork hartwork Jan 25, 2025

Choose a reason for hiding this comment

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

Let's add comments (or make otherwise more obvious) that…

  • that we're jumping to the start of the palette here (— we could also move the line_below_ comment /* Load the palette */ to better show it's part of palette loading?)
  • that this is the correct value (only) because we ruled out both BI_BITFIELDS and BI_ALPHABITFIELDS earlier (referring to the first paragraph at https://en.wikipedia.org/wiki/BMP_file_format#Color_table)

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, let me make that change.


if (bi_bitcount < 24) {
if (bi_clrused == 0) {
/* When the colors used variable is zero, use the
Expand Down Expand Up @@ -385,6 +396,8 @@ namespace LV {
if (palette)
video->set_palette (*palette);

/* Read and decode image data */

/* Set to the beginning of image data, note that MickeySoft likes stuff upside down .. */
fp.seekg (bf_bits, std::ios::beg);

Expand Down