/* 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 FIXRESET int RESET; #endif /* alphabet size */ #ifndef FIXALPHA int alpha, /* alphabet size */ ceil_half_alpha; /* ceil(alpha/2) */ #endif #ifdef POW2 int highmask; #endif /* Read one row of pixel values */ inline 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) != cols) fprintf(stderr,"Input file is truncated"); for(i=0; i1) 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;i1 ) fprintf(msgfile,"Number of contexts (non-run): %d regular + %d EOR = %d\n",CONTEXTS-LESS_CONTEXTS,EOR_CONTEXTS,TOT_CONTEXTS-LESS_CONTEXTS); /* Read image headers*/ if (multi==0) { if ( read_header_6(in, &columns, &rows, &alpha0, &components) != 0 ) error("Could not read image header. Must be PPM or PGM file.\n"); /* Single component => PLANE_INT */ if ( (color_mode==LINE_INT || color_mode==PIXEL_INT) && components==1) { /* fprintf(msgfile,"Single component received: Color mode changed to PLANE INTERLEAVED\n"); */ color_mode=PLANE_INT; color_mode_string = plane_int_string; multi=1; c_columns[0]=columns; c_rows[0] = rows; c_in[0]=in; } } else { for (i=0;i>=1, i++); if ( alpha != (1< MAXA8) { bpp16 = TRUE; lutmax = LUTMAX16; } else if (alpha <= MAXA8 && alpha >= 1) { bpp16 = FALSE; lutmax = LUTMAX8; } else { fprintf(stderr,"Got alpha = %d\n",alpha); error("Bad value for alpha. Sorry...\n"); } /* print out parameters */ if ( verbose ) { if (!multi) fprintf(msgfile,"Input file: %s\nOutput file: %s\n",infilename,outfilename); else { fprintf(msgfile,"Input files: "); for (i=0;i1) && (multi)) { min_size_columns=c_columns[components-1]; min_size_rows=c_rows[components-1]; for (i=0;i4) || ((c_columns[i]%min_size_columns)!=0)) { fprintf(stderr,"\nImage sizes not compatible\n"); exit(10); } if ((samplingy[i]>4) || ((c_rows[i]%min_size_rows)!=0)) { fprintf(stderr,"\nImage sizes not compatible\n"); exit(10); } } min_size_columns=c_columns[0]; whose_max_size_columns=0; min_size_rows=c_rows[0]; whose_max_size_rows=0; for (i=1;imin_size_columns) { whose_max_size_columns=i; min_size_columns=c_columns[i]; } if (c_rows[i]>min_size_rows) { whose_max_size_rows=i; min_size_rows=c_rows[i]; } } } else { for (i=0;icolumns=columns; head_frame->rows=rows; } else { head_frame->columns=c_columns[whose_max_size_columns]; head_frame->rows=c_rows[whose_max_size_rows]; } head_frame->alp=alpha; head_frame->comp=components; /* Assign component id and samplingx/samplingy */ for (i=0;icomp_ids[i]=i+1; head_frame->samplingx[i]=samplingx[i]; head_frame->samplingy[i]=samplingy[i]; } head_frame->NEAR=NEAR; /* Not needed, scan information */ head_frame->need_lse=need_lse; /* Not needed, for commpletness */ head_frame->color_mode=color_mode; /* Not needed, scan information */ head_frame->shift=shift; /* Not needed, scan information */ for (n_s=0;n_salp = alpha; head_scan[n_s]->NEAR = NEAR; head_scan[n_s]->T1 = T1; head_scan[n_s]->T2 = T2; head_scan[n_s]->T3 = T3; head_scan[n_s]->RES = RESET; head_scan[n_s]->shift = shift; head_scan[n_s]->color_mode = color_mode; } if (color_mode==PLANE_INT) { /* One plane per scan */ for (n_s=0;n_scomp=1; head_scan[n_s]->comp_ids[0]=n_s+1; } } else { for (n_s=0;n_scomp=head_frame->comp; for (n_c=0;n_ccomp;n_c++) head_scan[n_s]->comp_ids[n_c]=n_c+1; } } /* Write SOI */ all_header = write_marker(out, SOI); /* Write the frame */ all_header += write_jpegls_frame(out, head_frame); /* End of frame header writing */ if ((components>1) && (multi==0)) { /* Received PPM file, allocate auxiliary buffers */ local_scanl0 = (pixel *)safecalloc(columns+LEFTMARGIN+RIGHTMARGIN,sizeof(pixel) ); local_scanl1 = (pixel *)safecalloc(columns+LEFTMARGIN+RIGHTMARGIN,sizeof(pixel) ); local_pscanline = local_scanl0 + LEFTMARGIN-1; local_cscanline = local_scanl1 + LEFTMARGIN-1; } /* Go through each scan and process line by line */ for (n_s=0;n_sT1=T1; head_scan[n_s]->T2=T2; head_scan[n_s]->T3=T3; } /* After the thresholds are set, write LSE marker if we have */ /* non-default parameters or if we need a mapping table */ if ( need_lse != 0 ) all_header += write_jpegls_extmarker(out, head_scan[n_s], LSE_PARAMS, mappingtablefilename); if ( need_table != 0 ) all_header += write_jpegls_extmarker(out, head_scan[n_s], LSE_MAPTABLE, mappingtablefilename); /* If using restart markers, write the DRI header */ if ( need_restart != 0 ) { head_scan[n_s]->restart_interval = restart_interval; all_header += write_jpegls_restartmarker(out, head_scan[n_s]); } /* Print out parameters */ if (verbose) fprintf(msgfile,"Parameters: T1=%d T2=%d T3=%d RESET=%d limit=%d\n",T1, T2, T3,RESET,limit); /* Prepare LUTs for context quantization */ /* Must re-do when Thresholds change */ prepareLUTs(); if (lossy==TRUE) /* prepare div/mul tables for near-lossless quantization */ prepare_qtables(alpha, NEAR); /* Check for errors */ check_compatibility(head_frame, head_scan[0],0); } /* Restart Marker is reset after every scan */ MCUs_counted = 0; /* Write the scan header */ all_header += write_jpegls_scan(out, head_scan[n_s]); pos0 = ftell(out); /* position in output file, after header */ /* Initializations for each scan */ /* Start from 1st image row */ n=0; /* initialize stats arrays */ if (lossy==TRUE) init_stats(qbeta); else init_stats(alpha); /* initialize run processing */ init_process_run(MAXRUN); if (color_mode==LINE_INT) { /* line interleaved */ if (!multi) { /* Single file received */ /***********************************************************************/ /* Line interleaved mode with single file received */ /***********************************************************************/ if (lossy==FALSE) /* LOSSLESS mode */ while (++n <= rows) { read_one_line(cscanline + components, components*columns, in); tot_in += components*columns; /* 'extend' the edges */ for (n_c=0;n_c 1) { for (my_i=0;my_i 1) { for (my_i=0;my_i1) for (my_i=0;my_i : Alternate input specification, use -i- for stdin.\n\ -o: Output specification, use -o- for stdout (default=%s).\n\ -Ta, -Tb, -Tc: thresholds for context quantization (a.k.a.\n\ T1,T2,T3; must have Ta<=Tb<=Tc<=maxs; defaults depend on alphabet\n\ size and lossless/near-lossless mode; see standard specification).\n\ -r : Reset interval for statistics (standard default=%d).\n\ ", "locoe", OUTFILE COMPSUFFIX,DEFAULT_RESET); fprintf(stderr,"\ -c : Mode for multi-component images (program default=%d):\n\ %d:%s %d:%s %d:%s.\n", DEFAULT_COLOR_MODE, PLANE_INT, "plane-interleaved", LINE_INT, "line-interleaved", PIXEL_INT, "sample-interleaved" ); fprintf(stderr,"\ -n or\n\ -e : Max allowed loss per symbol (default = %d).\n", DEF_NEAR); fprintf(stderr,"\ -m : Use mapping table where
is a file in the format:\n\ 1st byte of
is the Table ID,\n\ 2nd byte of
is the Width of each table entry (in bytes),\n\ 3rd - 6th byte of
is the Max Table Index Value specified\n\ as an integer (4 bytes),\n\ 7th byte and on are the table entries.\n"); fprintf(stderr,"\ -t : Use Restart Markers where is the restart interval \n\ (ie. number of MCU's between restart markers).\n"); fprintf(stderr,"\ -h : Print this help.\n"); fprintf(stderr,"\ *** No spaces allowed between a flag and its argument.\n\ *** Use -Ta,-Tb,-Tc,-r only if you know what you are doing!\n\ "); } /* Print out message for a bad flag */ bad_flag(char *s) { fprintf(stderr,"Bad flag %s\n",s); usage(); exit(10); } /* Functions that read the PGM header */ #define HEADER_MAXLINE 256 int read_header_6(FILE *fin, int *widthp, int *heightp, int *maxvalp, int *comp) { char line[HEADER_MAXLINE]; int cols,rows,maxval; if ( nextline(line, fin) != 0 ) return -10; if (strncmp(line,"P5",2)==0) *comp=1; else if (strncmp(line,"P6",2)==0) *comp=3; else if (strncmp(line,"P7",2)==0) *comp=4; else return -1; if ( nextline(line, fin) != 0 ) return -10; if ( sscanf(line,"%d %d",&cols,&rows) != 2 ) return -1; if ( nextline(line, fin) != 0 ) return -10; if ( sscanf(line,"%d",&maxval) != 1 ) return -1; *widthp = cols; *heightp = rows; *maxvalp = maxval; return 0; } int read_header_5(FILE *fin, int *widthp, int *heightp, int *maxvalp) { char line[HEADER_MAXLINE]; int cols,rows,maxval; if ( nextline(line, fin) != 0 ) return -10; if (strncmp(line,"P5",2)!=0) return -1; if ( nextline(line, fin) != 0 ) return -10; if ( sscanf(line,"%d %d",&cols,&rows) != 2 ) return -1; if ( nextline(line, fin) != 0 ) return -10; if ( sscanf(line,"%d",&maxval) != 1 ) return -1; *widthp = cols; *heightp = rows; *maxvalp = maxval; return 0; } /* Used to read in header lines of PGM/PPM files */ int nextline(char *line, FILE *fp) { char *p; do { p = fgets(line, HEADER_MAXLINE, fp); /* if ( p != NULL ) fprintf(stderr,"%s",line); */ } while ( p != NULL && *p == '#' ); if ( p==NULL ) return -1; return 0; }