Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

rdswitch.c File Reference

#include "cdjpeg.h"
#include <ctype.h>

Go to the source code of this file.


Defines

#define MAX_SCANS   100

Functions

 text_getc (FILE *file)
 read_text_integer (FILE *file, long *result, int *termchar)
 read_quant_tables (j_compress_ptr cinfo, char *filename, int scale_factor, boolean force_baseline)
 read_scan_integer (FILE *file, long *result, int *termchar)
 read_scan_script (j_compress_ptr cinfo, char *filename)
 set_quant_slots (j_compress_ptr cinfo, char *arg)
 set_sample_factors (j_compress_ptr cinfo, char *arg)

Define Documentation

#define MAX_SCANS   100
 


Function Documentation

read_quant_tables j_compress_ptr    cinfo,
char *    filename,
int    scale_factor,
boolean    force_baseline
 

Definition at line 73 of file rdswitch.c.

References i, jpeg_add_quant_table(), NUM_QUANT_TBLS, and read_text_integer().

00076                                : decimal numbers with whitespace between.
00077  * Comments preceded by '#' may be included in the file.
00078  * There may be one to NUM_QUANT_TBLS tables in the file, each of 64 values.
00079  * The tables are implicitly numbered 0,1,etc.
00080  * NOTE: does not affect the qslots mapping, which will default to selecting
00081  * table 0 for luminance (or primary) components, 1 for chrominance components.
00082  * You must use -qslots if you want a different component->table mapping.
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)) { /* read 1st element of table */
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 }

read_scan_integer FILE *    file,
long *    result,
int *    termchar
 

Definition at line 129 of file rdswitch.c.

References file, read_text_integer(), text_getc(), and ungetc().

Referenced by read_scan_script().

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)) {            /* oops, put it back */
00142     if (ungetc(ch, file) == EOF)
00143       return FALSE;
00144     ch = ' ';
00145   } else {
00146     /* Any separators other than ';' and ':' are ignored;
00147      * this allows user to insert commas, etc, if desired.
00148      */
00149     if (ch != EOF && ch != ';' && ch != ':')
00150       ch = ' ';
00151   }
00152   *termchar = ch;
00153   return TRUE;
00154 }

read_scan_script j_compress_ptr    cinfo,
char *    filename
 

Definition at line 158 of file rdswitch.c.

References jpeg_scan_info::Ah, jpeg_scan_info::Al, jpeg_scan_info::component_index, jpeg_scan_info::comps_in_scan, JPOOL_IMAGE, MAX_COMPS_IN_SCAN, MEMCOPY, read_scan_integer(), jpeg_scan_info::Se, SIZEOF, and jpeg_scan_info::Ss.

Referenced by parse_switches().

00163                                    :' and four progressive-JPEG parameters.
00164  * The component indexes denote which component(s) are to be transmitted
00165  * in the current scan.  The first component has index 0.
00166  * Sequential JPEG is used if the progressive-JPEG parameters are omitted.
00167  * The file is free format text: any whitespace may appear between numbers
00168  * and the ':' and ';' punctuation marks.  Also, other punctuation (such
00169  * as commas or dashes) can be placed between numbers if desired.
00170  * Comments preceded by '#' may be included in the file.
00171  * Note: we do very little validity checking here;
00172  * jcmaster.c will validate the script parameters.
00173  */
00174 {
00175   FILE * fp;
00176   int scanno, ncomps, termchar;
00177   long val;
00178   jpeg_scan_info * scanptr;
00179 #define MAX_SCANS  100          /* quite arbitrary limit */
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       /* set non-progressive parameters */
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     /* Stash completed scan list in cinfo structure.
00247      * NOTE: for cjpeg's use, JPOOL_IMAGE is the right lifetime for this data,
00248      * but if you want to compress multiple images you'd want JPOOL_PERMANENT.
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 }

read_text_integer FILE *    file,
long *    result,
int *    termchar
 

Definition at line 38 of file rdswitch.c.

References file, and text_getc().

Referenced by read_quant_tables(), and read_scan_integer().

00041 {
00042   register int ch;
00043   register long val;
00044   
00045   /* Skip any leading whitespace, detect EOF */
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 }

set_quant_slots j_compress_ptr    cinfo,
char *    arg
 

Definition at line 266 of file rdswitch.c.

References arg, jpeg_compress_struct::comp_info, MAX_COMPONENTS, NUM_QUANT_TBLS, and jpeg_component_info::quant_tbl_no.

00271 {
00272   int val = 0;                  /* default table # */
00273   int ci;
00274   char ch;
00275 
00276   for (ci = 0; ci < MAX_COMPONENTS; ci++) {
00277     if (*arg) {
00278       ch = ',';                 /* if not set by sscanf, will be ',' */
00279       if (sscanf(arg, "%d%c", &val, &ch) < 1)
00280         return FALSE;
00281       if (ch != ',')            /* syntax check */
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++ != ',') /* advance to next segment of arg string */
00290         ;
00291     } else {
00292       /* reached end of parameter, set remaining components to last table */
00293       cinfo->comp_info[ci].quant_tbl_no = val;
00294     }
00295   }
00296   return TRUE;
00297 }

set_sample_factors j_compress_ptr    cinfo,
char *    arg
 

Definition at line 301 of file rdswitch.c.

References arg, jpeg_compress_struct::comp_info, jpeg_component_info::h_samp_factor, MAX_COMPONENTS, and jpeg_component_info::v_samp_factor.

00306 {
00307   int ci, val1, val2;
00308   char ch1, ch2;
00309 
00310   for (ci = 0; ci < MAX_COMPONENTS; ci++) {
00311     if (*arg) {
00312       ch2 = ',';                /* if not set by sscanf, will be ',' */
00313       if (sscanf(arg, "%d%c%d%c", &val1, &ch1, &val2, &ch2) < 3)
00314         return FALSE;
00315       if ((ch1 != 'x' && ch1 != 'X') || ch2 != ',') /* syntax check */
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++ != ',') /* advance to next segment of arg string */
00324         ;
00325     } else {
00326       /* reached end of parameter, set remaining components to 1x1 sampling */
00327       cinfo->comp_info[ci].h_samp_factor = 1;
00328       cinfo->comp_info[ci].v_samp_factor = 1;
00329     }
00330   }
00331   return TRUE;
00332 }

text_getc FILE *    file
 

Definition at line 21 of file rdswitch.c.

References file.

Referenced by read_scan_integer(), and read_text_integer().

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 }
 

Powered by Plone

This site conforms to the following standards: