Unable to load .BMP file correctly

Hi.
I have a task to perform and it’s not always working correctly: loading a 8-bit RGB color .BMP file from the filesystem, watermarking it and then save it.
The watermarking process is just fine and also the imdecode/imencode (or imread/imwrite) works for many of my images (~80.000) but sometimes it fails with this kind of result:

The original bitmap is 1193x2048 and I’d love to share it but I don’t have the rights for that (Boeing/Jeppesen property).
No matter what flags I use for reading or writing the image I get this data that looks like each line is “duplicated” but with the wrong background.

...
cv::Mat _image = cv::imread("something.bmp", cv::IMREAD_COLOR);
cv::imwrite("something_else.bmp", _image);
...

The BMP has a standard “BM” header and a simplified color table where only 4~6 colors are mentioned (this are aircraft navigation charts and are pretty simple images).

The code I shared is in C++ but I also tried in Python:

import cv2 as cv
img = cv.imread("source.bmp")
cv.imwrite("destin.bmp", img)

…with the same exact result!

I tried to open the original .bmp files with paint (Windows), Mirage (Linux), Gimp (Linux)… they are fine: no weird colors or errors!

Do you have any idea what I’m doing wrong or how to correct this behavior?
Thanks.

inspect the files with a tool like “imagemagick”. compare the properties of the breaking and good files. see anything that tells them apart?

can you create files that cause this?

can you edit these breaking files with some other program to redact them while maintaining their breaking nature?

Hey Crackwitz.
You are correct: it’s in the files (mostly).
The files are RLE compressed and the size of the uncompressed data does not match what the header says.
I’m working to fix the files upfront but it comes out that OpenCV is more “touchy” then all the other image editor/viewer I tried about this data misalignment.
I discovered the file issue with an online BMP checker because other tools like ImageMagick or Gimp says nothing about it.
Thanks.

if you can provide a file that OpenCV decodes badly, please submit an issue on opencv’s github.

I will try to obtain the authorization for that.

thank you.

actually, even if you can’t provide such a file, please file an issue anyway. include the metadata that you have gathered and the screenshot from earlier. perhaps that is enough to track the issue down.