Fixed a few warnings.
[tangerine.git] / compiler / libjpeg / test / rdswitch.c
blob714cbd03514f5e44d05c4388359841c0b99ba605
1 /*
2 $Id$
3 */
5 /*
6 * rdswitch.c
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() */
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 */
29 register int ch;
31 ch = getc(file);
32 if (ch == '#') {
33 do {
34 ch = getc(file);
35 } while (ch != '\n' && ch != EOF);
37 return ch;
41 LOCAL(boolean)
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 */
46 register int ch;
47 register long val;
49 /* Skip any leading whitespace, detect EOF */
50 do {
51 ch = text_getc(file);
52 if (ch == EOF) {
53 *termchar = ch;
54 return FALSE;
56 } while (isspace(ch));
58 if (! isdigit(ch)) {
59 *termchar = ch;
60 return FALSE;
63 val = ch - '0';
64 while ((ch = text_getc(file)) != EOF) {
65 if (! isdigit(ch))
66 break;
67 val *= 10;
68 val += ch - '0';
70 *result = val;
71 *termchar = ch;
72 return TRUE;
76 JGLOBAL(boolean)
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.
89 FILE * fp;
90 int tblno, i, termchar;
91 long val;
92 unsigned int table[DCTSIZE2];
94 if ((fp = fopen(filename, "r")) == NULL) {
95 fprintf(stderr, "Can't open table file %s\n", filename);
96 return FALSE;
98 tblno = 0;
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);
103 fclose(fp);
104 return FALSE;
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);
110 fclose(fp);
111 return FALSE;
113 table[i] = (unsigned int) val;
115 jpeg_add_quant_table(cinfo, tblno, table, scale_factor, force_baseline);
116 tblno++;
119 if (termchar != EOF) {
120 fprintf(stderr, "Non-numeric data in file %s\n", filename);
121 fclose(fp);
122 return FALSE;
125 fclose(fp);
126 return TRUE;
130 #ifdef C_MULTISCAN_FILES_SUPPORTED
132 LOCAL(boolean)
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.
138 register int ch;
140 if (! read_text_integer(file, result, termchar))
141 return FALSE;
142 ch = *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)
147 return FALSE;
148 ch = ' ';
149 } else {
150 /* Any separators other than ';' and ':' are ignored;
151 * this allows user to insert commas, etc, if desired.
153 if (ch != EOF && ch != ';' && ch != ':')
154 ch = ' ';
156 *termchar = ch;
157 return TRUE;
161 JGLOBAL(boolean)
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.
179 FILE * fp;
180 int scanno, ncomps, termchar;
181 long val;
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);
188 return FALSE;
190 scanptr = scans;
191 scanno = 0;
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);
196 fclose(fp);
197 return FALSE;
199 scanptr->component_index[0] = (int) val;
200 ncomps = 1;
201 while (termchar == ' ') {
202 if (ncomps >= MAX_COMPS_IN_SCAN) {
203 fprintf(stderr, "Too many components in one scan in file %s\n",
204 filename);
205 fclose(fp);
206 return FALSE;
208 if (! read_scan_integer(fp, &val, &termchar))
209 goto bogus;
210 scanptr->component_index[ncomps] = (int) val;
211 ncomps++;
213 scanptr->comps_in_scan = ncomps;
214 if (termchar == ':') {
215 if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
216 goto bogus;
217 scanptr->Ss = (int) val;
218 if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
219 goto bogus;
220 scanptr->Se = (int) val;
221 if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
222 goto bogus;
223 scanptr->Ah = (int) val;
224 if (! read_scan_integer(fp, &val, &termchar))
225 goto bogus;
226 scanptr->Al = (int) val;
227 } else {
228 /* set non-progressive parameters */
229 scanptr->Ss = 0;
230 scanptr->Se = DCTSIZE2-1;
231 scanptr->Ah = 0;
232 scanptr->Al = 0;
234 if (termchar != ';' && termchar != EOF) {
235 bogus:
236 fprintf(stderr, "Invalid scan entry format in file %s\n", filename);
237 fclose(fp);
238 return FALSE;
240 scanptr++, scanno++;
243 if (termchar != EOF) {
244 fprintf(stderr, "Non-numeric data in file %s\n", filename);
245 fclose(fp);
246 return FALSE;
249 if (scanno > 0) {
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;
262 fclose(fp);
263 return TRUE;
266 #endif /* C_MULTISCAN_FILES_SUPPORTED */
269 JGLOBAL(boolean)
270 set_quant_slots (j_compress_ptr cinfo, char *arg)
271 /* Process a quantization-table-selectors parameter string, of the form
272 * N[,N,...]
273 * If there are more components than parameters, the last value is replicated.
276 int val = 0; /* default table # */
277 int ci;
278 char ch;
280 for (ci = 0; ci < MAX_COMPONENTS; ci++) {
281 if (*arg) {
282 ch = ','; /* if not set by sscanf, will be ',' */
283 if (sscanf(arg, "%d%c", &val, &ch) < 1)
284 return FALSE;
285 if (ch != ',') /* syntax check */
286 return FALSE;
287 if (val < 0 || val >= NUM_QUANT_TBLS) {
288 fprintf(stderr, "JPEG quantization tables are numbered 0..%d\n",
289 NUM_QUANT_TBLS-1);
290 return FALSE;
292 cinfo->comp_info[ci].quant_tbl_no = val;
293 while (*arg && *arg++ != ',') /* advance to next segment of arg string */
295 } else {
296 /* reached end of parameter, set remaining components to last table */
297 cinfo->comp_info[ci].quant_tbl_no = val;
300 return TRUE;
304 JGLOBAL(boolean)
305 set_sample_factors (j_compress_ptr cinfo, char *arg)
306 /* Process a sample-factors parameter string, of the form
307 * HxV[,HxV,...]
308 * If there are more components than parameters, "1x1" is assumed for the rest.
311 int ci, val1, val2;
312 char ch1, ch2;
314 for (ci = 0; ci < MAX_COMPONENTS; ci++) {
315 if (*arg) {
316 ch2 = ','; /* if not set by sscanf, will be ',' */
317 if (sscanf(arg, "%d%c%d%c", &val1, &ch1, &val2, &ch2) < 3)
318 return FALSE;
319 if ((ch1 != 'x' && ch1 != 'X') || ch2 != ',') /* syntax check */
320 return FALSE;
321 if (val1 <= 0 || val1 > 4 || val2 <= 0 || val2 > 4) {
322 fprintf(stderr, "JPEG sampling factors must be 1..4\n");
323 return FALSE;
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 */
329 } else {
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;
335 return TRUE;
339 #ifdef C_LOSSLESS_SUPPORTED
341 JGLOBAL(boolean)
342 set_simple_lossless (j_compress_ptr cinfo, char *arg)
344 int pred, pt = 0;
345 char ch;
347 ch = ','; /* if not set by sscanf, will be ',' */
348 if (sscanf(arg, "%d%c", &pred, &ch) < 1)
349 return FALSE;
350 if (ch != ',') /* syntax check */
351 return FALSE;
352 while (*arg && *arg++ != ',') /* advance to next segment of arg string */
354 if (*arg) {
355 if (sscanf(arg, "%d", &pt) != 1)
356 pt = 0;
358 jpeg_simple_lossless(cinfo, pred, pt);
359 return TRUE;
362 #endif /* C_LOSSLESS_SUPPORTED */