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 */
26 while ((ch = getopt(argc, argv, "cdzt:q:")) != -1) {
39 volumesize = atol(optarg);
45 case 0: pagesize = 1024; /* 1K */
47 case 1: pagesize = 2048; /* 2K */
49 case 2: pagesize = 4096; /* 4K */
51 case 3: pagesize = 8192; /* 8K */
53 case 4: pagesize = 16384; /* 16K */
55 case 5: pagesize = 32768; /* 32K */
57 case 6: pagesize = 65536; /* 64K */
59 case 7: pagesize = 131072; /* 128K */
61 case 8: pagesize = 262144; /* 256K */
63 case 9: pagesize = 524288; /* 512K */
65 default: pagesize = 0; /* error */
69 default: fprintf(stderr, "Usage: %s [-cdpt] sourcefile targetfile\n", argv[0]);
74 if ( (argc == 1) || (cflag & dflag) || !(cflag | dflag) || ((argc - optind) < 2) ) {
75 fprintf(stderr, "Usage: %s [-cdt] sourcefile targetfile\n", argv[0]);
78 if ((tflag) && (volumesize <= 0l)) {
79 fprintf(stderr,"Error: The volume size must be a non-zero value\n");
82 if ((qflag) && (pagesize <= 1u)) {
83 fprintf(stderr,"Error: El nivel de compresión debe ser entre 0 (menor) y 9 (mayor).\n");
89 /* No me gusta el tmpfile ... es para probar como anda todo junto */
91 Uint32 i, j, total, k;
93 char *salida, *data, c;
97 data = malloc(sizeof(char)*pagesize);
98 /* Reservo lugar tambien para guardar el k y el tamaño */
99 salida = malloc(sizeof(char)*pagesize+sizeof(Uint32)*2);
100 bs = bs_create(pagesize);
102 fp = fopen(argv[optind], "rb");
103 fp_out = fopen("tmp.comp", "wb");
109 while ((!feof(fp)) && (i < pagesize)) {
116 bs_solve(data, salida, bs, &k, i);
118 printf("BS k=%ld\n", *(Uint32 *)(salida+sizeof(Uint32)));
119 printf("PageSize = %ld\n", *(Uint32 *)salida);
121 printf("Antes de MTF = %ld [", i);
124 for(ii=0; ii<(i+sizeof(Uint32)); ii++)
125 printf("(%c)", salida[ii+sizeof(Uint32)]);
128 /* Le aplico el MTF, salteo el tamaño del bloque para que no se pierda. */
129 mtf = jacu_mtf(salida+sizeof(Uint32), i+sizeof(Uint32), &z, &z_len);
131 printf("MTF Z (len=%d) = [", z_len);
134 for(ii=0; ii<z_len; ii++)
135 printf("(%c)", z[ii]);
139 /* Si me lo piden, aplico ZG. */
145 /* TODO HACER LO MISMO QUE EN EL ELSE XXX */
146 for (j = 0; j < i; ++j)
147 if ((len = zg_group(&zg, buff, mtf[j]))) fwrite(buff, 1, len, fp_out);
149 /* Guardo el PageSize */
150 fwrite(salida, sizeof(Uint32), 1, fp_out);
152 /* Guardo el Z len y el Z */
153 fwrite(&z_len, sizeof(int), 1, fp_out);
154 fwrite(z, z_len, sizeof(char), fp_out);
156 /* Guardo la salida del MTF */
157 printf("Despues de MTF : [");
158 for(j=0; j<(i+sizeof(Uint32)); j++) {
159 fputc(mtf[j], fp_out);
161 fputc(mtf[j], stdout);
175 /* Comprimo con huffman */
176 i = shuff_encode_file("tmp.comp", argv[optind+1], volumesize);
178 /* borro el temporal */
182 printf("Comprimido a %.04f bpb.\n", get_file_size(argv[optind+1])*8.0/get_file_size(argv[optind]));
190 Uint32 block_size, k;
191 char *block, *mtf, *orig;
195 shuff_decode_file(argv[optind], "tmp.comp"); /*argv[optind+1]);*/
196 fp_in = fopen("tmp.comp", "rb");
197 fp_out = fopen(argv[optind+1], "wb");
199 while (!feof(fp_in)) {
202 fread(&block_size, sizeof(Uint32), 1, fp_in);
203 printf("PageSize = %ld\n", block_size);
204 if (block_size > 0) {
205 fread(&z_len, sizeof(int), 1, fp_in);
206 z = malloc(sizeof(char)*z_len);
207 fread(z, z_len, sizeof(char), fp_in);
209 printf("MTF Z (len=%d) = [", z_len);
212 for(ii=0; ii<z_len; ii++)
213 printf("(%c)", z[ii]);
218 block = malloc(block_size*sizeof(char)+sizeof(Uint32));
219 orig = malloc(block_size*sizeof(char));
220 fread(block, block_size+sizeof(Uint32), sizeof(char), fp_in);
222 printf("Antes MTF_inv = [");
225 for(ii=0; ii<block_size+sizeof(Uint32); ii++)
226 printf("(%c)", block[ii]);
229 /* Hago el MTF inverso */
230 PERR("Haciendo MTF Inv");
231 mtf = jacu_mtf_inv(z, block, block_size*sizeof(char)+sizeof(Uint32));
233 printf("Luego de MTF Inv= [");
236 for(ii=0; ii<block_size+sizeof(Uint32); ii++)
237 printf("(%c)", mtf[ii]);
241 /* Luego de hacer el MTF inverso ya puedo recuperar el k */
242 PERR("Recuperando K");
243 memcpy(&k, mtf, sizeof(Uint32));
245 printf("Restored : k=%ld\n", k);
247 bs_restore(orig, mtf+sizeof(Uint32), k, block_size);
250 fwrite(orig, block_size, sizeof(char), fp_out);
263 long get_file_size(const char* filename)
268 if (!(file = fopen(filename, "ab"))) return -1;
269 file_size = ftell(file);