rdswitch.c 11 KB

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