/* * RLE - TP 1 du cours 8INF241 – Multimédia * * Copyright 2008 Michaël Larouche * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include #include using namespace std; static const char *input25Filename = "input25File.txt"; static const char *input100Filename = "input100File.txt"; static const char *out25Filename = "out25File.txt"; static const char *out100Filename = "out100File.txt"; void compressRle(fstream &input, fstream &output) { char readChar; char currentChar; int numChar = 1; input >> readChar; currentChar = readChar; while( !input.eof() ) { input >> readChar; if( readChar != currentChar || input.eof() ) { // Write frequency,letter and separator to output output << numChar << currentChar << "/"; // Reset currentChar and numChar numChar = 1; currentChar = readChar; } else { numChar++; } } } int fileSize(fstream &file) { int size = 0; file.seekg(0, ios_base::end); size = file.tellg(); file.seekg(0, ios_base::beg); return size; } void showContent(fstream &file) { file.seekg(0, fstream::beg); char temp; file >> temp; while( !file.eof() ) { cout << temp; file >> temp; } cout << endl; file.clear(); } void showCompressionRate(fstream &input, fstream &output) { int inputFileSize = fileSize(input); int outputFileSize = fileSize(output); int compressionRate = static_cast( (static_cast(outputFileSize) / static_cast(inputFileSize) ) * 100 ); cout << "Taille du fichier initial: " << inputFileSize << endl; cout << "Taille du fichier code: " << outputFileSize << endl; cout << "Taux de compression: " << compressionRate << "%" << endl; cout << "Gain de compression: " << 100-compressionRate << "%" << endl; } int main(int argc, char **argv) { cout << "Programme de compression de texte par principe de RLE" << endl; cout << "Par Michael Larouche " << endl << endl; // 25 character file fstream input25File(input25Filename, fstream::in); if( !input25File ) { cerr << "Erreur lors de l'ouverture du fichier " << input25Filename << ". Verifiez les droits d'accès du fichier." << endl; return 1; } fstream out25File(out25Filename, fstream::out); if( !out25File ) { cerr << "Erreur lors de l'ouverture du fichier " << out25Filename << ". Verifiez que vous avez les droits d'ecriture dans le repertoire." << endl; } // Do the RLE compression compressRle(input25File, out25File); // Clear the input file and re-open the out file as input input25File.clear(); out25File.close(); out25File.open(out25Filename, fstream::in ); cout << "Pour le fichier à 25 caractères:" << endl; cout << "Contenue du fichier original:" << endl; showContent(input25File); cout << "Contenue du fichier code:" << endl; showContent(out25File); showCompressionRate(input25File, out25File); cout << endl; // 100 character file fstream input100File(input100Filename, fstream::in); if( !input100File ) { cerr << "Erreur lors de l'ouverture du fichier " << input100Filename << ". Verifiez les droits d'accès du fichier." << endl; return 1; } fstream out100File(out100Filename, fstream::out); if( !out100File ) { cerr << "Erreur lors de l'ouverture du fichier " << out100Filename << ". Verifiez que vous avez les droits d'ecriture dans le repertoire." << endl; } compressRle(input100File, out100File); input100File.clear(); out100File.close(); out100File.open(out100Filename, fstream::in ); cout << "Pour le fichier à 100 caractères:" << endl; cout << "Contenue du fichier original:" << endl; showContent(input100File); cout << "Contenue du fichier code:" << endl; showContent(out100File); showCompressionRate(input100File, out100File); cout << endl; return 0; }