jpegint.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * jpegint.h
  3. *
  4. * Copyright (C) 1991-1997, Thomas G. Lane.
  5. * Modified 1997-2019 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 provides common declarations for the various JPEG modules.
  10. * These declarations are considered internal to the JPEG library; most
  11. * applications using the library shouldn't need to include this file.
  12. */
  13. /* Declarations for both compression & decompression */
  14. typedef enum { /* Operating modes for buffer controllers */
  15. JBUF_PASS_THRU, /* Plain stripwise operation */
  16. /* Remaining modes require a full-image buffer to have been created */
  17. JBUF_SAVE_SOURCE, /* Run source subobject only, save output */
  18. JBUF_CRANK_DEST, /* Run dest subobject only, using saved data */
  19. JBUF_SAVE_AND_PASS /* Run both subobjects, save output */
  20. } J_BUF_MODE;
  21. /* Values of global_state field (jdapi.c has some dependencies on ordering!) */
  22. #define CSTATE_START 100 /* after create_compress */
  23. #define CSTATE_SCANNING 101 /* start_compress done, write_scanlines OK */
  24. #define CSTATE_RAW_OK 102 /* start_compress done, write_raw_data OK */
  25. #define CSTATE_WRCOEFS 103 /* jpeg_write_coefficients done */
  26. #define DSTATE_START 200 /* after create_decompress */
  27. #define DSTATE_INHEADER 201 /* reading header markers, no SOS yet */
  28. #define DSTATE_READY 202 /* found SOS, ready for start_decompress */
  29. #define DSTATE_PRELOAD 203 /* reading multiscan file in start_decompress*/
  30. #define DSTATE_PRESCAN 204 /* performing dummy pass for 2-pass quant */
  31. #define DSTATE_SCANNING 205 /* start_decompress done, read_scanlines OK */
  32. #define DSTATE_RAW_OK 206 /* start_decompress done, read_raw_data OK */
  33. #define DSTATE_BUFIMAGE 207 /* expecting jpeg_start_output */
  34. #define DSTATE_BUFPOST 208 /* looking for SOS/EOI in jpeg_finish_output */
  35. #define DSTATE_RDCOEFS 209 /* reading file in jpeg_read_coefficients */
  36. #define DSTATE_STOPPING 210 /* looking for EOI in jpeg_finish_decompress */
  37. /* Declarations for compression modules */
  38. /* Master control module */
  39. struct jpeg_comp_master {
  40. JMETHOD(void, prepare_for_pass, (j_compress_ptr cinfo));
  41. JMETHOD(void, pass_startup, (j_compress_ptr cinfo));
  42. JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
  43. /* State variables made visible to other modules */
  44. boolean call_pass_startup; /* True if pass_startup must be called */
  45. boolean is_last_pass; /* True during last pass */
  46. };
  47. /* Main buffer control (downsampled-data buffer) */
  48. struct jpeg_c_main_controller {
  49. JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
  50. JMETHOD(void, process_data, (j_compress_ptr cinfo,
  51. JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
  52. JDIMENSION in_rows_avail));
  53. };
  54. /* Compression preprocessing (downsampling input buffer control) */
  55. struct jpeg_c_prep_controller {
  56. JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
  57. JMETHOD(void, pre_process_data, (j_compress_ptr cinfo,
  58. JSAMPARRAY input_buf,
  59. JDIMENSION *in_row_ctr,
  60. JDIMENSION in_rows_avail,
  61. JSAMPIMAGE output_buf,
  62. JDIMENSION *out_row_group_ctr,
  63. JDIMENSION out_row_groups_avail));
  64. };
  65. /* Coefficient buffer control */
  66. struct jpeg_c_coef_controller {
  67. JMETHOD(void, start_pass, (j_compress_ptr cinfo, J_BUF_MODE pass_mode));
  68. JMETHOD(boolean, compress_data, (j_compress_ptr cinfo,
  69. JSAMPIMAGE input_buf));
  70. };
  71. /* Colorspace conversion */
  72. struct jpeg_color_converter {
  73. JMETHOD(void, start_pass, (j_compress_ptr cinfo));
  74. JMETHOD(void, color_convert, (j_compress_ptr cinfo,
  75. JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
  76. JDIMENSION output_row, int num_rows));
  77. };
  78. /* Downsampling */
  79. struct jpeg_downsampler {
  80. JMETHOD(void, start_pass, (j_compress_ptr cinfo));
  81. JMETHOD(void, downsample, (j_compress_ptr cinfo,
  82. JSAMPIMAGE input_buf, JDIMENSION in_row_index,
  83. JSAMPIMAGE output_buf,
  84. JDIMENSION out_row_group_index));
  85. boolean need_context_rows; /* TRUE if need rows above & below */
  86. };
  87. /* Forward DCT (also controls coefficient quantization) */
  88. typedef JMETHOD(void, forward_DCT_ptr,
  89. (j_compress_ptr cinfo, jpeg_component_info * compptr,
  90. JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
  91. JDIMENSION start_row, JDIMENSION start_col,
  92. JDIMENSION num_blocks));
  93. struct jpeg_forward_dct {
  94. JMETHOD(void, start_pass, (j_compress_ptr cinfo));
  95. /* It is useful to allow each component to have a separate FDCT method. */
  96. forward_DCT_ptr forward_DCT[MAX_COMPONENTS];
  97. };
  98. /* Entropy encoding */
  99. struct jpeg_entropy_encoder {
  100. JMETHOD(void, start_pass, (j_compress_ptr cinfo, boolean gather_statistics));
  101. JMETHOD(boolean, encode_mcu, (j_compress_ptr cinfo, JBLOCKROW *MCU_data));
  102. JMETHOD(void, finish_pass, (j_compress_ptr cinfo));
  103. };
  104. /* Marker writing */
  105. struct jpeg_marker_writer {
  106. JMETHOD(void, write_file_header, (j_compress_ptr cinfo));
  107. JMETHOD(void, write_frame_header, (j_compress_ptr cinfo));
  108. JMETHOD(void, write_scan_header, (j_compress_ptr cinfo));
  109. JMETHOD(void, write_file_trailer, (j_compress_ptr cinfo));
  110. JMETHOD(void, write_tables_only, (j_compress_ptr cinfo));
  111. /* These routines are exported to allow insertion of extra markers */
  112. /* Probably only COM and APPn markers should be written this way */
  113. JMETHOD(void, write_marker_header, (j_compress_ptr cinfo, int marker,
  114. unsigned int datalen));
  115. JMETHOD(void, write_marker_byte, (j_compress_ptr cinfo, int val));
  116. };
  117. /* Declarations for decompression modules */
  118. /* Master control module */
  119. struct jpeg_decomp_master {
  120. JMETHOD(void, prepare_for_output_pass, (j_decompress_ptr cinfo));
  121. JMETHOD(void, finish_output_pass, (j_decompress_ptr cinfo));
  122. /* State variables made visible to other modules */
  123. boolean is_dummy_pass; /* True during 1st pass for 2-pass quant */
  124. };
  125. /* Input control module */
  126. struct jpeg_input_controller {
  127. JMETHOD(int, consume_input, (j_decompress_ptr cinfo));
  128. JMETHOD(void, reset_input_controller, (j_decompress_ptr cinfo));
  129. JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
  130. JMETHOD(void, finish_input_pass, (j_decompress_ptr cinfo));
  131. /* State variables made visible to other modules */
  132. boolean has_multiple_scans; /* True if file has multiple scans */
  133. boolean eoi_reached; /* True when EOI has been consumed */
  134. };
  135. /* Main buffer control (downsampled-data buffer) */
  136. struct jpeg_d_main_controller {
  137. JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
  138. JMETHOD(void, process_data, (j_decompress_ptr cinfo,
  139. JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
  140. JDIMENSION out_rows_avail));
  141. };
  142. /* Coefficient buffer control */
  143. struct jpeg_d_coef_controller {
  144. JMETHOD(void, start_input_pass, (j_decompress_ptr cinfo));
  145. JMETHOD(int, consume_data, (j_decompress_ptr cinfo));
  146. JMETHOD(void, start_output_pass, (j_decompress_ptr cinfo));
  147. JMETHOD(int, decompress_data, (j_decompress_ptr cinfo,
  148. JSAMPIMAGE output_buf));
  149. /* Pointer to array of coefficient virtual arrays, or NULL if none */
  150. jvirt_barray_ptr *coef_arrays;
  151. };
  152. /* Decompression postprocessing (color quantization buffer control) */
  153. struct jpeg_d_post_controller {
  154. JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
  155. JMETHOD(void, post_process_data, (j_decompress_ptr cinfo,
  156. JSAMPIMAGE input_buf,
  157. JDIMENSION *in_row_group_ctr,
  158. JDIMENSION in_row_groups_avail,
  159. JSAMPARRAY output_buf,
  160. JDIMENSION *out_row_ctr,
  161. JDIMENSION out_rows_avail));
  162. };
  163. /* Marker reading & parsing */
  164. struct jpeg_marker_reader {
  165. JMETHOD(void, reset_marker_reader, (j_decompress_ptr cinfo));
  166. /* Read markers until SOS or EOI.
  167. * Returns same codes as are defined for jpeg_consume_input:
  168. * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  169. */
  170. JMETHOD(int, read_markers, (j_decompress_ptr cinfo));
  171. /* Read a restart marker --- exported for use by entropy decoder only */
  172. jpeg_marker_parser_method read_restart_marker;
  173. /* State of marker reader --- nominally internal, but applications
  174. * supplying COM or APPn handlers might like to know the state.
  175. */
  176. boolean saw_SOI; /* found SOI? */
  177. boolean saw_SOF; /* found SOF? */
  178. int next_restart_num; /* next restart number expected (0-7) */
  179. unsigned int discarded_bytes; /* # of bytes skipped looking for a marker */
  180. };
  181. /* Entropy decoding */
  182. struct jpeg_entropy_decoder {
  183. JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
  184. JMETHOD(boolean, decode_mcu, (j_decompress_ptr cinfo, JBLOCKROW *MCU_data));
  185. JMETHOD(void, finish_pass, (j_decompress_ptr cinfo));
  186. };
  187. /* Inverse DCT (also performs dequantization) */
  188. typedef JMETHOD(void, inverse_DCT_method_ptr,
  189. (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  190. JCOEFPTR coef_block,
  191. JSAMPARRAY output_buf, JDIMENSION output_col));
  192. struct jpeg_inverse_dct {
  193. JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
  194. /* It is useful to allow each component to have a separate IDCT method. */
  195. inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS];
  196. };
  197. /* Upsampling (note that upsampler must also call color converter) */
  198. struct jpeg_upsampler {
  199. JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
  200. JMETHOD(void, upsample, (j_decompress_ptr cinfo,
  201. JSAMPIMAGE input_buf,
  202. JDIMENSION *in_row_group_ctr,
  203. JDIMENSION in_row_groups_avail,
  204. JSAMPARRAY output_buf,
  205. JDIMENSION *out_row_ctr,
  206. JDIMENSION out_rows_avail));
  207. boolean need_context_rows; /* TRUE if need rows above & below */
  208. };
  209. /* Colorspace conversion */
  210. struct jpeg_color_deconverter {
  211. JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
  212. JMETHOD(void, color_convert, (j_decompress_ptr cinfo,
  213. JSAMPIMAGE input_buf, JDIMENSION input_row,
  214. JSAMPARRAY output_buf, int num_rows));
  215. };
  216. /* Color quantization or color precision reduction */
  217. struct jpeg_color_quantizer {
  218. JMETHOD(void, start_pass, (j_decompress_ptr cinfo, boolean is_pre_scan));
  219. JMETHOD(void, color_quantize, (j_decompress_ptr cinfo,
  220. JSAMPARRAY input_buf, JSAMPARRAY output_buf,
  221. int num_rows));
  222. JMETHOD(void, finish_pass, (j_decompress_ptr cinfo));
  223. JMETHOD(void, new_color_map, (j_decompress_ptr cinfo));
  224. };
  225. /* Definition of range extension bits for decompression processes.
  226. * See the comments with prepare_range_limit_table (in jdmaster.c)
  227. * for more info.
  228. * The recommended default value for normal applications is 2.
  229. * Applications with special requirements may use a different value.
  230. * For example, Ghostscript wants to use 3 for proper handling of
  231. * wacky images with oversize coefficient values.
  232. */
  233. #define RANGE_BITS 2
  234. #define RANGE_CENTER (CENTERJSAMPLE << RANGE_BITS)
  235. /* Miscellaneous useful macros */
  236. #undef MAX
  237. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  238. #undef MIN
  239. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  240. /* We assume that right shift corresponds to signed division by 2 with
  241. * rounding towards minus infinity. This is correct for typical "arithmetic
  242. * shift" instructions that shift in copies of the sign bit. But some
  243. * C compilers implement >> with an unsigned shift. For these machines you
  244. * must define RIGHT_SHIFT_IS_UNSIGNED.
  245. * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity.
  246. * It is only applied with constant shift counts. SHIFT_TEMPS must be
  247. * included in the variables of any routine using RIGHT_SHIFT.
  248. */
  249. #ifdef RIGHT_SHIFT_IS_UNSIGNED
  250. #define SHIFT_TEMPS INT32 shift_temp;
  251. #define RIGHT_SHIFT(x,shft) \
  252. ((shift_temp = (x)) < 0 ? \
  253. (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \
  254. (shift_temp >> (shft)))
  255. #else
  256. #define SHIFT_TEMPS
  257. #define RIGHT_SHIFT(x,shft) ((x) >> (shft))
  258. #endif
  259. /* Descale and correctly round an INT32 value that's scaled by N bits.
  260. * We assume RIGHT_SHIFT rounds towards minus infinity, so adding
  261. * the fudge factor is correct for either sign of X.
  262. */
  263. #define DESCALE(x,n) RIGHT_SHIFT((x) + ((INT32) 1 << ((n)-1)), n)
  264. /* Short forms of external names for systems with brain-damaged linkers. */
  265. #ifdef NEED_SHORT_EXTERNAL_NAMES
  266. #define jinit_compress_master jICompress
  267. #define jinit_c_master_control jICMaster
  268. #define jinit_c_main_controller jICMainC
  269. #define jinit_c_prep_controller jICPrepC
  270. #define jinit_c_coef_controller jICCoefC
  271. #define jinit_color_converter jICColor
  272. #define jinit_downsampler jIDownsampler
  273. #define jinit_forward_dct jIFDCT
  274. #define jinit_huff_encoder jIHEncoder
  275. #define jinit_arith_encoder jIAEncoder
  276. #define jinit_marker_writer jIMWriter
  277. #define jinit_master_decompress jIDMaster
  278. #define jinit_d_main_controller jIDMainC
  279. #define jinit_d_coef_controller jIDCoefC
  280. #define jinit_d_post_controller jIDPostC
  281. #define jinit_input_controller jIInCtlr
  282. #define jinit_marker_reader jIMReader
  283. #define jinit_huff_decoder jIHDecoder
  284. #define jinit_arith_decoder jIADecoder
  285. #define jinit_inverse_dct jIIDCT
  286. #define jinit_upsampler jIUpsampler
  287. #define jinit_color_deconverter jIDColor
  288. #define jinit_1pass_quantizer jI1Quant
  289. #define jinit_2pass_quantizer jI2Quant
  290. #define jinit_merged_upsampler jIMUpsampler
  291. #define jinit_memory_mgr jIMemMgr
  292. #define jdiv_round_up jDivRound
  293. #define jround_up jRound
  294. #define jzero_far jZeroFar
  295. #define jcopy_sample_rows jCopySamples
  296. #define jcopy_block_row jCopyBlocks
  297. #define jpeg_zigzag_order jZIGTable
  298. #define jpeg_natural_order jZAGTable
  299. #define jpeg_natural_order7 jZAG7Table
  300. #define jpeg_natural_order6 jZAG6Table
  301. #define jpeg_natural_order5 jZAG5Table
  302. #define jpeg_natural_order4 jZAG4Table
  303. #define jpeg_natural_order3 jZAG3Table
  304. #define jpeg_natural_order2 jZAG2Table
  305. #define jpeg_aritab jAriTab
  306. #endif /* NEED_SHORT_EXTERNAL_NAMES */
  307. /* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays
  308. * and coefficient-block arrays. This won't work on 80x86 because the arrays
  309. * are FAR and we're assuming a small-pointer memory model. However, some
  310. * DOS compilers provide far-pointer versions of memcpy() and memset() even
  311. * in the small-model libraries. These will be used if USE_FMEM is defined.
  312. * Otherwise, the routines in jutils.c do it the hard way.
  313. */
  314. #ifndef NEED_FAR_POINTERS /* normal case, same as regular macro */
  315. #define FMEMZERO(target,size) MEMZERO(target,size)
  316. #else /* 80x86 case */
  317. #ifdef USE_FMEM
  318. #define FMEMZERO(target,size) _fmemset((void FAR *)(target), 0, (size_t)(size))
  319. #else
  320. EXTERN(void) jzero_far JPP((void FAR * target, size_t bytestozero));
  321. #define FMEMZERO(target,size) jzero_far(target, size)
  322. #endif
  323. #endif
  324. /* Compression module initialization routines */
  325. EXTERN(void) jinit_compress_master JPP((j_compress_ptr cinfo));
  326. EXTERN(void) jinit_c_master_control JPP((j_compress_ptr cinfo,
  327. boolean transcode_only));
  328. EXTERN(void) jinit_c_main_controller JPP((j_compress_ptr cinfo,
  329. boolean need_full_buffer));
  330. EXTERN(void) jinit_c_prep_controller JPP((j_compress_ptr cinfo,
  331. boolean need_full_buffer));
  332. EXTERN(void) jinit_c_coef_controller JPP((j_compress_ptr cinfo,
  333. boolean need_full_buffer));
  334. EXTERN(void) jinit_color_converter JPP((j_compress_ptr cinfo));
  335. EXTERN(void) jinit_downsampler JPP((j_compress_ptr cinfo));
  336. EXTERN(void) jinit_forward_dct JPP((j_compress_ptr cinfo));
  337. EXTERN(void) jinit_huff_encoder JPP((j_compress_ptr cinfo));
  338. EXTERN(void) jinit_arith_encoder JPP((j_compress_ptr cinfo));
  339. EXTERN(void) jinit_marker_writer JPP((j_compress_ptr cinfo));
  340. /* Decompression module initialization routines */
  341. EXTERN(void) jinit_master_decompress JPP((j_decompress_ptr cinfo));
  342. EXTERN(void) jinit_d_main_controller JPP((j_decompress_ptr cinfo,
  343. boolean need_full_buffer));
  344. EXTERN(void) jinit_d_coef_controller JPP((j_decompress_ptr cinfo,
  345. boolean need_full_buffer));
  346. EXTERN(void) jinit_d_post_controller JPP((j_decompress_ptr cinfo,
  347. boolean need_full_buffer));
  348. EXTERN(void) jinit_input_controller JPP((j_decompress_ptr cinfo));
  349. EXTERN(void) jinit_marker_reader JPP((j_decompress_ptr cinfo));
  350. EXTERN(void) jinit_huff_decoder JPP((j_decompress_ptr cinfo));
  351. EXTERN(void) jinit_arith_decoder JPP((j_decompress_ptr cinfo));
  352. EXTERN(void) jinit_inverse_dct JPP((j_decompress_ptr cinfo));
  353. EXTERN(void) jinit_upsampler JPP((j_decompress_ptr cinfo));
  354. EXTERN(void) jinit_color_deconverter JPP((j_decompress_ptr cinfo));
  355. EXTERN(void) jinit_1pass_quantizer JPP((j_decompress_ptr cinfo));
  356. EXTERN(void) jinit_2pass_quantizer JPP((j_decompress_ptr cinfo));
  357. EXTERN(void) jinit_merged_upsampler JPP((j_decompress_ptr cinfo));
  358. /* Memory manager initialization */
  359. EXTERN(void) jinit_memory_mgr JPP((j_common_ptr cinfo));
  360. /* Utility routines in jutils.c */
  361. EXTERN(long) jdiv_round_up JPP((long a, long b));
  362. EXTERN(long) jround_up JPP((long a, long b));
  363. EXTERN(void) jcopy_sample_rows JPP((JSAMPARRAY input_array, int source_row,
  364. JSAMPARRAY output_array, int dest_row,
  365. int num_rows, JDIMENSION num_cols));
  366. EXTERN(void) jcopy_block_row JPP((JBLOCKROW input_row, JBLOCKROW output_row,
  367. JDIMENSION num_blocks));
  368. /* Constant tables in jutils.c */
  369. #if 0 /* This table is not actually needed in v6a */
  370. extern const int jpeg_zigzag_order[]; /* natural coef order to zigzag order */
  371. #endif
  372. extern const int jpeg_natural_order[]; /* zigzag coef order to natural order */
  373. extern const int jpeg_natural_order7[]; /* zz to natural order for 7x7 block */
  374. extern const int jpeg_natural_order6[]; /* zz to natural order for 6x6 block */
  375. extern const int jpeg_natural_order5[]; /* zz to natural order for 5x5 block */
  376. extern const int jpeg_natural_order4[]; /* zz to natural order for 4x4 block */
  377. extern const int jpeg_natural_order3[]; /* zz to natural order for 3x3 block */
  378. extern const int jpeg_natural_order2[]; /* zz to natural order for 2x2 block */
  379. /* Arithmetic coding probability estimation tables in jaricom.c */
  380. extern const INT32 jpeg_aritab[];
  381. /* Suppress undefined-structure complaints if necessary. */
  382. #ifdef INCOMPLETE_TYPES_BROKEN
  383. #ifndef AM_MEMORY_MANAGER /* only jmemmgr.c defines these */
  384. struct jvirt_sarray_control { long dummy; };
  385. struct jvirt_barray_control { long dummy; };
  386. #endif
  387. #endif /* INCOMPLETE_TYPES_BROKEN */