Kafke

Compression

Dec 18th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. u8* BFF2::compress(u8* imageb, int size)
  2. {
  3.     cout << "Starting compression. Size of Raw image: " << size << endl;
  4.     int obcompVal = 0;
  5.     int* bcompCnt = new int[9];
  6.     for(int i = 0; i < size; i++)
  7.     {
  8.         //Check 1 byte looped multiple times.
  9.         //Done first because best compression.
  10.         //cout << "New CMD." << endl;
  11.         obcompVal = imageb[i];
  12.         bcompCnt[0] = 0;
  13.         for(int j = 1; j < 50; j++)
  14.         {
  15.             if(obcompVal != imageb[i+j])
  16.                 break;
  17.             bcompCnt[0]++;
  18.         }
  19.         //if(bcompCnt[0] > 8) //If the match count is high, just encode now.
  20.         //{
  21.             //int cmdbyte = encodeCommandByte(1, bcompCnt[0]);
  22.             //cout << cmdbyte << ":" << obcompVal << ":" << bcompCnt[0] << endl;
  23.             //i+=bcompCnt[0];
  24.         //}else //Otherwise it's time to start searching for a good compression.
  25.         //{
  26.             //We want to check every amount: 1-8 extra
  27.             for(int j = 1; j < 9; j++)
  28.             {
  29.                 if(i+j > size)
  30.                     break;
  31.                 bcompCnt[j] = 0;
  32.                 //Fill up a buffer to compare with.
  33.                 int* mbcompVals = new int[j+1];
  34.                 for(int n = 0; n <= j; n++)
  35.                     mbcompVals[n] = imageb[i+n];
  36.  
  37.                 //Now loop up to the max amount of possible
  38.                 //loops (8) to see how many matches
  39.                 for(int l = 1; l < 9; l++)
  40.                 {
  41.                     bool match = true;
  42.                     for(int n = 0; n <= j; n++)
  43.                     {
  44.                         if(mbcompVals[n] != imageb[i+(l*(j+1))+n])
  45.                             match = false;
  46.                     }
  47.                     if(match == false)
  48.                         break;
  49.                     bcompCnt[j]++;
  50.                 }
  51.             }
  52.         //}
  53.  
  54.         //Check the highest compression
  55.         int highest = 1;
  56.         int ind = 0;
  57.         for(int j = 0; j < 9; j++)
  58.         {
  59.             //cout << "h:"<<highest<<" i:"<<j<<" a:"<<bcompCnt[j]<<" v:"<<(j+1)*bcompCnt[j]<< endl;
  60.             if(((j+1)*bcompCnt[j]) >= highest)
  61.             {
  62.                 highest = ((j+1)*bcompCnt[j]);
  63.                 ind = j;
  64.             }
  65.         }
  66.         int cmdbyte = encodeCommandByte(ind+1, bcompCnt[ind]);
  67.         //if(ind != 0)
  68.         //cout << i << "." << ind << "=" << hex << cmdbyte << ":" << dec << bcompCnt[ind] << ":";
  69.         if(i+ind>=size)
  70.             break;
  71.         cout << setfill('0') << setw(2) << hex << cmdbyte << " ";
  72.         for(int n = 0; n <= ind; n++)
  73.             cout << setfill('0') << setw(2) << hex << (int)imageb[i+n] << " ";
  74.         //cout << " - " << ((ind+1)*bcompCnt[ind]);
  75.         //cout << endl;
  76.         //if(ind == 0 && bcompCnt[ind] == 0)
  77.             //bcompCnt[ind] = 1;
  78.         i+=((ind+1)*(bcompCnt[ind]+1))-1;
  79.     }
  80.  
  81.     return imageb;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment