2 #include "blocksorting/bs.h"
4 #include "zerogrouping/zerogrouping.h"
5 #include "statichuff/statichuff.h"
6 #include "vfile/vfile.h"
7 #include "vfile/common.h"
12 long get_file_size(const char* filename);
14 int main(int argc, char* argv[])
21 long int volumesize = 0;
22 size_t pagesize = 32768; /* 32KB */
27 while ((ch = getopt(argc, argv, "cdzt:q:")) != -1) {
40 volumesize = atol(optarg);
46 case 0: pagesize = 1024; /* 1K */
48 case 1: pagesize = 2048; /* 2K */
50 case 2: pagesize = 4096; /* 4K */
52 case 3: pagesize = 8192; /* 8K */
54 case 4: pagesize = 16384; /* 16K */
56 case 5: pagesize = 32768; /* 32K */
58 case 6: pagesize = 65536; /* 64K */
60 case 7: pagesize = 131072; /* 128K */
62 case 8: pagesize = 262144; /* 256K */
64 case 9: pagesize = 524288; /* 512K */
66 default: pagesize = 0; /* error */
70 default: fprintf(stderr, "Usage: %s [-cdpt] sourcefile targetfile\n", argv[0]);
75 if ( (argc == 1) || (cflag & dflag) || !(cflag | dflag) || ((argc - optind) < 2) ) {
76 fprintf(stderr, "Usage: %s [-cdt] sourcefile targetfile\n", argv[0]);
79 if ((tflag) && (volumesize <= 0l)) {
80 fprintf(stderr,"Error: The volume size must be a non-zero value\n");
83 if ((qflag) && (pagesize <= 1u)) {
84 fprintf(stderr,"Error: El nivel de compresión debe ser entre 0 (menor) y 9 (mayor).\n");
90 /* No me gusta el tmpfile ... es para probar como anda todo junto */
92 Uint32 i, j, total, k;
94 char *salida, *data, c;
98 data = malloc(sizeof(char)*pagesize);
99 /* Reservo lugar tambien para guardar el k y el tamaño */
100 salida = malloc(sizeof(char)*pagesize+sizeof(Uint32)*2);
101 bs = bs_create(pagesize);
103 fp = fopen(argv[optind], "rb");
104 fp_out = fopen("tmp.comp", "wb");
110 while ((!feof(fp)) && (i < pagesize)) {
111 data[i++] = fgetc(fp);
115 /* Saco un EOF que lee de mas */
119 bs_solve(data, salida, bs, &k, i);
121 /*printf("BS k=%ld\n", *(Uint32 *)(salida+sizeof(Uint32)));
122 printf("PageSize = %ld\n", *(Uint32 *)salida);
124 printf("Antes de MTF = %ld [", i);
127 for(ii=0; ii<(i+sizeof(Uint32)); ii++)
128 printf("(%c)", salida[ii+sizeof(Uint32)]);
131 /* Le aplico el MTF, salteo el tamaño del bloque para que no se pierda. */
132 mtf = jacu_mtf(salida+sizeof(Uint32), i+sizeof(Uint32), &z, &z_len);
134 printf("MTF Z (len=%d) = [", z_len);
137 for(ii=0; ii<z_len; ii++)
138 printf("(%c)", z[ii]);
142 /* Si me lo piden, aplico ZG. */
148 /* TODO HACER LO MISMO QUE EN EL ELSE XXX */
149 for (j = 0; j < i; ++j)
150 if ((len = zg_group(&zg, buff, mtf[j]))) fwrite(buff, 1, len, fp_out);
152 /* Guardo el PageSize */
153 fwrite(salida, sizeof(Uint32), 1, fp_out);
155 /* Guardo el Z len y el Z */
156 fwrite(&z_len, sizeof(int), 1, fp_out);
157 fwrite(z, z_len, sizeof(char), fp_out);
159 /* Guardo la salida del MTF */
160 /*printf("Despues de MTF : [");*/
161 for(j=0; j<(i+sizeof(Uint32)); j++) {
162 fputc(mtf[j], fp_out);
164 fputc(mtf[j], stdout);
178 /* Comprimo con huffman */
179 if ((shuff = shuff_init_encoder_byfile("tmp.comp", argv[optind+1], volumesize)) == NULL) return 1;
180 shuff_encode_file(shuff);
181 shuff_deinit_encoder(shuff);
184 /* borro el temporal */
188 printf("Comprimido a %.04f bpb.\n", get_file_size(argv[optind+1])*8.0/get_file_size(argv[optind]));
196 Uint32 block_size, k;
197 char *block, *mtf, *orig;
201 if ((shuff = shuff_init_decoder(argv[optind],"tmp.comp")) == NULL) return 1;
202 shuff_decode_file(shuff);
203 shuff_deinit_decoder(shuff);
206 fp_in = fopen("tmp.comp", "rb");
207 fp_out = fopen(argv[optind+1], "wb");
209 while (!feof(fp_in)) {
211 fread(&block_size, sizeof(Uint32), 1, fp_in);
212 if (block_size > 0) {
213 fread(&z_len, sizeof(int), 1, fp_in);
214 z = malloc(sizeof(char)*z_len);
215 fread(z, z_len, sizeof(char), fp_in);
217 /*printf("MTF Z (len=%d) = [", z_len);
220 for(ii=0; ii<z_len; ii++)
221 printf("(%c)", z[ii]);
226 block = malloc(block_size*sizeof(char)+sizeof(Uint32));
227 orig = malloc(block_size*sizeof(char));
228 fread(block, block_size+sizeof(Uint32), sizeof(char), fp_in);
230 /*printf("Antes MTF_inv = [");
233 for(ii=0; ii<block_size+sizeof(Uint32); ii++)
234 printf("(%c)", block[ii]);
237 /* Hago el MTF inverso */
238 mtf = jacu_mtf_inv(z, block, block_size*sizeof(char)+sizeof(Uint32));
240 /*printf("Luego de MTF Inv= [");
243 for(ii=0; ii<block_size+sizeof(Uint32); ii++)
244 printf("(%c)", mtf[ii]);
248 /* Luego de hacer el MTF inverso ya puedo recuperar el k */
249 memcpy(&k, mtf, sizeof(Uint32));
251 /*printf("Restored : k=%ld\n", k);*/
252 bs_restore(orig, mtf+sizeof(Uint32), k, block_size);
254 fwrite(orig, block_size, sizeof(char), fp_out);
268 long get_file_size(const char* filename)
273 if (!(file = fopen(filename, "ab"))) return -1;
274 file_size = ftell(file);