BMP compression?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
lockon
Posts: 1
Joined: 2018-08-09T10:43:43-07:00
Authentication code: 1152

BMP compression?

Post by lockon »

Hi,
I have a piece of software that ONLY support BMP format for stored images. There are thousands of scanned files in "grayscale" so you can imagine the disk space used is huge.

To the best of my understanding, BMP can't be compressed (outside of zip) - ie does not support native compression.

So, I'd like to know if there is any technique for retaining the BMP file format but reducing the filesizes without major screen viewed or printed degradation? The files are all scanned documents - 99% are B&W but we have to use "grayscale" because most of the hand-written documents are unreadable when B&W (2 bit) is used. I believe the bulk of them are 200x200dpi.

Thank you in advance
Sunil
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: BMP compression?

Post by fmw42 »

Please, always provide your IM version and platform when asking questions, since syntax may differ. Also provide your exact command line and if possible your images.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at http://www.imagemagick.org/discourse-se ... f=1&t=9620

If using Imagemagick 7, then see http://imagemagick.org/script/porting.php#cli


For novices, see

http://www.imagemagick.org/discourse-se ... f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/
https://github.com/ImageMagick/usage-markdown

If you cannot compress BMP and cannot convert it to B/W (bilevel), then I am not sure there is any way that I know to reduce the file size other than reducing the image size.

What version of BMP can you use? Have you tried converting to BMP (BMP4), or BMP3 or BMP2 to see if one of them is smaller than the others.

See https://www.imagemagick.org/Usage/formats/#bmp
Jason S
Posts: 103
Joined: 2010-12-14T19:42:12-07:00
Authentication code: 8675308

Re: BMP compression?

Post by Jason S »

BMP's RLE8 (for 256-color images) and RLE4 (for 16-color images) compression schemes are not too exotic, though I have no way to guess whether your software supports them.

And unfortunately, ImageMagick (as of this writing) doesn't encode RLE8 optimally, and AFAIK doesn't encode RLE4 at all.

Reducing to 16 colors seems like your only option for reducing the bits/pixel, as BMP doesn't really support anything else between 2 and 256. I'd guess that your images would look good enough, quantized to an optimal 16-color palette.

I took an image that might be similar to yours, encoded it in various ways, and got the following file sizes. This might give you an idea of whether it's worth the trouble.

Code: Select all

                     256-color  16-color
                     ---------  --------
BMP-uncompressed         3739K     1874K
BMP-RLE-ImageMagick      1868K       N/A
BMP-RLE-optimal          1320K      789K
PNG                       789K      467K
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: BMP compression?

Post by fmw42 »

See my link above. I am no expert on BMP, but BMP3: format allows bit depths of 0, 1, 4, 8 ,16, 24 and 32. So for 16 colors, you could try

Code: Select all

convert image -alpha off -colorspace gray -depth 4 -type palette BMP3:result.bmp
For example, I tried using the IM internal logo: image and got a grayscale palette image with 16 colors in the colormap.

Code: Select all

convert logo: -alpha off -colorspace gray -depth 4 -type palette BMP3:logo3_bmp3.bmp

Code: Select all

identify -verbose logo3_bmp3.bmp
 
Image: logo3_bmp3.bmp
  Format: BMP3 (Microsoft Windows bitmap image (V3))
  Class: PseudoClass
  Geometry: 640x480+0+0
  Units: PixelsPerCentimeter
  Colorspace: sRGB
  Type: Grayscale
  Base type: Undefined
  Endianess: Undefined
  Depth: 4-bit
  Channel depth:
    red: 4-bit
    green: 4-bit
    blue: 4-bit
  Channel statistics:
    Pixels: 307200
    Red:
      min: 0  (0)
      max: 15 (1)
      mean: 13.3571 (0.890472)
      standard deviation: 4.04173 (0.269449)
      kurtosis: 3.51762
      skewness: -2.26713
      entropy: 0.298121
    Green:
      min: 0  (0)
      max: 15 (1)
      mean: 13.3571 (0.890472)
      standard deviation: 4.04173 (0.269449)
      kurtosis: 3.51762
      skewness: -2.26713
      entropy: 0.298121
    Blue:
      min: 0  (0)
      max: 15 (1)
      mean: 13.3571 (0.890472)
      standard deviation: 4.04173 (0.269449)
      kurtosis: 3.51762
      skewness: -2.26713
      entropy: 0.298121
  Image statistics:
    Overall:
      min: 0  (0)
      max: 15 (1)
      mean: 13.3571 (0.890472)
      standard deviation: 4.04173 (0.269449)
      kurtosis: 3.51765
      skewness: -2.26714
      entropy: 0.298121
  Colors: 16
  Histogram:
      7150: (  0,  0,  0) #000000 black
      1417: ( 17, 17, 17) #111111 srgb(17,17,17)
      4163: ( 34, 34, 34) #222222 srgb(34,34,34)
      6901: ( 51, 51, 51) #333333 grey20
     14740: ( 68, 68, 68) #444444 srgb(68,68,68)
       767: ( 85, 85, 85) #555555 srgb(85,85,85)
       716: (102,102,102) #666666 grey40
      1022: (119,119,119) #777777 srgb(119,119,119)
      2000: (136,136,136) #888888 srgb(136,136,136)
      1482: (153,153,153) #999999 grey60
      3110: (170,170,170) #AAAAAA srgb(170,170,170)
      1539: (187,187,187) #BBBBBB srgb(187,187,187)
      1471: (204,204,204) #CCCCCC grey80
      2922: (221,221,221) #DDDDDD srgb(221,221,221)
      1403: (238,238,238) #EEEEEE srgb(238,238,238)
    256397: (255,255,255) #FFFFFF white
  Colormap entries: 16
  Colormap:
         0: (  0,  0,  0) #000000 black
         1: ( 17, 17, 17) #111111 srgb(17,17,17)
         2: ( 34, 34, 34) #222222 srgb(34,34,34)
         3: ( 51, 51, 51) #333333 grey20
         4: ( 68, 68, 68) #444444 srgb(68,68,68)
         5: ( 85, 85, 85) #555555 srgb(85,85,85)
         6: (102,102,102) #666666 grey40
         7: (119,119,119) #777777 srgb(119,119,119)
         8: (136,136,136) #888888 srgb(136,136,136)
         9: (153,153,153) #999999 grey60
        10: (170,170,170) #AAAAAA srgb(170,170,170)
        11: (187,187,187) #BBBBBB srgb(187,187,187)
        12: (204,204,204) #CCCCCC grey80
        13: (221,221,221) #DDDDDD srgb(221,221,221)
        14: (238,238,238) #EEEEEE srgb(238,238,238)
        15: (255,255,255) #FFFFFF white
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: white
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 640x480+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: None
  Orientation: Undefined
  Properties:
    date:create: 2018-08-10T18:07:10-07:00
    date:modify: 2018-08-10T18:07:10-07:00
    signature: 786d78cb30658035043ee355755e4e69cd4793953dbffaa5dcb872809cae866e
  Artifacts:
    filename: logo3_bmp3.bmp
    verbose: true
  Tainted: False
  Filesize: 153718B
  Number pixels: 307200
  Pixels per second: 30.72MB
  User time: 0.020u
  Elapsed time: 0:01.009
  Version: ImageMagick 6.9.10-9 Q16 x86_64 2018-08-06 https://www.imagemagick.org
Jason S
Posts: 103
Joined: 2010-12-14T19:42:12-07:00
Authentication code: 8675308

Re: BMP compression?

Post by Jason S »

Yeah, your command results in what we're supposing is the smallest uncompressed file size. But it uses a fixed grayscale palette, which is unnecessarily restrictive (though it might not make a huge difference in this case).

I don't know much about tuning IM's color quantization. Before long I'd probably reach for pngquant instead.

This:

Code: Select all

convert logo: -alpha off -colors 16 -compress none BMP3:temp.bmp
uses an optimized palette of some sort, and has the same file size.

The above with "-colorspace gray" seems to use an optimized grayscale palette, but the resulting image only has 11 unique colors, so it doesn't seem very optimal.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: BMP compression?

Post by fmw42 »

If you look carefully, you will see that I changed my post. Sorry. What I posted as a change does not use -colors.

Code: Select all

convert image -alpha off -colorspace gray -depth 4 -type palette BMP3:result.bmp
And has full set of 16 colors in the color table (not filled out to 16 by black). -colors has a tendency to produce fewer colors than you specify. So I took it out and added -type palette. That seems to work better. But I cannot say if it is optimize, only that it produces 16 gray colors in a color table and histogram.

Unfortunately, I do not use BMP much and know nothing about its permitted compressions.
Post Reply