Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- u8* BFF2::compress(u8* imageb, int size)
- {
- cout << "Starting compression. Size of Raw image: " << size << endl;
- int obcompVal = 0;
- int* bcompCnt = new int[9];
- for(int i = 0; i < size; i++)
- {
- //Check 1 byte looped multiple times.
- //Done first because best compression.
- //cout << "New CMD." << endl;
- obcompVal = imageb[i];
- bcompCnt[0] = 0;
- for(int j = 1; j < 50; j++)
- {
- if(obcompVal != imageb[i+j])
- break;
- bcompCnt[0]++;
- }
- //if(bcompCnt[0] > 8) //If the match count is high, just encode now.
- //{
- //int cmdbyte = encodeCommandByte(1, bcompCnt[0]);
- //cout << cmdbyte << ":" << obcompVal << ":" << bcompCnt[0] << endl;
- //i+=bcompCnt[0];
- //}else //Otherwise it's time to start searching for a good compression.
- //{
- //We want to check every amount: 1-8 extra
- for(int j = 1; j < 9; j++)
- {
- if(i+j > size)
- break;
- bcompCnt[j] = 0;
- //Fill up a buffer to compare with.
- int* mbcompVals = new int[j+1];
- for(int n = 0; n <= j; n++)
- mbcompVals[n] = imageb[i+n];
- //Now loop up to the max amount of possible
- //loops (8) to see how many matches
- for(int l = 1; l < 9; l++)
- {
- bool match = true;
- for(int n = 0; n <= j; n++)
- {
- if(mbcompVals[n] != imageb[i+(l*(j+1))+n])
- match = false;
- }
- if(match == false)
- break;
- bcompCnt[j]++;
- }
- }
- //}
- //Check the highest compression
- int highest = 1;
- int ind = 0;
- for(int j = 0; j < 9; j++)
- {
- //cout << "h:"<<highest<<" i:"<<j<<" a:"<<bcompCnt[j]<<" v:"<<(j+1)*bcompCnt[j]<< endl;
- if(((j+1)*bcompCnt[j]) >= highest)
- {
- highest = ((j+1)*bcompCnt[j]);
- ind = j;
- }
- }
- int cmdbyte = encodeCommandByte(ind+1, bcompCnt[ind]);
- //if(ind != 0)
- //cout << i << "." << ind << "=" << hex << cmdbyte << ":" << dec << bcompCnt[ind] << ":";
- if(i+ind>=size)
- break;
- cout << setfill('0') << setw(2) << hex << cmdbyte << " ";
- for(int n = 0; n <= ind; n++)
- cout << setfill('0') << setw(2) << hex << (int)imageb[i+n] << " ";
- //cout << " - " << ((ind+1)*bcompCnt[ind]);
- //cout << endl;
- //if(ind == 0 && bcompCnt[ind] == 0)
- //bcompCnt[ind] = 1;
- i+=((ind+1)*(bcompCnt[ind]+1))-1;
- }
- return imageb;
- }
Advertisement
Add Comment
Please, Sign In to add comment