RLE (Run-Length-Encoding) Compression Routine in MFC for 256-Color Bitmaps

Environment: VC6, MFC

This is a RLE compression routine, written in MFC. I wrote this because I cannot find handy code for such a thing.

RLE compression is useful when saving a SIMPLE Windows bitmap. My implementation is rather stupid and not optimized. Therefore, if the source bitmap is complex (like the one in the demo app window shown below), you could even get a bigger bitmap file after compression.



Click here for a larger image.

The compression routine is wrapped in an MFC bitmap control class, CDVDBitmapControl, which you can put conveniently in a dialog. The usage is easy:

  1. Add a static control (say, IDC_STATIC_BITMAP) in the dialog for the bitmap control.
  2. Add an #include "DVDBitmapControl.h" in the header file of your dialog.
  3. Add a member variable (say, m_bmp) of type "CDVDBitmapControl" in your dialog class.
  4. In OnInitDialog(), add this line:
    m_bmp.SubclassDlgItem( IDC_STATIC_BITMAP, this).
  5. Call m_bmp.LoadBitmapFile(CString) to load a bitmap into the control.
  6. Call m_bmp.SaveBitmapFile(CString) to save a bitmap into an 8-bit RLE bitmap.

If you need only the compression code, you can just extract the CDVDBitmapControl::CompressInRLE8() and CDVDBitmapControl::EndOfLine() routines from "DVDBitmapControl.cpp". It is quite straightforward to use them.

I did not write the decompression part because the Windows API LoadImage() already does it.

PS: in the demo app, if the loaded bitmap is not in 256 colors, [Save] will not work.

Downloads

Download demo project - 20 Kb
Download source - 5 Kb


Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Hundreds of millions of users have adopted public cloud storage solutions to satisfy their Private Online File Sharing and Collaboration (OFS) needs. With new headlines on cloud privacy issues appearing almost daily, the need to explore private alternatives has never been stronger. Join ESG Senior Analyst Terri McClure and Connected Data in this on-demand webinar to take a look at the business drivers behind OFS adoption, how organizations can benefit from on-premise deployments, and emerging private OFS …

Most Popular Programming Stories

More for Developers

RSS Feeds