c++ - Where can I find example of 24 bit bitmap image RLE -


i understand 24 bit bitmap image may not compress using rle. however, written digital circuit in vhdl sends vga signal display. in testbench want write down image data shall converted bitmap file using c program. since each image 800x600 in size, better if the file compressed , reduce file significantly. bitmap format supports run length encoding compression.

(1) have looked rle examples. however, can't find 24 bit bitmap image. can point out example? alternatively. there application me save 24 bit bitmap file can use hex editor , learn how format saved?

(2) also, example of rle used everywhere on internet below:

03 04 05 06 00 03 45 56 67 00 02 78 00 02 05 01 02 78 00 00 09 1e 00 01 

this bitmap expand follows (two-digit values represent color index single pixel):

04 04 04 06 06 06 06 06 45 56 67 78 78 move current position 5 right , 1 down 78 78 end of line 1e 1e 1e 1e 1e 1e 1e 1e 1e end of rle bitmap 

why 45 56 67 expand same thing?

ok, understand how write 24 bit rle. have written program file generated not being recognized paint-it. not sure if paint-it , other apps on machine not recognize 24 bit rle bitmap or bitmap file generated program wrong. thus:

#include <iostream> #include <windows.h> #include <fstream> #include <time.h>  using namespace std;  #pragma pack(push) #pragma pack(1)  struct bitmap_file_header_struct {     word    bftype;        // must 'bm'      dword   bfsize;        // size of whole .bmp file     word    bfreserved1;   // must 0     word    bfreserved2;   // must 0     dword   bfoffbits; } bitmap_file_header;  struct bitmap_file_info_struct {     dword  bisize;            // size of structure     long   biwidth;           // image width     long   biheight;          // image height     word   biplanes;          // bitplanes     word   bibitcount;        // resolution      dword  bicompression;     // compression     dword  bisizeimage;       // size of image     long   bixpelspermeter;   // pixels per meter x     long   biypelspermeter;   // pixels per meter y     dword  biclrused;         // colors used     dword  biclrimportant;    // important colors } bitmap_file_info;  #pragma pack(pop)  int main() {     // image 10 repeating pixels of value ff00ff followed end of scaneline , end of rle     unsigned char data[] = { 0x0a, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x01};       bitmap_file_header.bftype = 0x4d42; // fixed bitmap in windows     bitmap_file_header.bfsize = sizeof(bitmap_file_header) + sizeof(bitmap_file_info) + sizeof(data);      bitmap_file_header.bfreserved1 = 0; // fixed      bitmap_file_header.bfreserved2 = 0; // fixed     bitmap_file_header.bfoffbits = sizeof(bitmap_file_header) + sizeof(bitmap_file_info); //      bitmap_file_info.bisize = sizeof(bitmap_file_info);     bitmap_file_info.biwidth = 10;     bitmap_file_info.biheight = 1;     bitmap_file_info.biplanes = 1; // fixed     bitmap_file_info.bibitcount = 24; // fixed 24 bit image     bitmap_file_info.bicompression = 4; // fixed, 4 used set 24 bit run length encoding     bitmap_file_info.bisizeimage = sizeof(data); // <-     bitmap_file_info.bixpelspermeter = 0; // fixed     bitmap_file_info.biypelspermeter = 0; // fixed     bitmap_file_info.biclrused = 0; // colors used     bitmap_file_info.biclrimportant = 0; // colors important      std::ofstream myfile;     myfile.open("output.bmp", ios::out | ios::binary);     myfile.write((const char *)&bitmap_file_header, sizeof(bitmap_file_header));     myfile.write((const char *)&bitmap_file_info, sizeof(bitmap_file_info));     myfile.write((const char *)&data, sizeof(data));     myfile.close();       return 0; } 

the basic form of rle consists of pairs (or frames) of bytes of form <count> <value>. clearly, in form never have <count> byte zero.

that allows use of 0 act control code, signalling different type of frame in data.

your particular example has been encoded using wmf (windows meta file) rle format. wmf allows following types of frame, of must introduced using 00 control code:

  • 01 - end of data stream
  • 02 <x> <y> - relative move
  • 03 <raw uncompressed data stream follows> 00 - raw uncompressed data follows null terminated.

this explains why see 45 56 67 in uncompressed output.

as why format uses approach? rle inefficient encoding data not repeat. consider 45 56 67:

  • the simple approach renders 01 45 01 56 01 67 (6 bytes);
  • the extended approach results in 00 03 45 56 67 00 (also 6 bytes).

a better example if needed encode 01 02 03 04 05 06 07 08 09 0a:

  • this require 20 bytes using basic frames;
  • but 13 bytes using 00 03 <data> 00 frame

Comments