4 * Copyright (C) 1991-1996, 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.
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 * -quality N[,N,...] Set quality ratings
13 * -qslots N[,N,...] Set component quantization table selectors
14 * -sample HxV[,HxV,...] Set component sampling factors
17 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
18 #include <ctype.h> /* to declare isdigit(), isspace() */
22 text_getc (FILE * file)
23 /* Read next char, skipping over any comments (# to end of line) */
24 /* A comment/newline sequence is returned as a newline */
32 } while (ch != '\n' && ch != EOF);
39 read_text_integer (FILE * file, long * result, int * termchar)
40 /* Read an unsigned decimal integer from a file, store it in result */
41 /* Reads one trailing character after the integer; returns it in termchar */
46 /* Skip any leading whitespace, detect EOF */
53 } while (isspace(ch));
61 while ((ch = text_getc(file)) != EOF) {
74 read_quant_tables (j_compress_ptr cinfo, char * filename, boolean force_baseline)
75 /* Read a set of quantization tables from the specified file.
76 * The file is plain ASCII text: decimal numbers with whitespace between.
77 * Comments preceded by '#' may be included in the file.
78 * There may be one to NUM_QUANT_TBLS tables in the file, each of 64 values.
79 * The tables are implicitly numbered 0,1,etc.
80 * NOTE: does not affect the qslots mapping, which will default to selecting
81 * table 0 for luminance (or primary) components, 1 for chrominance components.
82 * You must use -qslots if you want a different component->table mapping.
86 int tblno, i, termchar;
88 unsigned int table[DCTSIZE2];
90 if ((fp = fopen(filename, "r")) == NULL) {
91 fprintf(stderr, "Can't open table file %s\n", filename);
96 while (read_text_integer(fp, &val, &termchar)) { /* read 1st element of table */
97 if (tblno >= NUM_QUANT_TBLS) {
98 fprintf(stderr, "Too many tables in file %s\n", filename);
102 table[0] = (unsigned int) val;
103 for (i = 1; i < DCTSIZE2; i++) {
104 if (! read_text_integer(fp, &val, &termchar)) {
105 fprintf(stderr, "Invalid table data in file %s\n", filename);
109 table[i] = (unsigned int) val;
111 jpeg_add_quant_table(cinfo, tblno, table, cinfo->q_scale_factor[tblno],
116 if (termchar != EOF) {
117 fprintf(stderr, "Non-numeric data in file %s\n", filename);
127 #ifdef C_MULTISCAN_FILES_SUPPORTED
130 read_scan_integer (FILE * file, long * result, int * termchar)
131 /* Variant of read_text_integer that always looks for a non-space termchar;
132 * this simplifies parsing of punctuation in scan scripts.
137 if (! read_text_integer(file, result, termchar))
140 while (ch != EOF && isspace(ch))
141 ch = text_getc(file);
142 if (isdigit(ch)) { /* oops, put it back */
143 if (ungetc(ch, file) == EOF)
147 /* Any separators other than ';' and ':' are ignored;
148 * this allows user to insert commas, etc, if desired.
150 if (ch != EOF && ch != ';' && ch != ':')
159 read_scan_script (j_compress_ptr cinfo, char * filename)
160 /* Read a scan script from the specified text file.
161 * Each entry in the file defines one scan to be emitted.
162 * Entries are separated by semicolons ';'.
163 * An entry contains one to four component indexes,
164 * optionally followed by a colon ':' and four progressive-JPEG parameters.
165 * The component indexes denote which component(s) are to be transmitted
166 * in the current scan. The first component has index 0.
167 * Sequential JPEG is used if the progressive-JPEG parameters are omitted.
168 * The file is free format text: any whitespace may appear between numbers
169 * and the ':' and ';' punctuation marks. Also, other punctuation (such
170 * as commas or dashes) can be placed between numbers if desired.
171 * Comments preceded by '#' may be included in the file.
172 * Note: we do very little validity checking here;
173 * jcmaster.c will validate the script parameters.
177 int scanno, ncomps, termchar;
179 jpeg_scan_info * scanptr;
180 #define MAX_SCANS 100 /* quite arbitrary limit */
181 jpeg_scan_info scans[MAX_SCANS];
183 if ((fp = fopen(filename, "r")) == NULL) {
184 fprintf(stderr, "Can't open scan definition file %s\n", filename);
190 while (read_scan_integer(fp, &val, &termchar)) {
191 if (scanno >= MAX_SCANS) {
192 fprintf(stderr, "Too many scans defined in file %s\n", filename);
196 scanptr->component_index[0] = (int) val;
198 while (termchar == ' ') {
199 if (ncomps >= MAX_COMPS_IN_SCAN) {
200 fprintf(stderr, "Too many components in one scan in file %s\n",
205 if (! read_scan_integer(fp, &val, &termchar))
207 scanptr->component_index[ncomps] = (int) val;
210 scanptr->comps_in_scan = ncomps;
211 if (termchar == ':') {
212 if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
214 scanptr->Ss = (int) val;
215 if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
217 scanptr->Se = (int) val;
218 if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
220 scanptr->Ah = (int) val;
221 if (! read_scan_integer(fp, &val, &termchar))
223 scanptr->Al = (int) val;
225 /* set non-progressive parameters */
227 scanptr->Se = DCTSIZE2-1;
231 if (termchar != ';' && termchar != EOF) {
233 fprintf(stderr, "Invalid scan entry format in file %s\n", filename);
240 if (termchar != EOF) {
241 fprintf(stderr, "Non-numeric data in file %s\n", filename);
247 /* Stash completed scan list in cinfo structure.
248 * NOTE: for cjpeg's use, JPOOL_IMAGE is the right lifetime for this data,
249 * but if you want to compress multiple images you'd want JPOOL_PERMANENT.
251 scanptr = (jpeg_scan_info *)
252 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
253 scanno * SIZEOF(jpeg_scan_info));
254 MEMCOPY(scanptr, scans, scanno * SIZEOF(jpeg_scan_info));
255 cinfo->scan_info = scanptr;
256 cinfo->num_scans = scanno;
263 #endif /* C_MULTISCAN_FILES_SUPPORTED */
267 set_quality_ratings (j_compress_ptr cinfo, char *arg, boolean force_baseline)
268 /* Process a quality-ratings parameter string, of the form
270 * If there are more q-table slots than parameters, the last value is replicated.
273 int val = 75; /* default value */
277 for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
279 ch = ','; /* if not set by sscanf, will be ',' */
280 if (sscanf(arg, "%d%c", &val, &ch) < 1)
282 if (ch != ',') /* syntax check */
284 /* Convert user 0-100 rating to percentage scaling */
285 cinfo->q_scale_factor[tblno] = jpeg_quality_scaling(val);
286 while (*arg && *arg++ != ',') /* advance to next segment of arg string */
289 /* reached end of parameter, set remaining factors to last value */
290 cinfo->q_scale_factor[tblno] = jpeg_quality_scaling(val);
293 jpeg_default_qtables(cinfo, force_baseline);
299 set_quant_slots (j_compress_ptr cinfo, char *arg)
300 /* Process a quantization-table-selectors parameter string, of the form
302 * If there are more components than parameters, the last value is replicated.
305 int val = 0; /* default table # */
309 for (ci = 0; ci < MAX_COMPONENTS; ci++) {
311 ch = ','; /* if not set by sscanf, will be ',' */
312 if (sscanf(arg, "%d%c", &val, &ch) < 1)
314 if (ch != ',') /* syntax check */
316 if (val < 0 || val >= NUM_QUANT_TBLS) {
317 fprintf(stderr, "JPEG quantization tables are numbered 0..%d\n",
321 cinfo->comp_info[ci].quant_tbl_no = val;
322 while (*arg && *arg++ != ',') /* advance to next segment of arg string */
325 /* reached end of parameter, set remaining components to last table */
326 cinfo->comp_info[ci].quant_tbl_no = val;
334 set_sample_factors (j_compress_ptr cinfo, char *arg)
335 /* Process a sample-factors parameter string, of the form
337 * If there are more components than parameters, "1x1" is assumed for the rest.
343 for (ci = 0; ci < MAX_COMPONENTS; ci++) {
345 ch2 = ','; /* if not set by sscanf, will be ',' */
346 if (sscanf(arg, "%d%c%d%c", &val1, &ch1, &val2, &ch2) < 3)
348 if ((ch1 != 'x' && ch1 != 'X') || ch2 != ',') /* syntax check */
350 if (val1 <= 0 || val1 > 4 || val2 <= 0 || val2 > 4) {
351 fprintf(stderr, "JPEG sampling factors must be 1..4\n");
354 cinfo->comp_info[ci].h_samp_factor = val1;
355 cinfo->comp_info[ci].v_samp_factor = val2;
356 while (*arg && *arg++ != ',') /* advance to next segment of arg string */
359 /* reached end of parameter, set remaining components to 1x1 sampling */
360 cinfo->comp_info[ci].h_samp_factor = 1;
361 cinfo->comp_info[ci].v_samp_factor = 1;