/*
- Copyright (C) Arturo San Emeterio Campos 1999. All rights reserved.
- Permission is granted to make verbatim copies of this file 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 file for private
+ use only. There is ABSOLUTELY NO WARRANTY. Use it at your OWN RISK.
- This file is: "ppmcmain.c"
- Email: arturo@arturocampos.com
- Web: http://www.arturocampos.com
+ This file is: "ppmcmain.c"
+Email: arturo@arturocampos.com
+Web: http://www.arturocampos.com
- Part of the ppmc encoder only.
+Part of the ppmc encoder only.
- This module is the main module and calls the different modules to do
- the encoding of a file. When done prints bpb and kbyps.
+This module is the main module and calls the different modules to do
+the encoding of a file. When done prints bpb and kbyps.
*/
//Main
int main (char argc, char *argv[])
{
- unsigned long counter, //temporal counter for loops like for or while
- counter2, //another temporal counter for sub loops
- size_file_input; //the size of the input file
+ unsigned long counter, //temporal counter for loops like for or while
+ counter2, //another temporal counter for sub loops
+ size_file_input; //the size of the input file
- // Print title, version and copyright
- printf("PPMC using range coder. (without exclusion)\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");
+ // Print title, version and copyright
+ printf("PPMC using range coder. (without exclusion)\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);
- }
+ // 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);
- }
+ // 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);
- }
+ if((file_output=fopen(argv[2],"w+b"))==NULL)
+ {
+ printf("Couldn't create %s.\n",argv[2]);
+ exit(1);
+ }
- // Check input file length and not accept 0 length files
- size_file_input=filesize(file_input);
+ // Check input file length and not accept 0 length files
+ size_file_input=filesize(file_input);
- if(size_file_input<5)
- {
- printf("Can't work with files below than 5 bytes!");
- exit(1);
- }
+ if(size_file_input<5)
+ {
+ printf("Can't work with files below than 5 bytes!");
+ exit(1);
+ }
- // First output file length
- fwrite(&size_file_input,1,4,file_output); //input length
+ // First output file length
+ fwrite(&size_file_input,1,4,file_output); //input length
- // Initialize ppmc encoder
- ppmc_alloc_memory(); //get memory
- ppmc_initialize_contexts(); //initialize model
- ppmc_encoder_initialize();
+ // Initialize ppmc encoder
+ ppmc_alloc_memory(); //get memory
+ ppmc_initialize_contexts(); //initialize model
+ ppmc_encoder_initialize();
- // Initialize range coder
- range_coder_init(&rc_coder,file_output);
+ // Initialize range coder
+ range_coder_init(&rc_coder,file_output);
- // Start main loop which codes the file
- while((byte=fgetc(file_input))!=EOF)
- {
+ // Start main loop which codes the file
+ while((byte=fgetc(file_input))!=EOF)
+ {
- // Try to code current byte under order-4 if possible then go to lower orders
- if(ppmc_code_byte_order4()==0)
- if(ppmc_code_byte_order3()==0)
- if(ppmc_code_byte_order2()==0)
- if(ppmc_code_byte_order1()==0)
- if(ppmc_code_byte_order0()==0) //else try to code under order-0
- {
- // Code under order-(-1)
- ppmc_get_prob_ordern1();
- range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
- coded_in_order=0; //update all the tables (unless order-(-1))
- }
+ // Try to code current byte under order-4 if possible then go to lower orders
+ if(ppmc_code_byte_order4()==0)
+ if(ppmc_code_byte_order3()==0)
+ if(ppmc_code_byte_order2()==0)
+ if(ppmc_code_byte_order1()==0)
+ if(ppmc_code_byte_order0()==0) //else try to code under order-0
+ {
+ // Code under order-(-1)
+ ppmc_get_prob_ordern1();
+ range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
+ coded_in_order=0; //update all the tables (unless order-(-1))
+ }
- // Now do update exclusion
+ // Now do update exclusion
- switch(coded_in_order)
- {
- case 0: ppmc_update_order0(); //update only order-0
- case 1: ppmc_update_order1(); //update order-0 and order-1
- case 2: ppmc_update_order2(); //update order-2 1 and 0...
- case 3: ppmc_update_order3();
- case 4: ppmc_update_order4();
- default: break;
- };
+ switch(coded_in_order)
+ {
+ case 0: ppmc_update_order0(); //update only order-0
+ case 1: ppmc_update_order1(); //update order-0 and order-1
+ case 2: ppmc_update_order2(); //update order-2 1 and 0...
+ case 3: ppmc_update_order3();
+ case 4: ppmc_update_order4();
+ default: break;
+ };
- // 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
- // Check if we run out of memory, in that case, flush the encoder
+ // Check if we run out of memory, in that case, flush the encoder
- if(ppmc_out_of_memory==1)
- {
- printf("Flushing memory! Output file might be not decodable.\n");
- ppmc_flush_mem_enc();
- }
+ if(ppmc_out_of_memory==1)
+ {
+ printf("Flushing memory! Output file might be not decodable.\n");
+ ppmc_flush_mem_enc();
+ }
- }
+ }
- // Flush range coder
- range_coder_flush(&rc_coder);
+ // Flush range coder
+ range_coder_flush(&rc_coder);
- // Free memory
- ppmc_free_memory();
+ // Free memory
+ ppmc_free_memory();
- // Print bpb and kbyps
- printf("%s at %f bpb.\n",argv[1],((float)filesize(file_output)/(float)size_file_input)*(float)8);
+ // Print bpb and kbyps
+ printf("%s at %f bpb.\n",argv[1],((float)filesize(file_output)/(float)size_file_input)*(float)8);
- // Close file handles
- fclose(file_input);
- fclose(file_output);
+ // Close file handles
+ fclose(file_input);
+ fclose(file_output);
- // Nicely exit
- return 0;
+ // Nicely exit
+ return 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;
}