rdswitch.c
Go to the documentation of this file.
00001 /*
00002  * rdswitch.c
00003  *
00004  * Copyright (C) 1991-1996, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains routines to process some of cjpeg's more complicated
00009  * command-line switches.  Switches processed here are:
00010  *      -qtables file           Read quantization tables from text file
00011  *      -scans file             Read scan script from text file
00012  *      -qslots N[,N,...]       Set component quantization table selectors
00013  *      -sample HxV[,HxV,...]   Set component sampling factors
00014  */
00015 
00016 #include "cdjpeg.h"             /* Common decls for cjpeg/djpeg applications */
00017 #include <ctype.h>              /* to declare isdigit(), isspace() */
00018 
00019 
00020 LOCAL(int)
00021 text_getc (FILE * file)
00022 /* Read next char, skipping over any comments (# to end of line) */
00023 /* A comment/newline sequence is returned as a newline */
00024 {
00025   register int ch;
00026   
00027   ch = getc(file);
00028   if (ch == '#') {
00029     do {
00030       ch = getc(file);
00031     } while (ch != '\n' && ch != EOF);
00032   }
00033   return ch;
00034 }
00035 
00036 
00037 LOCAL(boolean)
00038 read_text_integer (FILE * file, long * result, int * termchar)
00039 /* Read an unsigned decimal integer from a file, store it in result */
00040 /* Reads one trailing character after the integer; returns it in termchar */
00041 {
00042   register int ch;
00043   register long val;
00044   
00045   /* Skip any leading whitespace, detect EOF */
00046   do {
00047     ch = text_getc(file);
00048     if (ch == EOF) {
00049       *termchar = ch;
00050       return FALSE;
00051     }
00052   } while (isspace(ch));
00053   
00054   if (! isdigit(ch)) {
00055     *termchar = ch;
00056     return FALSE;
00057   }
00058 
00059   val = ch - '0';
00060   while ((ch = text_getc(file)) != EOF) {
00061     if (! isdigit(ch))
00062       break;
00063     val *= 10;
00064     val += ch - '0';
00065   }
00066   *result = val;
00067   *termchar = ch;
00068   return TRUE;
00069 }
00070 
00071 
00072 GLOBAL(boolean)
00073 read_quant_tables (j_compress_ptr cinfo, char * filename,
00074                    int scale_factor, boolean force_baseline)
00075 /* Read a set of quantization tables from the specified file.
00076  * The file is plain ASCII text: decimal numbers with whitespace between.
00077  * Comments preceded by '#' may be included in the file.
00078  * There may be one to NUM_QUANT_TBLS tables in the file, each of 64 values.
00079  * The tables are implicitly numbered 0,1,etc.
00080  * NOTE: does not affect the qslots mapping, which will default to selecting
00081  * table 0 for luminance (or primary) components, 1 for chrominance components.
00082  * You must use -qslots if you want a different component->table mapping.
00083  */
00084 {
00085   FILE * fp;
00086   int tblno, i, termchar;
00087   long val;
00088   unsigned int table[DCTSIZE2];
00089 
00090   if ((fp = fopen(filename, "r")) == NULL) {
00091     fprintf(stderr, "Can't open table file %s\n", filename);
00092     return FALSE;
00093   }
00094   tblno = 0;
00095 
00096   while (read_text_integer(fp, &val, &termchar)) { /* read 1st element of table */
00097     if (tblno >= NUM_QUANT_TBLS) {
00098       fprintf(stderr, "Too many tables in file %s\n", filename);
00099       fclose(fp);
00100       return FALSE;
00101     }
00102     table[0] = (unsigned int) val;
00103     for (i = 1; i < DCTSIZE2; i++) {
00104       if (! read_text_integer(fp, &val, &termchar)) {
00105         fprintf(stderr, "Invalid table data in file %s\n", filename);
00106         fclose(fp);
00107         return FALSE;
00108       }
00109       table[i] = (unsigned int) val;
00110     }
00111     jpeg_add_quant_table(cinfo, tblno, table, scale_factor, force_baseline);
00112     tblno++;
00113   }
00114 
00115   if (termchar != EOF) {
00116     fprintf(stderr, "Non-numeric data in file %s\n", filename);
00117     fclose(fp);
00118     return FALSE;
00119   }
00120 
00121   fclose(fp);
00122   return TRUE;
00123 }
00124 
00125 
00126 #ifdef C_MULTISCAN_FILES_SUPPORTED
00127 
00128 LOCAL(boolean)
00129 read_scan_integer (FILE * file, long * result, int * termchar)
00130 /* Variant of read_text_integer that always looks for a non-space termchar;
00131  * this simplifies parsing of punctuation in scan scripts.
00132  */
00133 {
00134   register int ch;
00135 
00136   if (! read_text_integer(file, result, termchar))
00137     return FALSE;
00138   ch = *termchar;
00139   while (ch != EOF && isspace(ch))
00140     ch = text_getc(file);
00141   if (isdigit(ch)) {            /* oops, put it back */
00142     if (ungetc(ch, file) == EOF)
00143       return FALSE;
00144     ch = ' ';
00145   } else {
00146     /* Any separators other than ';' and ':' are ignored;
00147      * this allows user to insert commas, etc, if desired.
00148      */
00149     if (ch != EOF && ch != ';' && ch != ':')
00150       ch = ' ';
00151   }
00152   *termchar = ch;
00153   return TRUE;
00154 }
00155 
00156 
00157 GLOBAL(boolean)
00158 read_scan_script (j_compress_ptr cinfo, char * filename)
00159 /* Read a scan script from the specified text file.
00160  * Each entry in the file defines one scan to be emitted.
00161  * Entries are separated by semicolons ';'.
00162  * An entry contains one to four component indexes,
00163  * optionally followed by a colon ':' and four progressive-JPEG parameters.
00164  * The component indexes denote which component(s) are to be transmitted
00165  * in the current scan.  The first component has index 0.
00166  * Sequential JPEG is used if the progressive-JPEG parameters are omitted.
00167  * The file is free format text: any whitespace may appear between numbers
00168  * and the ':' and ';' punctuation marks.  Also, other punctuation (such
00169  * as commas or dashes) can be placed between numbers if desired.
00170  * Comments preceded by '#' may be included in the file.
00171  * Note: we do very little validity checking here;
00172  * jcmaster.c will validate the script parameters.
00173  */
00174 {
00175   FILE * fp;
00176   int scanno, ncomps, termchar;
00177   long val;
00178   jpeg_scan_info * scanptr;
00179 #define MAX_SCANS  100          /* quite arbitrary limit */
00180   jpeg_scan_info scans[MAX_SCANS];
00181 
00182   if ((fp = fopen(filename, "r")) == NULL) {
00183     fprintf(stderr, "Can't open scan definition file %s\n", filename);
00184     return FALSE;
00185   }
00186   scanptr = scans;
00187   scanno = 0;
00188 
00189   while (read_scan_integer(fp, &val, &termchar)) {
00190     if (scanno >= MAX_SCANS) {
00191       fprintf(stderr, "Too many scans defined in file %s\n", filename);
00192       fclose(fp);
00193       return FALSE;
00194     }
00195     scanptr->component_index[0] = (int) val;
00196     ncomps = 1;
00197     while (termchar == ' ') {
00198       if (ncomps >= MAX_COMPS_IN_SCAN) {
00199         fprintf(stderr, "Too many components in one scan in file %s\n",
00200                 filename);
00201         fclose(fp);
00202         return FALSE;
00203       }
00204       if (! read_scan_integer(fp, &val, &termchar))
00205         goto bogus;
00206       scanptr->component_index[ncomps] = (int) val;
00207       ncomps++;
00208     }
00209     scanptr->comps_in_scan = ncomps;
00210     if (termchar == ':') {
00211       if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
00212         goto bogus;
00213       scanptr->Ss = (int) val;
00214       if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
00215         goto bogus;
00216       scanptr->Se = (int) val;
00217       if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
00218         goto bogus;
00219       scanptr->Ah = (int) val;
00220       if (! read_scan_integer(fp, &val, &termchar))
00221         goto bogus;
00222       scanptr->Al = (int) val;
00223     } else {
00224       /* set non-progressive parameters */
00225       scanptr->Ss = 0;
00226       scanptr->Se = DCTSIZE2-1;
00227       scanptr->Ah = 0;
00228       scanptr->Al = 0;
00229     }
00230     if (termchar != ';' && termchar != EOF) {
00231 bogus:
00232       fprintf(stderr, "Invalid scan entry format in file %s\n", filename);
00233       fclose(fp);
00234       return FALSE;
00235     }
00236     scanptr++, scanno++;
00237   }
00238 
00239   if (termchar != EOF) {
00240     fprintf(stderr, "Non-numeric data in file %s\n", filename);
00241     fclose(fp);
00242     return FALSE;
00243   }
00244 
00245   if (scanno > 0) {
00246     /* Stash completed scan list in cinfo structure.
00247      * NOTE: for cjpeg's use, JPOOL_IMAGE is the right lifetime for this data,
00248      * but if you want to compress multiple images you'd want JPOOL_PERMANENT.
00249      */
00250     scanptr = (jpeg_scan_info *)
00251       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00252                                   scanno * SIZEOF(jpeg_scan_info));
00253     MEMCOPY(scanptr, scans, scanno * SIZEOF(jpeg_scan_info));
00254     cinfo->scan_info = scanptr;
00255     cinfo->num_scans = scanno;
00256   }
00257 
00258   fclose(fp);
00259   return TRUE;
00260 }
00261 
00262 #endif /* C_MULTISCAN_FILES_SUPPORTED */
00263 
00264 
00265 GLOBAL(boolean)
00266 set_quant_slots (j_compress_ptr cinfo, char *arg)
00267 /* Process a quantization-table-selectors parameter string, of the form
00268  *     N[,N,...]
00269  * If there are more components than parameters, the last value is replicated.
00270  */
00271 {
00272   int val = 0;                  /* default table # */
00273   int ci;
00274   char ch;
00275 
00276   for (ci = 0; ci < MAX_COMPONENTS; ci++) {
00277     if (*arg) {
00278       ch = ',';                 /* if not set by sscanf, will be ',' */
00279       if (sscanf(arg, "%d%c", &val, &ch) < 1)
00280         return FALSE;
00281       if (ch != ',')            /* syntax check */
00282         return FALSE;
00283       if (val < 0 || val >= NUM_QUANT_TBLS) {
00284         fprintf(stderr, "JPEG quantization tables are numbered 0..%d\n",
00285                 NUM_QUANT_TBLS-1);
00286         return FALSE;
00287       }
00288       cinfo->comp_info[ci].quant_tbl_no = val;
00289       while (*arg && *arg++ != ',') /* advance to next segment of arg string */
00290         ;
00291     } else {
00292       /* reached end of parameter, set remaining components to last table */
00293       cinfo->comp_info[ci].quant_tbl_no = val;
00294     }
00295   }
00296   return TRUE;
00297 }
00298 
00299 
00300 GLOBAL(boolean)
00301 set_sample_factors (j_compress_ptr cinfo, char *arg)
00302 /* Process a sample-factors parameter string, of the form
00303  *     HxV[,HxV,...]
00304  * If there are more components than parameters, "1x1" is assumed for the rest.
00305  */
00306 {
00307   int ci, val1, val2;
00308   char ch1, ch2;
00309 
00310   for (ci = 0; ci < MAX_COMPONENTS; ci++) {
00311     if (*arg) {
00312       ch2 = ',';                /* if not set by sscanf, will be ',' */
00313       if (sscanf(arg, "%d%c%d%c", &val1, &ch1, &val2, &ch2) < 3)
00314         return FALSE;
00315       if ((ch1 != 'x' && ch1 != 'X') || ch2 != ',') /* syntax check */
00316         return FALSE;
00317       if (val1 <= 0 || val1 > 4 || val2 <= 0 || val2 > 4) {
00318         fprintf(stderr, "JPEG sampling factors must be 1..4\n");
00319         return FALSE;
00320       }
00321       cinfo->comp_info[ci].h_samp_factor = val1;
00322       cinfo->comp_info[ci].v_samp_factor = val2;
00323       while (*arg && *arg++ != ',') /* advance to next segment of arg string */
00324         ;
00325     } else {
00326       /* reached end of parameter, set remaining components to 1x1 sampling */
00327       cinfo->comp_info[ci].h_samp_factor = 1;
00328       cinfo->comp_info[ci].v_samp_factor = 1;
00329     }
00330   }
00331   return TRUE;
00332 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:19