Added function pack_bits_size().

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@11438 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Pfeiffer 2005-02-21 17:34:21 +00:00
parent 5d4b78560b
commit 367817c939

View File

@ -9,6 +9,7 @@
#include <fstream> #include <fstream>
#endif #endif
#include <stdio.h>
#include <string.h> #include <string.h>
#include "PackBits.h" #include "PackBits.h"
@ -29,100 +30,120 @@ enum STATUS {
MATCHED MATCHED
}; };
int pack_bits(unsigned char *pOut, unsigned char *pIn, int n) #define WRITE_TO_RUN_BUF(byte) { if (write) *runbuf ++ = byte; else runbuf ++; }
{ #define WRITE_CONTROL(byte) { if (write) *control = byte; }
int i;
unsigned char *control;
unsigned char *runbuf;
unsigned char thisbyte;
unsigned char runbyte;
STATUS status;
i = 0; template <bool write>
status = INITIAL; class PackBits {
control = runbuf = pOut; public:
runbyte = *pIn++; static int doIt(unsigned char *pOut, const unsigned char *pIn, int n)
{
while (--n) { int i;
thisbyte = *pIn++; unsigned char *control;
switch (status) { unsigned char *runbuf;
case INITIAL: unsigned char thisbyte;
control = runbuf++; unsigned char runbyte;
*runbuf++ = runbyte; STATUS status;
if (thisbyte == runbyte) {
status = UNDECIDED; i = 0;
} else { status = INITIAL;
runbyte = thisbyte; control = runbuf = pOut;
status = UNMATCHED; runbyte = *pIn++;
}
i = 1; while (--n) {
break; thisbyte = *pIn++;
switch (status) {
case UNDECIDED: case INITIAL:
if (i == MAXINBYTES) { control = runbuf++;
*control = CONTROL2(i); WRITE_TO_RUN_BUF(runbyte);
*runbuf++ = runbyte;
runbyte = thisbyte;
status = INITIAL;
} else if (thisbyte == runbyte) {
if (i > 1) {
*control = CONTROL2(i - 2);
control = runbuf - 1;
}
i = 2;
status = MATCHED;
} else {
*runbuf++ = runbyte;
runbyte = thisbyte;
status = UNMATCHED;
i++;
}
break;
case UNMATCHED:
if (i == MAXINBYTES) {
*control = CONTROL2(i);
status = INITIAL;
} else {
if (thisbyte == runbyte) { if (thisbyte == runbyte) {
status = UNDECIDED; status = UNDECIDED;
} else {
runbyte = thisbyte;
status = UNMATCHED;
} }
i++; i = 1;
} break;
*runbuf++ = runbyte;
runbyte = thisbyte; case UNDECIDED:
break; if (i == MAXINBYTES) {
WRITE_CONTROL(CONTROL2(i));
case MATCHED: WRITE_TO_RUN_BUF(runbyte);
if ((thisbyte != runbyte) || (i == MAXINBYTES)) { runbyte = thisbyte;
runbuf = control; status = INITIAL;
*runbuf++ = CONTROL1(i); } else if (thisbyte == runbyte) {
*runbuf++ = runbyte; if (i > 1) {
WRITE_CONTROL(CONTROL2(i - 2));
control = runbuf - 1;
}
i = 2;
status = MATCHED;
} else {
WRITE_TO_RUN_BUF(runbyte);
runbyte = thisbyte;
status = UNMATCHED;
i++;
}
break;
case UNMATCHED:
if (i == MAXINBYTES) {
WRITE_CONTROL(CONTROL2(i));
status = INITIAL;
} else {
if (thisbyte == runbyte) {
status = UNDECIDED;
}
i++;
}
WRITE_TO_RUN_BUF(runbyte);
runbyte = thisbyte; runbyte = thisbyte;
status = INITIAL; break;
} else {
i++; case MATCHED:
if ((thisbyte != runbyte) || (i == MAXINBYTES)) {
runbuf = control;
WRITE_TO_RUN_BUF(CONTROL1(i));
WRITE_TO_RUN_BUF(runbyte);
runbyte = thisbyte;
status = INITIAL;
} else {
i++;
}
break;
} }
}
switch (status) {
case INITIAL:
WRITE_TO_RUN_BUF(CONTROL2(1));
break;
case UNDECIDED:
case UNMATCHED:
WRITE_CONTROL(CONTROL2(i));
break;
case MATCHED:
runbuf = control;
WRITE_TO_RUN_BUF(CONTROL1(i));
break; break;
} }
WRITE_TO_RUN_BUF(runbyte);
return runbuf - pOut;
} }
};
switch (status) {
case INITIAL:
*runbuf++ = CONTROL2(1);
break;
case UNDECIDED:
case UNMATCHED:
*control = CONTROL2(i);
break;
case MATCHED:
runbuf = control;
*runbuf++ = CONTROL1(i);
break;
}
*runbuf++ = runbyte;
return runbuf - pOut; int pack_bits_size(const unsigned char *pIn, int n)
{
PackBits<false> calculateSize;
return calculateSize.doIt(NULL, pIn, n);
}
int pack_bits(unsigned char *pOut, const unsigned char *pIn, int n)
{
PackBits<true> compress;
return compress.doIt(pOut, pIn, n);
} }
#ifdef DBG_CON_STREAM #ifdef DBG_CON_STREAM
@ -132,24 +153,35 @@ int main(int argc, char **argv)
return -1; return -1;
} }
ifstream ifs(*++argv, ios::binary | ios::nocreate); FILE *input = fopen(*++argv, "rb");
if (!ifs) { if (input == NULL) {
return -1; return -1;
} }
ifs.seekg(0, ios::end); FILE *output = fopen("rle.out", "wb");
long size = ifs.tellg(); if (output == NULL) {
ifs.seekg(0, ios::beg); fclose(input);
return -1;
}
fseek(input, 0, SEEK_END);
long size = ftell(input);
fseek(input, 0, SEEK_SET);
unsigned char *pIn = new unsigned char[size]; unsigned char *pIn = new unsigned char[size];
unsigned char *pOut = new unsigned char[size * 3]; fread(pIn, size, 1, input);
ifs.read(pIn, size); long outSize = pack_bits_size(pIn, size);
printf("input size: %d\noutput size: %d\n", (int)size, (int)outSize);
int cnt = PackBits(pOut, pIn, size); unsigned char *pOut = new unsigned char[outSize];
ofstream ofs("test.bin", ios::binary); int cnt = pack_bits(pOut, pIn, size);
ofs.write(pOut, cnt);
fwrite(pOut, cnt, 1, output);
fclose(input);
fclose(output);
delete [] pIn; delete [] pIn;
delete [] pOut; delete [] pOut;