collapse collapse

* Who's Online

  • Dot Guests: 58
  • Dot Hidden: 0
  • Dot Users: 0

There aren't any users online.

* Board Stats

  • stats Total Members: 88
  • stats Total Posts: 11163
  • stats Total Topics: 1699
  • stats Total Categories: 4
  • stats Total Boards: 76
  • stats Most Online: 248

Author Topic: C++ Basic RLE Compression  (Read 3397 times)

0 Members and 1 Guest are viewing this topic.

Offline Celestialkey

  • Administrator
  • Hero Member
  • *****
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
C++ Basic RLE Compression
« on: January 11, 2015, 07:48:31 PM »
Code: C++
  1. #include <iostream>// std::cout
  2. #include <fstream> // std::fstream
  3. #include <sstream> // std::ostringstream
  4. #include <random>
  5. /*
  6.         Run Length Encoding - RLE Example to Compression
  7.         Http://Www.CelestialCoding.com
  8.  
  9.         You can go lower level then this tutorial. I will
  10.         only be looking at each byte and checking for duplicates.
  11.         You can do something similar at the macro
  12.         level (look for repeating collections of binary datasets) or even at the lowest
  13.         binary level (look for repeating binary datasets)
  14. */
  15.  
  16. // Used to generate a ~50 meg file for compression
  17. void GenerateRandomFile(){
  18.        
  19.         std::fstream fout;
  20.         fout.open("myfile.txt", std::ios::out);
  21.         for (int i = 0; i < 50000000; i++){
  22.                 int out = rand() % 2;
  23.                 fout << char(out+65);
  24.         }
  25.         fout.close();
  26. }
  27.  
  28. int main(){
  29.         GenerateRandomFile();
  30.  
  31.         // Create a file handlers.
  32.         std::fstream uncompressedFile;
  33.         std::fstream compressedFile;
  34.  
  35.         // Open the file to compress, check for errors
  36.         // Not we are opening in binary mode
  37.         uncompressedFile.open("myfile.txt", std::ios::ios_base::in|std::ios::ios_base::binary);
  38.         if (!uncompressedFile.is_open()){
  39.                 std::cout << "Unable to open file.\n";
  40.                 return -1;
  41.         }
  42.  
  43.         // Store the entire file contents for later iteration
  44.         std::ostringstream fileBuff;
  45.         fileBuff << uncompressedFile.rdbuf();
  46.         std::string     sContents = fileBuff.str();
  47.  
  48.         // Close the handle to the uncompressed file.
  49.         uncompressedFile.close();
  50.  
  51.  
  52.         // Start of the compression algorithm
  53.         // First, create a handle to a new file to write the compressed contents to
  54.         compressedFile.open("newFile.txt", std::ios::out | std::ios::binary);
  55.         if (!compressedFile.is_open()){
  56.                 std::cout << "Unable to create file.\n";
  57.                 return -2;
  58.         }
  59.  
  60.         // Iterate through stored data and look for repititions
  61.         // Start looping through the data to get the first data to check for
  62.         unsigned                uBuffSize = sContents.size()-1;
  63.         unsigned char   ucCurrentByte;
  64.         unsigned char   ucCompareByte;
  65.         unsigned                uCurrentPosition = 0;
  66.  
  67.         while (uCurrentPosition < uBuffSize){
  68.                 // Select the byte to look for repeats
  69.                 ucCompareByte = sContents.at(uCurrentPosition);
  70.                
  71.                 // Keep track of how many times the character repeats.
  72.                 // Never go above 9 repeats
  73.                 unsigned                uRepeats = 0;
  74.  
  75.                 // Find all repeats
  76.                 while (true){
  77.                         // Increment the position by 1
  78.                         uCurrentPosition++;
  79.  
  80.                         // Make sure we don't go over the buffersize
  81.                         if (uCurrentPosition > uBuffSize)
  82.                                 break;
  83.  
  84.                         // Grab the next byte
  85.                         ucCurrentByte = sContents.at(uCurrentPosition);
  86.                        
  87.                         // Check if it repeated
  88.                         if (ucCurrentByte == ucCompareByte){
  89.                                 uRepeats++;
  90.                                 if (uRepeats >= 9)
  91.                                         break;
  92.                         } else {
  93.                                 // If it does not, then we need to break out of this
  94.                                 // check, write what we found, then continue on to the
  95.                                 // next character
  96.                                 break;
  97.                         }
  98.                 }
  99.  
  100.                 // Prepare the data for writting
  101.                 std::ostringstream strOut;
  102.                 if (uRepeats)
  103.                         // If there were more than 2 repeats write the number of instances
  104.                         // and the byte that was repeated
  105.                         if(uRepeats > 2) strOut << uRepeats << ucCompareByte;
  106.                         // Otherwise, just write out the 1-2 repeats
  107.                         else for (int a = 0; a < uRepeats; a++) strOut << ucCompareByte;
  108.                 else
  109.                         // If there were no repeats, just print the character that was affected
  110.                         strOut << ucCompareByte;
  111.  
  112.                 // Write the reults to file for this comparison
  113.                 compressedFile << strOut.str();
  114.         }
  115.  
  116.         // Close the file we were writting to.
  117.         compressedFile.close();
  118.  
  119.         std::cout << "Compressed!\n";
  120.         return 0;
  121. }

Reading and decompressing the file is not show in this, but it is a simple matter of looking for identifiers (numbers 3-9 in this case) and then just repeating that data in memory to decompress the file.
« Last Edit: January 11, 2015, 07:56:50 PM by Celestialkey »
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

 

Donate


* Search


* Recent Posts

Image Comparison by Shishka
[May 15, 2017, 01:18:02 PM]


Re: srchub - free source code hosting by Nathan
[December 14, 2015, 11:37:02 PM]


Re: srchub - free source code hosting by Celestialkey
[November 27, 2015, 08:51:42 AM]


Updates by Nathan
[October 30, 2015, 08:27:36 PM]


Re: Client-Server Messaging by Nathan
[October 25, 2015, 05:48:57 PM]