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.

232 lines
6.4 KiB

3 years ago
  1. unit imjutils;
  2. { This file contains tables and miscellaneous utility routines needed
  3. for both compression and decompression.
  4. Note we prefix all global names with "j" to minimize conflicts with
  5. a surrounding application. }
  6. { Source: jutils.c; Copyright (C) 1991-1996, Thomas G. Lane. }
  7. interface
  8. {$I imjconfig.inc}
  9. uses
  10. imjmorecfg,
  11. imjinclude,
  12. imjpeglib;
  13. { jpeg_zigzag_order[i] is the zigzag-order position of the i'th element
  14. of a DCT block read in natural order (left to right, top to bottom). }
  15. {$ifdef FALSE} { This table is not actually needed in v6a }
  16. const
  17. jpeg_zigzag_order : array[0..DCTSIZE2] of int =
  18. (0, 1, 5, 6, 14, 15, 27, 28,
  19. 2, 4, 7, 13, 16, 26, 29, 42,
  20. 3, 8, 12, 17, 25, 30, 41, 43,
  21. 9, 11, 18, 24, 31, 40, 44, 53,
  22. 10, 19, 23, 32, 39, 45, 52, 54,
  23. 20, 22, 33, 38, 46, 51, 55, 60,
  24. 21, 34, 37, 47, 50, 56, 59, 61,
  25. 35, 36, 48, 49, 57, 58, 62, 63);
  26. {$endif}
  27. { jpeg_natural_order[i] is the natural-order position of the i'th element
  28. of zigzag order.
  29. When reading corrupted data, the Huffman decoders could attempt
  30. to reference an entry beyond the end of this array (if the decoded
  31. zero run length reaches past the end of the block). To prevent
  32. wild stores without adding an inner-loop test, we put some extra
  33. "63"s after the real entries. This will cause the extra coefficient
  34. to be stored in location 63 of the block, not somewhere random.
  35. The worst case would be a run-length of 15, which means we need 16
  36. fake entries. }
  37. const
  38. jpeg_natural_order : array[0..DCTSIZE2+16-1] of int =
  39. (0, 1, 8, 16, 9, 2, 3, 10,
  40. 17, 24, 32, 25, 18, 11, 4, 5,
  41. 12, 19, 26, 33, 40, 48, 41, 34,
  42. 27, 20, 13, 6, 7, 14, 21, 28,
  43. 35, 42, 49, 56, 57, 50, 43, 36,
  44. 29, 22, 15, 23, 30, 37, 44, 51,
  45. 58, 59, 52, 45, 38, 31, 39, 46,
  46. 53, 60, 61, 54, 47, 55, 62, 63,
  47. 63, 63, 63, 63, 63, 63, 63, 63, { extra entries for safety in decoder }
  48. 63, 63, 63, 63, 63, 63, 63, 63);
  49. { Arithmetic utilities }
  50. {GLOBAL}
  51. function jdiv_round_up (a : long; b : long) : long;
  52. {GLOBAL}
  53. function jround_up (a : long; b : long) : long;
  54. {GLOBAL}
  55. procedure jcopy_sample_rows (input_array : JSAMPARRAY;
  56. source_row : int;
  57. output_array : JSAMPARRAY; dest_row : int;
  58. num_rows : int; num_cols : JDIMENSION);
  59. {GLOBAL}
  60. procedure jcopy_block_row (input_row : JBLOCKROW;
  61. output_row : JBLOCKROW;
  62. num_blocks : JDIMENSION);
  63. {GLOBAL}
  64. procedure jzero_far (target : pointer;{far} bytestozero : size_t);
  65. procedure FMEMZERO(target : pointer; size : size_t);
  66. procedure FMEMCOPY(dest,src : pointer; size : size_t);
  67. implementation
  68. {GLOBAL}
  69. function jdiv_round_up (a : long; b : long) : long;
  70. { Compute a/b rounded up to next integer, ie, ceil(a/b) }
  71. { Assumes a >= 0, b > 0 }
  72. begin
  73. jdiv_round_up := (a + b - long(1)) div b;
  74. end;
  75. {GLOBAL}
  76. function jround_up (a : long; b : long) : long;
  77. { Compute a rounded up to next multiple of b, ie, ceil(a/b)*b }
  78. { Assumes a >= 0, b > 0 }
  79. begin
  80. Inc(a, b - long(1));
  81. jround_up := a - (a mod b);
  82. end;
  83. { On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays
  84. and coefficient-block arrays. This won't work on 80x86 because the arrays
  85. are FAR and we're assuming a small-pointer memory model. However, some
  86. DOS compilers provide far-pointer versions of memcpy() and memset() even
  87. in the small-model libraries. These will be used if USE_FMEM is defined.
  88. Otherwise, the routines below do it the hard way. (The performance cost
  89. is not all that great, because these routines aren't very heavily used.) }
  90. {$ifndef NEED_FAR_POINTERS} { normal case, same as regular macros }
  91. procedure FMEMZERO(target : pointer; size : size_t);
  92. begin
  93. FillChar(target^, size, 0);
  94. end;
  95. procedure FMEMCOPY(dest,src : pointer; size : size_t);
  96. begin
  97. Move(src^, dest^, size);
  98. end;
  99. {$else} { 80x86 case, define if we can }
  100. {$ifdef USE_FMEM}
  101. FMEMCOPY(dest,src,size) _fmemcpy((void FAR *)(dest), (const void FAR *)(src), (size_t)(size))
  102. FMEMZERO(target,size) _fmemset((void FAR *)(target), 0, (size_t)(size))
  103. {$endif}
  104. {$endif}
  105. {GLOBAL}
  106. procedure jcopy_sample_rows (input_array : JSAMPARRAY; source_row : int;
  107. output_array : JSAMPARRAY; dest_row : int;
  108. num_rows : int; num_cols : JDIMENSION);
  109. { Copy some rows of samples from one place to another.
  110. num_rows rows are copied from input_array[source_row++]
  111. to output_array[dest_row++]; these areas may overlap for duplication.
  112. The source and destination arrays must be at least as wide as num_cols. }
  113. var
  114. inptr, outptr : JSAMPLE_PTR; {register}
  115. {$ifdef FMEMCOPY}
  116. count : size_t; {register}
  117. {$else}
  118. count : JDIMENSION; {register}
  119. {$endif}
  120. row : int; {register}
  121. begin
  122. {$ifdef FMEMCOPY}
  123. count := size_t(num_cols * SIZEOF(JSAMPLE));
  124. {$endif}
  125. Inc(JSAMPROW_PTR(input_array), source_row);
  126. Inc(JSAMPROW_PTR(output_array), dest_row);
  127. for row := pred(num_rows) downto 0 do
  128. begin
  129. inptr := JSAMPLE_PTR(input_array^[0]);
  130. Inc(JSAMPROW_PTR(input_array));
  131. outptr := JSAMPLE_PTR(output_array^[0]);
  132. Inc(JSAMPROW_PTR(output_array));
  133. {$ifdef FMEMCOPY}
  134. FMEMCOPY(outptr, inptr, count);
  135. {$else}
  136. for count := pred(num_cols) downto 0 do
  137. begin
  138. outptr^ := inptr^; { needn't bother with GETJSAMPLE() here }
  139. Inc(inptr);
  140. Inc(outptr);
  141. end;
  142. {$endif}
  143. end;
  144. end;
  145. {GLOBAL}
  146. procedure jcopy_block_row (input_row : JBLOCKROW;
  147. output_row : JBLOCKROW;
  148. num_blocks : JDIMENSION);
  149. { Copy a row of coefficient blocks from one place to another. }
  150. {$ifdef FMEMCOPY}
  151. begin
  152. FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF)));
  153. {$else}
  154. var
  155. inptr, outptr : JCOEFPTR; {register}
  156. count : long; {register}
  157. begin
  158. inptr := JCOEFPTR (input_row);
  159. outptr := JCOEFPTR (output_row);
  160. for count := long(num_blocks) * DCTSIZE2 -1 downto 0 do
  161. begin
  162. outptr^ := inptr^;
  163. Inc(outptr);
  164. Inc(inptr);
  165. end;
  166. {$endif}
  167. end;
  168. {GLOBAL}
  169. procedure jzero_far (target : pointer;{far} bytestozero : size_t);
  170. { Zero out a chunk of FAR memory. }
  171. { This might be sample-array data, block-array data, or alloc_large data. }
  172. {$ifdef FMEMZERO}
  173. begin
  174. FMEMZERO(target, bytestozero);
  175. {$else}
  176. var
  177. ptr : byteptr;
  178. count : size_t; {register}
  179. begin
  180. ptr := target;
  181. for count := bytestozero-1 downto 0 do
  182. begin
  183. ptr^ := 0;
  184. Inc(ptr);
  185. end;
  186. {$endif}
  187. end;
  188. end.