00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "cdjpeg.h"
00017 #include <ctype.h>
00018
00019
00020 LOCAL(int)
00021 text_getc (FILE * file)
00022
00023
00024 {
00025 register int ch;
00026
00027 ch = getc(file);
00028 if (ch == '#') {
00029 do {
00030 ch = getc(file);
00031 } while (ch != '\n' && ch != EOF);
00032 }
00033 return ch;
00034 }
00035
00036
00037 LOCAL(boolean)
00038 read_text_integer (FILE * file, long * result, int * termchar)
00039
00040
00041 {
00042 register int ch;
00043 register long val;
00044
00045
00046 do {
00047 ch = text_getc(file);
00048 if (ch == EOF) {
00049 *termchar = ch;
00050 return FALSE;
00051 }
00052 } while (isspace(ch));
00053
00054 if (! isdigit(ch)) {
00055 *termchar = ch;
00056 return FALSE;
00057 }
00058
00059 val = ch - '0';
00060 while ((ch = text_getc(file)) != EOF) {
00061 if (! isdigit(ch))
00062 break;
00063 val *= 10;
00064 val += ch - '0';
00065 }
00066 *result = val;
00067 *termchar = ch;
00068 return TRUE;
00069 }
00070
00071
00072 GLOBAL(boolean)
00073 read_quant_tables (j_compress_ptr cinfo, char * filename,
00074 int scale_factor, boolean force_baseline)
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 {
00085 FILE * fp;
00086 int tblno, i, termchar;
00087 long val;
00088 unsigned int table[DCTSIZE2];
00089
00090 if ((fp = fopen(filename, "r")) == NULL) {
00091 fprintf(stderr, "Can't open table file %s\n", filename);
00092 return FALSE;
00093 }
00094 tblno = 0;
00095
00096 while (read_text_integer(fp, &val, &termchar)) {
00097 if (tblno >= NUM_QUANT_TBLS) {
00098 fprintf(stderr, "Too many tables in file %s\n", filename);
00099 fclose(fp);
00100 return FALSE;
00101 }
00102 table[0] = (unsigned int) val;
00103 for (i = 1; i < DCTSIZE2; i++) {
00104 if (! read_text_integer(fp, &val, &termchar)) {
00105 fprintf(stderr, "Invalid table data in file %s\n", filename);
00106 fclose(fp);
00107 return FALSE;
00108 }
00109 table[i] = (unsigned int) val;
00110 }
00111 jpeg_add_quant_table(cinfo, tblno, table, scale_factor, force_baseline);
00112 tblno++;
00113 }
00114
00115 if (termchar != EOF) {
00116 fprintf(stderr, "Non-numeric data in file %s\n", filename);
00117 fclose(fp);
00118 return FALSE;
00119 }
00120
00121 fclose(fp);
00122 return TRUE;
00123 }
00124
00125
00126 #ifdef C_MULTISCAN_FILES_SUPPORTED
00127
00128 LOCAL(boolean)
00129 read_scan_integer (FILE * file, long * result, int * termchar)
00130
00131
00132
00133 {
00134 register int ch;
00135
00136 if (! read_text_integer(file, result, termchar))
00137 return FALSE;
00138 ch = *termchar;
00139 while (ch != EOF && isspace(ch))
00140 ch = text_getc(file);
00141 if (isdigit(ch)) {
00142 if (ungetc(ch, file) == EOF)
00143 return FALSE;
00144 ch = ' ';
00145 } else {
00146
00147
00148
00149 if (ch != EOF && ch != ';' && ch != ':')
00150 ch = ' ';
00151 }
00152 *termchar = ch;
00153 return TRUE;
00154 }
00155
00156
00157 GLOBAL(boolean)
00158 read_scan_script (j_compress_ptr cinfo, char * filename)
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 {
00175 FILE * fp;
00176 int scanno, ncomps, termchar;
00177 long val;
00178 jpeg_scan_info * scanptr;
00179 #define MAX_SCANS 100
00180 jpeg_scan_info scans[MAX_SCANS];
00181
00182 if ((fp = fopen(filename, "r")) == NULL) {
00183 fprintf(stderr, "Can't open scan definition file %s\n", filename);
00184 return FALSE;
00185 }
00186 scanptr = scans;
00187 scanno = 0;
00188
00189 while (read_scan_integer(fp, &val, &termchar)) {
00190 if (scanno >= MAX_SCANS) {
00191 fprintf(stderr, "Too many scans defined in file %s\n", filename);
00192 fclose(fp);
00193 return FALSE;
00194 }
00195 scanptr->component_index[0] = (int) val;
00196 ncomps = 1;
00197 while (termchar == ' ') {
00198 if (ncomps >= MAX_COMPS_IN_SCAN) {
00199 fprintf(stderr, "Too many components in one scan in file %s\n",
00200 filename);
00201 fclose(fp);
00202 return FALSE;
00203 }
00204 if (! read_scan_integer(fp, &val, &termchar))
00205 goto bogus;
00206 scanptr->component_index[ncomps] = (int) val;
00207 ncomps++;
00208 }
00209 scanptr->comps_in_scan = ncomps;
00210 if (termchar == ':') {
00211 if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
00212 goto bogus;
00213 scanptr->Ss = (int) val;
00214 if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
00215 goto bogus;
00216 scanptr->Se = (int) val;
00217 if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
00218 goto bogus;
00219 scanptr->Ah = (int) val;
00220 if (! read_scan_integer(fp, &val, &termchar))
00221 goto bogus;
00222 scanptr->Al = (int) val;
00223 } else {
00224
00225 scanptr->Ss = 0;
00226 scanptr->Se = DCTSIZE2-1;
00227 scanptr->Ah = 0;
00228 scanptr->Al = 0;
00229 }
00230 if (termchar != ';' && termchar != EOF) {
00231 bogus:
00232 fprintf(stderr, "Invalid scan entry format in file %s\n", filename);
00233 fclose(fp);
00234 return FALSE;
00235 }
00236 scanptr++, scanno++;
00237 }
00238
00239 if (termchar != EOF) {
00240 fprintf(stderr, "Non-numeric data in file %s\n", filename);
00241 fclose(fp);
00242 return FALSE;
00243 }
00244
00245 if (scanno > 0) {
00246
00247
00248
00249
00250 scanptr = (jpeg_scan_info *)
00251 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00252 scanno * SIZEOF(jpeg_scan_info));
00253 MEMCOPY(scanptr, scans, scanno * SIZEOF(jpeg_scan_info));
00254 cinfo->scan_info = scanptr;
00255 cinfo->num_scans = scanno;
00256 }
00257
00258 fclose(fp);
00259 return TRUE;
00260 }
00261
00262 #endif
00263
00264
00265 GLOBAL(boolean)
00266 set_quant_slots (j_compress_ptr cinfo, char *arg)
00267
00268
00269
00270
00271 {
00272 int val = 0;
00273 int ci;
00274 char ch;
00275
00276 for (ci = 0; ci < MAX_COMPONENTS; ci++) {
00277 if (*arg) {
00278 ch = ',';
00279 if (sscanf(arg, "%d%c", &val, &ch) < 1)
00280 return FALSE;
00281 if (ch != ',')
00282 return FALSE;
00283 if (val < 0 || val >= NUM_QUANT_TBLS) {
00284 fprintf(stderr, "JPEG quantization tables are numbered 0..%d\n",
00285 NUM_QUANT_TBLS-1);
00286 return FALSE;
00287 }
00288 cinfo->comp_info[ci].quant_tbl_no = val;
00289 while (*arg && *arg++ != ',')
00290 ;
00291 } else {
00292
00293 cinfo->comp_info[ci].quant_tbl_no = val;
00294 }
00295 }
00296 return TRUE;
00297 }
00298
00299
00300 GLOBAL(boolean)
00301 set_sample_factors (j_compress_ptr cinfo, char *arg)
00302
00303
00304
00305
00306 {
00307 int ci, val1, val2;
00308 char ch1, ch2;
00309
00310 for (ci = 0; ci < MAX_COMPONENTS; ci++) {
00311 if (*arg) {
00312 ch2 = ',';
00313 if (sscanf(arg, "%d%c%d%c", &val1, &ch1, &val2, &ch2) < 3)
00314 return FALSE;
00315 if ((ch1 != 'x' && ch1 != 'X') || ch2 != ',')
00316 return FALSE;
00317 if (val1 <= 0 || val1 > 4 || val2 <= 0 || val2 > 4) {
00318 fprintf(stderr, "JPEG sampling factors must be 1..4\n");
00319 return FALSE;
00320 }
00321 cinfo->comp_info[ci].h_samp_factor = val1;
00322 cinfo->comp_info[ci].v_samp_factor = val2;
00323 while (*arg && *arg++ != ',')
00324 ;
00325 } else {
00326
00327 cinfo->comp_info[ci].h_samp_factor = 1;
00328 cinfo->comp_info[ci].v_samp_factor = 1;
00329 }
00330 }
00331 return TRUE;
00332 }