]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - QRScanner/mobile/jni/thirdparty/jpeg/rdswitch.c
Add MuPDF native source codes
[hornmich/skoda-qr-demo.git] / QRScanner / mobile / jni / thirdparty / jpeg / rdswitch.c
1 /*
2  * rdswitch.c
3  *
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.
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  *      -quality N[,N,...]      Set quality ratings
13  *      -qslots N[,N,...]       Set component quantization table selectors
14  *      -sample HxV[,HxV,...]   Set component sampling factors
15  */
16
17 #include "cdjpeg.h"             /* Common decls for cjpeg/djpeg applications */
18 #include <ctype.h>              /* to declare isdigit(), isspace() */
19
20
21 LOCAL(int)
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 */
25 {
26   register int ch;
27   
28   ch = getc(file);
29   if (ch == '#') {
30     do {
31       ch = getc(file);
32     } while (ch != '\n' && ch != EOF);
33   }
34   return ch;
35 }
36
37
38 LOCAL(boolean)
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 */
42 {
43   register int ch;
44   register long val;
45   
46   /* Skip any leading whitespace, detect EOF */
47   do {
48     ch = text_getc(file);
49     if (ch == EOF) {
50       *termchar = ch;
51       return FALSE;
52     }
53   } while (isspace(ch));
54   
55   if (! isdigit(ch)) {
56     *termchar = ch;
57     return FALSE;
58   }
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
72
73 GLOBAL(boolean)
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.
83  */
84 {
85   FILE * fp;
86   int tblno, i, termchar;
87   long val;
88   unsigned int table[DCTSIZE2];
89
90   if ((fp = fopen(filename, "r")) == NULL) {
91     fprintf(stderr, "Can't open table file %s\n", filename);
92     return FALSE;
93   }
94   tblno = 0;
95
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);
99       fclose(fp);
100       return FALSE;
101     }
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);
106         fclose(fp);
107         return FALSE;
108       }
109       table[i] = (unsigned int) val;
110     }
111     jpeg_add_quant_table(cinfo, tblno, table, cinfo->q_scale_factor[tblno],
112                          force_baseline);
113     tblno++;
114   }
115
116   if (termchar != EOF) {
117     fprintf(stderr, "Non-numeric data in file %s\n", filename);
118     fclose(fp);
119     return FALSE;
120   }
121
122   fclose(fp);
123   return TRUE;
124 }
125
126
127 #ifdef C_MULTISCAN_FILES_SUPPORTED
128
129 LOCAL(boolean)
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.
133  */
134 {
135   register int ch;
136
137   if (! read_text_integer(file, result, termchar))
138     return FALSE;
139   ch = *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)
144       return FALSE;
145     ch = ' ';
146   } else {
147     /* Any separators other than ';' and ':' are ignored;
148      * this allows user to insert commas, etc, if desired.
149      */
150     if (ch != EOF && ch != ';' && ch != ':')
151       ch = ' ';
152   }
153   *termchar = ch;
154   return TRUE;
155 }
156
157
158 GLOBAL(boolean)
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.
174  */
175 {
176   FILE * fp;
177   int scanno, ncomps, termchar;
178   long val;
179   jpeg_scan_info * scanptr;
180 #define MAX_SCANS  100          /* quite arbitrary limit */
181   jpeg_scan_info scans[MAX_SCANS];
182
183   if ((fp = fopen(filename, "r")) == NULL) {
184     fprintf(stderr, "Can't open scan definition file %s\n", filename);
185     return FALSE;
186   }
187   scanptr = scans;
188   scanno = 0;
189
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);
193       fclose(fp);
194       return FALSE;
195     }
196     scanptr->component_index[0] = (int) val;
197     ncomps = 1;
198     while (termchar == ' ') {
199       if (ncomps >= MAX_COMPS_IN_SCAN) {
200         fprintf(stderr, "Too many components in one scan in file %s\n",
201                 filename);
202         fclose(fp);
203         return FALSE;
204       }
205       if (! read_scan_integer(fp, &val, &termchar))
206         goto bogus;
207       scanptr->component_index[ncomps] = (int) val;
208       ncomps++;
209     }
210     scanptr->comps_in_scan = ncomps;
211     if (termchar == ':') {
212       if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
213         goto bogus;
214       scanptr->Ss = (int) val;
215       if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
216         goto bogus;
217       scanptr->Se = (int) val;
218       if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
219         goto bogus;
220       scanptr->Ah = (int) val;
221       if (! read_scan_integer(fp, &val, &termchar))
222         goto bogus;
223       scanptr->Al = (int) val;
224     } else {
225       /* set non-progressive parameters */
226       scanptr->Ss = 0;
227       scanptr->Se = DCTSIZE2-1;
228       scanptr->Ah = 0;
229       scanptr->Al = 0;
230     }
231     if (termchar != ';' && termchar != EOF) {
232 bogus:
233       fprintf(stderr, "Invalid scan entry format in file %s\n", filename);
234       fclose(fp);
235       return FALSE;
236     }
237     scanptr++, scanno++;
238   }
239
240   if (termchar != EOF) {
241     fprintf(stderr, "Non-numeric data in file %s\n", filename);
242     fclose(fp);
243     return FALSE;
244   }
245
246   if (scanno > 0) {
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.
250      */
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;
257   }
258
259   fclose(fp);
260   return TRUE;
261 }
262
263 #endif /* C_MULTISCAN_FILES_SUPPORTED */
264
265
266 GLOBAL(boolean)
267 set_quality_ratings (j_compress_ptr cinfo, char *arg, boolean force_baseline)
268 /* Process a quality-ratings parameter string, of the form
269  *     N[,N,...]
270  * If there are more q-table slots than parameters, the last value is replicated.
271  */
272 {
273   int val = 75;                 /* default value */
274   int tblno;
275   char ch;
276
277   for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
278     if (*arg) {
279       ch = ',';                 /* if not set by sscanf, will be ',' */
280       if (sscanf(arg, "%d%c", &val, &ch) < 1)
281         return FALSE;
282       if (ch != ',')            /* syntax check */
283         return FALSE;
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 */
287         ;
288     } else {
289       /* reached end of parameter, set remaining factors to last value */
290       cinfo->q_scale_factor[tblno] = jpeg_quality_scaling(val);
291     }
292   }
293   jpeg_default_qtables(cinfo, force_baseline);
294   return TRUE;
295 }
296
297
298 GLOBAL(boolean)
299 set_quant_slots (j_compress_ptr cinfo, char *arg)
300 /* Process a quantization-table-selectors parameter string, of the form
301  *     N[,N,...]
302  * If there are more components than parameters, the last value is replicated.
303  */
304 {
305   int val = 0;                  /* default table # */
306   int ci;
307   char ch;
308
309   for (ci = 0; ci < MAX_COMPONENTS; ci++) {
310     if (*arg) {
311       ch = ',';                 /* if not set by sscanf, will be ',' */
312       if (sscanf(arg, "%d%c", &val, &ch) < 1)
313         return FALSE;
314       if (ch != ',')            /* syntax check */
315         return FALSE;
316       if (val < 0 || val >= NUM_QUANT_TBLS) {
317         fprintf(stderr, "JPEG quantization tables are numbered 0..%d\n",
318                 NUM_QUANT_TBLS-1);
319         return FALSE;
320       }
321       cinfo->comp_info[ci].quant_tbl_no = val;
322       while (*arg && *arg++ != ',') /* advance to next segment of arg string */
323         ;
324     } else {
325       /* reached end of parameter, set remaining components to last table */
326       cinfo->comp_info[ci].quant_tbl_no = val;
327     }
328   }
329   return TRUE;
330 }
331
332
333 GLOBAL(boolean)
334 set_sample_factors (j_compress_ptr cinfo, char *arg)
335 /* Process a sample-factors parameter string, of the form
336  *     HxV[,HxV,...]
337  * If there are more components than parameters, "1x1" is assumed for the rest.
338  */
339 {
340   int ci, val1, val2;
341   char ch1, ch2;
342
343   for (ci = 0; ci < MAX_COMPONENTS; ci++) {
344     if (*arg) {
345       ch2 = ',';                /* if not set by sscanf, will be ',' */
346       if (sscanf(arg, "%d%c%d%c", &val1, &ch1, &val2, &ch2) < 3)
347         return FALSE;
348       if ((ch1 != 'x' && ch1 != 'X') || ch2 != ',') /* syntax check */
349         return FALSE;
350       if (val1 <= 0 || val1 > 4 || val2 <= 0 || val2 > 4) {
351         fprintf(stderr, "JPEG sampling factors must be 1..4\n");
352         return FALSE;
353       }
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 */
357         ;
358     } else {
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;
362     }
363   }
364   return TRUE;
365 }