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.

501 lines
16 KiB

3 years ago
  1. unit imjdcolor;
  2. { This file contains output colorspace conversion routines. }
  3. { Original: jdcolor.c ; Copyright (C) 1991-1997, Thomas G. Lane. }
  4. interface
  5. {$I imjconfig.inc}
  6. uses
  7. imjmorecfg,
  8. imjinclude,
  9. imjutils,
  10. imjdeferr,
  11. imjerror,
  12. imjpeglib;
  13. { Module initialization routine for output colorspace conversion. }
  14. {GLOBAL}
  15. procedure jinit_color_deconverter (cinfo : j_decompress_ptr);
  16. implementation
  17. { Private subobject }
  18. type
  19. int_Color_Table = array[0..MAXJSAMPLE+1-1] of int;
  20. int_table_ptr = ^int_Color_Table;
  21. INT32_Color_Table = array[0..MAXJSAMPLE+1-1] of INT32;
  22. INT32_table_ptr = ^INT32_Color_Table;
  23. type
  24. my_cconvert_ptr = ^my_color_deconverter;
  25. my_color_deconverter = record
  26. pub : jpeg_color_deconverter; { public fields }
  27. { Private state for YCC^.RGB conversion }
  28. Cr_r_tab : int_table_ptr; { => table for Cr to R conversion }
  29. Cb_b_tab : int_table_ptr; { => table for Cb to B conversion }
  30. Cr_g_tab : INT32_table_ptr; { => table for Cr to G conversion }
  31. Cb_g_tab : INT32_table_ptr; { => table for Cb to G conversion }
  32. end;
  33. {*************** YCbCr ^. RGB conversion: most common case *************}
  34. { YCbCr is defined per CCIR 601-1, except that Cb and Cr are
  35. normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
  36. The conversion equations to be implemented are therefore
  37. R = Y + 1.40200 * Cr
  38. G = Y - 0.34414 * Cb - 0.71414 * Cr
  39. B = Y + 1.77200 * Cb
  40. where Cb and Cr represent the incoming values less CENTERJSAMPLE.
  41. (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
  42. To avoid floating-point arithmetic, we represent the fractional constants
  43. as integers scaled up by 2^16 (about 4 digits precision); we have to divide
  44. the products by 2^16, with appropriate rounding, to get the correct answer.
  45. Notice that Y, being an integral input, does not contribute any fraction
  46. so it need not participate in the rounding.
  47. For even more speed, we avoid doing any multiplications in the inner loop
  48. by precalculating the constants times Cb and Cr for all possible values.
  49. For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
  50. for 12-bit samples it is still acceptable. It's not very reasonable for
  51. 16-bit samples, but if you want lossless storage you shouldn't be changing
  52. colorspace anyway.
  53. The Cr=>R and Cb=>B values can be rounded to integers in advance; the
  54. values for the G calculation are left scaled up, since we must add them
  55. together before rounding. }
  56. const
  57. SCALEBITS = 16; { speediest right-shift on some machines }
  58. ONE_HALF = (INT32(1) shl (SCALEBITS-1));
  59. { Initialize tables for YCC->RGB colorspace conversion. }
  60. {LOCAL}
  61. procedure build_ycc_rgb_table (cinfo : j_decompress_ptr);
  62. const
  63. FIX_1_40200 = INT32(Round( 1.40200 * (1 shl SCALEBITS)));
  64. FIX_1_77200 = INT32(Round( 1.77200 * (1 shl SCALEBITS)));
  65. FIX_0_71414 = INT32(Round( 0.71414 * (1 shl SCALEBITS)));
  66. FIX_0_34414 = INT32(Round( 0.34414 * (1 shl SCALEBITS)));
  67. var
  68. cconvert : my_cconvert_ptr;
  69. i : int;
  70. x : INT32;
  71. var
  72. shift_temp : INT32;
  73. begin
  74. cconvert := my_cconvert_ptr (cinfo^.cconvert);
  75. cconvert^.Cr_r_tab := int_table_ptr(
  76. cinfo^.mem^.alloc_small ( j_common_ptr(cinfo), JPOOL_IMAGE,
  77. (MAXJSAMPLE+1) * SIZEOF(int)) );
  78. cconvert^.Cb_b_tab := int_table_ptr (
  79. cinfo^.mem^.alloc_small ( j_common_ptr(cinfo), JPOOL_IMAGE,
  80. (MAXJSAMPLE+1) * SIZEOF(int)) );
  81. cconvert^.Cr_g_tab := INT32_table_ptr (
  82. cinfo^.mem^.alloc_small ( j_common_ptr(cinfo), JPOOL_IMAGE,
  83. (MAXJSAMPLE+1) * SIZEOF(INT32)) );
  84. cconvert^.Cb_g_tab := INT32_table_ptr (
  85. cinfo^.mem^.alloc_small ( j_common_ptr(cinfo), JPOOL_IMAGE,
  86. (MAXJSAMPLE+1) * SIZEOF(INT32)) );
  87. x := -CENTERJSAMPLE;
  88. for i := 0 to MAXJSAMPLE do
  89. begin
  90. { i is the actual input pixel value, in the range 0..MAXJSAMPLE }
  91. { The Cb or Cr value we are thinking of is x := i - CENTERJSAMPLE }
  92. { Cr=>R value is nearest int to 1.40200 * x }
  93. shift_temp := FIX_1_40200 * x + ONE_HALF;
  94. if shift_temp < 0 then { SHIFT arithmetic RIGHT }
  95. cconvert^.Cr_r_tab^[i] := int((shift_temp shr SCALEBITS)
  96. or ( (not INT32(0)) shl (32-SCALEBITS)))
  97. else
  98. cconvert^.Cr_r_tab^[i] := int(shift_temp shr SCALEBITS);
  99. { Cb=>B value is nearest int to 1.77200 * x }
  100. shift_temp := FIX_1_77200 * x + ONE_HALF;
  101. if shift_temp < 0 then { SHIFT arithmetic RIGHT }
  102. cconvert^.Cb_b_tab^[i] := int((shift_temp shr SCALEBITS)
  103. or ( (not INT32(0)) shl (32-SCALEBITS)))
  104. else
  105. cconvert^.Cb_b_tab^[i] := int(shift_temp shr SCALEBITS);
  106. { Cr=>G value is scaled-up -0.71414 * x }
  107. cconvert^.Cr_g_tab^[i] := (- FIX_0_71414 ) * x;
  108. { Cb=>G value is scaled-up -0.34414 * x }
  109. { We also add in ONE_HALF so that need not do it in inner loop }
  110. cconvert^.Cb_g_tab^[i] := (- FIX_0_34414 ) * x + ONE_HALF;
  111. Inc(x);
  112. end;
  113. end;
  114. { Convert some rows of samples to the output colorspace.
  115. Note that we change from noninterleaved, one-plane-per-component format
  116. to interleaved-pixel format. The output buffer is therefore three times
  117. as wide as the input buffer.
  118. A starting row offset is provided only for the input buffer. The caller
  119. can easily adjust the passed output_buf value to accommodate any row
  120. offset required on that side. }
  121. {METHODDEF}
  122. procedure ycc_rgb_convert (cinfo : j_decompress_ptr;
  123. input_buf : JSAMPIMAGE;
  124. input_row : JDIMENSION;
  125. output_buf : JSAMPARRAY;
  126. num_rows : int);
  127. var
  128. cconvert : my_cconvert_ptr;
  129. {register} y, cb, cr : int;
  130. {register} outptr : JSAMPROW;
  131. {register} inptr0, inptr1, inptr2 : JSAMPROW;
  132. {register} col : JDIMENSION;
  133. num_cols : JDIMENSION;
  134. { copy these pointers into registers if possible }
  135. {register} range_limit : range_limit_table_ptr;
  136. {register} Crrtab : int_table_ptr;
  137. {register} Cbbtab : int_table_ptr;
  138. {register} Crgtab : INT32_table_ptr;
  139. {register} Cbgtab : INT32_table_ptr;
  140. var
  141. shift_temp : INT32;
  142. begin
  143. cconvert := my_cconvert_ptr (cinfo^.cconvert);
  144. num_cols := cinfo^.output_width;
  145. range_limit := cinfo^.sample_range_limit;
  146. Crrtab := cconvert^.Cr_r_tab;
  147. Cbbtab := cconvert^.Cb_b_tab;
  148. Crgtab := cconvert^.Cr_g_tab;
  149. Cbgtab := cconvert^.Cb_g_tab;
  150. while (num_rows > 0) do
  151. begin
  152. Dec(num_rows);
  153. inptr0 := input_buf^[0]^[input_row];
  154. inptr1 := input_buf^[1]^[input_row];
  155. inptr2 := input_buf^[2]^[input_row];
  156. Inc(input_row);
  157. outptr := output_buf^[0];
  158. Inc(JSAMPROW_PTR(output_buf));
  159. for col := 0 to pred(num_cols) do
  160. begin
  161. y := GETJSAMPLE(inptr0^[col]);
  162. cb := GETJSAMPLE(inptr1^[col]);
  163. cr := GETJSAMPLE(inptr2^[col]);
  164. { Range-limiting is essential due to noise introduced by DCT losses. }
  165. outptr^[RGB_RED] := range_limit^[y + Crrtab^[cr]];
  166. shift_temp := Cbgtab^[cb] + Crgtab^[cr];
  167. if shift_temp < 0 then { SHIFT arithmetic RIGHT }
  168. outptr^[RGB_GREEN] := range_limit^[y + int((shift_temp shr SCALEBITS)
  169. or ( (not INT32(0)) shl (32-SCALEBITS)))]
  170. else
  171. outptr^[RGB_GREEN] := range_limit^[y + int(shift_temp shr SCALEBITS)];
  172. outptr^[RGB_BLUE] := range_limit^[y + Cbbtab^[cb]];
  173. Inc(JSAMPLE_PTR(outptr), RGB_PIXELSIZE);
  174. end;
  175. end;
  176. end;
  177. {*************** Cases other than YCbCr -> RGB *************}
  178. { Color conversion for no colorspace change: just copy the data,
  179. converting from separate-planes to interleaved representation. }
  180. {METHODDEF}
  181. procedure null_convert (cinfo : j_decompress_ptr;
  182. input_buf : JSAMPIMAGE;
  183. input_row : JDIMENSION;
  184. output_buf : JSAMPARRAY;
  185. num_rows : int);
  186. var
  187. {register} inptr,
  188. outptr : JSAMPLE_PTR;
  189. {register} count : JDIMENSION;
  190. {register} num_components : int;
  191. num_cols : JDIMENSION;
  192. ci : int;
  193. begin
  194. num_components := cinfo^.num_components;
  195. num_cols := cinfo^.output_width;
  196. while (num_rows > 0) do
  197. begin
  198. Dec(num_rows);
  199. for ci := 0 to pred(num_components) do
  200. begin
  201. inptr := JSAMPLE_PTR(input_buf^[ci]^[input_row]);
  202. outptr := JSAMPLE_PTR(@(output_buf^[0]^[ci]));
  203. for count := pred(num_cols) downto 0 do
  204. begin
  205. outptr^ := inptr^; { needn't bother with GETJSAMPLE() here }
  206. Inc(inptr);
  207. Inc(outptr, num_components);
  208. end;
  209. end;
  210. Inc(input_row);
  211. Inc(JSAMPROW_PTR(output_buf));
  212. end;
  213. end;
  214. { Color conversion for grayscale: just copy the data.
  215. This also works for YCbCr -> grayscale conversion, in which
  216. we just copy the Y (luminance) component and ignore chrominance. }
  217. {METHODDEF}
  218. procedure grayscale_convert (cinfo : j_decompress_ptr;
  219. input_buf : JSAMPIMAGE;
  220. input_row : JDIMENSION;
  221. output_buf : JSAMPARRAY;
  222. num_rows : int);
  223. begin
  224. jcopy_sample_rows(input_buf^[0], int(input_row), output_buf, 0,
  225. num_rows, cinfo^.output_width);
  226. end;
  227. { Convert grayscale to RGB: just duplicate the graylevel three times.
  228. This is provided to support applications that don't want to cope
  229. with grayscale as a separate case. }
  230. {METHODDEF}
  231. procedure gray_rgb_convert (cinfo : j_decompress_ptr;
  232. input_buf : JSAMPIMAGE;
  233. input_row : JDIMENSION;
  234. output_buf : JSAMPARRAY;
  235. num_rows : int);
  236. var
  237. {register} inptr, outptr : JSAMPLE_PTR;
  238. {register} col : JDIMENSION;
  239. num_cols : JDIMENSION;
  240. begin
  241. num_cols := cinfo^.output_width;
  242. while (num_rows > 0) do
  243. begin
  244. inptr := JSAMPLE_PTR(input_buf^[0]^[input_row]);
  245. Inc(input_row);
  246. outptr := JSAMPLE_PTR(@output_buf^[0]);
  247. Inc(JSAMPROW_PTR(output_buf));
  248. for col := 0 to pred(num_cols) do
  249. begin
  250. { We can dispense with GETJSAMPLE() here }
  251. JSAMPROW(outptr)^[RGB_RED] := inptr^;
  252. JSAMPROW(outptr)^[RGB_GREEN] := inptr^;
  253. JSAMPROW(outptr)^[RGB_BLUE] := inptr^;
  254. Inc(inptr);
  255. Inc(outptr, RGB_PIXELSIZE);
  256. end;
  257. Dec(num_rows);
  258. end;
  259. end;
  260. { Adobe-style YCCK -> CMYK conversion.
  261. We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
  262. conversion as above, while passing K (black) unchanged.
  263. We assume build_ycc_rgb_table has been called. }
  264. {METHODDEF}
  265. procedure ycck_cmyk_convert (cinfo : j_decompress_ptr;
  266. input_buf : JSAMPIMAGE;
  267. input_row : JDIMENSION;
  268. output_buf : JSAMPARRAY;
  269. num_rows : int);
  270. var
  271. cconvert : my_cconvert_ptr;
  272. {register} y, cb, cr : int;
  273. {register} outptr : JSAMPROW;
  274. {register} inptr0, inptr1, inptr2, inptr3 : JSAMPROW;
  275. {register} col : JDIMENSION;
  276. num_cols : JDIMENSION;
  277. { copy these pointers into registers if possible }
  278. {register} range_limit : range_limit_table_ptr;
  279. {register} Crrtab : int_table_ptr;
  280. {register} Cbbtab : int_table_ptr;
  281. {register} Crgtab : INT32_table_ptr;
  282. {register} Cbgtab : INT32_table_ptr;
  283. var
  284. shift_temp : INT32;
  285. begin
  286. cconvert := my_cconvert_ptr (cinfo^.cconvert);
  287. num_cols := cinfo^.output_width;
  288. { copy these pointers into registers if possible }
  289. range_limit := cinfo^.sample_range_limit;
  290. Crrtab := cconvert^.Cr_r_tab;
  291. Cbbtab := cconvert^.Cb_b_tab;
  292. Crgtab := cconvert^.Cr_g_tab;
  293. Cbgtab := cconvert^.Cb_g_tab;
  294. while (num_rows > 0) do
  295. begin
  296. Dec(num_rows);
  297. inptr0 := input_buf^[0]^[input_row];
  298. inptr1 := input_buf^[1]^[input_row];
  299. inptr2 := input_buf^[2]^[input_row];
  300. inptr3 := input_buf^[3]^[input_row];
  301. Inc(input_row);
  302. outptr := output_buf^[0];
  303. Inc(JSAMPROW_PTR(output_buf));
  304. for col := 0 to pred(num_cols) do
  305. begin
  306. y := GETJSAMPLE(inptr0^[col]);
  307. cb := GETJSAMPLE(inptr1^[col]);
  308. cr := GETJSAMPLE(inptr2^[col]);
  309. { Range-limiting is essential due to noise introduced by DCT losses. }
  310. outptr^[0] := range_limit^[MAXJSAMPLE - (y + Crrtab^[cr])]; { red }
  311. shift_temp := Cbgtab^[cb] + Crgtab^[cr];
  312. if shift_temp < 0 then
  313. outptr^[1] := range_limit^[MAXJSAMPLE - (y + int(
  314. (shift_temp shr SCALEBITS) or ((not INT32(0)) shl (32-SCALEBITS))
  315. ) )]
  316. else
  317. outptr^[1] := range_limit^[MAXJSAMPLE - { green }
  318. (y + int(shift_temp shr SCALEBITS) )];
  319. outptr^[2] := range_limit^[MAXJSAMPLE - (y + Cbbtab^[cb])]; { blue }
  320. { K passes through unchanged }
  321. outptr^[3] := inptr3^[col]; { don't need GETJSAMPLE here }
  322. Inc(JSAMPLE_PTR(outptr), 4);
  323. end;
  324. end;
  325. end;
  326. { Empty method for start_pass. }
  327. {METHODDEF}
  328. procedure start_pass_dcolor (cinfo : j_decompress_ptr);
  329. begin
  330. { no work needed }
  331. end;
  332. { Module initialization routine for output colorspace conversion. }
  333. {GLOBAL}
  334. procedure jinit_color_deconverter (cinfo : j_decompress_ptr);
  335. var
  336. cconvert : my_cconvert_ptr;
  337. ci : int;
  338. begin
  339. cconvert := my_cconvert_ptr (
  340. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  341. SIZEOF(my_color_deconverter)) );
  342. cinfo^.cconvert := jpeg_color_deconverter_ptr (cconvert);
  343. cconvert^.pub.start_pass := start_pass_dcolor;
  344. { Make sure num_components agrees with jpeg_color_space }
  345. case (cinfo^.jpeg_color_space) of
  346. JCS_GRAYSCALE:
  347. if (cinfo^.num_components <> 1) then
  348. ERREXIT(j_common_ptr(cinfo), JERR_BAD_J_COLORSPACE);
  349. JCS_RGB,
  350. JCS_YCbCr:
  351. if (cinfo^.num_components <> 3) then
  352. ERREXIT(j_common_ptr(cinfo), JERR_BAD_J_COLORSPACE);
  353. JCS_CMYK,
  354. JCS_YCCK:
  355. if (cinfo^.num_components <> 4) then
  356. ERREXIT(j_common_ptr(cinfo), JERR_BAD_J_COLORSPACE);
  357. else { JCS_UNKNOWN can be anything }
  358. if (cinfo^.num_components < 1) then
  359. ERREXIT(j_common_ptr(cinfo), JERR_BAD_J_COLORSPACE);
  360. end;
  361. { Set out_color_components and conversion method based on requested space.
  362. Also clear the component_needed flags for any unused components,
  363. so that earlier pipeline stages can avoid useless computation. }
  364. case (cinfo^.out_color_space) of
  365. JCS_GRAYSCALE:
  366. begin
  367. cinfo^.out_color_components := 1;
  368. if (cinfo^.jpeg_color_space = JCS_GRAYSCALE)
  369. or (cinfo^.jpeg_color_space = JCS_YCbCr) then
  370. begin
  371. cconvert^.pub.color_convert := grayscale_convert;
  372. { For color -> grayscale conversion, only the
  373. Y (0) component is needed }
  374. for ci := 1 to pred(cinfo^.num_components) do
  375. cinfo^.comp_info^[ci].component_needed := FALSE;
  376. end
  377. else
  378. ERREXIT(j_common_ptr(cinfo), JERR_CONVERSION_NOTIMPL);
  379. end;
  380. JCS_RGB:
  381. begin
  382. cinfo^.out_color_components := RGB_PIXELSIZE;
  383. if (cinfo^.jpeg_color_space = JCS_YCbCr) then
  384. begin
  385. cconvert^.pub.color_convert := ycc_rgb_convert;
  386. build_ycc_rgb_table(cinfo);
  387. end
  388. else
  389. if (cinfo^.jpeg_color_space = JCS_GRAYSCALE) then
  390. begin
  391. cconvert^.pub.color_convert := gray_rgb_convert;
  392. end
  393. else
  394. if (cinfo^.jpeg_color_space = JCS_RGB) and (RGB_PIXELSIZE = 3) then
  395. begin
  396. cconvert^.pub.color_convert := null_convert;
  397. end
  398. else
  399. ERREXIT(j_common_ptr(cinfo), JERR_CONVERSION_NOTIMPL);
  400. end;
  401. JCS_CMYK:
  402. begin
  403. cinfo^.out_color_components := 4;
  404. if (cinfo^.jpeg_color_space = JCS_YCCK) then
  405. begin
  406. cconvert^.pub.color_convert := ycck_cmyk_convert;
  407. build_ycc_rgb_table(cinfo);
  408. end
  409. else
  410. if (cinfo^.jpeg_color_space = JCS_CMYK) then
  411. begin
  412. cconvert^.pub.color_convert := null_convert;
  413. end
  414. else
  415. ERREXIT(j_common_ptr(cinfo), JERR_CONVERSION_NOTIMPL);
  416. end;
  417. else
  418. begin { Permit null conversion to same output space }
  419. if (cinfo^.out_color_space = cinfo^.jpeg_color_space) then
  420. begin
  421. cinfo^.out_color_components := cinfo^.num_components;
  422. cconvert^.pub.color_convert := null_convert;
  423. end
  424. else { unsupported non-null conversion }
  425. ERREXIT(j_common_ptr(cinfo), JERR_CONVERSION_NOTIMPL);
  426. end;
  427. end;
  428. if (cinfo^.quantize_colors) then
  429. cinfo^.output_components := 1 { single colormapped output component }
  430. else
  431. cinfo^.output_components := cinfo^.out_color_components;
  432. end;
  433. end.