rdswitch.c
Upload User: zlh9724
Upload Date: 2007-01-04
Package Size: 1991k
Code Size: 10k
Category:

Browser Client

Development Platform:

Unix_Linux

  1. /*
  2.  * rdswitch.c
  3.  *
  4.  * Copyright (C) 1991-1995, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains routines to process some of cjpeg's more complicated
  9.  * command-line switches.  Switches processed here are:
  10.  * -qtables file Read quantization tables from text file
  11.  * -scans file Read scan script from text file
  12.  * -qslots N[,N,...] Set component quantization table selectors
  13.  * -sample HxV[,HxV,...] Set component sampling factors
  14.  */
  15. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  16. #include <ctype.h> /* to declare isdigit(), isspace() */
  17. /* Hack: get access to jpeg_zigzag_order[] table in jutils.c.
  18.  * Since it's declared in jpegint.h, normally can't see it from an application.
  19.  */
  20. #ifdef NEED_SHORT_EXTERNAL_NAMES
  21. #define jpeg_zigzag_order jZIGTable
  22. #endif
  23. extern const int jpeg_zigzag_order[];
  24. LOCAL int
  25. text_getc (FILE * file)
  26. /* Read next char, skipping over any comments (# to end of line) */
  27. /* A comment/newline sequence is returned as a newline */
  28. {
  29.   register int ch;
  30.   
  31.   ch = getc(file);
  32.   if (ch == '#') {
  33.     do {
  34.       ch = getc(file);
  35.     } while (ch != 'n' && ch != EOF);
  36.   }
  37.   return ch;
  38. }
  39. LOCAL boolean
  40. read_text_integer (FILE * file, long * result, int * termchar)
  41. /* Read an unsigned decimal integer from a file, store it in result */
  42. /* Reads one trailing character after the integer; returns it in termchar */
  43. {
  44.   register int ch;
  45.   register long val;
  46.   
  47.   /* Skip any leading whitespace, detect EOF */
  48.   do {
  49.     ch = text_getc(file);
  50.     if (ch == EOF) {
  51.       *termchar = ch;
  52.       return FALSE;
  53.     }
  54.   } while (isspace(ch));
  55.   
  56.   if (! isdigit(ch)) {
  57.     *termchar = ch;
  58.     return FALSE;
  59.   }
  60.   val = ch - '0';
  61.   while ((ch = text_getc(file)) != EOF) {
  62.     if (! isdigit(ch))
  63.       break;
  64.     val *= 10;
  65.     val += ch - '0';
  66.   }
  67.   *result = val;
  68.   *termchar = ch;
  69.   return TRUE;
  70. }
  71. GLOBAL boolean
  72. read_quant_tables (j_compress_ptr cinfo, char * filename,
  73.    int scale_factor, boolean force_baseline)
  74. /* Read a set of quantization tables from the specified file.
  75.  * The file is plain ASCII text: decimal numbers with whitespace between.
  76.  * Comments preceded by '#' may be included in the file.
  77.  * There may be one to NUM_QUANT_TBLS tables in the file, each of 64 values.
  78.  * The tables are implicitly numbered 0,1,etc.
  79.  * NOTE: does not affect the qslots mapping, which will default to selecting
  80.  * table 0 for luminance (or primary) components, 1 for chrominance components.
  81.  * You must use -qslots if you want a different component->table mapping.
  82.  */
  83. {
  84.   FILE * fp;
  85.   int tblno, i, termchar;
  86.   long val;
  87.   unsigned int table[DCTSIZE2];
  88.   if ((fp = fopen(filename, "r")) == NULL) {
  89.     fprintf(stderr, "Can't open table file %sn", filename);
  90.     return FALSE;
  91.   }
  92.   tblno = 0;
  93.   while (read_text_integer(fp, &val, &termchar)) { /* read 1st element of table */
  94.     if (tblno >= NUM_QUANT_TBLS) {
  95.       fprintf(stderr, "Too many tables in file %sn", filename);
  96.       fclose(fp);
  97.       return FALSE;
  98.     }
  99.     table[0] = (unsigned int) val;
  100.     for (i = 1; i < DCTSIZE2; i++) {
  101.       if (! read_text_integer(fp, &val, &termchar)) {
  102. fprintf(stderr, "Invalid table data in file %sn", filename);
  103. fclose(fp);
  104. return FALSE;
  105.       }
  106.       /* Convert from natural order in the file to zigzag table order. */
  107.       table[jpeg_zigzag_order[i]] = (unsigned int) val;
  108.     }
  109.     jpeg_add_quant_table(cinfo, tblno, table, scale_factor, force_baseline);
  110.     tblno++;
  111.   }
  112.   if (termchar != EOF) {
  113.     fprintf(stderr, "Non-numeric data in file %sn", filename);
  114.     fclose(fp);
  115.     return FALSE;
  116.   }
  117.   fclose(fp);
  118.   return TRUE;
  119. }
  120. #ifdef C_MULTISCAN_FILES_SUPPORTED
  121. LOCAL boolean
  122. read_scan_integer (FILE * file, long * result, int * termchar)
  123. /* Variant of read_text_integer that always looks for a non-space termchar;
  124.  * this simplifies parsing of punctuation in scan scripts.
  125.  */
  126. {
  127.   register int ch;
  128.   if (! read_text_integer(file, result, termchar))
  129.     return FALSE;
  130.   ch = *termchar;
  131.   while (ch != EOF && isspace(ch))
  132.     ch = text_getc(file);
  133.   if (isdigit(ch)) { /* oops, put it back */
  134.     if (ungetc(ch, file) == EOF)
  135.       return FALSE;
  136.     ch = ' ';
  137.   } else {
  138.     /* Any separators other than ';' and ':' are ignored;
  139.      * this allows user to insert commas, etc, if desired.
  140.      */
  141.     if (ch != EOF && ch != ';' && ch != ':')
  142.       ch = ' ';
  143.   }
  144.   *termchar = ch;
  145.   return TRUE;
  146. }
  147. GLOBAL boolean
  148. read_scan_script (j_compress_ptr cinfo, char * filename)
  149. /* Read a scan script from the specified text file.
  150.  * Each entry in the file defines one scan to be emitted.
  151.  * Entries are separated by semicolons ';'.
  152.  * An entry contains one to four component indexes,
  153.  * optionally followed by a colon ':' and four progressive-JPEG parameters.
  154.  * The component indexes denote which component(s) are to be transmitted
  155.  * in the current scan.  The first component has index 0.
  156.  * Sequential JPEG is used if the progressive-JPEG parameters are omitted.
  157.  * The file is free format text: any whitespace may appear between numbers
  158.  * and the ':' and ';' punctuation marks.  Also, other punctuation (such
  159.  * as commas or dashes) can be placed between numbers if desired.
  160.  * Comments preceded by '#' may be included in the file.
  161.  * Note: we do very little validity checking here;
  162.  * jcmaster.c will validate the script parameters.
  163.  */
  164. {
  165.   FILE * fp;
  166.   int scanno, ncomps, termchar;
  167.   long val;
  168.   jpeg_scan_info * scanptr;
  169. #define MAX_SCANS  100 /* quite arbitrary limit */
  170.   jpeg_scan_info scans[MAX_SCANS];
  171.   if ((fp = fopen(filename, "r")) == NULL) {
  172.     fprintf(stderr, "Can't open scan definition file %sn", filename);
  173.     return FALSE;
  174.   }
  175.   scanptr = scans;
  176.   scanno = 0;
  177.   while (read_scan_integer(fp, &val, &termchar)) {
  178.     if (scanno >= MAX_SCANS) {
  179.       fprintf(stderr, "Too many scans defined in file %sn", filename);
  180.       fclose(fp);
  181.       return FALSE;
  182.     }
  183.     scanptr->component_index[0] = (int) val;
  184.     ncomps = 1;
  185.     while (termchar == ' ') {
  186.       if (ncomps >= MAX_COMPS_IN_SCAN) {
  187. fprintf(stderr, "Too many components in one scan in file %sn",
  188. filename);
  189. fclose(fp);
  190. return FALSE;
  191.       }
  192.       if (! read_scan_integer(fp, &val, &termchar))
  193. goto bogus;
  194.       scanptr->component_index[ncomps] = (int) val;
  195.       ncomps++;
  196.     }
  197.     scanptr->comps_in_scan = ncomps;
  198.     if (termchar == ':') {
  199.       if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
  200. goto bogus;
  201.       scanptr->Ss = (int) val;
  202.       if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
  203. goto bogus;
  204.       scanptr->Se = (int) val;
  205.       if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
  206. goto bogus;
  207.       scanptr->Ah = (int) val;
  208.       if (! read_scan_integer(fp, &val, &termchar))
  209. goto bogus;
  210.       scanptr->Al = (int) val;
  211.     } else {
  212.       /* set non-progressive parameters */
  213.       scanptr->Ss = 0;
  214.       scanptr->Se = DCTSIZE2-1;
  215.       scanptr->Ah = 0;
  216.       scanptr->Al = 0;
  217.     }
  218.     if (termchar != ';' && termchar != EOF) {
  219. bogus:
  220.       fprintf(stderr, "Invalid scan entry format in file %sn", filename);
  221.       fclose(fp);
  222.       return FALSE;
  223.     }
  224.     scanptr++, scanno++;
  225.   }
  226.   if (termchar != EOF) {
  227.     fprintf(stderr, "Non-numeric data in file %sn", filename);
  228.     fclose(fp);
  229.     return FALSE;
  230.   }
  231.   if (scanno > 0) {
  232.     /* Stash completed scan list in cinfo structure.
  233.      * NOTE: for cjpeg's use, JPOOL_IMAGE is the right lifetime for this data,
  234.      * but if you want to compress multiple images you'd want JPOOL_PERMANENT.
  235.      */
  236.     scanptr = (jpeg_scan_info *)
  237.       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  238.   scanno * SIZEOF(jpeg_scan_info));
  239.     MEMCOPY(scanptr, scans, scanno * SIZEOF(jpeg_scan_info));
  240.     cinfo->scan_info = scanptr;
  241.     cinfo->num_scans = scanno;
  242.   }
  243.   fclose(fp);
  244.   return TRUE;
  245. }
  246. #endif /* C_MULTISCAN_FILES_SUPPORTED */
  247. GLOBAL boolean
  248. set_quant_slots (j_compress_ptr cinfo, char *arg)
  249. /* Process a quantization-table-selectors parameter string, of the form
  250.  *     N[,N,...]
  251.  * If there are more components than parameters, the last value is replicated.
  252.  */
  253. {
  254.   int val = 0; /* default table # */
  255.   int ci;
  256.   char ch;
  257.   for (ci = 0; ci < MAX_COMPONENTS; ci++) {
  258.     if (*arg) {
  259.       ch = ','; /* if not set by sscanf, will be ',' */
  260.       if (sscanf(arg, "%d%c", &val, &ch) < 1)
  261. return FALSE;
  262.       if (ch != ',') /* syntax check */
  263. return FALSE;
  264.       if (val < 0 || val >= NUM_QUANT_TBLS) {
  265. fprintf(stderr, "JPEG quantization tables are numbered 0..%dn",
  266. NUM_QUANT_TBLS-1);
  267. return FALSE;
  268.       }
  269.       cinfo->comp_info[ci].quant_tbl_no = val;
  270.       while (*arg && *arg++ != ',') /* advance to next segment of arg string */
  271. ;
  272.     } else {
  273.       /* reached end of parameter, set remaining components to last table */
  274.       cinfo->comp_info[ci].quant_tbl_no = val;
  275.     }
  276.   }
  277.   return TRUE;
  278. }
  279. GLOBAL boolean
  280. set_sample_factors (j_compress_ptr cinfo, char *arg)
  281. /* Process a sample-factors parameter string, of the form
  282.  *     HxV[,HxV,...]
  283.  * If there are more components than parameters, "1x1" is assumed for the rest.
  284.  */
  285. {
  286.   int ci, val1, val2;
  287.   char ch1, ch2;
  288.   for (ci = 0; ci < MAX_COMPONENTS; ci++) {
  289.     if (*arg) {
  290.       ch2 = ','; /* if not set by sscanf, will be ',' */
  291.       if (sscanf(arg, "%d%c%d%c", &val1, &ch1, &val2, &ch2) < 3)
  292. return FALSE;
  293.       if ((ch1 != 'x' && ch1 != 'X') || ch2 != ',') /* syntax check */
  294. return FALSE;
  295.       if (val1 <= 0 || val1 > 4 || val2 <= 0 || val2 > 4) {
  296. fprintf(stderr, "JPEG sampling factors must be 1..4n");
  297. return FALSE;
  298.       }
  299.       cinfo->comp_info[ci].h_samp_factor = val1;
  300.       cinfo->comp_info[ci].v_samp_factor = val2;
  301.       while (*arg && *arg++ != ',') /* advance to next segment of arg string */
  302. ;
  303.     } else {
  304.       /* reached end of parameter, set remaining components to 1x1 sampling */
  305.       cinfo->comp_info[ci].h_samp_factor = 1;
  306.       cinfo->comp_info[ci].v_samp_factor = 1;
  307.     }
  308.   }
  309.   return TRUE;
  310. }