8 * Copyright (C) 1991-1996, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains routines to process some of cjpeg's more complicated
13 * command-line switches. Switches processed here are:
14 * -qtables file Read quantization tables from text file
15 * -scans file Read scan script from text file
16 * -qslots N[,N,...] Set component quantization table selectors
17 * -sample HxV[,HxV,...] Set component sampling factors
20 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
21 #include <ctype.h> /* to declare isdigit(), isspace() */
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 */
35 } while (ch
!= '\n' && ch
!= EOF
);
42 read_text_integer (FILE * file
, long * result
, int * termchar
)
43 /* Read an unsigned decimal integer from a file, store it in result */
44 /* Reads one trailing character after the integer; returns it in termchar */
49 /* Skip any leading whitespace, detect EOF */
56 } while (isspace(ch
));
64 while ((ch
= text_getc(file
)) != EOF
) {
77 read_quant_tables (j_compress_ptr cinfo
, char * filename
,
78 int scale_factor
, boolean force_baseline
)
79 /* Read a set of quantization tables from the specified file.
80 * The file is plain ASCII text: decimal numbers with whitespace between.
81 * Comments preceded by '#' may be included in the file.
82 * There may be one to NUM_QUANT_TBLS tables in the file, each of 64 values.
83 * The tables are implicitly numbered 0,1,etc.
84 * NOTE: does not affect the qslots mapping, which will default to selecting
85 * table 0 for luminance (or primary) components, 1 for chrominance components.
86 * You must use -qslots if you want a different component->table mapping.
90 int tblno
, i
, termchar
;
92 unsigned int table
[DCTSIZE2
];
94 if ((fp
= fopen(filename
, "r")) == NULL
) {
95 fprintf(stderr
, "Can't open table file %s\n", filename
);
100 while (read_text_integer(fp
, &val
, &termchar
)) { /* read 1st element of table */
101 if (tblno
>= NUM_QUANT_TBLS
) {
102 fprintf(stderr
, "Too many tables in file %s\n", filename
);
106 table
[0] = (unsigned int) val
;
107 for (i
= 1; i
< DCTSIZE2
; i
++) {
108 if (! read_text_integer(fp
, &val
, &termchar
)) {
109 fprintf(stderr
, "Invalid table data in file %s\n", filename
);
113 table
[i
] = (unsigned int) val
;
115 jpeg_add_quant_table(cinfo
, tblno
, table
, scale_factor
, force_baseline
);
119 if (termchar
!= EOF
) {
120 fprintf(stderr
, "Non-numeric data in file %s\n", filename
);
130 #ifdef C_MULTISCAN_FILES_SUPPORTED
133 read_scan_integer (FILE * file
, long * result
, int * termchar
)
134 /* Variant of read_text_integer that always looks for a non-space termchar;
135 * this simplifies parsing of punctuation in scan scripts.
140 if (! read_text_integer(file
, result
, termchar
))
143 while (ch
!= EOF
&& isspace(ch
))
144 ch
= text_getc(file
);
145 if (isdigit(ch
)) { /* oops, put it back */
146 if (ungetc(ch
, file
) == EOF
)
150 /* Any separators other than ';' and ':' are ignored;
151 * this allows user to insert commas, etc, if desired.
153 if (ch
!= EOF
&& ch
!= ';' && ch
!= ':')
162 read_scan_script (j_compress_ptr cinfo
, char * filename
)
163 /* Read a scan script from the specified text file.
164 * Each entry in the file defines one scan to be emitted.
165 * Entries are separated by semicolons ';'.
166 * An entry contains one to four component indexes,
167 * optionally followed by a colon ':' and four progressive-JPEG parameters.
168 * The component indexes denote which component(s) are to be transmitted
169 * in the current scan. The first component has index 0.
170 * Sequential JPEG is used if the progressive-JPEG parameters are omitted.
171 * The file is free format text: any whitespace may appear between numbers
172 * and the ':' and ';' punctuation marks. Also, other punctuation (such
173 * as commas or dashes) can be placed between numbers if desired.
174 * Comments preceded by '#' may be included in the file.
175 * Note: we do very little validity checking here;
176 * jcmaster.c will validate the script parameters.
180 int scanno
, ncomps
, termchar
;
182 jpeg_scan_info
* scanptr
;
183 #define MAX_SCANS 100 /* quite arbitrary limit */
184 jpeg_scan_info scans
[MAX_SCANS
];
186 if ((fp
= fopen(filename
, "r")) == NULL
) {
187 fprintf(stderr
, "Can't open scan definition file %s\n", filename
);
193 while (read_scan_integer(fp
, &val
, &termchar
)) {
194 if (scanno
>= MAX_SCANS
) {
195 fprintf(stderr
, "Too many scans defined in file %s\n", filename
);
199 scanptr
->component_index
[0] = (int) val
;
201 while (termchar
== ' ') {
202 if (ncomps
>= MAX_COMPS_IN_SCAN
) {
203 fprintf(stderr
, "Too many components in one scan in file %s\n",
208 if (! read_scan_integer(fp
, &val
, &termchar
))
210 scanptr
->component_index
[ncomps
] = (int) val
;
213 scanptr
->comps_in_scan
= ncomps
;
214 if (termchar
== ':') {
215 if (! read_scan_integer(fp
, &val
, &termchar
) || termchar
!= ' ')
217 scanptr
->Ss
= (int) val
;
218 if (! read_scan_integer(fp
, &val
, &termchar
) || termchar
!= ' ')
220 scanptr
->Se
= (int) val
;
221 if (! read_scan_integer(fp
, &val
, &termchar
) || termchar
!= ' ')
223 scanptr
->Ah
= (int) val
;
224 if (! read_scan_integer(fp
, &val
, &termchar
))
226 scanptr
->Al
= (int) val
;
228 /* set non-progressive parameters */
230 scanptr
->Se
= DCTSIZE2
-1;
234 if (termchar
!= ';' && termchar
!= EOF
) {
236 fprintf(stderr
, "Invalid scan entry format in file %s\n", filename
);
243 if (termchar
!= EOF
) {
244 fprintf(stderr
, "Non-numeric data in file %s\n", filename
);
250 /* Stash completed scan list in cinfo structure.
251 * NOTE: for cjpeg's use, JPOOL_IMAGE is the right lifetime for this data,
252 * but if you want to compress multiple images you'd want JPOOL_PERMANENT.
254 scanptr
= (jpeg_scan_info
*)
255 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
256 scanno
* SIZEOF(jpeg_scan_info
));
257 MEMCOPY(scanptr
, scans
, scanno
* SIZEOF(jpeg_scan_info
));
258 cinfo
->scan_info
= scanptr
;
259 cinfo
->num_scans
= scanno
;
266 #endif /* C_MULTISCAN_FILES_SUPPORTED */
270 set_quant_slots (j_compress_ptr cinfo
, char *arg
)
271 /* Process a quantization-table-selectors parameter string, of the form
273 * If there are more components than parameters, the last value is replicated.
276 int val
= 0; /* default table # */
280 for (ci
= 0; ci
< MAX_COMPONENTS
; ci
++) {
282 ch
= ','; /* if not set by sscanf, will be ',' */
283 if (sscanf(arg
, "%d%c", &val
, &ch
) < 1)
285 if (ch
!= ',') /* syntax check */
287 if (val
< 0 || val
>= NUM_QUANT_TBLS
) {
288 fprintf(stderr
, "JPEG quantization tables are numbered 0..%d\n",
292 cinfo
->comp_info
[ci
].quant_tbl_no
= val
;
293 while (*arg
&& *arg
++ != ',') /* advance to next segment of arg string */
296 /* reached end of parameter, set remaining components to last table */
297 cinfo
->comp_info
[ci
].quant_tbl_no
= val
;
305 set_sample_factors (j_compress_ptr cinfo
, char *arg
)
306 /* Process a sample-factors parameter string, of the form
308 * If there are more components than parameters, "1x1" is assumed for the rest.
314 for (ci
= 0; ci
< MAX_COMPONENTS
; ci
++) {
316 ch2
= ','; /* if not set by sscanf, will be ',' */
317 if (sscanf(arg
, "%d%c%d%c", &val1
, &ch1
, &val2
, &ch2
) < 3)
319 if ((ch1
!= 'x' && ch1
!= 'X') || ch2
!= ',') /* syntax check */
321 if (val1
<= 0 || val1
> 4 || val2
<= 0 || val2
> 4) {
322 fprintf(stderr
, "JPEG sampling factors must be 1..4\n");
325 cinfo
->comp_info
[ci
].h_samp_factor
= val1
;
326 cinfo
->comp_info
[ci
].v_samp_factor
= val2
;
327 while (*arg
&& *arg
++ != ',') /* advance to next segment of arg string */
330 /* reached end of parameter, set remaining components to 1x1 sampling */
331 cinfo
->comp_info
[ci
].h_samp_factor
= 1;
332 cinfo
->comp_info
[ci
].v_samp_factor
= 1;
339 #ifdef C_LOSSLESS_SUPPORTED
342 set_simple_lossless (j_compress_ptr cinfo
, char *arg
)
347 ch
= ','; /* if not set by sscanf, will be ',' */
348 if (sscanf(arg
, "%d%c", &pred
, &ch
) < 1)
350 if (ch
!= ',') /* syntax check */
352 while (*arg
&& *arg
++ != ',') /* advance to next segment of arg string */
355 if (sscanf(arg
, "%d", &pt
) != 1)
358 jpeg_simple_lossless(cinfo
, pred
, pt
);
362 #endif /* C_LOSSLESS_SUPPORTED */