2 Copyright (C) Arturo San Emeterio Campos 1999. All rights reserved.
3 Permission is granted to make verbatim copies of this file for private
4 use only. There is ABSOLUTELY NO WARRANTY. Use it at your OWN RISK.
6 This file is: "ppmcmain.c"
7 Email: arturo@arturocampos.com
8 Web: http://www.arturocampos.com
10 Part of the ppmc encoder only.
12 This module is the main module and calls the different modules to do
13 the encoding of a file. When done prints bpb and kbyps.
19 #include "range.h" //the range coder functions and data
23 long filesize(FILE *stream);
28 int main (char argc, char *argv[])
30 unsigned long counter, //temporal counter for loops like for or while
31 counter2, //another temporal counter for sub loops
32 size_file_input; //the size of the input file
35 // Print title, version and copyright
36 printf("PPMC using range coder. (without exclusion)\n");
37 printf("Copyright (C) Arturo San Emeterio Campos 1999. All rights reserved.\n");
38 printf("Permission is granted to make verbatim copies of this program for private\n");
39 printf("use only. There is ABSOLUTELY NO WARRANTY. Use it at your OWN RISK.\n");
41 // Check for correct number of parameters
44 printf("Bad number of arguments.\n");
49 // Try to open input and output files
50 if((file_input=fopen(argv[1],"r+b"))==NULL)
52 printf("Couldn't open %s.\n",argv[1]);
56 if((file_output=fopen(argv[2],"w+b"))==NULL)
58 printf("Couldn't create %s.\n",argv[2]);
63 // Check input file length and not accept 0 length files
64 size_file_input=filesize(file_input);
68 printf("Can't work with files below than 5 bytes!");
73 // First output file length
74 fwrite(&size_file_input,1,4,file_output); //input length
77 // Initialize ppmc encoder
78 ppmc_alloc_memory(); //get memory
79 ppmc_initialize_contexts(); //initialize model
80 ppmc_encoder_initialize();
82 // Initialize range coder
83 range_coder_init(&rc_coder,file_output);
86 // Start main loop which codes the file
87 while((byte=fgetc(file_input))!=EOF)
90 // Try to code current byte under order-4 if possible then go to lower orders
91 if(ppmc_code_byte_order4()==0)
92 if(ppmc_code_byte_order3()==0)
93 if(ppmc_code_byte_order2()==0)
94 if(ppmc_code_byte_order1()==0)
95 if(ppmc_code_byte_order0()==0) //else try to code under order-0
97 // Code under order-(-1)
98 ppmc_get_prob_ordern1();
99 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
100 coded_in_order=0; //update all the tables (unless order-(-1))
104 // Now do update exclusion
106 switch(coded_in_order)
108 case 0: ppmc_update_order0(); //update only order-0
109 case 1: ppmc_update_order1(); //update order-0 and order-1
110 case 2: ppmc_update_order2(); //update order-2 1 and 0...
111 case 3: ppmc_update_order3();
112 case 4: ppmc_update_order4();
118 // Update order variables
123 o1_byte=byte; //current one is next time order-1
126 // Check if we run out of memory, in that case, flush the encoder
128 if(ppmc_out_of_memory==1)
130 printf("Flushing memory! Output file might be not decodable.\n");
131 ppmc_flush_mem_enc();
139 range_coder_flush(&rc_coder);
145 // Print bpb and kbyps
146 printf("%s at %f bpb.\n",argv[1],((float)filesize(file_output)/(float)size_file_input)*(float)8);
149 // Close file handles
160 // Routines not used by ppmc but rather by main.
161 // Not including the range coder.
164 // Returns the file size of a given file.
165 long filesize(FILE *stream)
169 curpos = ftell(stream);
170 fseek(stream, 0L, SEEK_END);
171 length = ftell(stream);
172 fseek(stream, curpos, SEEK_SET);