m2.enlarge-your-data/src/rle2/eydrle2_compress.cpp
2005-10-31 17:54:08 +00:00

57 lines
1.5 KiB
C++

#include "eydrle2.hh"
namespace EydTools {
void EydRle2::compress(){
EydLib::BitGroup data;
EydLib::BitReader bitread(_cellsize, 256); // on lit bit à bit expres
bitread.open(_input_file);
EydLib::BitWriter bitwrite(_cellsize,256);
bitwrite.open(_output_file);
unsigned char c = (unsigned char)_cellsize;
bitwrite.writeDirect(&c, 1);
EydLib::BitCompressorRle2 compressor(_cellsize);
printf("File opened\n");
bool done=false;
std::vector<EydLib::BitGroup> record;
while(!done){
try{
data = bitread.read();
compressor.append(data);
if (compressor.hasContent()){
std::list<EydLib::BitGroup> compressedData = compressor.flush();
std::list<EydLib::BitGroup>::iterator cmpDataIt;
for(cmpDataIt = compressedData.begin();
cmpDataIt != compressedData.end();
cmpDataIt++){
bitwrite.write((*cmpDataIt)); // cellule
}
}
} catch (EydLib::eBitReaderEndOfFile& e) {
done = true;
// on flushe le contenu de record
compressor.forceFlush();
std::list<EydLib::BitGroup> compressedData = compressor.flush();
std::list<EydLib::BitGroup>::iterator cmpDataIt;
for(cmpDataIt = compressedData.begin();
cmpDataIt != compressedData.end();
cmpDataIt++){
bitwrite.write((*cmpDataIt)); // cellule
}
}
}
printf("compression done\n");
bitread.close();
bitwrite.close();
printf("file closed\n");
printf("Compression ratio : %f\n", compressor.getRatio());
}
}