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.

514 lines
18 KiB

3 years ago
  1. unit imjdmerge;
  2. { This file contains code for merged upsampling/color conversion.
  3. This file combines functions from jdsample.c and jdcolor.c;
  4. read those files first to understand what's going on.
  5. When the chroma components are to be upsampled by simple replication
  6. (ie, box filtering), we can save some work in color conversion by
  7. calculating all the output pixels corresponding to a pair of chroma
  8. samples at one time. In the conversion equations
  9. R := Y + K1 * Cr
  10. G := Y + K2 * Cb + K3 * Cr
  11. B := Y + K4 * Cb
  12. only the Y term varies among the group of pixels corresponding to a pair
  13. of chroma samples, so the rest of the terms can be calculated just once.
  14. At typical sampling ratios, this eliminates half or three-quarters of the
  15. multiplications needed for color conversion.
  16. This file currently provides implementations for the following cases:
  17. YCbCr => RGB color conversion only.
  18. Sampling ratios of 2h1v or 2h2v.
  19. No scaling needed at upsample time.
  20. Corner-aligned (non-CCIR601) sampling alignment.
  21. Other special cases could be added, but in most applications these are
  22. the only common cases. (For uncommon cases we fall back on the more
  23. general code in jdsample.c and jdcolor.c.) }
  24. { Original: jdmerge.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
  25. interface
  26. {$I imjconfig.inc}
  27. uses
  28. imjmorecfg,
  29. imjinclude,
  30. imjpeglib,
  31. imjutils;
  32. { Module initialization routine for merged upsampling/color conversion.
  33. NB: this is called under the conditions determined by use_merged_upsample()
  34. in jdmaster.c. That routine MUST correspond to the actual capabilities
  35. of this module; no safety checks are made here. }
  36. {GLOBAL}
  37. procedure jinit_merged_upsampler (cinfo : j_decompress_ptr);
  38. implementation
  39. { Private subobject }
  40. type { the same definition as in JdColor }
  41. int_Color_Table = array[0..MAXJSAMPLE+1-1] of int;
  42. int_CConvertPtr = ^int_Color_Table;
  43. INT32_Color_Table = array[0..MAXJSAMPLE+1-1] of INT32;
  44. INT32_CConvertPtr = ^INT32_Color_Table;
  45. type
  46. my_upsample_ptr = ^my_upsampler;
  47. my_upsampler = record
  48. pub : jpeg_upsampler; { public fields }
  49. { Pointer to routine to do actual upsampling/conversion of one row group }
  50. upmethod : procedure (cinfo : j_decompress_ptr;
  51. input_buf : JSAMPIMAGE;
  52. in_row_group_ctr : JDIMENSION;
  53. output_buf : JSAMPARRAY);
  54. { Private state for YCC->RGB conversion }
  55. Cr_r_tab : int_CConvertPtr; { => table for Cr to R conversion }
  56. Cb_b_tab : int_CConvertPtr; { => table for Cb to B conversion }
  57. Cr_g_tab : INT32_CConvertPtr; { => table for Cr to G conversion }
  58. Cb_g_tab : INT32_CConvertPtr; { => table for Cb to G conversion }
  59. { For 2:1 vertical sampling, we produce two output rows at a time.
  60. We need a "spare" row buffer to hold the second output row if the
  61. application provides just a one-row buffer; we also use the spare
  62. to discard the dummy last row if the image height is odd. }
  63. spare_row : JSAMPROW;
  64. spare_full : boolean; { TRUE if spare buffer is occupied }
  65. out_row_width : JDIMENSION; { samples per output row }
  66. rows_to_go : JDIMENSION; { counts rows remaining in image }
  67. end; {my_upsampler;}
  68. const
  69. SCALEBITS = 16; { speediest right-shift on some machines }
  70. ONE_HALF = (INT32(1) shl (SCALEBITS-1));
  71. { Initialize tables for YCC->RGB colorspace conversion.
  72. This is taken directly from jdcolor.c; see that file for more info. }
  73. {LOCAL}
  74. procedure build_ycc_rgb_table (cinfo : j_decompress_ptr);
  75. const
  76. FIX_1_40200 = INT32( Round(1.40200 * (INT32(1) shl SCALEBITS)) );
  77. FIX_1_77200 = INT32( Round(1.77200 * (INT32(1) shl SCALEBITS)) );
  78. FIX_0_71414 = INT32( Round(0.71414 * (INT32(1) shl SCALEBITS)) );
  79. FIX_0_34414 = INT32( Round(0.34414 * (INT32(1) shl SCALEBITS)) );
  80. var
  81. upsample : my_upsample_ptr;
  82. i : int;
  83. x : INT32;
  84. var
  85. shift_temp : INT32;
  86. begin
  87. upsample := my_upsample_ptr (cinfo^.upsample);
  88. upsample^.Cr_r_tab := int_CConvertPtr (
  89. cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
  90. (MAXJSAMPLE+1) * SIZEOF(int)) );
  91. upsample^.Cb_b_tab := int_CConvertPtr (
  92. cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
  93. (MAXJSAMPLE+1) * SIZEOF(int)) );
  94. upsample^.Cr_g_tab := INT32_CConvertPtr (
  95. cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
  96. (MAXJSAMPLE+1) * SIZEOF(INT32)) );
  97. upsample^.Cb_g_tab := INT32_CConvertPtr (
  98. cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
  99. (MAXJSAMPLE+1) * SIZEOF(INT32)) );
  100. x := -CENTERJSAMPLE;
  101. for i := 0 to pred(MAXJSAMPLE) do
  102. begin
  103. { i is the actual input pixel value, in the range 0..MAXJSAMPLE }
  104. { The Cb or Cr value we are thinking of is x := i - CENTERJSAMPLE }
  105. { Cr=>R value is nearest int to 1.40200 * x }
  106. {upsample^.Cr_r_tab^[i] := int(
  107. RIGHT_SHIFT(FIX_1_40200 * x + ONE_HALF, SCALEBITS) );}
  108. shift_temp := FIX_1_40200 * x + ONE_HALF;
  109. if shift_temp < 0 then { SHIFT arithmetic RIGHT }
  110. upsample^.Cr_r_tab^[i] := int((shift_temp shr SCALEBITS)
  111. or ( (not INT32(0)) shl (32-SCALEBITS)))
  112. else
  113. upsample^.Cr_r_tab^[i] := int(shift_temp shr SCALEBITS);
  114. { Cb=>B value is nearest int to 1.77200 * x }
  115. {upsample^.Cb_b_tab^[i] := int(
  116. RIGHT_SHIFT(FIX_1_77200 * x + ONE_HALF, SCALEBITS) );}
  117. shift_temp := FIX_1_77200 * x + ONE_HALF;
  118. if shift_temp < 0 then { SHIFT arithmetic RIGHT }
  119. upsample^.Cb_b_tab^[i] := int((shift_temp shr SCALEBITS)
  120. or ( (not INT32(0)) shl (32-SCALEBITS)))
  121. else
  122. upsample^.Cb_b_tab^[i] := int(shift_temp shr SCALEBITS);
  123. { Cr=>G value is scaled-up -0.71414 * x }
  124. upsample^.Cr_g_tab^[i] := (- FIX_0_71414) * x;
  125. { Cb=>G value is scaled-up -0.34414 * x }
  126. { We also add in ONE_HALF so that need not do it in inner loop }
  127. upsample^.Cb_g_tab^[i] := (- FIX_0_34414) * x + ONE_HALF;
  128. Inc(x);
  129. end;
  130. end;
  131. { Initialize for an upsampling pass. }
  132. {METHODDEF}
  133. procedure start_pass_merged_upsample (cinfo : j_decompress_ptr);
  134. var
  135. upsample : my_upsample_ptr;
  136. begin
  137. upsample := my_upsample_ptr (cinfo^.upsample);
  138. { Mark the spare buffer empty }
  139. upsample^.spare_full := FALSE;
  140. { Initialize total-height counter for detecting bottom of image }
  141. upsample^.rows_to_go := cinfo^.output_height;
  142. end;
  143. { Control routine to do upsampling (and color conversion).
  144. The control routine just handles the row buffering considerations. }
  145. {METHODDEF}
  146. procedure merged_2v_upsample (cinfo : j_decompress_ptr;
  147. input_buf : JSAMPIMAGE;
  148. var in_row_group_ctr : JDIMENSION;
  149. in_row_groups_avail : JDIMENSION;
  150. output_buf : JSAMPARRAY;
  151. var out_row_ctr : JDIMENSION;
  152. out_rows_avail : JDIMENSION);
  153. { 2:1 vertical sampling case: may need a spare row. }
  154. var
  155. upsample : my_upsample_ptr;
  156. work_ptrs : array[0..2-1] of JSAMPROW;
  157. num_rows : JDIMENSION; { number of rows returned to caller }
  158. begin
  159. upsample := my_upsample_ptr (cinfo^.upsample);
  160. if (upsample^.spare_full) then
  161. begin
  162. { If we have a spare row saved from a previous cycle, just return it. }
  163. jcopy_sample_rows(JSAMPARRAY(@upsample^.spare_row),
  164. 0,
  165. JSAMPARRAY(@ output_buf^[out_row_ctr]),
  166. 0, 1, upsample^.out_row_width);
  167. num_rows := 1;
  168. upsample^.spare_full := FALSE;
  169. end
  170. else
  171. begin
  172. { Figure number of rows to return to caller. }
  173. num_rows := 2;
  174. { Not more than the distance to the end of the image. }
  175. if (num_rows > upsample^.rows_to_go) then
  176. num_rows := upsample^.rows_to_go;
  177. { And not more than what the client can accept: }
  178. Dec(out_rows_avail, {var} out_row_ctr);
  179. if (num_rows > out_rows_avail) then
  180. num_rows := out_rows_avail;
  181. { Create output pointer array for upsampler. }
  182. work_ptrs[0] := output_buf^[out_row_ctr];
  183. if (num_rows > 1) then
  184. begin
  185. work_ptrs[1] := output_buf^[out_row_ctr + 1];
  186. end
  187. else
  188. begin
  189. work_ptrs[1] := upsample^.spare_row;
  190. upsample^.spare_full := TRUE;
  191. end;
  192. { Now do the upsampling. }
  193. upsample^.upmethod (cinfo, input_buf, {var}in_row_group_ctr,
  194. JSAMPARRAY(@work_ptrs));
  195. end;
  196. { Adjust counts }
  197. Inc(out_row_ctr, num_rows);
  198. Dec(upsample^.rows_to_go, num_rows);
  199. { When the buffer is emptied, declare this input row group consumed }
  200. if (not upsample^.spare_full) then
  201. Inc(in_row_group_ctr);
  202. end;
  203. {METHODDEF}
  204. procedure merged_1v_upsample (cinfo : j_decompress_ptr;
  205. input_buf : JSAMPIMAGE;
  206. var in_row_group_ctr : JDIMENSION;
  207. in_row_groups_avail : JDIMENSION;
  208. output_buf : JSAMPARRAY;
  209. var out_row_ctr : JDIMENSION;
  210. out_rows_avail : JDIMENSION);
  211. { 1:1 vertical sampling case: much easier, never need a spare row. }
  212. var
  213. upsample : my_upsample_ptr;
  214. begin
  215. upsample := my_upsample_ptr (cinfo^.upsample);
  216. { Just do the upsampling. }
  217. upsample^.upmethod (cinfo, input_buf, in_row_group_ctr,
  218. JSAMPARRAY(@ output_buf^[out_row_ctr]));
  219. { Adjust counts }
  220. Inc(out_row_ctr);
  221. Inc(in_row_group_ctr);
  222. end;
  223. { These are the routines invoked by the control routines to do
  224. the actual upsampling/conversion. One row group is processed per call.
  225. Note: since we may be writing directly into application-supplied buffers,
  226. we have to be honest about the output width; we can't assume the buffer
  227. has been rounded up to an even width. }
  228. { Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical. }
  229. {METHODDEF}
  230. procedure h2v1_merged_upsample (cinfo : j_decompress_ptr;
  231. input_buf : JSAMPIMAGE;
  232. in_row_group_ctr : JDIMENSION;
  233. output_buf : JSAMPARRAY);
  234. var
  235. upsample : my_upsample_ptr;
  236. {register} y, cred, cgreen, cblue : int;
  237. cb, cr : int;
  238. {register} outptr : JSAMPROW;
  239. inptr0, inptr1, inptr2 : JSAMPLE_PTR;
  240. col : JDIMENSION;
  241. { copy these pointers into registers if possible }
  242. {register} range_limit : range_limit_table_ptr;
  243. Crrtab : int_CConvertPtr;
  244. Cbbtab : int_CConvertPtr;
  245. Crgtab : INT32_CConvertPtr;
  246. Cbgtab : INT32_CConvertPtr;
  247. var
  248. shift_temp : INT32;
  249. begin
  250. upsample := my_upsample_ptr (cinfo^.upsample);
  251. range_limit := cinfo^.sample_range_limit;
  252. Crrtab := upsample^.Cr_r_tab;
  253. Cbbtab := upsample^.Cb_b_tab;
  254. Crgtab := upsample^.Cr_g_tab;
  255. Cbgtab := upsample^.Cb_g_tab;
  256. inptr0 := JSAMPLE_PTR(input_buf^[0]^[in_row_group_ctr]);
  257. inptr1 := JSAMPLE_PTR(input_buf^[1]^[in_row_group_ctr]);
  258. inptr2 := JSAMPLE_PTR(input_buf^[2]^[in_row_group_ctr]);
  259. outptr := output_buf^[0];
  260. { Loop for each pair of output pixels }
  261. for col := pred(cinfo^.output_width shr 1) downto 0 do
  262. begin
  263. { Do the chroma part of the calculation }
  264. cb := GETJSAMPLE(inptr1^);
  265. Inc(inptr1);
  266. cr := GETJSAMPLE(inptr2^);
  267. Inc(inptr2);
  268. cred := Crrtab^[cr];
  269. {cgreen := int( RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS) );}
  270. shift_temp := Cbgtab^[cb] + Crgtab^[cr];
  271. if shift_temp < 0 then { SHIFT arithmetic RIGHT }
  272. cgreen := int((shift_temp shr SCALEBITS)
  273. or ( (not INT32(0)) shl (32-SCALEBITS)))
  274. else
  275. cgreen := int(shift_temp shr SCALEBITS);
  276. cblue := Cbbtab^[cb];
  277. { Fetch 2 Y values and emit 2 pixels }
  278. y := GETJSAMPLE(inptr0^);
  279. Inc(inptr0);
  280. outptr^[RGB_RED] := range_limit^[y + cred];
  281. outptr^[RGB_GREEN] := range_limit^[y + cgreen];
  282. outptr^[RGB_BLUE] := range_limit^[y + cblue];
  283. Inc(JSAMPLE_PTR(outptr), RGB_PIXELSIZE);
  284. y := GETJSAMPLE(inptr0^);
  285. Inc(inptr0);
  286. outptr^[RGB_RED] := range_limit^[y + cred];
  287. outptr^[RGB_GREEN] := range_limit^[y + cgreen];
  288. outptr^[RGB_BLUE] := range_limit^[y + cblue];
  289. Inc(JSAMPLE_PTR(outptr), RGB_PIXELSIZE);
  290. end;
  291. { If image width is odd, do the last output column separately }
  292. if Odd(cinfo^.output_width) then
  293. begin
  294. cb := GETJSAMPLE(inptr1^);
  295. cr := GETJSAMPLE(inptr2^);
  296. cred := Crrtab^[cr];
  297. {cgreen := int ( RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS) );}
  298. shift_temp := Cbgtab^[cb] + Crgtab^[cr];
  299. if shift_temp < 0 then { SHIFT arithmetic RIGHT }
  300. cgreen := int((shift_temp shr SCALEBITS)
  301. or ( (not INT32(0)) shl (32-SCALEBITS)))
  302. else
  303. cgreen := int(shift_temp shr SCALEBITS);
  304. cblue := Cbbtab^[cb];
  305. y := GETJSAMPLE(inptr0^);
  306. outptr^[RGB_RED] := range_limit^[y + cred];
  307. outptr^[RGB_GREEN] := range_limit^[y + cgreen];
  308. outptr^[RGB_BLUE] := range_limit^[y + cblue];
  309. end;
  310. end;
  311. { Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical. }
  312. {METHODDEF}
  313. procedure h2v2_merged_upsample (cinfo : j_decompress_ptr;
  314. input_buf : JSAMPIMAGE;
  315. in_row_group_ctr : JDIMENSION;
  316. output_buf : JSAMPARRAY);
  317. var
  318. upsample : my_upsample_ptr;
  319. {register} y, cred, cgreen, cblue : int;
  320. cb, cr : int;
  321. {register} outptr0, outptr1 : JSAMPROW;
  322. inptr00, inptr01, inptr1, inptr2 : JSAMPLE_PTR;
  323. col : JDIMENSION;
  324. { copy these pointers into registers if possible }
  325. {register} range_limit : range_limit_table_ptr;
  326. Crrtab : int_CConvertPtr;
  327. Cbbtab : int_CConvertPtr;
  328. Crgtab : INT32_CConvertPtr;
  329. Cbgtab : INT32_CConvertPtr;
  330. var
  331. shift_temp : INT32;
  332. begin
  333. upsample := my_upsample_ptr (cinfo^.upsample);
  334. range_limit := cinfo^.sample_range_limit;
  335. Crrtab := upsample^.Cr_r_tab;
  336. Cbbtab := upsample^.Cb_b_tab;
  337. Crgtab := upsample^.Cr_g_tab;
  338. Cbgtab := upsample^.Cb_g_tab;
  339. inptr00 := JSAMPLE_PTR(input_buf^[0]^[in_row_group_ctr*2]);
  340. inptr01 := JSAMPLE_PTR(input_buf^[0]^[in_row_group_ctr*2 + 1]);
  341. inptr1 := JSAMPLE_PTR(input_buf^[1]^[in_row_group_ctr]);
  342. inptr2 := JSAMPLE_PTR(input_buf^[2]^[in_row_group_ctr]);
  343. outptr0 := output_buf^[0];
  344. outptr1 := output_buf^[1];
  345. { Loop for each group of output pixels }
  346. for col := pred(cinfo^.output_width shr 1) downto 0 do
  347. begin
  348. { Do the chroma part of the calculation }
  349. cb := GETJSAMPLE(inptr1^);
  350. Inc(inptr1);
  351. cr := GETJSAMPLE(inptr2^);
  352. Inc(inptr2);
  353. cred := Crrtab^[cr];
  354. {cgreen := int( RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS) );}
  355. shift_temp := Cbgtab^[cb] + Crgtab^[cr];
  356. if shift_temp < 0 then { SHIFT arithmetic RIGHT }
  357. cgreen := int((shift_temp shr SCALEBITS)
  358. or ( (not INT32(0)) shl (32-SCALEBITS)))
  359. else
  360. cgreen := int(shift_temp shr SCALEBITS);
  361. cblue := Cbbtab^[cb];
  362. { Fetch 4 Y values and emit 4 pixels }
  363. y := GETJSAMPLE(inptr00^);
  364. Inc(inptr00);
  365. outptr0^[RGB_RED] := range_limit^[y + cred];
  366. outptr0^[RGB_GREEN] := range_limit^[y + cgreen];
  367. outptr0^[RGB_BLUE] := range_limit^[y + cblue];
  368. Inc(JSAMPLE_PTR(outptr0), RGB_PIXELSIZE);
  369. y := GETJSAMPLE(inptr00^);
  370. Inc(inptr00);
  371. outptr0^[RGB_RED] := range_limit^[y + cred];
  372. outptr0^[RGB_GREEN] := range_limit^[y + cgreen];
  373. outptr0^[RGB_BLUE] := range_limit^[y + cblue];
  374. Inc(JSAMPLE_PTR(outptr0), RGB_PIXELSIZE);
  375. y := GETJSAMPLE(inptr01^);
  376. Inc(inptr01);
  377. outptr1^[RGB_RED] := range_limit^[y + cred];
  378. outptr1^[RGB_GREEN] := range_limit^[y + cgreen];
  379. outptr1^[RGB_BLUE] := range_limit^[y + cblue];
  380. Inc(JSAMPLE_PTR(outptr1), RGB_PIXELSIZE);
  381. y := GETJSAMPLE(inptr01^);
  382. Inc(inptr01);
  383. outptr1^[RGB_RED] := range_limit^[y + cred];
  384. outptr1^[RGB_GREEN] := range_limit^[y + cgreen];
  385. outptr1^[RGB_BLUE] := range_limit^[y + cblue];
  386. Inc(JSAMPLE_PTR(outptr1), RGB_PIXELSIZE);
  387. end;
  388. { If image width is odd, do the last output column separately }
  389. if Odd(cinfo^.output_width) then
  390. begin
  391. cb := GETJSAMPLE(inptr1^);
  392. cr := GETJSAMPLE(inptr2^);
  393. cred := Crrtab^[cr];
  394. {cgreen := int (RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS));}
  395. shift_temp := Cbgtab^[cb] + Crgtab^[cr];
  396. if shift_temp < 0 then { SHIFT arithmetic RIGHT }
  397. cgreen := int((shift_temp shr SCALEBITS)
  398. or ( (not INT32(0)) shl (32-SCALEBITS)))
  399. else
  400. cgreen := int(shift_temp shr SCALEBITS);
  401. cblue := Cbbtab^[cb];
  402. y := GETJSAMPLE(inptr00^);
  403. outptr0^[RGB_RED] := range_limit^[y + cred];
  404. outptr0^[RGB_GREEN] := range_limit^[y + cgreen];
  405. outptr0^[RGB_BLUE] := range_limit^[y + cblue];
  406. y := GETJSAMPLE(inptr01^);
  407. outptr1^[RGB_RED] := range_limit^[y + cred];
  408. outptr1^[RGB_GREEN] := range_limit^[y + cgreen];
  409. outptr1^[RGB_BLUE] := range_limit^[y + cblue];
  410. end;
  411. end;
  412. { Module initialization routine for merged upsampling/color conversion.
  413. NB: this is called under the conditions determined by use_merged_upsample()
  414. in jdmaster.c. That routine MUST correspond to the actual capabilities
  415. of this module; no safety checks are made here. }
  416. {GLOBAL}
  417. procedure jinit_merged_upsampler (cinfo : j_decompress_ptr);
  418. var
  419. upsample : my_upsample_ptr;
  420. begin
  421. upsample := my_upsample_ptr (
  422. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  423. SIZEOF(my_upsampler)) );
  424. cinfo^.upsample := jpeg_upsampler_ptr (upsample);
  425. upsample^.pub.start_pass := start_pass_merged_upsample;
  426. upsample^.pub.need_context_rows := FALSE;
  427. upsample^.out_row_width := cinfo^.output_width * JDIMENSION(cinfo^.out_color_components);
  428. if (cinfo^.max_v_samp_factor = 2) then
  429. begin
  430. upsample^.pub.upsample := merged_2v_upsample;
  431. upsample^.upmethod := h2v2_merged_upsample;
  432. { Allocate a spare row buffer }
  433. upsample^.spare_row := JSAMPROW(
  434. cinfo^.mem^.alloc_large ( j_common_ptr(cinfo), JPOOL_IMAGE,
  435. size_t (upsample^.out_row_width * SIZEOF(JSAMPLE))) );
  436. end
  437. else
  438. begin
  439. upsample^.pub.upsample := merged_1v_upsample;
  440. upsample^.upmethod := h2v1_merged_upsample;
  441. { No spare row needed }
  442. upsample^.spare_row := NIL;
  443. end;
  444. build_ycc_rgb_table(cinfo);
  445. end;
  446. end.