📄 decoder.c
字号:
/* SPMG/JPEG-LS IMPLEMENTATION V.2.1 ===================================== These programs are Copyright (c) University of British Columbia. All right reserved. They may be freely redistributed in their entirety provided that this copyright notice is not removed. THEY MAY NOT BE SOLD FOR PROFIT OR INCORPORATED IN COMMERCIAL PROGRAMS WITHOUT THE WRITTEN PERMISSION OF THE COPYRIGHT HOLDER. Each program is provided as is, without any express or implied warranty, without even the warranty of fitness for a particular purpose. ========================================================= THIS SOFTWARE IS BASED ON HP's implementation of jpeg-ls: ========================================================= LOCO-I/JPEG-LS IMPLEMENTATION V.0.90 ------------------------------------------------------------------------------- (c) COPYRIGHT HEWLETT-PACKARD COMPANY, 1995-1999. HEWLETT-PACKARD COMPANY ("HP") DOES NOT WARRANT THE ACCURACY OR COMPLETENESS OF THE INFORMATION GIVEN HERE. ANY USE MADE OF, OR RELIANCE ON, SUCH INFORMATION IS ENTIRELY AT USER'S OWN RISK. BY DOWNLOADING THE LOCO-I/JPEG-LS COMPRESSORS/DECOMPRESSORS ("THE SOFTWARE") YOU AGREE TO BE BOUND BY THE TERMS AND CONDITIONS OF THIS LICENSING AGREEMENT. YOU MAY DOWNLOAD AND USE THE SOFTWARE FOR NON-COMMERCIAL PURPOSES FREE OF CHARGE OR FURTHER OBLIGATION. YOU MAY NOT, DIRECTLY OR INDIRECTLY, DISTRIBUTE THE SOFTWARE FOR A FEE, INCORPORATE THIS SOFTWARE INTO ANY PRODUCT OFFERED FOR SALE, OR USE THE SOFTWARE TO PROVIDE A SERVICE FOR WHICH A FEE IS CHARGED. YOU MAY MAKE COPIES OF THE SOFTWARE AND DISTRIBUTE SUCH COPIES TO OTHER PERSONS PROVIDED THAT SUCH COPIES ARE ACCOMPANIED BY HEWLETT-PACKARD'S COPYRIGHT NOTICE AND THIS AGREEMENT AND THAT SUCH OTHER PERSONS AGREE TO BE BOUND BY THE TERMS OF THIS AGREEMENT. THE SOFTWARE IS NOT OF PRODUCT QUALITY AND MAY HAVE ERRORS OR DEFECTS. THE JPEG-LS STANDARD IS STILL UNDER DEVELOPMENT. THE SOFTWARE IS NOT A FINAL OR FULL IMPLEMENTATION OF THE STANDARD. HP GIVES NO EXPRESS OR IMPLIED WARRANTY OF ANY KIND AND ANY IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR PURPOSE ARE DISCLAIMED. HP SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE. -------------------------------------------------------------------------------*//* decoder.c --- the main module, argument parsing, file I/O * * Initial code by Alex Jakulin, Aug. 1995 * * Modified and optimized: Gadiel Seroussi, October 1995 * * Color Enhancement: Guillermo Sapiro, August 1996 * * Modified and added Restart marker and input tables by: * David Cheng-Hsiu Chu, and Ismail R. Ismail march 1999 */#include "global.h"#include <string.h>#include "jpegmark.h"#define PGMNAME PGMPREFIX "d"#define EPGMNAME PGMPREFIX "e"static char *banner="\n\=============================================\n\SPMG/JPEG-LS DECOMPRESSOR " JPEGLSVERSION "\n\=============================================\n\These programs are Copyright (c) University of British Columbia.\n\All rights reserved. They may be freely redistributed in their\n\entirety provided that this copyright notice is not removed.\n\They may not be sold for profit or incorporated in commercial\n\programs without the written permission of the copyright holder.\n\Each program is provided as is, without any express or implied\n\warranty, without even the warranty of fitness for a particular\n\purpose.\n\\n\=========================================================\n\THIS SOFTWARE IS BASED ON HP's implementation of jpeg-ls:\n\=========================================================\n\(c) COPYRIGHT HEWLETT-PACKARD COMPANY, 1995-1999.\n";pixel *pscanline, *cscanline, *pscanl0, *cscanl0;pixel *c_pscanline[MAX_COMPONENTS], *c_cscanline[MAX_COMPONENTS], *c_pscanl0[MAX_COMPONENTS], *c_cscanl0[MAX_COMPONENTS];jpeg_ls_header *head_frame, *head_scan[MAX_SCANS];int columns, rows, components, samplingx[MAX_COMPONENTS], samplingy[MAX_COMPONENTS];int c_columns[MAX_COMPONENTS], c_rows[MAX_COMPONENTS];int whose_max_size_columns, whose_max_size_rows, number_of_scans;int color_mode;int shift; /* Shift value for sparse images */int got_lse; /* got an LSE marker */int got_table; /* got a mapping table */int got_restart; /* got a restart marker indicatino */int restart_interval; /* the restart interval */int multi; /* if the files are separate */int application_header; /* application bytes in the header */int lossy; /* Indicates if in lossy mode or not */int bpp16; /* Indicates if 16 bits per pixel mode or not */int lutmax; /* lutmax is either 256 or 4501 *//* reset */#ifndef FIXRESETint RESET;#endif/* alphabet size */#ifndef FIXALPHAint alpha, /* alphabet size */ ceil_half_alpha; /* ceil(alpha/2) */#endif#ifdef POW2int highmask;#endif/* Write one row of pixel values */inline void write_one_line(pixel* line, int cols, FILE* outfile){ int i, index; unsigned int* maptable; /* No mapping tables used */ if(!(head_scan[0]->need_table)) if (bpp16==FALSE) { unsigned char* line8; line8 = (unsigned char*)safealloc(cols); for (i=0; i< cols; i++) *(line8+i)=ENDIAN8(*(line+i)); fwrite(line8, sizeof(unsigned char), cols, outfile); free(line8); } else { fwrite(line, sizeof(short), cols, outfile); } /* Mapping tables used */ else { if (bpp16==FALSE) { /* Write one byte per table entry */ if (head_scan[0]->Wt == 1) { unsigned char* line8; line8 = (unsigned char*)safealloc(cols); /* If don't have 2, it mallocs over the table? */ maptable = head_scan[0]->TABLE[head_scan[0]->TID]; for (i=0; i<cols; i++) { index = *(line+i); *(line8+i) = ENDIAN8(maptable[index]); } fwrite(line8, sizeof(unsigned char), cols, outfile); free(line8); } /* Write two bytes per table entry */ else if (head_scan[0]->Wt == 2) { unsigned short* line16; line16 = (unsigned short*)safealloc(cols*2); maptable = head_scan[0]->TABLE[head_scan[0]->TID]; for (i=0; i<cols; i++) { index = *(line+i); *(line16+i) = (unsigned short) maptable[index]; } fwrite(line16, sizeof(short), cols, outfile); free(line16); } /* Write three bytes per table entry */ else if (head_scan[0]->Wt == 3) { unsigned char* line8_3; line8_3 = (unsigned char*)safealloc(cols*3); maptable = head_scan[0]->TABLE[head_scan[0]->TID]; for (i=0; i<cols; i++) { index = *(line+i); *(line8_3 + (i*3)) = (unsigned char) (maptable[index] >> 16); *(line8_3 + (i*3) + 1) = (unsigned char) (maptable[index] >> 8); *(line8_3 + (i*3) + 2) = (unsigned char) (maptable[index]); } fwrite(line8_3, sizeof(char), cols*3, outfile); free(line8_3); } /* Can't do 16 bit index values */ } else { fprintf(stderr, "Sorry, mapping tables are only supported for 8 bpp images in this implementation.\n"); exit(0); } }}void initbuffers(int multi, int comp) { int i; if (multi) /* Output to several files */ for (i=0;i<comp;i++) { c_pscanl0[i] = (pixel *)safecalloc((c_columns[i]+LEFTMARGIN+RIGHTMARGIN), sizeof(pixel) ); c_cscanl0[i] = (pixel *)safecalloc((c_columns[i]+LEFTMARGIN+RIGHTMARGIN), sizeof(pixel) ); /* Adjust scan line pointers taking into account the margins, and also the fact that indexing for scan lines starts from 1 */ c_pscanline[i] = c_pscanl0[i] + (LEFTMARGIN-1); c_cscanline[i] = c_cscanl0[i] + (LEFTMARGIN-1); } else { /* Output to a single file */ pscanl0 = (pixel *)safecalloc(components*(columns+LEFTMARGIN+RIGHTMARGIN), sizeof(pixel) ); cscanl0 = (pixel *)safecalloc(components*(columns+LEFTMARGIN+RIGHTMARGIN), sizeof(pixel) ); /* Adjust scan line pointers taking into account the margins, and also the fact that indexing for scan lines starts from 1 (this will probably have to be changed in the future) */ pscanline = pscanl0 + components*(LEFTMARGIN-1); cscanline = cscanl0 + components*(LEFTMARGIN-1); } createzeroLUT();}swaplines(){ pixel *temp; temp = pscanl0; pscanl0 = cscanl0; cscanl0 = temp; pscanline = pscanl0 + components*(LEFTMARGIN-1); cscanline = cscanl0 + components*(LEFTMARGIN-1);}c_swaplines(int i){ pixel *temp; temp = c_pscanl0[i]; c_pscanl0[i] = c_cscanl0[i]; c_cscanl0[i] = temp; c_pscanline[i] = c_pscanl0[i] + (LEFTMARGIN-1); c_cscanline[i] = c_cscanl0[i] + (LEFTMARGIN-1);}void closebuffers(int multi) { int i; fclose(in); if (multi==0) fclose(out); else for (i=0;i<components;i++) fclose(c_out[i]); free(pscanl0); free(cscanl0);}int initialize(int argc, char *argv[]) { char line[256],tmp_char[1], *infilename = NULL, *outfilename = OUTFILE ".out", *c_outfilename[MAX_COMPONENTS], *color_mode_string; int i, max_samp_columns, max_samp_rows, mk, n_s, end_of_seek=0, seek_return, alpha0, gotinf = 0, gotoutf = 0, out_files=0; int pos; /* position in the file, after the header */ for (i=0;i<MAX_COMPONENTS;i++) { c_outfilename[i]=malloc(strlen(OUTFILE)+20); sprintf(c_outfilename[i],"%s%d.out",OUTFILE,i+1); } multi=1; for ( i=1; i<argc; i++ ) if ( argv[i][0] == '-' ) { switch ( argv[i][1] ) { case 'i': infilename = argv[i]+2; gotinf = 1; break; case 'o': outfilename = c_outfilename[out_files++] = argv[i]+2; gotoutf = 1; break; case 'P': multi=0; break; case 'v': if ( sscanf(argv[i],"-v%d",&verbose) != 1 ) verbose=2; break; default: usage(); exit(0); } } else { if ( ! gotinf ) { infilename = argv[i]; gotinf = 1; } else if ( ! gotoutf ) outfilename = c_outfilename[out_files++] = argv[i]; } if ( verbose < 1 ) verbose = 1; /* at least the banner will show */ if ((!multi) && (out_files>1)) { fprintf(stderr,"\nERROR: Only 1 output file expected with -P option.\nThis option is valid only for line/pixel interleaved and not subsampled.\n"); exit(10); } /* Open in file */ if ( infilename == NULL ) { usage(); exit(0); } else { if ( strcmp(infilename,"-")==0 ) in = stdin; else if ( (in=fopen(infilename,"rb")) == NULL ) { perror(infilename); exit(10); } } /* Read the compressed image frame header */ bufiinit(in); head_frame = (jpeg_ls_header *) safecalloc(1,sizeof(jpeg_ls_header)); for (n_s=0;n_s<MAX_SCANS;n_s++) { head_scan[n_s] = (jpeg_ls_header *) safecalloc(1,sizeof(jpeg_ls_header)); head_scan[n_s]->T1=head_scan[n_s]->T2=head_scan[n_s]->T3=0; head_scan[n_s]->RES = DEFAULT_RESET; } /* Read SOI */ seek_return = seek_marker(in,&mk); if (seek_return == EOF) { fprintf(stderr,"*** Premature End of File seeking SOI\n"); exit(10); } else { pos = seek_return; if (mk != SOI) { fprintf(stderr,"Marker %04x found: first marker must be SOI (%04x) in this implementation\n", mk, SOI); exit(10); } } /* Read SOF_LS */ seek_return = seek_marker(in,&mk); if (seek_return == EOF) { fprintf(stderr,"*** Premature End of File seeking SOF_LS\n"); exit(10); } else { pos += seek_return; /* Read SOF_LS */ if (mk != SOF_LS) { fprintf(stderr,"Marker %04x found: second marker must be SOF_LS (%04x) in this implementation\n", mk, SOF_LS); exit(10); } } /* Read the frame header (SOF) */ seek_return = read_jpegls_frame(in,head_frame); if (seek_return == EOF) { fprintf(stderr,"*** Premature End of File reading frame header\n"); exit(10); } else pos += seek_return; head_scan[0]->alp = head_frame->alp; /* default alpha */ /* LSE Extension header */ /* This version supports only 2 extension headers, and 1 set of parameters for all the scans */ got_lse = 0; head_scan[0]->need_table = 0; while (!end_of_seek) { seek_return=seek_marker(in, &mk); if (seek_return == EOF) { fprintf(stderr,"*** Premature End of File seeking SOS or LSE marker\n"); exit(10); } pos +=seek_return; switch(mk) { case LSE: seek_return = read_jpegls_extmarker(in, head_scan[0]); if (seek_return == EOF) { fprintf(stderr,"*** Premature End of File\n"); exit(10); } pos += seek_return; got_lse = 1; break; case DRI: seek_return = read_jpegls_restartmarker(in, head_scan[0]); pos += seek_return; got_restart = 1; restart_interval = head_scan[0]->restart_interval; break; case SOS: end_of_seek=1; break; } } /* End of extension header */ /* Read the scan header*/ seek_return = read_jpegls_scan(in,head_scan[0]); if (seek_return == EOF) { fprintf(stderr,"*** Premature End of File reading scan marker segment\n"); exit(10); } pos+=seek_return; shift=head_scan[0]->shift; if (shift!=0) { fprintf(stderr,"Got shift = %d != 0 : not implemented.\n",shift); exit(10); } NEAR=head_scan[0]->NEAR; color_mode=head_scan[0]->color_mode; columns=head_frame->columns; rows=head_frame->rows; alpha0=head_scan[0]->alp; head_frame->alp = alpha0; components=head_frame->comp; if (color_mode==PLANE_INT) number_of_scans=components; else number_of_scans=1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -