Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
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.

416 lines
14 KiB

3 years ago
  1. unit imjdinput;
  2. { Original: jdinput.c ; Copyright (C) 1991-1997, Thomas G. Lane. }
  3. { This file is part of the Independent JPEG Group's software.
  4. For conditions of distribution and use, see the accompanying README file.
  5. This file contains input control logic for the JPEG decompressor.
  6. These routines are concerned with controlling the decompressor's input
  7. processing (marker reading and coefficient decoding). The actual input
  8. reading is done in jdmarker.c, jdhuff.c, and jdphuff.c. }
  9. interface
  10. {$I imjconfig.inc}
  11. uses
  12. imjmorecfg,
  13. imjpeglib,
  14. imjdeferr,
  15. imjerror,
  16. imjinclude, imjutils;
  17. { Initialize the input controller module.
  18. This is called only once, when the decompression object is created. }
  19. {GLOBAL}
  20. procedure jinit_input_controller (cinfo : j_decompress_ptr);
  21. implementation
  22. { Private state }
  23. type
  24. my_inputctl_ptr = ^my_input_controller;
  25. my_input_controller = record
  26. pub : jpeg_input_controller; { public fields }
  27. inheaders : boolean; { TRUE until first SOS is reached }
  28. end; {my_input_controller;}
  29. { Forward declarations }
  30. {METHODDEF}
  31. function consume_markers (cinfo : j_decompress_ptr) : int; forward;
  32. { Routines to calculate various quantities related to the size of the image. }
  33. {LOCAL}
  34. procedure initial_setup (cinfo : j_decompress_ptr);
  35. { Called once, when first SOS marker is reached }
  36. var
  37. ci : int;
  38. compptr : jpeg_component_info_ptr;
  39. begin
  40. { Make sure image isn't bigger than I can handle }
  41. if (long(cinfo^.image_height) > long (JPEG_MAX_DIMENSION)) or
  42. (long(cinfo^.image_width) > long(JPEG_MAX_DIMENSION)) then
  43. ERREXIT1(j_common_ptr(cinfo), JERR_IMAGE_TOO_BIG, uInt(JPEG_MAX_DIMENSION));
  44. { For now, precision must match compiled-in value... }
  45. if (cinfo^.data_precision <> BITS_IN_JSAMPLE) then
  46. ERREXIT1(j_common_ptr(cinfo), JERR_BAD_PRECISION, cinfo^.data_precision);
  47. { Check that number of components won't exceed internal array sizes }
  48. if (cinfo^.num_components > MAX_COMPONENTS) then
  49. ERREXIT2(j_common_ptr(cinfo), JERR_COMPONENT_COUNT, cinfo^.num_components,
  50. MAX_COMPONENTS);
  51. { Compute maximum sampling factors; check factor validity }
  52. cinfo^.max_h_samp_factor := 1;
  53. cinfo^.max_v_samp_factor := 1;
  54. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  55. for ci := 0 to pred(cinfo^.num_components) do
  56. begin
  57. if (compptr^.h_samp_factor<=0) or (compptr^.h_samp_factor>MAX_SAMP_FACTOR) or
  58. (compptr^.v_samp_factor<=0) or (compptr^.v_samp_factor>MAX_SAMP_FACTOR) then
  59. ERREXIT(j_common_ptr(cinfo), JERR_BAD_SAMPLING);
  60. {cinfo^.max_h_samp_factor := MAX(cinfo^.max_h_samp_factor,
  61. compptr^.h_samp_factor);
  62. cinfo^.max_v_samp_factor := MAX(cinfo^.max_v_samp_factor,
  63. compptr^.v_samp_factor);}
  64. if cinfo^.max_h_samp_factor < compptr^.h_samp_factor then
  65. cinfo^.max_h_samp_factor := compptr^.h_samp_factor;
  66. if cinfo^.max_v_samp_factor < compptr^.v_samp_factor then
  67. cinfo^.max_v_samp_factor := compptr^.v_samp_factor;
  68. Inc(compptr);
  69. end;
  70. { We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
  71. In the full decompressor, this will be overridden by jdmaster.c;
  72. but in the transcoder, jdmaster.c is not used, so we must do it here. }
  73. cinfo^.min_DCT_scaled_size := DCTSIZE;
  74. { Compute dimensions of components }
  75. compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  76. for ci := 0 to pred(cinfo^.num_components) do
  77. begin
  78. compptr^.DCT_scaled_size := DCTSIZE;
  79. { Size in DCT blocks }
  80. compptr^.width_in_blocks := JDIMENSION(
  81. jdiv_round_up( long(cinfo^.image_width) * long(compptr^.h_samp_factor),
  82. long(cinfo^.max_h_samp_factor * DCTSIZE)) );
  83. compptr^.height_in_blocks := JDIMENSION (
  84. jdiv_round_up(long (cinfo^.image_height) * long(compptr^.v_samp_factor),
  85. long (cinfo^.max_v_samp_factor * DCTSIZE)) );
  86. { downsampled_width and downsampled_height will also be overridden by
  87. jdmaster.c if we are doing full decompression. The transcoder library
  88. doesn't use these values, but the calling application might. }
  89. { Size in samples }
  90. compptr^.downsampled_width := JDIMENSION (
  91. jdiv_round_up(long (cinfo^.image_width) * long(compptr^.h_samp_factor),
  92. long (cinfo^.max_h_samp_factor)) );
  93. compptr^.downsampled_height := JDIMENSION (
  94. jdiv_round_up(long (cinfo^.image_height) * long(compptr^.v_samp_factor),
  95. long (cinfo^.max_v_samp_factor)) );
  96. { Mark component needed, until color conversion says otherwise }
  97. compptr^.component_needed := TRUE;
  98. { Mark no quantization table yet saved for component }
  99. compptr^.quant_table := NIL;
  100. Inc(compptr);
  101. end;
  102. { Compute number of fully interleaved MCU rows. }
  103. cinfo^.total_iMCU_rows := JDIMENSION(
  104. jdiv_round_up(long(cinfo^.image_height),
  105. long(cinfo^.max_v_samp_factor*DCTSIZE)) );
  106. { Decide whether file contains multiple scans }
  107. if (cinfo^.comps_in_scan < cinfo^.num_components) or
  108. (cinfo^.progressive_mode) then
  109. cinfo^.inputctl^.has_multiple_scans := TRUE
  110. else
  111. cinfo^.inputctl^.has_multiple_scans := FALSE;
  112. end;
  113. {LOCAL}
  114. procedure per_scan_setup (cinfo : j_decompress_ptr);
  115. { Do computations that are needed before processing a JPEG scan }
  116. { cinfo^.comps_in_scan and cinfo^.cur_comp_info[] were set from SOS marker }
  117. var
  118. ci, mcublks, tmp : int;
  119. compptr : jpeg_component_info_ptr;
  120. begin
  121. if (cinfo^.comps_in_scan = 1) then
  122. begin
  123. { Noninterleaved (single-component) scan }
  124. compptr := cinfo^.cur_comp_info[0];
  125. { Overall image size in MCUs }
  126. cinfo^.MCUs_per_row := compptr^.width_in_blocks;
  127. cinfo^.MCU_rows_in_scan := compptr^.height_in_blocks;
  128. { For noninterleaved scan, always one block per MCU }
  129. compptr^.MCU_width := 1;
  130. compptr^.MCU_height := 1;
  131. compptr^.MCU_blocks := 1;
  132. compptr^.MCU_sample_width := compptr^.DCT_scaled_size;
  133. compptr^.last_col_width := 1;
  134. { For noninterleaved scans, it is convenient to define last_row_height
  135. as the number of block rows present in the last iMCU row. }
  136. tmp := int (LongInt(compptr^.height_in_blocks) mod compptr^.v_samp_factor);
  137. if (tmp = 0) then
  138. tmp := compptr^.v_samp_factor;
  139. compptr^.last_row_height := tmp;
  140. { Prepare array describing MCU composition }
  141. cinfo^.blocks_in_MCU := 1;
  142. cinfo^.MCU_membership[0] := 0;
  143. end
  144. else
  145. begin
  146. { Interleaved (multi-component) scan }
  147. if (cinfo^.comps_in_scan <= 0) or (cinfo^.comps_in_scan > MAX_COMPS_IN_SCAN) then
  148. ERREXIT2(j_common_ptr(cinfo), JERR_COMPONENT_COUNT, cinfo^.comps_in_scan,
  149. MAX_COMPS_IN_SCAN);
  150. { Overall image size in MCUs }
  151. cinfo^.MCUs_per_row := JDIMENSION (
  152. jdiv_round_up(long (cinfo^.image_width),
  153. long (cinfo^.max_h_samp_factor*DCTSIZE)) );
  154. cinfo^.MCU_rows_in_scan := JDIMENSION (
  155. jdiv_round_up(long (cinfo^.image_height),
  156. long (cinfo^.max_v_samp_factor*DCTSIZE)) );
  157. cinfo^.blocks_in_MCU := 0;
  158. for ci := 0 to pred(cinfo^.comps_in_scan) do
  159. begin
  160. compptr := cinfo^.cur_comp_info[ci];
  161. { Sampling factors give # of blocks of component in each MCU }
  162. compptr^.MCU_width := compptr^.h_samp_factor;
  163. compptr^.MCU_height := compptr^.v_samp_factor;
  164. compptr^.MCU_blocks := compptr^.MCU_width * compptr^.MCU_height;
  165. compptr^.MCU_sample_width := compptr^.MCU_width * compptr^.DCT_scaled_size;
  166. { Figure number of non-dummy blocks in last MCU column & row }
  167. tmp := int (LongInt(compptr^.width_in_blocks) mod compptr^.MCU_width);
  168. if (tmp = 0) then
  169. tmp := compptr^.MCU_width;
  170. compptr^.last_col_width := tmp;
  171. tmp := int (LongInt(compptr^.height_in_blocks) mod compptr^.MCU_height);
  172. if (tmp = 0) then
  173. tmp := compptr^.MCU_height;
  174. compptr^.last_row_height := tmp;
  175. { Prepare array describing MCU composition }
  176. mcublks := compptr^.MCU_blocks;
  177. if (LongInt(cinfo^.blocks_in_MCU) + mcublks > D_MAX_BLOCKS_IN_MCU) then
  178. ERREXIT(j_common_ptr(cinfo), JERR_BAD_MCU_SIZE);
  179. while (mcublks > 0) do
  180. begin
  181. Dec(mcublks);
  182. cinfo^.MCU_membership[cinfo^.blocks_in_MCU] := ci;
  183. Inc(cinfo^.blocks_in_MCU);
  184. end;
  185. end;
  186. end;
  187. end;
  188. { Save away a copy of the Q-table referenced by each component present
  189. in the current scan, unless already saved during a prior scan.
  190. In a multiple-scan JPEG file, the encoder could assign different components
  191. the same Q-table slot number, but change table definitions between scans
  192. so that each component uses a different Q-table. (The IJG encoder is not
  193. currently capable of doing this, but other encoders might.) Since we want
  194. to be able to dequantize all the components at the end of the file, this
  195. means that we have to save away the table actually used for each component.
  196. We do this by copying the table at the start of the first scan containing
  197. the component.
  198. The JPEG spec prohibits the encoder from changing the contents of a Q-table
  199. slot between scans of a component using that slot. If the encoder does so
  200. anyway, this decoder will simply use the Q-table values that were current
  201. at the start of the first scan for the component.
  202. The decompressor output side looks only at the saved quant tables,
  203. not at the current Q-table slots. }
  204. {LOCAL}
  205. procedure latch_quant_tables (cinfo : j_decompress_ptr);
  206. var
  207. ci, qtblno : int;
  208. compptr : jpeg_component_info_ptr;
  209. qtbl : JQUANT_TBL_PTR;
  210. begin
  211. for ci := 0 to pred(cinfo^.comps_in_scan) do
  212. begin
  213. compptr := cinfo^.cur_comp_info[ci];
  214. { No work if we already saved Q-table for this component }
  215. if (compptr^.quant_table <> NIL) then
  216. continue;
  217. { Make sure specified quantization table is present }
  218. qtblno := compptr^.quant_tbl_no;
  219. if (qtblno < 0) or (qtblno >= NUM_QUANT_TBLS) or
  220. (cinfo^.quant_tbl_ptrs[qtblno] = NIL) then
  221. ERREXIT1(j_common_ptr(cinfo), JERR_NO_QUANT_TABLE, qtblno);
  222. { OK, save away the quantization table }
  223. qtbl := JQUANT_TBL_PTR(
  224. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  225. SIZEOF(JQUANT_TBL)) );
  226. MEMCOPY(qtbl, cinfo^.quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
  227. compptr^.quant_table := qtbl;
  228. end;
  229. end;
  230. { Initialize the input modules to read a scan of compressed data.
  231. The first call to this is done by jdmaster.c after initializing
  232. the entire decompressor (during jpeg_start_decompress).
  233. Subsequent calls come from consume_markers, below. }
  234. {METHODDEF}
  235. procedure start_input_pass (cinfo : j_decompress_ptr);
  236. begin
  237. per_scan_setup(cinfo);
  238. latch_quant_tables(cinfo);
  239. cinfo^.entropy^.start_pass (cinfo);
  240. cinfo^.coef^.start_input_pass (cinfo);
  241. cinfo^.inputctl^.consume_input := cinfo^.coef^.consume_data;
  242. end;
  243. { Finish up after inputting a compressed-data scan.
  244. This is called by the coefficient controller after it's read all
  245. the expected data of the scan. }
  246. {METHODDEF}
  247. procedure finish_input_pass (cinfo : j_decompress_ptr);
  248. begin
  249. cinfo^.inputctl^.consume_input := consume_markers;
  250. end;
  251. { Read JPEG markers before, between, or after compressed-data scans.
  252. Change state as necessary when a new scan is reached.
  253. Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  254. The consume_input method pointer points either here or to the
  255. coefficient controller's consume_data routine, depending on whether
  256. we are reading a compressed data segment or inter-segment markers. }
  257. {METHODDEF}
  258. function consume_markers (cinfo : j_decompress_ptr) : int;
  259. var
  260. val : int;
  261. inputctl : my_inputctl_ptr;
  262. begin
  263. inputctl := my_inputctl_ptr (cinfo^.inputctl);
  264. if (inputctl^.pub.eoi_reached) then { After hitting EOI, read no further }
  265. begin
  266. consume_markers := JPEG_REACHED_EOI;
  267. exit;
  268. end;
  269. val := cinfo^.marker^.read_markers (cinfo);
  270. case (val) of
  271. JPEG_REACHED_SOS: { Found SOS }
  272. begin
  273. if (inputctl^.inheaders) then
  274. begin { 1st SOS }
  275. initial_setup(cinfo);
  276. inputctl^.inheaders := FALSE;
  277. { Note: start_input_pass must be called by jdmaster.c
  278. before any more input can be consumed. jdapimin.c is
  279. responsible for enforcing this sequencing. }
  280. end
  281. else
  282. begin { 2nd or later SOS marker }
  283. if (not inputctl^.pub.has_multiple_scans) then
  284. ERREXIT(j_common_ptr(cinfo), JERR_EOI_EXPECTED); { Oops, I wasn't expecting this! }
  285. start_input_pass(cinfo);
  286. end;
  287. end;
  288. JPEG_REACHED_EOI: { Found EOI }
  289. begin
  290. inputctl^.pub.eoi_reached := TRUE;
  291. if (inputctl^.inheaders) then
  292. begin { Tables-only datastream, apparently }
  293. if (cinfo^.marker^.saw_SOF) then
  294. ERREXIT(j_common_ptr(cinfo), JERR_SOF_NO_SOS);
  295. end
  296. else
  297. begin
  298. { Prevent infinite loop in coef ctlr's decompress_data routine
  299. if user set output_scan_number larger than number of scans. }
  300. if (cinfo^.output_scan_number > cinfo^.input_scan_number) then
  301. cinfo^.output_scan_number := cinfo^.input_scan_number;
  302. end;
  303. end;
  304. JPEG_SUSPENDED:;
  305. end;
  306. consume_markers := val;
  307. end;
  308. { Reset state to begin a fresh datastream. }
  309. {METHODDEF}
  310. procedure reset_input_controller (cinfo : j_decompress_ptr);
  311. var
  312. inputctl : my_inputctl_ptr;
  313. begin
  314. inputctl := my_inputctl_ptr (cinfo^.inputctl);
  315. inputctl^.pub.consume_input := consume_markers;
  316. inputctl^.pub.has_multiple_scans := FALSE; { "unknown" would be better }
  317. inputctl^.pub.eoi_reached := FALSE;
  318. inputctl^.inheaders := TRUE;
  319. { Reset other modules }
  320. cinfo^.err^.reset_error_mgr (j_common_ptr(cinfo));
  321. cinfo^.marker^.reset_marker_reader (cinfo);
  322. { Reset progression state -- would be cleaner if entropy decoder did this }
  323. cinfo^.coef_bits := NIL;
  324. end;
  325. { Initialize the input controller module.
  326. This is called only once, when the decompression object is created. }
  327. {GLOBAL}
  328. procedure jinit_input_controller (cinfo : j_decompress_ptr);
  329. var
  330. inputctl : my_inputctl_ptr;
  331. begin
  332. { Create subobject in permanent pool }
  333. inputctl := my_inputctl_ptr(
  334. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_PERMANENT,
  335. SIZEOF(my_input_controller)) );
  336. cinfo^.inputctl := jpeg_input_controller_ptr(inputctl);
  337. { Initialize method pointers }
  338. inputctl^.pub.consume_input := consume_markers;
  339. inputctl^.pub.reset_input_controller := reset_input_controller;
  340. inputctl^.pub.start_input_pass := start_input_pass;
  341. inputctl^.pub.finish_input_pass := finish_input_pass;
  342. { Initialize state: can't use reset_input_controller since we don't
  343. want to try to reset other modules yet. }
  344. inputctl^.pub.has_multiple_scans := FALSE; { "unknown" would be better }
  345. inputctl^.pub.eoi_reached := FALSE;
  346. inputctl^.inheaders := TRUE;
  347. end;
  348. end.