Esempio n. 1
0
/*
 * This expansion routine demonstrates the basic algorithm used for
 * decompression in this article.  It first goes to the modeling
 * module and gets the scale for the current context.  (Note that
 * the scale is fixed here, since this is not an adaptive model).
 * It then asks the arithmetic decoder to give a high and low
 * value for the current input number scaled to match the current
 * range.  Finally, it asks the modeling unit to convert the
 * high and low values to a symbol.
 */
void expand()
{
    FILE *compressed_file;
    SYMBOL s;
    char c;
    int count;

    compressed_file=fopen( "software/benchmarks/data/test_comp.cmp", "rb" );
    if ( compressed_file == NULL )
        error_exit( "Could not open output file" );
    puts( "Decoding..." );
    printf( "Incoming characters: " );
    initialize_input_bitstream();
    initialize_arithmetic_decoder( compressed_file );
    for ( ; ; )
    {
        s.scale = 11;
        count = get_current_count( &s );
        c = convert_symbol_to_int( count, &s );
        if ( c == '\0' )
            break;
        remove_symbol_from_stream( compressed_file, &s );
        putc( c, stdout );
    }
    putc( '\n', stdout );
}
Esempio n. 2
0
/**
 * Decompresses the data in an input file and writes it in a new file.
 * @param[in] filename name and path of the input file.
 * @param[in] output name and path of the output file.
 * @param[in] see if see will be used.
 */
static void unzip(char *filename, char *output, BOOL see) {
  FILE *output_file, *compressed_file;
  int i, header, parts, part;
  Uint textlen = 0;
  decoderTree_t tree;

  initDecoderTreeStack();

  compressed_file = fopen(filename, "rb");
  if (!compressed_file) {
    perror( "Could not open input file");
    exit(1);
  }

  if (!output) {
    output = strrchr(filename, '.');
    if (output) {
      *output = '\0';
    } 
    else {
      strcat(filename, ".out");
    }
    output = filename;
  }

  output_file = fopen(output, "wb");
  if (!output_file) {
    perror( "Could not open output file");
    exit(1);
  }

  /* check magic */
  header = getc(compressed_file) << 8;
  header += getc(compressed_file);
  if (header != MAGIC) {
    fprintf(stderr, "Invalid compressed file\n");
    exit(1);
  }

  /* read parts */
  parts = getc(compressed_file);

  initialize_input_bitstream();
  initialize_arithmetic_decoder(compressed_file);

  readAlphabet(compressed_file);

  setMaxCount();

  for (part = 1; part <= parts; part++) {  
    printf("---------- part %d ---------------\n", part);
    /* read textlen */
    for (textlen=0, i=3; i>=0; i--) {
      textlen += readByte(compressed_file) << (8 * i);
    }

    /*if (part == 1) {*/
      tree = readDecoderTree(compressed_file);
      /*}*/

    printf("Tree built\n");
    printf("Textlen: %ld\n", textlen);
    printf("FSM...\n"); 
    /*if (part == 1) {*/
    DEBUGCODE(printDecoderTree(tree));
    makeDecoderFsm(tree);
    DEBUGCODE(printDecoderTree(tree));
      /*}*/

    printf("Decoding...\n");
    decode(tree, textlen, compressed_file, output_file, see);
  }
  /*freeDecoderTree(tree);*/
  fclose(compressed_file);
  fclose(output_file);
}