]> git.llucax.com Git - z.facultad/75.06/jacu.git/blobdiff - src/jacu.c
COMMIT 800 !!!
[z.facultad/75.06/jacu.git] / src / jacu.c
index f0c3444d55525813611fc874529a6b2eb607a932..baf28da868b9a2b4648060b70da3482fcede6f66 100644 (file)
@@ -1,4 +1,5 @@
 
+/* Jacu Team - GPL */
 #include "blocksorting/bs.h"
 #include "mtf/mtf.h"
 #include "zerogrouping/zerogrouping.h"
@@ -99,9 +100,9 @@ int main(int argc, char* argv[])
                /* Comprimo */          
                FILE *fp;
                Uint32 i, j, total, k;
-               char *mtf;
-               char *salida, *data;
-               char *z;
+               unsigned char *mtf;
+               unsigned char *salida, *data;
+               unsigned char *z;
                int z_len;
                
                /* Preparo el compresor huffman */
@@ -109,8 +110,8 @@ int main(int argc, char* argv[])
                if (mflag == 1) shuff_loadmodel(shuff,staticmodel);
                
                /* Preparo el BS alocando mem para el K, el Block y su Size */
-               data = malloc(sizeof(char)*pagesize);
-               salida = malloc(sizeof(char)*pagesize+sizeof(Uint32)*2);
+               data = malloc(sizeof(unsigned char)*pagesize);
+               salida = malloc(sizeof(unsigned char)*pagesize+sizeof(Uint32)*2);
                bs = bs_create(pagesize);
 
                /* Abrimos el archivo a comprimir y encodeamos bloques */
@@ -130,28 +131,9 @@ int main(int argc, char* argv[])
                        /* Aplico el BlockSorting */
                        bs_solve(data, salida, bs, &k, i);
 
-                       /*printf("BS k=%ld\n", *(Uint32 *)(salida+sizeof(Uint32)));
-                       printf("PageSize = %ld\n", *(Uint32 *)salida);
-
-                       printf("Antes de MTF = %ld [", i);
-                       {
-                               int ii;
-                               for(ii=0; ii<(i+sizeof(Uint32)); ii++)
-                                       printf("(%c)", salida[ii+sizeof(Uint32)]);
-                               printf("]\n");
-                       }*/
-                       
                        /* Le aplico el MTF, salteo el tamaƱo del bloque para que no se pierda. */
                        mtf = jacu_mtf(salida+sizeof(Uint32), i+sizeof(Uint32), &z, &z_len);
-/*
-                       printf("MTF Z (len=%d) = [", z_len);
-                       {
-                               int ii;
-                               for(ii=0; ii<z_len; ii++)
-                                       printf("(%c)", z[ii]);
-                               printf("]\n");
-
-                       }*/
+                       
                        /* Si me lo piden, aplico ZG. */
                        if (zflag) {
                                size_t len;
@@ -195,58 +177,32 @@ int main(int argc, char* argv[])
        if (dflag == 1) { 
                /* Descomprimo */
                FILE *fp_out;
-               FILE *fp_in;
+               /*FILE *fp_in;*/
                Uint32 block_size, k;
-               char *block, *mtf, *orig;
-               char *z;
-               int z_len;
+               unsigned char *block, *mtf, *orig;
+               unsigned char *z;
+               int z_len,moredata = 0,decoded = 0;
 
-               if ((shuff = shuff_init_decoder(argv[optind],"tmp.comp")) == NULL) return 1;
-               shuff_decode_file(shuff);
-               shuff_deinit_decoder(shuff);
-               free(shuff);
-               
-               fp_in = fopen("tmp.comp", "rb");
+               /* Inicializo el descompresor */
+               if ((shuff = shuff_init_decoder(argv[optind],NULL)) == NULL) return 1;
+                               
+               /* Abrimos el archivo de salida */
                fp_out = fopen(argv[optind+1], "wb");
 
-               while (!feof(fp_in)) {
-                       block_size = 0;
-                       fread(&block_size, sizeof(Uint32), 1, fp_in);
+               /* Descomprimimos de a chunks segun convenga */
+               do {
+                       block_size = 0;                                         
+                       moredata = shuff_decode_chunk(shuff,(char*)&block_size,sizeof(Uint32),&decoded);
                        if (block_size > 0) {
-                               fread(&z_len, sizeof(int), 1, fp_in);
-                               z = malloc(sizeof(char)*z_len);
-                               fread(z, z_len, sizeof(char), fp_in);
+                               moredata = shuff_decode_chunk(shuff,(char*)&z_len,sizeof(int),&decoded);                                
+                               z = malloc(sizeof(unsigned char)*z_len);
+                               moredata = shuff_decode_chunk(shuff,z,z_len,&decoded);                          
 
-                               /*printf("MTF Z (len=%d) = [", z_len);
-                               {
-                                       int ii;
-                                       for(ii=0; ii<z_len; ii++)
-                                               printf("(%c)", z[ii]);
-                                       printf("]\n");
-       
-                               }*/
-                               
-                               block = malloc(block_size*sizeof(char)+sizeof(Uint32));
-                               orig = malloc(block_size*sizeof(char));
-                               fread(block, block_size+sizeof(Uint32), sizeof(char), fp_in);
+                               block = malloc(block_size*sizeof(unsigned char)+sizeof(Uint32));
+                               orig = malloc(block_size*sizeof(unsigned char));
+                               moredata = shuff_decode_chunk(shuff,block,block_size+sizeof(Uint32),&decoded);                          
 
-                               /*printf("Antes MTF_inv = [");
-                               {
-                                       int ii;
-                                       for(ii=0; ii<block_size+sizeof(Uint32); ii++)
-                                               printf("(%c)", block[ii]);
-                                       printf("]\n");
-                               }*/
-                               /* Hago el MTF inverso */
-                               mtf = jacu_mtf_inv(z, block, block_size*sizeof(char)+sizeof(Uint32));
-
-                               /*printf("Luego de MTF Inv= [");
-                               {
-                                       int ii;
-                                       for(ii=0; ii<block_size+sizeof(Uint32); ii++)
-                                               printf("(%c)", mtf[ii]);
-                                       printf("]\n");
-                               }*/
+                               mtf = jacu_mtf_inv(z, block, block_size*sizeof(unsigned char)+sizeof(Uint32));
 
                                /* Luego de hacer el MTF inverso ya puedo recuperar el k */
                                memcpy(&k, mtf, sizeof(Uint32));
@@ -254,15 +210,20 @@ int main(int argc, char* argv[])
                                /*printf("Restored : k=%ld\n", k);*/
                                bs_restore(orig, mtf+sizeof(Uint32), k, block_size);
 
-                               fwrite(orig, block_size, sizeof(char), fp_out);
+                               fwrite(orig, block_size, sizeof(unsigned char), fp_out);
                                free(block);
                                free(orig);
                                free(mtf);
                                free(z);
                        }
-               }
-               fclose(fp_in);
+               } while (moredata);             
+               
+               /* Close up files */
                fclose(fp_out);
+               
+               /* Shutdown Huffman */
+               shuff_deinit_decoder(shuff);
+               free(shuff);
        }
 
        return 0;