| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "cdjpeg.h" |
| 17 |
#include <ctype.h> |
| 18 |
|
| 19 |
|
| 20 |
LOCAL(int) |
| 21 |
text_getc (FILE * file) |
| 22 |
|
| 23 |
|
| 24 |
{ |
| 25 |
register int ch; |
| 26 |
|
| 27 |
ch = getc(file); |
| 28 |
if (ch == '#') { |
| 29 |
do { |
| 30 |
ch = getc(file); |
| 31 |
} while (ch != '\n' && ch != EOF); |
| 32 |
} |
| 33 |
return ch; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
LOCAL(boolean) |
| 38 |
read_text_integer (FILE * file, long * result, int * termchar) |
| 39 |
|
| 40 |
|
| 41 |
{ |
| 42 |
register int ch; |
| 43 |
register long val; |
| 44 |
|
| 45 |
|
| 46 |
do { |
| 47 |
ch = text_getc(file); |
| 48 |
if (ch == EOF) { |
| 49 |
*termchar = ch; |
| 50 |
return FALSE; |
| 51 |
} |
| 52 |
} while (isspace(ch)); |
| 53 |
|
| 54 |
if (! isdigit(ch)) { |
| 55 |
*termchar = ch; |
| 56 |
return FALSE; |
| 57 |
} |
| 58 |
|
| 59 |
val = ch - '0'; |
| 60 |
while ((ch = text_getc(file)) != EOF) { |
| 61 |
if (! isdigit(ch)) |
| 62 |
break; |
| 63 |
val *= 10; |
| 64 |
val += ch - '0'; |
| 65 |
} |
| 66 |
*result = val; |
| 67 |
*termchar = ch; |
| 68 |
return TRUE; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
GLOBAL(boolean) |
| 73 |
read_quant_tables (j_compress_ptr cinfo, char * filename, |
| 74 |
int scale_factor, boolean force_baseline) |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 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)) { |
| 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, scale_factor, force_baseline); |
| 112 |
tblno++; |
| 113 |
} |
| 114 |
|
| 115 |
if (termchar != EOF) { |
| 116 |
fprintf(stderr, "Non-numeric data in file %s\n", filename); |
| 117 |
fclose(fp); |
| 118 |
return FALSE; |
| 119 |
} |
| 120 |
|
| 121 |
fclose(fp); |
| 122 |
return TRUE; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
#ifdef C_MULTISCAN_FILES_SUPPORTED |
| 127 |
|
| 128 |
LOCAL(boolean) |
| 129 |
read_scan_integer (FILE * file, long * result, int * termchar) |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
{ |
| 134 |
register int ch; |
| 135 |
|
| 136 |
if (! read_text_integer(file, result, termchar)) |
| 137 |
return FALSE; |
| 138 |
ch = *termchar; |
| 139 |
while (ch != EOF && isspace(ch)) |
| 140 |
ch = text_getc(file); |
| 141 |
if (isdigit(ch)) { |
| 142 |
if (ungetc(ch, file) == EOF) |
| 143 |
return FALSE; |
| 144 |
ch = ' '; |
| 145 |
} else { |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
if (ch != EOF && ch != ';' && ch != ':') |
| 150 |
ch = ' '; |
| 151 |
} |
| 152 |
*termchar = ch; |
| 153 |
return TRUE; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
GLOBAL(boolean) |
| 158 |
read_scan_script (j_compress_ptr cinfo, char * filename) |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
{ |
| 175 |
FILE * fp; |
| 176 |
int scanno, ncomps, termchar; |
| 177 |
long val; |
| 178 |
jpeg_scan_info * scanptr; |
| 179 |
#define MAX_SCANS 100 |
| 180 |
jpeg_scan_info scans[MAX_SCANS]; |
| 181 |
|
| 182 |
if ((fp = fopen(filename, "r")) == NULL) { |
| 183 |
fprintf(stderr, "Can't open scan definition file %s\n", filename); |
| 184 |
return FALSE; |
| 185 |
} |
| 186 |
scanptr = scans; |
| 187 |
scanno = 0; |
| 188 |
|
| 189 |
while (read_scan_integer(fp, &val, &termchar)) { |
| 190 |
if (scanno >= MAX_SCANS) { |
| 191 |
fprintf(stderr, "Too many scans defined in file %s\n", filename); |
| 192 |
fclose(fp); |
| 193 |
return FALSE; |
| 194 |
} |
| 195 |
scanptr->component_index[0] = (int) val; |
| 196 |
ncomps = 1; |
| 197 |
while (termchar == ' ') { |
| 198 |
if (ncomps >= MAX_COMPS_IN_SCAN) { |
| 199 |
fprintf(stderr, "Too many components in one scan in file %s\n", |
| 200 |
filename); |
| 201 |
fclose(fp); |
| 202 |
return FALSE; |
| 203 |
} |
| 204 |
if (! read_scan_integer(fp, &val, &termchar)) |
| 205 |
goto bogus; |
| 206 |
scanptr->component_index[ncomps] = (int) val; |
| 207 |
ncomps++; |
| 208 |
} |
| 209 |
scanptr->comps_in_scan = ncomps; |
| 210 |
if (termchar == ':') { |
| 211 |
if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ') |
| 212 |
goto bogus; |
| 213 |
scanptr->Ss = (int) val; |
| 214 |
if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ') |
| 215 |
goto bogus; |
| 216 |
scanptr->Se = (int) val; |
| 217 |
if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ') |
| 218 |
goto bogus; |
| 219 |
scanptr->Ah = (int) val; |
| 220 |
if (! read_scan_integer(fp, &val, &termchar)) |
| 221 |
goto bogus; |
| 222 |
scanptr->Al = (int) val; |
| 223 |
} else { |
| 224 |
|
| 225 |
scanptr->Ss = 0; |
| 226 |
scanptr->Se = DCTSIZE2-1; |
| 227 |
scanptr->Ah = 0; |
| 228 |
scanptr->Al = 0; |
| 229 |
} |
| 230 |
if (termchar != ';' && termchar != EOF) { |
| 231 |
bogus: |
| 232 |
fprintf(stderr, "Invalid scan entry format in file %s\n", filename); |
| 233 |
fclose(fp); |
| 234 |
return FALSE; |
| 235 |
} |
| 236 |
scanptr++, scanno++; |
| 237 |
} |
| 238 |
|
| 239 |
if (termchar != EOF) { |
| 240 |
fprintf(stderr, "Non-numeric data in file %s\n", filename); |
| 241 |
fclose(fp); |
| 242 |
return FALSE; |
| 243 |
} |
| 244 |
|
| 245 |
if (scanno > 0) { |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
scanptr = (jpeg_scan_info *) |
| 251 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 252 |
scanno * SIZEOF(jpeg_scan_info)); |
| 253 |
MEMCOPY(scanptr, scans, scanno * SIZEOF(jpeg_scan_info)); |
| 254 |
cinfo->scan_info = scanptr; |
| 255 |
cinfo->num_scans = scanno; |
| 256 |
} |
| 257 |
|
| 258 |
fclose(fp); |
| 259 |
return TRUE; |
| 260 |
} |
| 261 |
|
| 262 |
#endif |
| 263 |
|
| 264 |
|
| 265 |
GLOBAL(boolean) |
| 266 |
set_quant_slots (j_compress_ptr cinfo, char *arg) |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
{ |
| 272 |
int val = 0; |
| 273 |
int ci; |
| 274 |
char ch; |
| 275 |
|
| 276 |
for (ci = 0; ci < MAX_COMPONENTS; ci++) { |
| 277 |
if (*arg) { |
| 278 |
ch = ','; |
| 279 |
if (sscanf(arg, "%d%c", &val, &ch) < 1) |
| 280 |
return FALSE; |
| 281 |
if (ch != ',') |
| 282 |
return FALSE; |
| 283 |
if (val < 0 || val >= NUM_QUANT_TBLS) { |
| 284 |
fprintf(stderr, "JPEG quantization tables are numbered 0..%d\n", |
| 285 |
NUM_QUANT_TBLS-1); |
| 286 |
return FALSE; |
| 287 |
} |
| 288 |
cinfo->comp_info[ci].quant_tbl_no = val; |
| 289 |
while (*arg && *arg++ != ',') |
| 290 |
; |
| 291 |
} else { |
| 292 |
|
| 293 |
cinfo->comp_info[ci].quant_tbl_no = val; |
| 294 |
} |
| 295 |
} |
| 296 |
return TRUE; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
GLOBAL(boolean) |
| 301 |
set_sample_factors (j_compress_ptr cinfo, char *arg) |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
{ |
| 307 |
int ci, val1, val2; |
| 308 |
char ch1, ch2; |
| 309 |
|
| 310 |
for (ci = 0; ci < MAX_COMPONENTS; ci++) { |
| 311 |
if (*arg) { |
| 312 |
ch2 = ','; |
| 313 |
if (sscanf(arg, "%d%c%d%c", &val1, &ch1, &val2, &ch2) < 3) |
| 314 |
return FALSE; |
| 315 |
if ((ch1 != 'x' && ch1 != 'X') || ch2 != ',') |
| 316 |
return FALSE; |
| 317 |
if (val1 <= 0 || val1 > 4 || val2 <= 0 || val2 > 4) { |
| 318 |
fprintf(stderr, "JPEG sampling factors must be 1..4\n"); |
| 319 |
return FALSE; |
| 320 |
} |
| 321 |
cinfo->comp_info[ci].h_samp_factor = val1; |
| 322 |
cinfo->comp_info[ci].v_samp_factor = val2; |
| 323 |
while (*arg && *arg++ != ',') |
| 324 |
; |
| 325 |
} else { |
| 326 |
|
| 327 |
cinfo->comp_info[ci].h_samp_factor = 1; |
| 328 |
cinfo->comp_info[ci].v_samp_factor = 1; |
| 329 |
} |
| 330 |
} |
| 331 |
return TRUE; |
| 332 |
} |