📄 encoder.c
字号:
/* SPMG/JPEG-LS IMPLEMENTATION V.2.1 ===================================== These programs are Copyright (c) University of British Columbia. All rights 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. -------------------------------------------------------------------------------*//* encoder.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 "math.h"#include "string.h"#include "jpegmark.h"static char *banner="\n\=============================================\n\SPMG/JPEG-LS COMPRESSOR " 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, *scanl0, *scanl1;pixel *c_pscanline[MAX_COMPONENTS], *c_cscanline[MAX_COMPONENTS], *c_scanl0[MAX_COMPONENTS], *c_scanl1[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];int c_rows[MAX_COMPONENTS];int whose_max_size_rows, whose_max_size_columns;int color_mode; int need_lse; /* if we need an LSE marker (non-default params) */int need_table; /* if we need an LSE marker (mapping table) */int need_restart; /* if we need to add restart markers */int restart_interval; /* indicates the restart interval */int multi; /* if the files are separate */int application_header; /* application bytes written in the header */int all_header; /* all bytes of the header, including application bytes and JPEG-LS bytes */int shift=0; /* Shift value for sparse images */int palete=0; /* for paletized images */int lossy; /* Indicates if in lossy mode or not */int lutmax; /* lutmax is either 256 or 4501 */int bpp16; /* Indicates if 16 bits per pixel mode or not */char *mappingtablefilename=NULL; /* Mapping table filename *//* reset */#ifndef FIXRESETint RESET;#endif/* alphabet size */#ifndef FIXALPHAint alpha, /* alphabet size */ ceil_half_alpha; /* ceil(alpha/2) */#endif#ifdef POW2int highmask;#endif/* Read one row of pixel values */void read_one_line(pixel* line, int cols, FILE* infile){ unsigned char* line8; int i; if (bpp16==FALSE) { line8 = (unsigned char *)safealloc(cols); if (fread(line8, 1, cols, infile) != (unsigned int)cols) fprintf(stderr,"Input file is truncated"); for(i=0; i<cols; i++) line[i] = line8[i]; free(line8); } else { if (fread(line, 2, cols, infile) != (unsigned int)cols) fprintf(stderr,"Input file is truncated"); }}/* Initialize the buffers for each line */void initbuffers(int multi, int comp) { int i; if (multi) /* The files are received independent */ for (i=0;i<comp;i++) { c_scanl0[i] = (pixel *)safecalloc((c_columns[i]+LEFTMARGIN+RIGHTMARGIN), sizeof(pixel) ); c_scanl1[i] = (pixel *)safecalloc((c_columns[i]+LEFTMARGIN+RIGHTMARGIN), sizeof(pixel) ); c_pscanline[i] = c_scanl0[i] + (LEFTMARGIN-1); c_cscanline[i] = c_scanl1[i] + (LEFTMARGIN-1); } else { /* Only 1 file received */ scanl0 = (pixel *)safecalloc(components*(columns+LEFTMARGIN+RIGHTMARGIN), sizeof(pixel) ); scanl1 = (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 */ pscanline = scanl0 + components*(LEFTMARGIN-1); cscanline = scanl1 + components*(LEFTMARGIN-1); } bitoinit();}/* Swap the pointers to the current and previous scanlines */void swaplines(){ pixel *temp; temp = pscanline; pscanline = cscanline; cscanline = temp;}/* Swap the pointers to the current and previous scanlines */void c_swaplines(int i){ pixel *temp; temp = c_pscanline[i]; c_pscanline[i] = c_cscanline[i]; c_cscanline[i] = temp;}/* close the line buffers */int closebuffers(int multi) { int pos,i; bitoflush(); if (multi==0) fclose(in); else for (i=0;i<components;i++) fclose(c_in[i]); pos = ftell(out); fclose(out); free(scanl0); free(scanl1); return pos;}/* Initialization Function - Reads in parameters from image and command line */
extern int read_header_5(FILE *fin, int *widthp, int *heightp, int *maxvalp);
extern bad_flag(char *s);
extern usage();
extern int read_header_6(FILE *fin, int *widthp, int *heightp, int *maxvalp, int *comp);void initialize(int argc, char *argv[]) { char *infilename=NULL, *c_infilename[MAX_COMPONENTS], *outfilename = OUTFILE COMPSUFFIX, *color_mode_string; int i, n_c, common_rows, common_alpha, min_size_rows, min_size_columns, temp_reset, alpha0, gotinf = 0, gotoutf = 0; n_c=0; multi=0; color_mode=DEFAULT_COLOR_MODE; need_lse=0; need_table=0; need_restart=0; restart_interval=0; components=0; T1=T2=T3=0;#ifndef FIXRESET RESET=DEFAULT_RESET;#endif /* Initialize NEAR to zero and loss-less mode */ NEAR = DEF_NEAR; lossy = FALSE; /* Go through the arguments in command line */ for ( i=1; i<argc; i++ ) if ( argv[i][0] == '-' ) { switch ( argv[i][1] ) { /* Enable use of Restart Markers */ case 't': need_restart = 1; if ( sscanf(argv[i]+2,"%d",&restart_interval) != 1 ) { bad_flag(argv[i]); } break; /* Enable use of Mapping Tables */ case 'm': need_table = 2; mappingtablefilename = argv[i]+2; break; /* Reset value */ case 'r': if ( sscanf(argv[i]+2,"%d",&temp_reset) != 1 ) { bad_flag(argv[i]); } if ( temp_reset != DEFAULT_RESET ) { need_lse = 1;#ifdef FIXRESET fprintf(stderr,"ERROR: This version compiled with fixed RESET = %d, got %d\n",DEFAULT_RESET,temp_reset); exit(10);#else RESET = temp_reset;#endif } break; /* Colour mode */ case 'c': if ( sscanf(argv[i]+2,"%d",&color_mode) != 1 ) { bad_flag(argv[i]); } break; /* Sparse(?) mode */ case 'p': if ( sscanf(argv[i]+2,"%d",&shift) != 1 ) { bad_flag(argv[i]); } if (shift!=0) { fprintf(stderr,"Sorry, sparse mode not implemented (shift=%d).\n",shift); exit(10); } break; /* Infile names */ case 'i': infilename = c_infilename[components++] = argv[i]+2; gotinf = 1; break; /* Outfile names */ case 'o': outfilename = argv[i]+2; gotoutf = 1; break; /* Verbose level */ case 'v': if ( sscanf(argv[i],"-v%d",&verbose) != 1 ) { verbose=2; } break; /* Error level - if 0 then means loss-less mode */ case 'e': case 'n': if ( sscanf(argv[i]+2,"%d",&NEAR) != 1 ) { bad_flag(argv[i]); } if ( NEAR == 0 ) lossy = FALSE; else lossy = TRUE; break; /* Threshold Levels */ case 's': case 'S': case 'T': need_lse = 1; switch(argv[i][2]) { case 'a': if ( sscanf(argv[i]+3,"%d",&T1) != 1 ) { bad_flag(argv[i]); } break; case 'b': if ( sscanf(argv[i]+3,"%d",&T2) != 1 ) { bad_flag(argv[i]); } break; case 'c': if ( sscanf(argv[i]+3,"%d",&T3) != 1 ) { bad_flag(argv[i]); } break; default: bad_flag(argv[i]); break; } break; default: usage(); exit(0); } } else { infilename = c_infilename[components++] = argv[i]; gotinf = 1; } if ( verbose < 1 ) verbose = 1; /* at least the banner will show */ /* check that color mode is valid and pick color mode string */ switch ( color_mode ) { case PLANE_INT: color_mode_string = plane_int_string; multi=1; break; case LINE_INT: color_mode_string = line_int_string; if (components>1) multi=1; break; case PIXEL_INT: color_mode_string = pixel_int_string; if (components>1){ fprintf(stderr,"ERROR: specified more than 1 input file in pixel interleaved mode\n"); exit(10); } break; default: fprintf(stderr,"ERROR: Invalid color mode %d\n",color_mode); usage(); exit(10); } /* Assign file pointers to in files */ if ( (infilename == NULL) && (multi==0 || components<1) ) { usage(); exit(0); } else { if ( strcmp(infilename,"-") == 0 ) in = stdin; else { if (multi==0) { if ( (in=fopen(infilename,"rb")) == NULL ) { perror(infilename); exit(10); } } else { for (i=0;i<components;i++) if ( (c_in[i]=fopen(c_infilename[i],"rb")) == NULL ) { perror(c_infilename[i]); exit(10); } } } } /* Assigns pointers to out files */ if ( outfilename == NULL ) { usage(); exit(0); } else { if ( strcmp(outfilename,"-") == 0 ) { out = stdout; msgfile = stderr; } else if ( (out=fopen(outfilename,"wb")) == NULL ) { perror(outfilename); exit(10); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -