Message-ID: <366AB03F.DC924D2@geocities.com> From: Sahab Yazdani X-Mailer: Mozilla 4.5 [en] (Win95; I) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.os.msdos.djgpp Subject: Compression Algo... (Please Help) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 180 Date: Sun, 06 Dec 1998 16:26:39 +0000 NNTP-Posting-Host: 209.103.50.171 X-Complaints-To: abuse AT sprint DOT ca X-Trace: hme2.newscontent-01.sprint.ca 912979668 209.103.50.171 (Sun, 06 Dec 1998 16:27:48 EDT) NNTP-Posting-Date: Sun, 06 Dec 1998 16:27:48 EDT Organization: Sprint Canada Inc. To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com I've created a simple run-length compression Algorytm for graphics for a project... The project was to convert the PCX (256 colour) format into my own format, anyways I realized that converting to a raw format was going to make the files too big. So I decided on the run-length. My code was designed like this: Covert the PCX file to a raw graphic (no compression) and then take that data and compress. (The portion of my code that reads the original PCX file is ok and works fine) My compression code will leave out the Header that includes the size of the image and the palette information (This is (256 * 3) + 4 bytes long) and just write them into the output file. And then reads 3 bytes from the file, sees if they are the same (a=b=c) Now if they are the same, it keeps on reading until a byte that doesn't equal a. It counts how many of the same it took (up to 256) and writes two bytes into the output file (the colour of the pixel, and then the number of occurances). now if they weren't the same it'll still write two bytes to the output file: the colour of the pixel and 0 to indicate that the pixel only happend once. Then repeats... This is generally it, so heres the code (doesn't include conversion of PCX to raw...): ART2CMP.CC #include #include #include // Simple Run-Length Compressor int main( int argc, char *argv[] ) { FILE *in, *out; long counter; unsigned char run_length[3]; unsigned char next; long run_counter = 0; int first = 1; if ( argc != 3 ) { printf( "Usage: ART2CMP.EXE [source] [destination]\n" ); return 1; } in = fopen( argv[1], "rb" ); out = fopen( argv[2], "wb" ); if ( in == NULL ) { printf( "Input file is not found or corrupted\n" ); return 1; } if ( out == NULL ) { printf( "Output file is not found or corrupted\n" ); fclose( in ); return 1; } printf( "Compressing..." ); fflush( stdout ); for ( counter=0;counter<(255 * 3) + 4 ;counter++ ) { fputc( fgetc( in ), out ); } while ( !feof( in ) ) { run_length[0] = fgetc( in ); run_length[1] = fgetc( in ); run_length[2] = fgetc( in ); if ( ( run_length[0] == run_length[1] ) && ( run_length[0] == run_length[2] ) ) { while ( run_counter != 255 ) { next = fgetc( in ); if ( next != run_length[0] ) break; run_counter++; } fputc( run_length[0], out ); fputc( run_counter, out ); // Number of occurances + 3 printf( "%i, %i\n", run_length[0], run_counter ); first = 1; run_counter = 0; } else { fseek( in, -2, SEEK_CUR ); // Move back 2 spaces fputc( run_length[0], out ); fputc( 0, out ); // Only 1 occurance printf( "%i, %i\n", run_length[0], 0 ); // I port to a file to make sure that the output is correct // run_length[0] = run_length[1]; // run_length[1] = run_length[2]; } } printf( "Done!\n" ); fclose( in ); fclose( out ); return 1; } Now i'm not sure but I think that this is also working fine but that my decoder is kinda screwed... DECODER.CC - Method LoadFile() int LoadFile() { FILE *in; unsigned int counter=0; unsigned char run_length[2]; unsigned int count; in=fopen( filename, "rb" ); if (in==NULL) { return 0; } length = ( fgetc(in) * 256 ) + fgetc(in); width = ( fgetc(in) * 256 ) + fgetc(in); bmp_ptr = (unsigned char *) malloc( GetLength() * GetWidth() ); if (bmp_ptr==NULL) { return 0; } for (counter=0;counter<255;counter++) { palette->SetRGB( counter, GRGB( fgetc( in ), fgetc( in ), fgetc( in ) ) ); } /* for (counter=0;counter