Skip to content

Commit

Permalink
Merge pull request #386 from Libvisual/fix-bmp-palette-loading
Browse files Browse the repository at this point in the history
Fix BMP palette loading.
  • Loading branch information
kaixiong authored Jan 25, 2025
2 parents 5a3a3c8 + 175e9b2 commit 1788dec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
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);

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

0 comments on commit 1788dec

Please sign in to comment.