Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

365 lines
10 KiB

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