DavidKeller
DavidKeller

Reputation: 1

self-written BMP files with RLE8 compression not recognized by readers like Preview

I am trying to write BMP image files using RLE8 compression. Most files I read/write are recognized by standard readers, but some fail. Images with large blank areas are not recognized unless they are quite small (<512x512). However, images with complex color patterns are recognized, even when very large.

This is problematic because RLE compression works best with simple images, and my images of interest are mainly simple line drawings—exactly the kind that fails. BMP files with RLE8 compression, generated by others, have headers, color tables, and RLE code strings identical in format to mine, as far as I can tell. The success or failure seems to depend on the pattern of colors in the image: simple blank images fail, while complex images work.

Is there some sort of checksum or other validation that these files must pass? I see no systematic difference between my images and those generated by others that are recognized.

I’ve tested images of many sizes and aspect ratios. Blank images beyond a small size are not recognized by standard readers. The same code works well for both self-generated images and many standard test images, as long as they aren't too simple. I’ve compared RLE codes for images generated by others, and they seem identical (mutatis mutandis) to my own.

Additional Info: Microsoft apps like Paint display all my images without any issues. So, the problem seems to lie in Apple reader apps. Does anyone know if this is a known issue, and if so, how to work around it?

I haven't found an Apple app that can display the "invalid" images. I've tried Preview, FreeForm, ColorSync Utility, and the image import function in Word. All of these work with the smaller images but fail to recognize larger ones as a valid graphic format.

Example: Here are two RLE8 image files:

The first one is flagged as invalid by readers. The second one is almost identical, but it is read and displayed correctly. Both files use the same RLE codes, but the first file is for a 1024x64 image (64 lines), and the second is for a 1024x32 image (32 lines). Aside from the number of lines and corresponding differences in the headers, the files are the same.

BM                     BM
file_size              1846
reserved1              0
reserved2              0
offset_to_pixels       1078
info_header_size       40
xpix                   1024
ypix                   64
planes                 1
bits_per_pixel         8
compression            1
bytes_in_pixel_block   768
xpixelspermeter        0
ypixelspermeter        0
colors_used            256
colors_important       256

RLE Code Block (Flagged as invalid):

Line  0: 255,111 255,111 255,111 255,111 4,111 0, 0
Line  1: 255,111 255,111 255,111 255,111 4,111 0, 0
...
Line 63: 255,111 255,111 255,111 255,111 4,111 0, 1

Valid File

BM                     BM
file_size              1462
reserved1              0
reserved2              0
offset_to_pixels       1078
info_header_size       40
xpix                   1024
ypix                   32
planes                 1
bits_per_pixel         8
compression            1
bytes_in_pixel_block   384
xpixelspermeter        0
ypixelspermeter        0
colors_used            256
colors_important       256

RLE Code Block (Valid):

Line  0: 255,111 255,111 255,111 255,111 4,111 0, 0
Line  1: 255,111 255,111 255,111 255,111 4,111 0, 0
...
Line 31: 255,111 255,111 255,111 255,111 4,111 0, 1

Summary: The RLE lines are the same for both files, except the first has 64 lines, while the second has 32. However, the first one is flagged as invalid. Could it be related to some limit in the Apple reader or something else I’m overlooking?

Upvotes: 0

Views: 59

Answers (1)

DavidKeller
DavidKeller

Reputation: 1

To answer my own question, as far as I can at the moment: all the Apple image display programs fail to recognize BMP images with RLE8 compression, for simple line images with mostly one-color background, above a certain size. In folder windows they don't show a thumbnail. When clicked they give an error message. But these same files open without issue in Windows apps on Windows machines, and display correctly in Google drive. Also at least some non-Apple apps that run on Macs read and display the images correctly in all particulars. It's clear that the Apple image readers/display apps apply some non-obvious validity test that some valid BMP images do not pass. I have not been able to figure out what the test is, despite running many cases.

Upvotes: 0

Related Questions