/*
- Copyright (C) Arturo San Emeterio Campos 1999. All rights reserved.
- Permission is granted to make verbatim copies of this program for private
- use only. There is ABSOLUTELY NO WARRANTY. Use it at your OWN RISK.
+ Copyright (C) Arturo San Emeterio Campos 1999. All rights reserved.
+ Permission is granted to make verbatim copies of this program for private
+ use only. There is ABSOLUTELY NO WARRANTY. Use it at your OWN RISK.
- This file is: "unppmc.c"
- Email: arturo@arturocampos.com
- Web: http://www.arturocampos.com
+ This file is: "unppmc.c"
+Email: arturo@arturocampos.com
+Web: http://www.arturocampos.com
- Part of the ppmc decoder.
+Part of the ppmc decoder.
- This module is the main module and calls the different modules to do
- the decoding of a file. When done prints kbyps.
+This module is the main module and calls the different modules to do
+the decoding of a file. When done prints kbyps.
*/
//Main
void main (int argc, char *argv[])
{
- unsigned long counter, //temporal counter for loops like for or while
- counter2, //another temporal counter for sub loops
- size_file_output, //the size of the output file
- main_counter; //used in main
- char expected_flush=0; //used for checking flushing which can't be done
-
-
- // Print title, version and copyright
- printf("UNPPMC using range coder.\n");
- printf("Copyright (C) Arturo San Emeterio Campos 1999. All rights reserved.\n");
- printf("Permission is granted to make verbatim copies of this program for private\n");
- printf("use only. There is ABSOLUTELY NO WARRANTY. Use it at your OWN RISK.\n");
-
-
-
- // Check for correct number of parameters
- if(argc!=3)
- {
- printf("Bad number of arguments.\n");
- exit(1);
- }
-
-
- // Try to open input and output files
- if((file_input=fopen(argv[1],"r+b"))==NULL)
- {
- printf("Couldn't open %s.\n",argv[1]);
- exit(1);
- }
-
- if((file_output=fopen(argv[2],"w+b"))==NULL)
- {
- printf("Couldn't create %s.\n",argv[2]);
- exit(1);
- }
-
-
- // Get output length
- fread(&size_file_output,1,4,file_input);
-
+ unsigned long counter, //temporal counter for loops like for or while
+ counter2, //another temporal counter for sub loops
+ size_file_output, //the size of the output file
+ main_counter; //used in main
+ char expected_flush=0; //used for checking flushing which can't be done
+
+
+ // Print title, version and copyright
+ printf("UNPPMC using range coder.\n");
+ printf("Copyright (C) Arturo San Emeterio Campos 1999. All rights reserved.\n");
+ printf("Permission is granted to make verbatim copies of this program for private\n");
+ printf("use only. There is ABSOLUTELY NO WARRANTY. Use it at your OWN RISK.\n");
+
+
+
+ // Check for correct number of parameters
+ if(argc!=3)
+ {
+ printf("Bad number of arguments.\n");
+ exit(1);
+ }
+
+
+ // Try to open input and output files
+ if((file_input=fopen(argv[1],"r+b"))==NULL)
+ {
+ printf("Couldn't open %s.\n",argv[1]);
+ exit(1);
+ }
+
+ if((file_output=fopen(argv[2],"w+b"))==NULL)
+ {
+ printf("Couldn't create %s.\n",argv[2]);
+ exit(1);
+ }
+
+
+ // Get output length
+ fread(&size_file_output,1,4,file_input);
+
- // Initialize ppmc decoder
- ppmc_alloc_memory();
- ppmc_initialize_contexts();
- ppmc_decoder_initialize();
+ // Initialize ppmc decoder
+ ppmc_alloc_memory();
+ ppmc_initialize_contexts();
+ ppmc_decoder_initialize();
- // Initialize decoder
- range_decoder_init(&rc_decoder,file_input);
+ // Initialize decoder
+ range_decoder_init(&rc_decoder,file_input);
- // Start main loop which decodes the file
- main_counter=size_file_output-4; //take in account the bytes already written
- expected_flush=0; //we don't expect a flush yet
+ // Start main loop which decodes the file
+ main_counter=size_file_output-4; //take in account the bytes already written
+ expected_flush=0; //we don't expect a flush yet
- while(main_counter!=0)
- {
+ while(main_counter!=0)
+ {
-// Try to decode current byte in order-4 if possible, else in lower ones
-ppmc_decode_order4();
-if(byte==-1)
- ppmc_decode_order3();
- if(byte==-1)
- {
- ppmc_decode_order2();
- if(byte==-1)
- {
- ppmc_decode_order1();
- if(byte==-1)
- {
- ppmc_decode_order0();
- if(byte==-1) //check if it was an escape code
- {
- // Decode in order-(-1)
- ppmc_get_totf_ordern1();
- symb_cump=range_decoder_decode(&rc_decoder,total_cump);
- byte=ppmc_get_symbol_ordern1();
- ppmc_get_prob_ordern1();
- range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
- coded_in_order=0; //update all orders
+ // Try to decode current byte in order-4 if possible, else in lower ones
+ ppmc_decode_order4();
+ if(byte==-1)
+ ppmc_decode_order3();
+ if(byte==-1)
+ {
+ ppmc_decode_order2();
+ if(byte==-1)
+ {
+ ppmc_decode_order1();
+ if(byte==-1)
+ {
+ ppmc_decode_order0();
+ if(byte==-1) //check if it was an escape code
+ {
+ // Decode in order-(-1)
+ ppmc_get_totf_ordern1();
+ symb_cump=range_decoder_decode(&rc_decoder,total_cump);
+ byte=ppmc_get_symbol_ordern1();
+ ppmc_get_prob_ordern1();
+ range_decoder_update(&rc_decoder,total_cump,symb_cump,symb_prob);
+ coded_in_order=0; //update all orders
- // Now see if it's the code of flushing
+ // Now see if it's the code of flushing
- if(symb_cump==256)
- {
- printf("Flushing.\n");
- ppmc_flush_mem_dec();
- expected_flush=0;
- continue; //do not output byte nor update
- }
+ if(symb_cump==256)
+ {
+ printf("Flushing.\n");
+ ppmc_flush_mem_dec();
+ expected_flush=0;
+ continue; //do not output byte nor update
+ }
- }
- }
- }
- }
+ }
+ }
+ }
+ }
- // Output byte and update model
+ // Output byte and update model
- fputc(byte,file_output);
+ fputc(byte,file_output);
- switch(coded_in_order) //update exclusion
- {
- case 0: ppmc_update_order0(); //update only order-0
- case 1: ppmc_update_order1(); //update order-0 and order-1
- case 2: ppmc_update_dec_order2(); //update order-0 1 and 2
- case 3: ppmc_update_dec_order3();
- case 4: ppmc_update_dec_order4();
- default: break;
- };
+ switch(coded_in_order) //update exclusion
+ {
+ case 0: ppmc_update_order0(); //update only order-0
+ case 1: ppmc_update_order1(); //update order-0 and order-1
+ case 2: ppmc_update_dec_order2(); //update order-0 1 and 2
+ case 3: ppmc_update_dec_order3();
+ case 4: ppmc_update_dec_order4();
+ default: break;
+ };
- // Check if flushing has to be done and has not been done.
- // This is optional, in case you limit the memory usage, you don't
- // need to include this
+ // Check if flushing has to be done and has not been done.
+ // This is optional, in case you limit the memory usage, you don't
+ // need to include this
- if(expected_flush==1) // If flushing didn't happen, we can't decode
- {
- printf("Can't decompress file. Not enough memory.\nTry in a machine with more memory.\n");
- exit(1);
- }
- if(ppmc_out_of_memory==1)
- {
- expected_flush=1; // Next code must be a flush code, otherwise we don't
- // have enough memory, and therefore we can't decode
- }
+ if(expected_flush==1) // If flushing didn't happen, we can't decode
+ {
+ printf("Can't decompress file. Not enough memory.\nTry in a machine with more memory.\n");
+ exit(1);
+ }
+ if(ppmc_out_of_memory==1)
+ {
+ expected_flush=1; // Next code must be a flush code, otherwise we don't
+ // have enough memory, and therefore we can't decode
+ }
- // Update order variables
+ // Update order variables
- o4_byte=o3_byte;
- o3_byte=o2_byte;
- o2_byte=o1_byte;
- o1_byte=byte; //current one, is next time order-1
+ o4_byte=o3_byte;
+ o3_byte=o2_byte;
+ o2_byte=o1_byte;
+ o1_byte=byte; //current one, is next time order-1
- // Byte decoded and model updated, loop
- main_counter--;
+ // Byte decoded and model updated, loop
+ main_counter--;
- }
+ }
- ppmc_free_memory();
+ ppmc_free_memory();
- // Close file handles and free memory
- fclose(file_input);
- fclose(file_output);
-
-
- // Nicely exit
- exit(0);
+ // Close file handles and free memory
+ fclose(file_input);
+ fclose(file_output);
+
+
+ // Nicely exit
+ exit(0);
}
// Returns the file size of a given file.
long filesize(FILE *stream)
{
- long curpos, length;
+ long curpos, length;
- curpos = ftell(stream);
- fseek(stream, 0L, SEEK_END);
- length = ftell(stream);
- fseek(stream, curpos, SEEK_SET);
- return length;
+ curpos = ftell(stream);
+ fseek(stream, 0L, SEEK_END);
+ length = ftell(stream);
+ fseek(stream, curpos, SEEK_SET);
+ return length;
}