📄 decompress.c
字号:
{
name = argv[i+1];
argv[i] = argv[i+1] = "";
}
if (name == NULL)
{
fprintf(stderr,"Must supply input file name via `-i' switch!\n");
exit(-1);
}
fp = local_fopen(name,"rb");
if (fp == NULL)
{
fprintf(stderr,"Cannot open input file, \"%s\"!\n",name);
exit(-1);
}
header_bytes = 0;
/* Read magic string and header size information. */
magic[4] = '\0';
fread(magic,1,4,fp);
if (strcmp(magic,"JPLB") != 0)
{
fprintf(stderr,"Input file, \"%s\", has invalid magic string!",name);
exit(-1);
}
header_bytes += 4;
*header_size = read_word(fp,2); header_bytes += 2;
/* Read the number of original and current resolution levels. */
tmp = read_word(fp,1); header_bytes += 1;
*original_levels = tmp & 0x000F;
*num_levels = tmp >> 4;
assert(*num_levels <= *original_levels);
/* Read filter info. */
tmp = read_word(fp,1); header_bytes += 1;
*filter_set = tmp & 0x000F;
*filter_flags = tmp >> 4;
/* Read profile flags. */
*coder_profile = read_word(fp,1); header_bytes += 1;
/* Read max bitplanes. */
*max_bitplanes = read_word(fp,1); header_bytes += 1;
/* Read number of components. */
*num_components = read_word(fp,1); header_bytes += 1;
if ((*num_components != 1) && (*num_components != 3))
{
fprintf(stderr,
"Can only handle 1 or 3 components in this wrapper!\n");
exit(-1);
}
for (comp=0; comp < *num_components; comp++)
{
int num_rows, num_cols;
/* Read dimensions, building up from the base (LL band) dimensions. */
num_rows = read_word(fp,2); header_bytes += 2;
num_cols = read_word(fp,2); header_bytes += 2;
i = (*num_levels+3)>>2;
assert(i <= 4);
tmp = read_word(fp,i); header_bytes += i;
for (i=0; i < *num_levels; i++)
{
num_rows = (num_rows << 1) - (tmp & 1);
tmp >>= 1;
num_cols = (num_cols << 1) - (tmp & 1);
tmp >>= 1;
}
component_rows[comp] = num_rows;
component_cols[comp] = num_cols;
/* Read base quantization step. */
fixed_point_step = read_word(fp,2); header_bytes += 2;
component_base_steps[comp] =
((float) fixed_point_step) / ((float)(1<<16));
}
/* Skip over rest of header. */
while (header_bytes < *header_size)
{
read_word(fp,1);
header_bytes++;
}
if (*header_size < header_bytes)
{
fprintf(stderr,"Illegal header found on compressed file!\n"
" Header appears to require %d bytes,\n"
" but only reserves %d bytes for itself!\n",
header_bytes,*header_size);
exit(-1);
}
return(fp);
}
/*****************************************************************************/
/* main */
/*****************************************************************************/
int
main(int argc, char *argv[])
{
char **copy_argv;
int original_levels, in_levels, out_levels;
int out_rows, out_cols;
int in_components, out_components;
int coder_profile, verbose;
int filter_set, filter_flags;
char *filter_id, *local_filter_name, filter_id_buf[2];
int header_bytes, max_bytes, actual_bytes, truncate;
float max_bpp;
FILE *image_fp, *bitstream_fp;
float component_base_steps[3];
int component_rows_in[3], component_cols_in[3];
int component_rows_out[3], component_cols_out[3];
char *component_ids[3];
int component_flags[3];
ifc_int *interleaved_line, *line_buf;
int max_bitplanes;
int r, n;
filter_info_ref info;
synthesis_ref synthesis;
dequantizer_ref dequantizer;
decoder_ref decoder;
bitstream_source_ref bitstream;
/* Set default decompression parameters. */
verbose = 1;
local_filter_name = NULL;
max_bpp = 100.0F;
out_levels = 16;
out_components = MAX_COMPONENTS;
truncate = 0;
/* Modify default parameters based on command-line switches. */
if (argc == 1)
{
print_usage(out_components,out_levels,max_bpp,argv[0],stderr);
return(-1);
}
for (r=1; r < argc; r++)
if (strcmp(argv[r],"-u") == 0)
{ /* Allow usage statements to be printed to a file or other stdout. */
FILE *fp;
fp = stderr;
r++;
if ((r < argc) &&
(((argv[r])[0] != '-') || ((argv[r])[1] == '\0')))
{
fp = local_fopen(argv[r],"w");
if (fp == NULL)
fp = stderr;
}
print_usage(out_components,out_levels,max_bpp,argv[0],fp);
local_fclose(fp);
return(-1);
}
copy_argv = (char **) local_malloc(sizeof(char *)*argc);
memcpy(copy_argv,argv,sizeof(char *)*argc);
parse_common_arguments(argc,copy_argv,&out_components,&out_levels,
&local_filter_name,&max_bpp,&truncate,&verbose);
/* Open compressed input file and read header. */
bitstream_fp = open_input_bitstream(argc,copy_argv,&original_levels,
&in_levels,&filter_set,&filter_flags,
&coder_profile,&max_bitplanes,
&in_components,component_rows_in,
component_cols_in,component_base_steps,
&header_bytes);
if (out_components > in_components)
out_components = in_components;
if (out_components < 3)
out_components = 1;
if (out_levels > in_levels)
out_levels = in_levels;
if (!truncate)
header_bytes -= 8*(in_components-out_components);
for (n=0; n < out_components; n++)
{
component_rows_out[n] =
1 + ((component_rows_in[n]-1) >> (in_levels-out_levels));
component_cols_out[n] =
1 + ((component_cols_in[n]-1) >> (in_levels-out_levels));
}
out_cols = component_cols_out[0];
out_rows = component_rows_out[0];
for (n=0; n < out_components; n++)
if ((out_cols != component_cols_out[n]) ||
(out_rows != component_rows_out[n]))
{
fprintf(stderr,
"Subsampled components not supported in this wrapper!\n");
exit(-1);
}
max_bytes = (int)(0.125F * max_bpp * (float)(out_rows * out_cols));
if (verbose)
{
fprintf(stderr,"\nBit-stream has the following feature profile:\n");
print_profile_vector(coder_profile,stderr);
}
if (verbose)
fprintf(stderr,
"Components = %d (reconstructing %d); "
"Levels = %d (reconstructing %d)\n"
"Header size = %d bytes; Base step size = %g;\n"
"Max bit rate = %g bpp (i.e. %d bytes); method = %s\n",
in_components,out_components,in_levels,out_levels,header_bytes,
component_base_steps[0],max_bpp,max_bytes,
(truncate)?"by truncation":"by parsing");
if ((local_filter_name != NULL) && (filter_set != 15))
fprintf(stderr,"Warning: the local filter set file name supplied on the\n"
" command line will be ignored, since the bit-stream identifies\n"
" a built-in filter set!!\n");
else if ((local_filter_name == NULL) && (filter_set == 15))
{
fprintf(stderr,"The image was compressed with a filter set derived "
"from a\n local file! You must supply this file explicitly "
"via the `-loc' argument!\n");
exit(-1);
}
if (filter_set == 15)
filter_id = local_filter_name;
else
{
filter_id = filter_id_buf;
sprintf(filter_id,"%1d",filter_set);
}
/* Open output image and set dimensions. */
image_fp = open_output_image_file(argc,copy_argv,out_rows,out_cols,
out_components);
/* Create objects. */
info = info_creator();
synthesis = synthesis_creator();
dequantizer = dequantizer_creator();
decoder = decoder_creator();
bitstream = bitstream_creator();
/* Initialize objects. */
for (n=0; n < in_components; n++)
{
component_ids[n] = filter_id;
component_flags[n] = filter_flags;
}
info->initialize(info,DECOMPOSITION__MALLAT,original_levels,in_components,
component_ids,component_flags,component_base_steps,
&max_bitplanes,argc,copy_argv);
bitstream->initialize(bitstream,(truncate)?(max_bytes-header_bytes):INT_MAX,
bitstream_fp,argc,copy_argv);
decoder->initialize(decoder,DECOMPOSITION__MALLAT,in_levels,in_components,
component_rows_in,component_cols_in,info,coder_profile,
header_bytes,max_bytes-header_bytes,out_levels,
out_components,bitstream,argc,copy_argv);
dequantizer->initialize(dequantizer,DECOMPOSITION__MALLAT,in_levels,
in_components,component_rows_in,component_cols_in,
info,decoder,argc,copy_argv);
synthesis->initialize(synthesis,DECOMPOSITION__MALLAT,out_levels,
out_components,component_rows_out,component_cols_out,
info,dequantizer,argc,copy_argv);
check_all_arguments_used(argc,copy_argv);
line_buf = interleaved_line = (ifc_int *)
local_malloc(sizeof(ifc_int) * out_cols * out_components);
if (out_components > 1)
line_buf = (ifc_int *)
local_malloc(sizeof(ifc_int) * out_cols);
/* Pull image rows out of the synthesis stage one at a time. */
for (r=0; r < out_rows; r++)
{
if (out_components == 1)
synthesis->pull_line(synthesis,interleaved_line,0);
else
{
for (n=0; n < out_components; n++)
{
ifc_int *sp, *dp;
int c;
synthesis->pull_line(synthesis,line_buf,n);
for (dp=interleaved_line+n, sp=line_buf,
c=out_cols; c > 0; c--, dp += out_components)
*dp = *(sp++);
}
}
write_image_line(interleaved_line,image_fp,out_cols,out_components);
}
/* Terminate all objects. */
synthesis->terminate(synthesis);
dequantizer->terminate(dequantizer);
actual_bytes = decoder->terminate(decoder);
bitstream->terminate(bitstream);
info->terminate(info);
if (verbose)
fprintf(stderr,"Actual bit rate = %g bpp (i.e. %d bytes)\n",
(8.0F*(float)(actual_bytes+header_bytes))/
((float)(out_rows*out_cols)),actual_bytes+header_bytes);
if (actual_bytes > max_bytes)
{
fprintf(stderr,"\nDecoder failed to parse bit-stream correctly!!\n"
" Decompressed bit-stream had %d bytes more than allowed!\n",
actual_bytes-max_bytes);
return(-1);
}
/* Finish up. */
local_free(interleaved_line);
if (line_buf != interleaved_line)
local_free(line_buf);
local_fclose(image_fp);
local_fclose(bitstream_fp);
local_free(copy_argv);
return (0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -