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)) {
110 data[i++] = fgetc(fp);
114 /* Saco un EOF que lee de mas */
118 bs_solve(data, salida, bs, &k, i);
120 /*printf("BS k=%ld\n", *(Uint32 *)(salida+sizeof(Uint32)));
121 printf("PageSize = %ld\n", *(Uint32 *)salida);
123 printf("Antes de MTF = %ld [", i);
126 for(ii=0; ii<(i+sizeof(Uint32)); ii++)
127 printf("(%c)", salida[ii+sizeof(Uint32)]);
130 /* Le aplico el MTF, salteo el tamaño del bloque para que no se pierda. */
131 mtf = jacu_mtf(salida+sizeof(Uint32), i+sizeof(Uint32), &z, &z_len);
133 printf("MTF Z (len=%d) = [", z_len);
136 for(ii=0; ii<z_len; ii++)
137 printf("(%c)", z[ii]);
141 /* Si me lo piden, aplico ZG. */
147 /* TODO HACER LO MISMO QUE EN EL ELSE XXX */
148 for (j = 0; j < i; ++j)
149 if ((len = zg_group(&zg, buff, mtf[j]))) fwrite(buff, 1, len, fp_out);
151 /* Guardo el PageSize */
152 fwrite(salida, sizeof(Uint32), 1, fp_out);
154 /* Guardo el Z len y el Z */
155 fwrite(&z_len, sizeof(int), 1, fp_out);
156 fwrite(z, z_len, sizeof(char), fp_out);
158 /* Guardo la salida del MTF */
159 /*printf("Despues de MTF : [");*/
160 for(j=0; j<(i+sizeof(Uint32)); j++) {
161 fputc(mtf[j], fp_out);
163 fputc(mtf[j], stdout);
177 /* Comprimo con huffman */
178 i = shuff_encode_file("tmp.comp", argv[optind+1], volumesize);
180 /* borro el temporal */
184 printf("Comprimido a %.04f bpb.\n", get_file_size(argv[optind+1])*8.0/get_file_size(argv[optind]));
192 Uint32 block_size, k;
193 char *block, *mtf, *orig;
197 shuff_decode_file(argv[optind], "tmp.comp"); /*argv[optind+1]);*/
198 fp_in = fopen("tmp.comp", "rb");
199 fp_out = fopen(argv[optind+1], "wb");
201 while (!feof(fp_in)) {
203 fread(&block_size, sizeof(Uint32), 1, fp_in);
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 mtf = jacu_mtf_inv(z, block, block_size*sizeof(char)+sizeof(Uint32));
232 /*printf("Luego de MTF Inv= [");
235 for(ii=0; ii<block_size+sizeof(Uint32); ii++)
236 printf("(%c)", mtf[ii]);
240 /* Luego de hacer el MTF inverso ya puedo recuperar el k */
241 memcpy(&k, mtf, sizeof(Uint32));
243 /*printf("Restored : k=%ld\n", k);*/
244 bs_restore(orig, mtf+sizeof(Uint32), k, block_size);
246 fwrite(orig, block_size, sizeof(char), fp_out);
260 long get_file_size(const char* filename)
265 if (!(file = fopen(filename, "ab"))) return -1;
266 file_size = ftell(file);