rdswitch.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. * -qslots N[,N,...] Set component quantization table selectors
  13. * -sample HxV[,HxV,...] Set component sampling factors
  14. */
  15. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  16. #include <ctype.h> /* to declare isdigit(), isspace() */
  17. LOCAL(int)
  18. text_getc (FILE * file)
  19. /* Read next char, skipping over any comments (# to end of line) */
  20. /* A comment/newline sequence is returned as a newline */
  21. {
  22. register int ch;
  23. ch = getc(file);
  24. if (ch == '#') {
  25. do {
  26. ch = getc(file);
  27. } while (ch != '\n' && ch != EOF);
  28. }
  29. return ch;
  30. }
  31. LOCAL(boolean)
  32. read_text_integer (FILE * file, long * result, int * termchar)
  33. /* Read an unsigned decimal integer from a file, store it in result */
  34. /* Reads one trailing character after the integer; returns it in termchar */
  35. {
  36. register int ch;
  37. register long val;
  38. /* Skip any leading whitespace, detect EOF */
  39. do {
  40. ch = text_getc(file);
  41. if (ch == EOF) {
  42. *termchar = ch;
  43. return FALSE;
  44. }
  45. } while (isspace(ch));
  46. if (! isdigit(ch)) {
  47. *termchar = ch;
  48. return FALSE;
  49. }
  50. val = ch - '0';
  51. while ((ch = text_getc(file)) != EOF) {
  52. if (! isdigit(ch))
  53. break;
  54. val *= 10;
  55. val += ch - '0';
  56. }
  57. *result = val;
  58. *termchar = ch;
  59. return TRUE;
  60. }
  61. GLOBAL(boolean)
  62. read_quant_tables (j_compress_ptr cinfo, char * filename,
  63. int scale_factor, boolean force_baseline)
  64. /* Read a set of quantization tables from the specified file.
  65. * The file is plain ASCII text: decimal numbers with whitespace between.
  66. * Comments preceded by '#' may be included in the file.
  67. * There may be one to NUM_QUANT_TBLS tables in the file, each of 64 values.
  68. * The tables are implicitly numbered 0,1,etc.
  69. * NOTE: does not affect the qslots mapping, which will default to selecting
  70. * table 0 for luminance (or primary) components, 1 for chrominance components.
  71. * You must use -qslots if you want a different component->table mapping.
  72. */
  73. {
  74. FILE * fp;
  75. int tblno, i, termchar;
  76. long val;
  77. unsigned int table[DCTSIZE2];
  78. if ((fp = fopen(filename, "r")) == NULL) {
  79. fprintf(stderr, "Can't open table file %s\n", filename);
  80. return FALSE;
  81. }
  82. tblno = 0;
  83. while (read_text_integer(fp, &val, &termchar)) { /* read 1st element of table */
  84. if (tblno >= NUM_QUANT_TBLS) {
  85. fprintf(stderr, "Too many tables in file %s\n", filename);
  86. fclose(fp);
  87. return FALSE;
  88. }
  89. table[0] = (unsigned int) val;
  90. for (i = 1; i < DCTSIZE2; i++) {
  91. if (! read_text_integer(fp, &val, &termchar)) {
  92. fprintf(stderr, "Invalid table data in file %s\n", filename);
  93. fclose(fp);
  94. return FALSE;
  95. }
  96. table[i] = (unsigned int) val;
  97. }
  98. jpeg_add_quant_table(cinfo, tblno, table, scale_factor, force_baseline);
  99. tblno++;
  100. }
  101. if (termchar != EOF) {
  102. fprintf(stderr, "Non-numeric data in file %s\n", filename);
  103. fclose(fp);
  104. return FALSE;
  105. }
  106. fclose(fp);
  107. return TRUE;
  108. }
  109. #ifdef C_MULTISCAN_FILES_SUPPORTED
  110. LOCAL(boolean)
  111. read_scan_integer (FILE * file, long * result, int * termchar)
  112. /* Variant of read_text_integer that always looks for a non-space termchar;
  113. * this simplifies parsing of punctuation in scan scripts.
  114. */
  115. {
  116. register int ch;
  117. if (! read_text_integer(file, result, termchar))
  118. return FALSE;
  119. ch = *termchar;
  120. while (ch != EOF && isspace(ch))
  121. ch = text_getc(file);
  122. if (isdigit(ch)) { /* oops, put it back */
  123. if (ungetc(ch, file) == EOF)
  124. return FALSE;
  125. ch = ' ';
  126. } else {
  127. /* Any separators other than ';' and ':' are ignored;
  128. * this allows user to insert commas, etc, if desired.
  129. */
  130. if (ch != EOF && ch != ';' && ch != ':')
  131. ch = ' ';
  132. }
  133. *termchar = ch;
  134. return TRUE;
  135. }
  136. GLOBAL(boolean)
  137. read_scan_script (j_compress_ptr cinfo, char * filename)
  138. /* Read a scan script from the specified text file.
  139. * Each entry in the file defines one scan to be emitted.
  140. * Entries are separated by semicolons ';'.
  141. * An entry contains one to four component indexes,
  142. * optionally followed by a colon ':' and four progressive-JPEG parameters.
  143. * The component indexes denote which component(s) are to be transmitted
  144. * in the current scan. The first component has index 0.
  145. * Sequential JPEG is used if the progressive-JPEG parameters are omitted.
  146. * The file is free format text: any whitespace may appear between numbers
  147. * and the ':' and ';' punctuation marks. Also, other punctuation (such
  148. * as commas or dashes) can be placed between numbers if desired.
  149. * Comments preceded by '#' may be included in the file.
  150. * Note: we do very little validity checking here;
  151. * jcmaster.c will validate the script parameters.
  152. */
  153. {
  154. FILE * fp;
  155. int scanno, ncomps, termchar;
  156. long val;
  157. jpeg_scan_info * scanptr;
  158. #define MAX_SCANS 100 /* quite arbitrary limit */
  159. jpeg_scan_info scans[MAX_SCANS];
  160. if ((fp = fopen(filename, "r")) == NULL) {
  161. fprintf(stderr, "Can't open scan definition file %s\n", filename);
  162. return FALSE;
  163. }
  164. scanptr = scans;
  165. scanno = 0;
  166. while (read_scan_integer(fp, &val, &termchar)) {
  167. if (scanno >= MAX_SCANS) {
  168. fprintf(stderr, "Too many scans defined in file %s\n", filename);
  169. fclose(fp);
  170. return FALSE;
  171. }
  172. scanptr->component_index[0] = (int) val;
  173. ncomps = 1;
  174. while (termchar == ' ') {
  175. if (ncomps >= MAX_COMPS_IN_SCAN) {
  176. fprintf(stderr, "Too many components in one scan in file %s\n",
  177. filename);
  178. fclose(fp);
  179. return FALSE;
  180. }
  181. if (! read_scan_integer(fp, &val, &termchar))
  182. goto bogus;
  183. scanptr->component_index[ncomps] = (int) val;
  184. ncomps++;
  185. }
  186. scanptr->comps_in_scan = ncomps;
  187. if (termchar == ':') {
  188. if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
  189. goto bogus;
  190. scanptr->Ss = (int) val;
  191. if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
  192. goto bogus;
  193. scanptr->Se = (int) val;
  194. if (! read_scan_integer(fp, &val, &termchar) || termchar != ' ')
  195. goto bogus;
  196. scanptr->Ah = (int) val;
  197. if (! read_scan_integer(fp, &val, &termchar))
  198. goto bogus;
  199. scanptr->Al = (int) val;
  200. } else {
  201. /* set non-progressive parameters */
  202. scanptr->Ss = 0;
  203. scanptr->Se = DCTSIZE2-1;
  204. scanptr->Ah = 0;
  205. scanptr->Al = 0;
  206. }
  207. if (termchar != ';' && termchar != EOF) {
  208. bogus:
  209. fprintf(stderr, "Invalid scan entry format in file %s\n", filename);
  210. fclose(fp);
  211. return FALSE;
  212. }
  213. scanptr++, scanno++;
  214. }
  215. if (termchar != EOF) {
  216. fprintf(stderr, "Non-numeric data in file %s\n", filename);
  217. fclose(fp);
  218. return FALSE;
  219. }
  220. if (scanno > 0) {
  221. /* Stash completed scan list in cinfo structure.
  222. * NOTE: for cjpeg's use, JPOOL_IMAGE is the right lifetime for this data,
  223. * but if you want to compress multiple images you'd want JPOOL_PERMANENT.
  224. */
  225. scanptr = (jpeg_scan_info *)
  226. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  227. scanno * SIZEOF(jpeg_scan_info));
  228. MEMCOPY(scanptr, scans, scanno * SIZEOF(jpeg_scan_info));
  229. cinfo->scan_info = scanptr;
  230. cinfo->num_scans = scanno;
  231. }
  232. fclose(fp);
  233. return TRUE;
  234. }
  235. #endif /* C_MULTISCAN_FILES_SUPPORTED */
  236. GLOBAL(boolean)
  237. set_quant_slots (j_compress_ptr cinfo, char *arg)
  238. /* Process a quantization-table-selectors parameter string, of the form
  239. * N[,N,...]
  240. * If there are more components than parameters, the last value is replicated.
  241. */
  242. {
  243. int val = 0; /* default table # */
  244. int ci;
  245. char ch;
  246. for (ci = 0; ci < MAX_COMPONENTS; ci++) {
  247. if (*arg) {
  248. ch = ','; /* if not set by sscanf, will be ',' */
  249. if (sscanf(arg, "%d%c", &val, &ch) < 1)
  250. return FALSE;
  251. if (ch != ',') /* syntax check */
  252. return FALSE;
  253. if (val < 0 || val >= NUM_QUANT_TBLS) {
  254. fprintf(stderr, "JPEG quantization tables are numbered 0..%d\n",
  255. NUM_QUANT_TBLS-1);
  256. return FALSE;
  257. }
  258. cinfo->comp_info[ci].quant_tbl_no = val;
  259. while (*arg && *arg++ != ',') /* advance to next segment of arg string */
  260. ;
  261. } else {
  262. /* reached end of parameter, set remaining components to last table */
  263. cinfo->comp_info[ci].quant_tbl_no = val;
  264. }
  265. }
  266. return TRUE;
  267. }
  268. GLOBAL(boolean)
  269. set_sample_factors (j_compress_ptr cinfo, char *arg)
  270. /* Process a sample-factors parameter string, of the form
  271. * HxV[,HxV,...]
  272. * If there are more components than parameters, "1x1" is assumed for the rest.
  273. */
  274. {
  275. int ci, val1, val2;
  276. char ch1, ch2;
  277. for (ci = 0; ci < MAX_COMPONENTS; ci++) {
  278. if (*arg) {
  279. ch2 = ','; /* if not set by sscanf, will be ',' */
  280. if (sscanf(arg, "%d%c%d%c", &val1, &ch1, &val2, &ch2) < 3)
  281. return FALSE;
  282. if ((ch1 != 'x' && ch1 != 'X') || ch2 != ',') /* syntax check */
  283. return FALSE;
  284. if (val1 <= 0 || val1 > 4 || val2 <= 0 || val2 > 4) {
  285. fprintf(stderr, "JPEG sampling factors must be 1..4\n");
  286. return FALSE;
  287. }
  288. cinfo->comp_info[ci].h_samp_factor = val1;
  289. cinfo->comp_info[ci].v_samp_factor = val2;
  290. while (*arg && *arg++ != ',') /* advance to next segment of arg string */
  291. ;
  292. } else {
  293. /* reached end of parameter, set remaining components to 1x1 sampling */
  294. cinfo->comp_info[ci].h_samp_factor = 1;
  295. cinfo->comp_info[ci].v_samp_factor = 1;
  296. }
  297. }
  298. return TRUE;
  299. }