1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
| #include "utypes.h"
#include "gtbitio.h"
#include "sf.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
typedef struct
{
char algorithm[4];
ulong file_size;
} file_stamp;
sffreq_type freq;
char filename_in[256];
char filename_out[256];
FILE *in, *out;
file_stamp fstamp;
ulong in_file_len, out_file_len;
ulong i_file_size = 0;
#define IN_BUFSIZE 4096
char input_buf[ IN_BUFSIZE ];
uint nread = 0, in_i = 0;
void read_stats ( FILE *in, sffreq_type *sffreq );
void get_name(void);
int main(void)
{
unsigned int c = 0;
char ch;
time_t start, end;
//clrscr();
printf("\n\t Fano-arh.programm.");
printf("\n\t what are you want?");
printf("\n\t\t 1.Compress file");
printf("\n\t\t 2.DeCompress file\n");
printf("\n\t Enter your choise: ");
ch = getc(stdin);
printf("\n");
switch(ch)
{
case '1':
{
get_name();
start=time(NULL);
gIN=in;
pOUT=out;
init_put_buffer();
fprintf(stderr, "\n--( A Static *Shannon-Fano* Coding Implementation )--\n");
fprintf(stderr, "\nName of input file : %s", filename_in);
fseek( in, 0, SEEK_END );
in_file_len = ftell( in );
fprintf(stderr, "\nLength of input file = %15lu bytes", in_file_len );
init_sffreq();
fprintf(stderr, "\n\nAnalyzing file...");
read_stats( in, sffreq );
fprintf(stderr, "complete.");
fprintf(stderr, "\nShannon-Fano Compressing...");
init_sflist();
create_symbol_list();
top = create_node();
top->next = list;
create_shannon_fano_tree( top );
rewind( pOUT );
strcpy( fstamp.algorithm, "TSF" );
fstamp.file_size = in_file_len;
fwrite( &fstamp, sizeof(file_stamp), 1, out );
fwrite( &sfcount, sizeof(unsigned int), 1, out );
for ( c = 0; c < SF_MAX; c++ )
{
if ( sffreq[c].f > 0 )
{
fwrite( &sffreq[c], sizeof(sffreq_type), 1, out );
}
}
rewind(in);
while( true )
{
nread = fread( input_buf, 1, IN_BUFSIZE, in );
if ( nread == 0 ) break;
in_i = 0;
while( in_i < nread )
{
c = (uchar) *(input_buf+in_i);
++in_i;
sfcompress( sflist[c] );
}
}
flush_put_buffer(); /* flush output buffer */
fprintf(stderr, "complete.");
out_file_len = ftell( out );
end=time(NULL);
fprintf(stderr, "\n\nName of output file: %s", filename_out);
fprintf(stderr, "\nLength of input file = %15lu bytes", in_file_len );
fprintf(stderr, "\nLength of output file = %15lu bytes", out_file_len );
fprintf(stderr, "\nCompression ratio: %15.2f %%\n",
( ((float) in_file_len - (float) out_file_len) / (float) in_file_len
) * (float) 100 );
fprintf(stderr, "\nCompress time = %f Second", difftime(end,start));
//halt_prog:
getchar();
}break;
case '2':
{
get_name();
start=time(NULL);
gIN=in;
fread( &fstamp, sizeof(file_stamp), 1, in );
init_sffreq();
fread( &sfcount, sizeof(int), 1, in );
for ( c = 0; c < sfcount; c++ )
{
fread( &freq, sizeof(sffreq_type), 1, in );
sffreq[ (unsigned char) freq.b ] = freq;
}
init_sflist();
create_symbol_list();
top = create_node();
top->next = list;
create_shannon_fano_tree( top );
init_get_buffer();
fprintf(stderr, "\n Name of input file : %s", filename_in );
fprintf(stderr, "\n Decompressing...");
rewind( out );
for ( i_file_size = fstamp.file_size; i_file_size; i_file_size-- )
{
c = sfdecompress( top );
fputc( (uchar) c, out );
}
fprintf(stderr, "complete.\n");
end=time(NULL);
fprintf(stderr, "\n Name of output file : %s\n", filename_out );
fprintf(stderr, "\n Decompress time = %f second\n", difftime(end,start));
getchar() ;
}break;
default:
{
printf("\n\tError - You enter bad number");
getchar() ;
} break;
}
free_put_buffer();
if ( in ) fclose( in );
if ( out ) fclose( out );
return 0;
}
void get_name(void)
{
int i=0;
for(i=0;i<256;i++)
{
filename_in[i]=filename_out[i]=0;
}
printf("Enter name of input file: ");
//gets(filename_in);
scanf("%s",filename_in);
if ( (in = fopen( filename_in, "rb" )) == NULL )
{
fprintf(stderr, "\n\tError opening input file.");
getchar();
exit(0);
}
printf("\n\tEnter name of output file: ");
// gets(filename_out);
scanf("%s",filename_out);
if ( (out = fopen( filename_out, "wb" )) == NULL )
{
fprintf(stderr, "\n\tError opening output file.");
getchar();
exit(0);
}
}
void read_stats( FILE *in, sffreq_type *sffreq )
{
int c;
rewind(in);
while ( (c=fgetc(in)) != EOF ) {
sffreq[ c ].f++;
}
} |