|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* rdswitch.c
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* Copyright (C) 1991-1996, Thomas G. Lane.
|
|
kusano |
7d535a |
* This file is part of the Independent JPEG Group's software.
|
|
kusano |
7d535a |
* For conditions of distribution and use, see the accompanying README file.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* This file contains routines to process some of cjpeg's more complicated
|
|
kusano |
7d535a |
* command-line switches. Switches processed here are:
|
|
kusano |
7d535a |
* -qtables file Read quantization tables from text file
|
|
kusano |
7d535a |
* -scans file Read scan script from text file
|
|
kusano |
7d535a |
* -quality N[,N,...] Set quality ratings
|
|
kusano |
7d535a |
* -qslots N[,N,...] Set component quantization table selectors
|
|
kusano |
7d535a |
* -sample HxV[,HxV,...] Set component sampling factors
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
|
|
kusano |
7d535a |
#include <ctype.h> /* to declare isdigit(), isspace() */</ctype.h>
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
LOCAL(int)
|
|
kusano |
7d535a |
text_getc (FILE * file)
|
|
kusano |
7d535a |
/* Read next char, skipping over any comments (# to end of line) */
|
|
kusano |
7d535a |
/* A comment/newline sequence is returned as a newline */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
register int ch;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
ch = getc(file);
|
|
kusano |
7d535a |
if (ch == '#') {
|
|
kusano |
7d535a |
do {
|
|
kusano |
7d535a |
ch = getc(file);
|
|
kusano |
7d535a |
} while (ch != '\n' && ch != EOF);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
return ch;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
LOCAL(boolean)
|
|
kusano |
7d535a |
read_text_integer (FILE * file, long * result, int * termchar)
|
|
kusano |
7d535a |
/* Read an unsigned decimal integer from a file, store it in result */
|
|
kusano |
7d535a |
/* Reads one trailing character after the integer; returns it in termchar */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
register int ch;
|
|
kusano |
7d535a |
register long val;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Skip any leading whitespace, detect EOF */
|
|
kusano |
7d535a |
do {
|
|
kusano |
7d535a |
ch = text_getc(file);
|
|
kusano |
7d535a |
if (ch == EOF) {
|
|
kusano |
7d535a |
*termchar = ch;
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
} while (isspace(ch));
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (! isdigit(ch)) {
|
|
kusano |
7d535a |
*termchar = ch;
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
val = ch - '0';
|
|
kusano |
7d535a |
while ((ch = text_getc(file)) != EOF) {
|
|
kusano |
7d535a |
if (! isdigit(ch))
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
val *= 10;
|
|
kusano |
7d535a |
val += ch - '0';
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
*result = val;
|
|
kusano |
7d535a |
*termchar = ch;
|
|
kusano |
7d535a |
return TRUE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
GLOBAL(boolean)
|
|
kusano |
7d535a |
read_quant_tables (j_compress_ptr cinfo, char * filename, boolean force_baseline)
|
|
kusano |
7d535a |
/* Read a set of quantization tables from the specified file.
|
|
kusano |
7d535a |
* The file is plain ASCII text: decimal numbers with whitespace between.
|
|
kusano |
7d535a |
* Comments preceded by '#' may be included in the file.
|
|
kusano |
7d535a |
* There may be one to NUM_QUANT_TBLS tables in the file, each of 64 values.
|
|
kusano |
7d535a |
* The tables are implicitly numbered 0,1,etc.
|
|
kusano |
7d535a |
* NOTE: does not affect the qslots mapping, which will default to selecting
|
|
kusano |
7d535a |
* table 0 for luminance (or primary) components, 1 for chrominance components.
|
|
kusano |
7d535a |
* You must use -qslots if you want a different component->table mapping.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
FILE * fp;
|
|
kusano |
7d535a |
int tblno, i, termchar;
|
|
kusano |
7d535a |
long val;
|
|
kusano |
7d535a |
unsigned int table[DCTSIZE2];
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if ((fp = fopen(filename, "r")) == NULL) {
|
|
kusano |
7d535a |
fprintf(stderr, "Can't open table file %s\n", filename);
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
tblno = 0;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
while (read_text_integer(fp, &val, &termchar)) { /* read 1st element of table */
|
|
kusano |
7d535a |
if (tblno >= NUM_QUANT_TBLS) {
|
|
kusano |
7d535a |
fprintf(stderr, "Too many tables in file %s\n", filename);
|
|
kusano |
7d535a |
fclose(fp);
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
table[0] = (unsigned int) val;
|
|
kusano |
7d535a |
for (i = 1; i < DCTSIZE2; i++) {
|
|
kusano |
7d535a |
if (! read_text_integer(fp, &val, &termchar)) {
|
|
kusano |
7d535a |
fprintf(stderr, "Invalid table data in file %s\n", filename);
|
|
kusano |
7d535a |
fclose(fp);
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
table[i] = (unsigned int) val;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
jpeg_add_quant_table(cinfo, tblno, table, cinfo->q_scale_factor[tblno],
|
|
kusano |
7d535a |
force_baseline);
|
|
kusano |
7d535a |
tblno++;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (termchar != EOF) {
|
|
kusano |
7d535a |
fprintf(stderr, "Non-numeric data in file %s\n", filename);
|
|
kusano |
7d535a |
fclose(fp);
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
fclose(fp);
|
|
kusano |
7d535a |
return TRUE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#ifdef C_MULTISCAN_FILES_SUPPORTED
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
LOCAL(boolean)
|
|
kusano |
7d535a |
read_scan_integer (FILE * file, long * result, int * termchar)
|
|
kusano |
7d535a |
/* Variant of read_text_integer that always looks for a non-space termchar;
|
|
kusano |
7d535a |
* this simplifies parsing of punctuation in scan scripts.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
register int ch;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (! read_text_integer(file, result, termchar))
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
ch = *termchar;
|
|
kusano |
7d535a |
while (ch != EOF && isspace(ch))
|
|
kusano |
7d535a |
ch = text_getc(file);
|
|
kusano |
7d535a |
if (isdigit(ch)) { /* oops, put it back */
|
|
kusano |
7d535a |
if (ungetc(ch, file) == EOF)
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
ch = ' ';
|
|
kusano |
7d535a |
} else {
|
|
kusano |
7d535a |
/* Any separators other than ';' and ':' are ignored;
|
|
kusano |
7d535a |
* this allows user to insert commas, etc, if desired.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
if (ch != EOF && ch != ';' && ch != ':')
|
|
kusano |
7d535a |
ch = ' ';
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
*termchar = ch;
|
|
kusano |
7d535a |
return TRUE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
GLOBAL(boolean)
|
|
kusano |
7d535a |
read_scan_script (j_compress_ptr cinfo, char * filename)
|
|
kusano |
7d535a |
/* Read a scan script from the specified text file.
|
|
kusano |
7d535a |
* Each entry in the file defines one scan to be emitted.
|
|
kusano |
7d535a |
* Entries are separated by semicolons ';'.
|
|
kusano |
7d535a |
* An entry contains one to four component indexes,
|
|
kusano |
7d535a |
* optionally followed by a colon ':' and four progressive-JPEG parameters.
|
|
kusano |
7d535a |
* The component indexes denote which component(s) are to be transmitted
|
|
kusano |
7d535a |
* in the current scan. The first component has index 0.
|
|
kusano |
7d535a |
* Sequential JPEG is used if the progressive-JPEG parameters are omitted.
|
|
kusano |
7d535a |
* The file is free format text: any whitespace may appear between numbers
|
|
kusano |
7d535a |
* and the ':' and ';' punctuation marks. Also, other punctuation (such
|
|
kusano |
7d535a |
* as commas or dashes) can be placed between numbers if desired.
|
|
kusano |
7d535a |
* Comments preceded by '#' may be included in the file.
|
|
kusano |
7d535a |
* Note: we do very little validity checking here;
|
|
kusano |
7d535a |
* jcmaster.c will validate the script parameters.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
FILE * fp;
|
|
kusano |
7d535a |
int scanno, ncomps, termchar;
|
|
kusano |
7d535a |
long val;
|
|
kusano |
7d535a |
jpeg_scan_info * scanptr;
|
|
kusano |
7d535a |
#define MAX_SCANS 100 /* quite arbitrary limit */
|
|
kusano |
7d535a |
jpeg_scan_info scans[MAX_SCANS];
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if ((fp = fopen(filename, "r")) == NULL) {
|
|
kusano |
7d535a |
fprintf(stderr, "Can't open scan definition file %s\n", filename);
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
scanptr = scans;
|
|
kusano |
7d535a |
scanno = 0;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
while (read_scan_integer(fp, &val, &termchar)) {
|
|
kusano |
7d535a |
if (scanno >= MAX_SCANS) {
|
|
kusano |
7d535a |
fprintf(stderr, "Too many scans defined in file %s\n", filename);
|
|
kusano |
7d535a |
fclose(fp);
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
scanptr->component_index[0] = (int) val;
|
|
kusano |
7d535a |
ncomps = 1;
|
|
kusano |
7d535a |
while (termchar == ' ') {
|
|
kusano |
7d535a |
if (ncomps >= MAX_COMPS_IN_SCAN) {
|
|
kusano |
7d535a |
fprintf(stderr, "Too many components in one scan in file %s\n",
|
|
kusano |
7d535a |
filename);
|
|
kusano |
7d535a |
fclose(fp);
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (! read_scan_integer(fp, &val, &termchar))
|
|
kusano |
7d535a |
goto bogus;
|
|
kusano |
7d535a |
scanptr->component_index[ncomps] = (int) val;
|
|
kusano |
7d535a |
ncomps++;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
scanptr->comps_in_scan = ncomps;
|
|
kusano |
7d535a |
if (termchar == ':') {
|
|
kusano |
7d535a |
if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
|
|
kusano |
7d535a |
goto bogus;
|
|
kusano |
7d535a |
scanptr->Ss = (int) val;
|
|
kusano |
7d535a |
if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
|
|
kusano |
7d535a |
goto bogus;
|
|
kusano |
7d535a |
scanptr->Se = (int) val;
|
|
kusano |
7d535a |
if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
|
|
kusano |
7d535a |
goto bogus;
|
|
kusano |
7d535a |
scanptr->Ah = (int) val;
|
|
kusano |
7d535a |
if (! read_scan_integer(fp, &val, &termchar))
|
|
kusano |
7d535a |
goto bogus;
|
|
kusano |
7d535a |
scanptr->Al = (int) val;
|
|
kusano |
7d535a |
} else {
|
|
kusano |
7d535a |
/* set non-progressive parameters */
|
|
kusano |
7d535a |
scanptr->Ss = 0;
|
|
kusano |
7d535a |
scanptr->Se = DCTSIZE2-1;
|
|
kusano |
7d535a |
scanptr->Ah = 0;
|
|
kusano |
7d535a |
scanptr->Al = 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (termchar != ';' && termchar != EOF) {
|
|
kusano |
7d535a |
bogus:
|
|
kusano |
7d535a |
fprintf(stderr, "Invalid scan entry format in file %s\n", filename);
|
|
kusano |
7d535a |
fclose(fp);
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
scanptr++, scanno++;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (termchar != EOF) {
|
|
kusano |
7d535a |
fprintf(stderr, "Non-numeric data in file %s\n", filename);
|
|
kusano |
7d535a |
fclose(fp);
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (scanno > 0) {
|
|
kusano |
7d535a |
/* Stash completed scan list in cinfo structure.
|
|
kusano |
7d535a |
* NOTE: for cjpeg's use, JPOOL_IMAGE is the right lifetime for this data,
|
|
kusano |
7d535a |
* but if you want to compress multiple images you'd want JPOOL_PERMANENT.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
scanptr = (jpeg_scan_info *)
|
|
kusano |
7d535a |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
|
kusano |
7d535a |
scanno * SIZEOF(jpeg_scan_info));
|
|
kusano |
7d535a |
MEMCOPY(scanptr, scans, scanno * SIZEOF(jpeg_scan_info));
|
|
kusano |
7d535a |
cinfo->scan_info = scanptr;
|
|
kusano |
7d535a |
cinfo->num_scans = scanno;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
fclose(fp);
|
|
kusano |
7d535a |
return TRUE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#endif /* C_MULTISCAN_FILES_SUPPORTED */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
GLOBAL(boolean)
|
|
kusano |
7d535a |
set_quality_ratings (j_compress_ptr cinfo, char *arg, boolean force_baseline)
|
|
kusano |
7d535a |
/* Process a quality-ratings parameter string, of the form
|
|
kusano |
7d535a |
* N[,N,...]
|
|
kusano |
7d535a |
* If there are more q-table slots than parameters, the last value is replicated.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int val = 75; /* default value */
|
|
kusano |
7d535a |
int tblno;
|
|
kusano |
7d535a |
char ch;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
|
|
kusano |
7d535a |
if (*arg) {
|
|
kusano |
7d535a |
ch = ','; /* if not set by sscanf, will be ',' */
|
|
kusano |
7d535a |
if (sscanf(arg, "%d%c", &val, &ch) < 1)
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
if (ch != ',') /* syntax check */
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
/* Convert user 0-100 rating to percentage scaling */
|
|
kusano |
7d535a |
cinfo->q_scale_factor[tblno] = jpeg_quality_scaling(val);
|
|
kusano |
7d535a |
while (*arg && *arg++ != ',') /* advance to next segment of arg string */
|
|
kusano |
7d535a |
;
|
|
kusano |
7d535a |
} else {
|
|
kusano |
7d535a |
/* reached end of parameter, set remaining factors to last value */
|
|
kusano |
7d535a |
cinfo->q_scale_factor[tblno] = jpeg_quality_scaling(val);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
jpeg_default_qtables(cinfo, force_baseline);
|
|
kusano |
7d535a |
return TRUE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
GLOBAL(boolean)
|
|
kusano |
7d535a |
set_quant_slots (j_compress_ptr cinfo, char *arg)
|
|
kusano |
7d535a |
/* Process a quantization-table-selectors parameter string, of the form
|
|
kusano |
7d535a |
* N[,N,...]
|
|
kusano |
7d535a |
* If there are more components than parameters, the last value is replicated.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int val = 0; /* default table # */
|
|
kusano |
7d535a |
int ci;
|
|
kusano |
7d535a |
char ch;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
for (ci = 0; ci < MAX_COMPONENTS; ci++) {
|
|
kusano |
7d535a |
if (*arg) {
|
|
kusano |
7d535a |
ch = ','; /* if not set by sscanf, will be ',' */
|
|
kusano |
7d535a |
if (sscanf(arg, "%d%c", &val, &ch) < 1)
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
if (ch != ',') /* syntax check */
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
if (val < 0 || val >= NUM_QUANT_TBLS) {
|
|
kusano |
7d535a |
fprintf(stderr, "JPEG quantization tables are numbered 0..%d\n",
|
|
kusano |
7d535a |
NUM_QUANT_TBLS-1);
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
cinfo->comp_info[ci].quant_tbl_no = val;
|
|
kusano |
7d535a |
while (*arg && *arg++ != ',') /* advance to next segment of arg string */
|
|
kusano |
7d535a |
;
|
|
kusano |
7d535a |
} else {
|
|
kusano |
7d535a |
/* reached end of parameter, set remaining components to last table */
|
|
kusano |
7d535a |
cinfo->comp_info[ci].quant_tbl_no = val;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
return TRUE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
GLOBAL(boolean)
|
|
kusano |
7d535a |
set_sample_factors (j_compress_ptr cinfo, char *arg)
|
|
kusano |
7d535a |
/* Process a sample-factors parameter string, of the form
|
|
kusano |
7d535a |
* HxV[,HxV,...]
|
|
kusano |
7d535a |
* If there are more components than parameters, "1x1" is assumed for the rest.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int ci, val1, val2;
|
|
kusano |
7d535a |
char ch1, ch2;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
for (ci = 0; ci < MAX_COMPONENTS; ci++) {
|
|
kusano |
7d535a |
if (*arg) {
|
|
kusano |
7d535a |
ch2 = ','; /* if not set by sscanf, will be ',' */
|
|
kusano |
7d535a |
if (sscanf(arg, "%d%c%d%c", &val1, &ch1, &val2, &ch2) < 3)
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
if ((ch1 != 'x' && ch1 != 'X') || ch2 != ',') /* syntax check */
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
if (val1 <= 0 || val1 > 4 || val2 <= 0 || val2 > 4) {
|
|
kusano |
7d535a |
fprintf(stderr, "JPEG sampling factors must be 1..4\n");
|
|
kusano |
7d535a |
return FALSE;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
cinfo->comp_info[ci].h_samp_factor = val1;
|
|
kusano |
7d535a |
cinfo->comp_info[ci].v_samp_factor = val2;
|
|
kusano |
7d535a |
while (*arg && *arg++ != ',') /* advance to next segment of arg string */
|
|
kusano |
7d535a |
;
|
|
kusano |
7d535a |
} else {
|
|
kusano |
7d535a |
/* reached end of parameter, set remaining components to 1x1 sampling */
|
|
kusano |
7d535a |
cinfo->comp_info[ci].h_samp_factor = 1;
|
|
kusano |
7d535a |
cinfo->comp_info[ci].v_samp_factor = 1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
return TRUE;
|
|
kusano |
7d535a |
}
|