]> git.llucax.com Git - z.facultad/75.06/jacu.git/commitdiff
Listo en teoria Huffman Compresor/Descompresor ByFile y Bychunk. Maniana detallo...
authorAlan Kennedy <kennedya@3dgames.com.ar>
Wed, 23 Jun 2004 08:28:26 +0000 (08:28 +0000)
committerAlan Kennedy <kennedya@3dgames.com.ar>
Wed, 23 Jun 2004 08:28:26 +0000 (08:28 +0000)
src/statichuff/main_bychunk.c
src/statichuff/statichuff.c
src/statichuff/statichuff.h

index eafe694df01b5682177f52625c9b894406dc42e8..43426e841622dae8996fafd6972eacbfef626dcd 100644 (file)
@@ -11,7 +11,7 @@ int main(int argc, char* argv[])
        int dflag = 0;
        int tflag = 0;
        long int volumesize = 0;
-       int lastchunk,i,j,ch;
+       int lastchunk,i,j,ch,decoded = 0;
                        
        while ((ch = getopt(argc, argv, "cdt:")) != -1) { 
                 
@@ -58,7 +58,7 @@ int main(int argc, char* argv[])
                /* Le indico al huffman que efectivamente comprima los chunks */
                shuff_encode_file(shuff);
                
-               /* De init shuffman by chunks */
+               /* De init encoder */
                shuff_deinit_encoder(shuff);
                
                /* Free mem allocated by main */
@@ -70,8 +70,21 @@ int main(int argc, char* argv[])
        }
        
        if (dflag == 1) { 
-               /* Descomprimo */
-               /*return shuff_decode_file(argv[optind],argv[optind+1]);*/              
+               /* Init decoder */
+               shuff = shuff_init_decoder(argv[optind],NULL);
+               fp = fopen("chunkdump.txt","w");
+               
+               /* Gimme chunks till last one */
+               while (shuff_decode_chunk(shuff,chunk,4,&decoded))
+                       fwrite(chunk,decoded,1,fp);
+               
+               /* Last chunk written alone */
+               fwrite(chunk,decoded,1,fp);
+               fclose(fp);
+               
+               /* Deinit decoder */
+               shuff_deinit_decoder(shuff);
+               free(shuff);
        }
                
        return 0;
index a2f6265c91b667f7ddeddfef255cbee571dfc8fd..908729be341efc986cff022f2c6aa42f018543b9 100644 (file)
@@ -308,6 +308,36 @@ SHUFFNODE *shuff_decode_symbols(SHUFFNODE *entrynode, unsigned long int buffer,
        else return shuff_decode_symbols(entrynode->rchild,buffer,bitsleft,symbol);
 }
 
+int shuff_decode_chunk(HUFF_STATE *shuff, char *chunk, int chunksize, int *decodedbytes)
+{
+       SHUFFNODE *currnode = shuff->codetree;  
+       unsigned short int decoded_symbol;      
+       *decodedbytes = 0;
+       
+       while (!vfeof(shuff->decoderfp) && (shuff->bytesleft > 0) && (*decodedbytes < chunksize)) {
+               
+               /* Leo un buffer de 32 bits si es que quedo vacio el anterior */
+               if (shuff->bitsleft == 0) {
+                       if (vfread(&(shuff->codebuffer),sizeof(unsigned long int),1,shuff->decoderfp) != 1) continue;
+                       shuff->bitsleft = sizeof(unsigned long int) * 8;
+               }
+               
+               /* Proceso el buffer sacando simbolos till se me agote el buffer, file o chunk */
+               while ((shuff->bitsleft > 0) && (shuff->bytesleft > 0) && (*decodedbytes < chunksize)) {        
+                       currnode = shuff_decode_symbols(currnode,shuff->codebuffer,&(shuff->bitsleft),&decoded_symbol);
+                       /* Si obtuve un symbolo valido lo emito*/
+                       if (decoded_symbol != 256) {                            
+                               chunk[(*decodedbytes)++] = decoded_symbol;
+                               currnode = shuff->codetree;                             
+                               --(shuff->bytesleft);
+                       }                                               
+               }               
+       }
+       
+       if (shuff->bytesleft == 0) return 0;
+       else return 1;
+}
+
 int shuff_decode_file(HUFF_STATE *shuff)
 {      
        SHUFFNODE *currnode;    
@@ -316,7 +346,7 @@ int shuff_decode_file(HUFF_STATE *shuff)
        unsigned short int decoded_symbol;
        int bitsleft;   
        
-       /* Levanto cuantos bytes decodeo y la freq table */     
+       /* Comienzo a decodificar, pues la tabla ya la levante en el decinit */
        if ((fpdest = fopen(shuff->targetfile,"w")) == NULL) return 0;  
        currnode = shuff->codetree;
        
@@ -338,6 +368,7 @@ int shuff_decode_file(HUFF_STATE *shuff)
                }               
        }
                        
+       /* Close destination */
        fclose(fpdest); 
        
        return 1;
@@ -350,6 +381,8 @@ HUFF_STATE *shuff_init_decoder(char *inputfile, char *outputfile)
        shuff->freqtable = (t_freq*)malloc(sizeof(t_freq)*256); 
        
        /* Init fields */
+       shuff->codebuffer = 0;
+       shuff->bitsleft = 0;
        shuff->coderfp = NULL;
        shuff->targetfile = NULL;
        shuff->sourcefile = (char*)malloc(sizeof(char)*(strlen(inputfile)+1));
@@ -425,8 +458,8 @@ void shuff_deinit_encoder(HUFF_STATE *shuff)
        free(shuff->freqtable); 
        if (shuff->coderfp != NULL) fclose(shuff->coderfp);
        if (shuff->preloadfreq == 1) unlink(shuff->sourcefile);
-       free(shuff->sourcefile);
-       free(shuff->targetfile);                        
+       if (shuff->targetfile != NULL) free(shuff->targetfile);                 
+       free(shuff->sourcefile);        
        
        /* Destruyo recursivamente el arbol de codigos */
 }
index d892aea107b3a01939bd8545c2c9c62c08fc5248..35a550c5d31c1728662bbcb47ab5453e480136b4 100644 (file)
@@ -30,7 +30,9 @@ typedef struct t_huff {
        t_freq *freqtable; /* Tabla de frecuencias */
        t_freq sumfreq; /* Frecuencia total acumulada */
        SHUFFNODE *codetree; /* Puntero al arbol de codigos prefijos */         
-       unsigned long int bytesleft; /* cuanto falta descomprimir en un bychunk */      
+       unsigned long int bytesleft; /* Cuanto falta descomprimir en un bychunk */      
+       unsigned long int codebuffer; /* Buffer de descompresion para bychunk */
+       int bitsleft; /* Posicion en el buffer de descompresion para bychunk */
 } HUFF_STATE;