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" (exclusions)
7 Email: arturo@arturocampos.com
8 Web: http://www.arturocampos.com
10 Part of the ppmc encoder only. Using exclusion.
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);
25 unsigned long debug=0;
29 void main (char argc, char *argv[])
31 unsigned long counter, //temporal counter for loops like for or while
32 counter2, //another temporal counter for sub loops
33 size_file_input; //the size of the input file
37 printf("PPMC using range coder. (with exclusion)\n");
38 printf("Copyright (C) Arturo San Emeterio Campos 1999. All rights reserved.\n");
39 printf("Permission is granted to make verbatim copies of this program for private\n");
40 printf("use only. There is ABSOLUTELY NO WARRANTY. Use it at your OWN RISK.\n");
44 // Check for correct number of parameters
47 printf("Bad number of arguments.\n");
52 // Try to open input and output files
53 if((file_input=fopen(argv[1],"r+b"))==NULL)
55 printf("Couldn't open %s.\n",argv[1]);
59 if((file_output=fopen(argv[2],"w+b"))==NULL)
61 printf("Couldn't create %s.\n",argv[2]);
66 // Check input file length and not accept 0 length files
67 size_file_input=filesize(file_input);
71 printf("Can't work with files below than 5 bytes!");
76 // First output file length
77 fwrite(&size_file_input,1,4,file_output); //input length
80 // Initialize ppmc encoder
81 ppmc_alloc_memory(); //get memory
82 ppmc_initialize_contexts(); //initialize model
83 ppmc_encoder_initialize();
85 // Initialize range coder
86 range_coder_init(&rc_coder,file_output);
89 // Start main loop which codes the file
90 while((byte=fgetc(file_input))!=EOF)
93 // Clear exclusion table
94 for(counter=0;counter!=256;++counter)
98 // Try to code current byte under order-4 if possible then go to lower orders
99 if(ppmc_code_byte_order4()==0)
100 if(ppmc_code_byte_order3()==0)
101 if(ppmc_code_byte_order2()==0)
102 if(ppmc_code_byte_order1()==0)
103 if(ppmc_code_byte_order0()==0) //else try to code under order-0
105 // Code under order-(-1)
106 ppmc_get_prob_ordern1();
107 range_coder_encode(&rc_coder,total_cump,symb_cump,symb_prob);
108 coded_in_order=0; //update all the tables (unless order-(-1))
112 // Now do update exclusion
114 switch(coded_in_order)
116 case 0: ppmc_update_order0(); //update only order-0
117 case 1: ppmc_update_order1(); //update order-0 and order-1
118 case 2: ppmc_update_order2(); //update order-2 1 and 0...
119 case 3: ppmc_update_order3();
120 case 4: ppmc_update_order4();
125 // Update order variables
130 o1_byte=byte; //current one is next time order-1
134 // Check if we run out of memory, in that case, flush the encoder
136 if(ppmc_out_of_memory==1)
138 printf("Flushing memory! Output file might be not decodable.\n");
139 ppmc_flush_mem_enc();
147 range_coder_flush(&rc_coder);
153 // Print bpb and kbyps
154 printf("%s at %f bpb.\n",argv[1],((float)filesize(file_output)/(float)size_file_input)*(float)8);
157 // Close file handles
168 // Routines not used by ppmc but rather by main.
169 // Not including the range coder.
172 // Returns the file size of a given file.
173 long filesize(FILE *stream)
177 curpos = ftell(stream);
178 fseek(stream, 0L, SEEK_END);
179 length = ftell(stream);
180 fseek(stream, curpos, SEEK_SET);