Audio plugin host https://kx.studio/carla
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.

jdapimin.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * jdapimin.c
  3. *
  4. * Copyright (C) 1994-1998, 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 application interface code for the decompression half
  9. * of the JPEG library. These are the "minimum" API routines that may be
  10. * needed in either the normal full-decompression case or the
  11. * transcoding-only case.
  12. *
  13. * Most of the routines intended to be called directly by an application
  14. * are in this file or in jdapistd.c. But also see jcomapi.c for routines
  15. * shared by compression and decompression, and jdtrans.c for the transcoding
  16. * case.
  17. */
  18. #define JPEG_INTERNALS
  19. #include "jinclude.h"
  20. #include "jpeglib.h"
  21. /*
  22. * Initialization of a JPEG decompression object.
  23. * The error manager must already be set up (in case memory manager fails).
  24. */
  25. GLOBAL(void)
  26. jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
  27. {
  28. int i;
  29. /* Guard against version mismatches between library and caller. */
  30. cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
  31. if (version != JPEG_LIB_VERSION)
  32. ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
  33. if (structsize != SIZEOF(struct jpeg_decompress_struct))
  34. ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
  35. (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
  36. /* For debugging purposes, we zero the whole master structure.
  37. * But the application has already set the err pointer, and may have set
  38. * client_data, so we have to save and restore those fields.
  39. * Note: if application hasn't set client_data, tools like Purify may
  40. * complain here.
  41. */
  42. {
  43. struct jpeg_error_mgr * err = cinfo->err;
  44. void * client_data = cinfo->client_data; /* ignore Purify complaint here */
  45. MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
  46. cinfo->err = err;
  47. cinfo->client_data = client_data;
  48. }
  49. cinfo->is_decompressor = TRUE;
  50. /* Initialize a memory manager instance for this object */
  51. jinit_memory_mgr((j_common_ptr) cinfo);
  52. /* Zero out pointers to permanent structures. */
  53. cinfo->progress = NULL;
  54. cinfo->src = NULL;
  55. for (i = 0; i < NUM_QUANT_TBLS; i++)
  56. cinfo->quant_tbl_ptrs[i] = NULL;
  57. for (i = 0; i < NUM_HUFF_TBLS; i++) {
  58. cinfo->dc_huff_tbl_ptrs[i] = NULL;
  59. cinfo->ac_huff_tbl_ptrs[i] = NULL;
  60. }
  61. /* Initialize marker processor so application can override methods
  62. * for COM, APPn markers before calling jpeg_read_header.
  63. */
  64. cinfo->marker_list = NULL;
  65. jinit_marker_reader(cinfo);
  66. /* And initialize the overall input controller. */
  67. jinit_input_controller(cinfo);
  68. /* OK, I'm ready */
  69. cinfo->global_state = DSTATE_START;
  70. }
  71. /*
  72. * Destruction of a JPEG decompression object
  73. */
  74. GLOBAL(void)
  75. jpeg_destroy_decompress (j_decompress_ptr cinfo)
  76. {
  77. jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
  78. }
  79. /*
  80. * Abort processing of a JPEG decompression operation,
  81. * but don't destroy the object itself.
  82. */
  83. GLOBAL(void)
  84. jpeg_abort_decompress (j_decompress_ptr cinfo)
  85. {
  86. jpeg_abort((j_common_ptr) cinfo); /* use common routine */
  87. }
  88. /*
  89. * Set default decompression parameters.
  90. */
  91. LOCAL(void)
  92. default_decompress_parms (j_decompress_ptr cinfo)
  93. {
  94. /* Guess the input colorspace, and set output colorspace accordingly. */
  95. /* (Wish JPEG committee had provided a real way to specify this...) */
  96. /* Note application may override our guesses. */
  97. switch (cinfo->num_components) {
  98. case 1:
  99. cinfo->jpeg_color_space = JCS_GRAYSCALE;
  100. cinfo->out_color_space = JCS_GRAYSCALE;
  101. break;
  102. case 3:
  103. if (cinfo->saw_JFIF_marker) {
  104. cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
  105. } else if (cinfo->saw_Adobe_marker) {
  106. switch (cinfo->Adobe_transform) {
  107. case 0:
  108. cinfo->jpeg_color_space = JCS_RGB;
  109. break;
  110. case 1:
  111. cinfo->jpeg_color_space = JCS_YCbCr;
  112. break;
  113. default:
  114. WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  115. cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  116. break;
  117. }
  118. } else {
  119. /* Saw no special markers, try to guess from the component IDs */
  120. int cid0 = cinfo->comp_info[0].component_id;
  121. int cid1 = cinfo->comp_info[1].component_id;
  122. int cid2 = cinfo->comp_info[2].component_id;
  123. if (cid0 == 1 && cid1 == 2 && cid2 == 3)
  124. cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
  125. else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
  126. cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
  127. else {
  128. TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
  129. cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  130. }
  131. }
  132. /* Always guess RGB is proper output colorspace. */
  133. cinfo->out_color_space = JCS_RGB;
  134. break;
  135. case 4:
  136. if (cinfo->saw_Adobe_marker) {
  137. switch (cinfo->Adobe_transform) {
  138. case 0:
  139. cinfo->jpeg_color_space = JCS_CMYK;
  140. break;
  141. case 2:
  142. cinfo->jpeg_color_space = JCS_YCCK;
  143. break;
  144. default:
  145. WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  146. cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
  147. break;
  148. }
  149. } else {
  150. /* No special markers, assume straight CMYK. */
  151. cinfo->jpeg_color_space = JCS_CMYK;
  152. }
  153. cinfo->out_color_space = JCS_CMYK;
  154. break;
  155. default:
  156. cinfo->jpeg_color_space = JCS_UNKNOWN;
  157. cinfo->out_color_space = JCS_UNKNOWN;
  158. break;
  159. }
  160. /* Set defaults for other decompression parameters. */
  161. cinfo->scale_num = 1; /* 1:1 scaling */
  162. cinfo->scale_denom = 1;
  163. cinfo->output_gamma = 1.0;
  164. cinfo->buffered_image = FALSE;
  165. cinfo->raw_data_out = FALSE;
  166. cinfo->dct_method = JDCT_DEFAULT;
  167. cinfo->do_fancy_upsampling = TRUE;
  168. cinfo->do_block_smoothing = TRUE;
  169. cinfo->quantize_colors = FALSE;
  170. /* We set these in case application only sets quantize_colors. */
  171. cinfo->dither_mode = JDITHER_FS;
  172. #ifdef QUANT_2PASS_SUPPORTED
  173. cinfo->two_pass_quantize = TRUE;
  174. #else
  175. cinfo->two_pass_quantize = FALSE;
  176. #endif
  177. cinfo->desired_number_of_colors = 256;
  178. cinfo->colormap = NULL;
  179. /* Initialize for no mode change in buffered-image mode. */
  180. cinfo->enable_1pass_quant = FALSE;
  181. cinfo->enable_external_quant = FALSE;
  182. cinfo->enable_2pass_quant = FALSE;
  183. }
  184. /*
  185. * Decompression startup: read start of JPEG datastream to see what's there.
  186. * Need only initialize JPEG object and supply a data source before calling.
  187. *
  188. * This routine will read as far as the first SOS marker (ie, actual start of
  189. * compressed data), and will save all tables and parameters in the JPEG
  190. * object. It will also initialize the decompression parameters to default
  191. * values, and finally return JPEG_HEADER_OK. On return, the application may
  192. * adjust the decompression parameters and then call jpeg_start_decompress.
  193. * (Or, if the application only wanted to determine the image parameters,
  194. * the data need not be decompressed. In that case, call jpeg_abort or
  195. * jpeg_destroy to release any temporary space.)
  196. * If an abbreviated (tables only) datastream is presented, the routine will
  197. * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
  198. * re-use the JPEG object to read the abbreviated image datastream(s).
  199. * It is unnecessary (but OK) to call jpeg_abort in this case.
  200. * The JPEG_SUSPENDED return code only occurs if the data source module
  201. * requests suspension of the decompressor. In this case the application
  202. * should load more source data and then re-call jpeg_read_header to resume
  203. * processing.
  204. * If a non-suspending data source is used and require_image is TRUE, then the
  205. * return code need not be inspected since only JPEG_HEADER_OK is possible.
  206. *
  207. * This routine is now just a front end to jpeg_consume_input, with some
  208. * extra error checking.
  209. */
  210. GLOBAL(int)
  211. jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
  212. {
  213. int retcode;
  214. if (cinfo->global_state != DSTATE_START &&
  215. cinfo->global_state != DSTATE_INHEADER)
  216. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  217. retcode = jpeg_consume_input(cinfo);
  218. switch (retcode) {
  219. case JPEG_REACHED_SOS:
  220. retcode = JPEG_HEADER_OK;
  221. break;
  222. case JPEG_REACHED_EOI:
  223. if (require_image) /* Complain if application wanted an image */
  224. ERREXIT(cinfo, JERR_NO_IMAGE);
  225. /* Reset to start state; it would be safer to require the application to
  226. * call jpeg_abort, but we can't change it now for compatibility reasons.
  227. * A side effect is to free any temporary memory (there shouldn't be any).
  228. */
  229. jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
  230. retcode = JPEG_HEADER_TABLES_ONLY;
  231. break;
  232. case JPEG_SUSPENDED:
  233. /* no work */
  234. break;
  235. }
  236. return retcode;
  237. }
  238. /*
  239. * Consume data in advance of what the decompressor requires.
  240. * This can be called at any time once the decompressor object has
  241. * been created and a data source has been set up.
  242. *
  243. * This routine is essentially a state machine that handles a couple
  244. * of critical state-transition actions, namely initial setup and
  245. * transition from header scanning to ready-for-start_decompress.
  246. * All the actual input is done via the input controller's consume_input
  247. * method.
  248. */
  249. GLOBAL(int)
  250. jpeg_consume_input (j_decompress_ptr cinfo)
  251. {
  252. int retcode = JPEG_SUSPENDED;
  253. /* NB: every possible DSTATE value should be listed in this switch */
  254. switch (cinfo->global_state) {
  255. case DSTATE_START:
  256. /* Start-of-datastream actions: reset appropriate modules */
  257. (*cinfo->inputctl->reset_input_controller) (cinfo);
  258. /* Initialize application's data source module */
  259. (*cinfo->src->init_source) (cinfo);
  260. cinfo->global_state = DSTATE_INHEADER;
  261. /*FALLTHROUGH*/
  262. case DSTATE_INHEADER:
  263. retcode = (*cinfo->inputctl->consume_input) (cinfo);
  264. if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
  265. /* Set up default parameters based on header data */
  266. default_decompress_parms(cinfo);
  267. /* Set global state: ready for start_decompress */
  268. cinfo->global_state = DSTATE_READY;
  269. }
  270. break;
  271. case DSTATE_READY:
  272. /* Can't advance past first SOS until start_decompress is called */
  273. retcode = JPEG_REACHED_SOS;
  274. break;
  275. case DSTATE_PRELOAD:
  276. case DSTATE_PRESCAN:
  277. case DSTATE_SCANNING:
  278. case DSTATE_RAW_OK:
  279. case DSTATE_BUFIMAGE:
  280. case DSTATE_BUFPOST:
  281. case DSTATE_STOPPING:
  282. retcode = (*cinfo->inputctl->consume_input) (cinfo);
  283. break;
  284. default:
  285. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  286. }
  287. return retcode;
  288. }
  289. /*
  290. * Have we finished reading the input file?
  291. */
  292. GLOBAL(boolean)
  293. jpeg_input_complete (j_decompress_ptr cinfo)
  294. {
  295. /* Check for valid jpeg object */
  296. if (cinfo->global_state < DSTATE_START ||
  297. cinfo->global_state > DSTATE_STOPPING)
  298. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  299. return cinfo->inputctl->eoi_reached;
  300. }
  301. /*
  302. * Is there more than one scan?
  303. */
  304. GLOBAL(boolean)
  305. jpeg_has_multiple_scans (j_decompress_ptr cinfo)
  306. {
  307. /* Only valid after jpeg_read_header completes */
  308. if (cinfo->global_state < DSTATE_READY ||
  309. cinfo->global_state > DSTATE_STOPPING)
  310. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  311. return cinfo->inputctl->has_multiple_scans;
  312. }
  313. /*
  314. * Finish JPEG decompression.
  315. *
  316. * This will normally just verify the file trailer and release temp storage.
  317. *
  318. * Returns FALSE if suspended. The return value need be inspected only if
  319. * a suspending data source is used.
  320. */
  321. GLOBAL(boolean)
  322. jpeg_finish_decompress (j_decompress_ptr cinfo)
  323. {
  324. if ((cinfo->global_state == DSTATE_SCANNING ||
  325. cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
  326. /* Terminate final pass of non-buffered mode */
  327. if (cinfo->output_scanline < cinfo->output_height)
  328. ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
  329. (*cinfo->master->finish_output_pass) (cinfo);
  330. cinfo->global_state = DSTATE_STOPPING;
  331. } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
  332. /* Finishing after a buffered-image operation */
  333. cinfo->global_state = DSTATE_STOPPING;
  334. } else if (cinfo->global_state != DSTATE_STOPPING) {
  335. /* STOPPING = repeat call after a suspension, anything else is error */
  336. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  337. }
  338. /* Read until EOI */
  339. while (! cinfo->inputctl->eoi_reached) {
  340. if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
  341. return FALSE; /* Suspend, come back later */
  342. }
  343. /* Do final cleanup */
  344. (*cinfo->src->term_source) (cinfo);
  345. /* We can use jpeg_abort to release memory and reset global_state */
  346. jpeg_abort((j_common_ptr) cinfo);
  347. return TRUE;
  348. }