rdswitch.c 13 KB

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