PureBasic supports RLE compressed BMP-files

Share your advanced PureBasic knowledge/code with the community.
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

PureBasic supports RLE compressed BMP-files

Post by dell_jockey »

Hi,

no code in this "Tricks 'n' Tips", just a snippet of information I discovered last weekend. It might be common knowledge to many of you, but for me it was something new:

PureBasic supports reading and displaying RLE-compressed BMP-files.

I didn't know that the BMP file definition allows for compressed files, as MS-Paint and other tools on a standard MS-setup don't offer to save BMP as compressed files.
Experimenting with PhotoShop I discovered this possibility. PhotoShop offers RLE-compressed BMP formats for 1-bit, 4-bit and 8-bit formats (2, 16 and 256 colors). Apparently, 24-bit BMP's can't be RLE encoded, which probably wouldn't make sense anyway if you need that many colors. In those cases, use PNG or JPG.

Only a single RLE-compressed BMP format is not supported by PureBasic: 1-bit color files (black&white, no grey-tones or other colors.) These can't be displayed by PureBasic.

Compression rates are as good as GIF or PNG, I didn't test with other formats. Much depends on image color contents and image size. Using fewer colors = getting better compression. By the way, RLE compression is loss-less.

What's this good for?

This forum presented code to embed images in data sections of EXE-files. Using compressed BMP's for that reduces EXE image size considerably, since the data section is much smaller, and since we don't need to link in the PNG (or other) format handling runtime...
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

That is indeed handy to know. (I had no idea BMPs could be compressed, let alone that it was lossless and that PB could handle them).

Thanks!
@}--`--,-- A rose by any other name ..
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

Dare2, RLE compression is lossless :)

http://www.prepressure.com/techno/compressionrle.htm
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

I remember reading in Petzold's goliath of a book that Windows supported such compression natively, but that it was hardly ever used. I can't remember why that should be the case; maybe a speed issue?

Maybe a left over from the original OS/2 bitmap spec?
I may look like a mule, but I'm not a complete ass.
Fred
Administrator
Administrator
Posts: 18243
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Actually the compression is good for simple shape bitmaps, but for complex one, it's really not useful at all (try it on a photography for example).
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Fred wrote:Actually the compression is good for simple shape bitmaps, but for complex one, it's really not useful at all (try it on a photography for example).
Fred's right, that's why I wrote above that when color content increases, you'd better use PNG or JPG. According to what PhotoShop is offering, RLE compression is not supported for files that carry more than 8 bit color information.

For my current project (see http://www.purebasic.fr/english/viewtopic.php?t=21025 ) it's extremely useful though....
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post by Bonne_den_kule »

RLE only works on 8-bit because RLE would be useless in other formats (f.exp 24-bit).
Dr_Wildrick
User
User
Posts: 36
Joined: Fri Feb 23, 2007 8:00 pm
Location: New York

RLE compression on 24 bit (or more) images

Post by Dr_Wildrick »

Yes, the BMP file format, as it stands is very poor at compressing complex images. However, using the same very simple RLE system, you CAN make it work on 24 bit images if you are willing to break a few rules.
Targa file - back in the day hused to be the "BMP" file of the time - they were true color with no compression, however they did have a very simple, yet amazingly effective compression system - very similar to BMP files - except the way that it was done. I will explain in a moment. Don't expect Jpeg like compression out of this.
How they managed it was that RGB or ARGB triplets or quads were very random - very hard to run length encode. Machines at the time did not have the speed for a lot of complex math. So a solution was sought - what they did was separate the image into its planes. So the file was the same size except that rather than have RGB RGB RGB they had RRR RRR RRR GGG GGG GGG BBB BBB BBB because the variation on a plane are far less than those in a full RGB value - than they were just reassembled back into triplets ot quads and viola - the compression becase much more effective. To further increase compression, try Run length encoding the planes from top to bottom rather than side to side, there is less variation up and down than side to side (I would explain why but I don't want to go into image capture 101.. so lets just says its a hardware thing (and an eye thing as well)
Good luck
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Post by superadnim »

Shouldn't even mention JPEG when it comes to compression since JPEG is an encoding schema. (It DOES use RLE for the data blocks though).
Dr_Wildrick
User
User
Posts: 36
Joined: Fri Feb 23, 2007 8:00 pm
Location: New York

RLE and you

Post by Dr_Wildrick »

RLE CAN be used in Jpeg data blocks, however as they stand there are over 25 "flavors" of jpeg - the most common is the hoffman / baseline however Jpegs can be interlaced, Multi-layered, even lossless! (I don't recommend lossless) very bad compression. It is alo possible, if your are familiar with the DCT ot the FDCT to calculate the wave frequencies that would give you the same values that were there before - many have done this as "Area only" jpeg updating. - This is a horrid waste of time. But Yes, jpegs can use several types of compression. Many of the features included in the original ISO DIS 10918-2 final draft have never been implemented outside of a few experimenters. The reason is quite simple, you art asking a newbie to wavelet theory if they want sub sampling on a 1x1 or 1x2 or lossless, hoffman, Binary encoding if if they want to optimize the quantilization matrix, you have lost them at sub-sampling.
However it should be notied that most Jpeg encoders use the ISO standard Matrix - it was designed to be most efficient on images of 720 x 576. And much better matrix values can be calculated. If fact, if you know the matrix vales, its possible to use a hex editor to find them, then edit them to a more optimized matrix - this is very useful if you have a scanner which sets the compression at 70 or 80, you can go in and hack it up to 99. Much larger files, but then they are raw "for edit" anyway.
I'm rambling... sorry! I did my dissertation on wavelet behavior.
Cheers
8)
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: PureBasic supports RLE compressed BMP-files

Post by Keya »

This is a very cool FREE trick, thanks!! i just took advantage of it and shaved quite a few kilobytes off my app just by resaving my 8bit BMPs with RLE compression :) (they're just 2d stuff not photos or anything so loved the RLE!)
And even though BMP RLE is apparently a "Windows thing" i confirm PB renders them fine in Linux and OSX too! :)

I had a problem with one of my RLE BMP images though - for some weird reason its white background was being rendered as black in my PB app. Might've been something to do with nonstandard palette as my brief tests couldn't replicate it with standard palette. It looked fine in Photoshop, which is what I had used to save it.

Anyway I googled for other apps that can save as BMP with RLE and the first one I tried (freeware for personal use, ~4mb) called XnView successfully made a correctly-rendering RLE BMP that the Photoshop one had problems with, phew! Its output size in my case was not quite as reduced as Photoshops one but pretty close.

So if you experience a rendering glitch try resaving the bmp with another gfx app!
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: PureBasic supports RLE compressed BMP-files

Post by walbus »

Bevore 30 years and more i have programmed this for the Atari ST.
For bitmap graphics it is mostly good, also usable for executables.
Primary, i have used it as a simple protection for pictures.
User avatar
J. Baker
Addict
Addict
Posts: 2185
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: PureBasic supports RLE compressed BMP-files

Post by J. Baker »

Gimp on OS X supports RLE!

64x64 8-Bit image test. PNG image is shown below.

Image

1.) BMP - 5,238 bytes
2.) RLE - 4,410 bytes
3.) PNG - 3,136 bytes
4.) JPG - 7,022 bytes (Quality = 100)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
Post Reply