rdswitch.c 12 KB

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