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.

530 lines
17 KiB

3 years ago
  1. unit imjccolor;
  2. { This file contains input colorspace conversion routines. }
  3. { Original : jccolor.c ; Copyright (C) 1991-1996, Thomas G. Lane. }
  4. interface
  5. {$I imjconfig.inc}
  6. uses
  7. imjmorecfg,
  8. imjinclude,
  9. imjdeferr,
  10. imjerror,
  11. imjpeglib;
  12. { Module initialization routine for input colorspace conversion. }
  13. {GLOBAL}
  14. procedure jinit_color_converter (cinfo : j_compress_ptr);
  15. implementation
  16. { Private subobject }
  17. type
  18. INT32_FIELD = array[0..MaxInt div SizeOf(INT32) - 1] of INT32;
  19. INT32_FIELD_PTR = ^INT32_FIELD;
  20. type
  21. my_cconvert_ptr = ^my_color_converter;
  22. my_color_converter = record
  23. pub : jpeg_color_converter; { public fields }
  24. { Private state for RGB -> YCC conversion }
  25. rgb_ycc_tab : INT32_FIELD_PTR; { => table for RGB to YCbCr conversion }
  26. end; {my_color_converter;}
  27. {*************** RGB -> YCbCr conversion: most common case *************}
  28. {
  29. YCbCr is defined per CCIR 601-1, except that Cb and Cr are
  30. normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
  31. The conversion equations to be implemented are therefore
  32. Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
  33. Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B + CENTERJSAMPLE
  34. Cr = 0.50000 * R - 0.41869 * G - 0.08131 * B + CENTERJSAMPLE
  35. (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
  36. Note: older versions of the IJG code used a zero offset of MAXJSAMPLE/2,
  37. rather than CENTERJSAMPLE, for Cb and Cr. This gave equal positive and
  38. negative swings for Cb/Cr, but meant that grayscale values (Cb=Cr=0)
  39. were not represented exactly. Now we sacrifice exact representation of
  40. maximum red and maximum blue in order to get exact grayscales.
  41. To avoid floating-point arithmetic, we represent the fractional constants
  42. as integers scaled up by 2^16 (about 4 digits precision); we have to divide
  43. the products by 2^16, with appropriate rounding, to get the correct answer.
  44. For even more speed, we avoid doing any multiplications in the inner loop
  45. by precalculating the constants times R,G,B for all possible values.
  46. For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
  47. for 12-bit samples it is still acceptable. It's not very reasonable for
  48. 16-bit samples, but if you want lossless storage you shouldn't be changing
  49. colorspace anyway.
  50. The CENTERJSAMPLE offsets and the rounding fudge-factor of 0.5 are included
  51. in the tables to save adding them separately in the inner loop. }
  52. const
  53. SCALEBITS = 16; { speediest right-shift on some machines }
  54. CBCR_OFFSET = INT32(CENTERJSAMPLE shl SCALEBITS);
  55. ONE_HALF = INT32(1) shl (SCALEBITS-1);
  56. { We allocate one big table and divide it up into eight parts, instead of
  57. doing eight alloc_small requests. This lets us use a single table base
  58. address, which can be held in a register in the inner loops on many
  59. machines (more than can hold all eight addresses, anyway). }
  60. R_Y_OFF = 0; { offset to R => Y section }
  61. G_Y_OFF = 1*(MAXJSAMPLE+1); { offset to G => Y section }
  62. B_Y_OFF = 2*(MAXJSAMPLE+1); { etc. }
  63. R_CB_OFF = 3*(MAXJSAMPLE+1);
  64. G_CB_OFF = 4*(MAXJSAMPLE+1);
  65. B_CB_OFF = 5*(MAXJSAMPLE+1);
  66. R_CR_OFF = B_CB_OFF; { B=>Cb, R=>Cr are the same }
  67. G_CR_OFF = 6*(MAXJSAMPLE+1);
  68. B_CR_OFF = 7*(MAXJSAMPLE+1);
  69. TABLE_SIZE = 8*(MAXJSAMPLE+1);
  70. { Initialize for RGB->YCC colorspace conversion. }
  71. {METHODDEF}
  72. procedure rgb_ycc_start (cinfo : j_compress_ptr);
  73. const
  74. FIX_0_29900 = INT32(Round(0.29900 * (1 shl SCALEBITS)));
  75. FIX_0_58700 = INT32(Round(0.58700 * (1 shl SCALEBITS)));
  76. FIX_0_11400 = INT32(Round(0.11400 * (1 shl SCALEBITS)));
  77. FIX_0_16874 = INT32(Round(0.16874 * (1 shl SCALEBITS)));
  78. FIX_0_33126 = INT32(Round(0.33126 * (1 shl SCALEBITS)));
  79. FIX_0_50000 = INT32(Round(0.50000 * (1 shl SCALEBITS)));
  80. FIX_0_41869 = INT32(Round(0.41869 * (1 shl SCALEBITS)));
  81. FIX_0_08131 = INT32(Round(0.08131 * (1 shl SCALEBITS)));
  82. var
  83. cconvert : my_cconvert_ptr;
  84. rgb_ycc_tab : INT32_FIELD_PTR;
  85. i : INT32;
  86. begin
  87. cconvert := my_cconvert_ptr (cinfo^.cconvert);
  88. { Allocate and fill in the conversion tables. }
  89. rgb_ycc_tab := INT32_FIELD_PTR(
  90. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  91. (TABLE_SIZE * SIZEOF(INT32))) );
  92. cconvert^.rgb_ycc_tab := rgb_ycc_tab;
  93. for i := 0 to MAXJSAMPLE do
  94. begin
  95. rgb_ycc_tab^[i+R_Y_OFF] := FIX_0_29900 * i;
  96. rgb_ycc_tab^[i+G_Y_OFF] := FIX_0_58700 * i;
  97. rgb_ycc_tab^[i+B_Y_OFF] := FIX_0_11400 * i + ONE_HALF;
  98. rgb_ycc_tab^[i+R_CB_OFF] := (-FIX_0_16874) * i;
  99. rgb_ycc_tab^[i+G_CB_OFF] := (-FIX_0_33126) * i;
  100. { We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
  101. This ensures that the maximum output will round to MAXJSAMPLE
  102. not MAXJSAMPLE+1, and thus that we don't have to range-limit. }
  103. rgb_ycc_tab^[i+B_CB_OFF] := FIX_0_50000 * i + CBCR_OFFSET + ONE_HALF-1;
  104. { B=>Cb and R=>Cr tables are the same
  105. rgb_ycc_tab^[i+R_CR_OFF] := FIX_0_50000 * i + CBCR_OFFSET + ONE_HALF-1;
  106. }
  107. rgb_ycc_tab^[i+G_CR_OFF] := (-FIX_0_41869) * i;
  108. rgb_ycc_tab^[i+B_CR_OFF] := (-FIX_0_08131) * i;
  109. end;
  110. end;
  111. { Convert some rows of samples to the JPEG colorspace.
  112. Note that we change from the application's interleaved-pixel format
  113. to our internal noninterleaved, one-plane-per-component format.
  114. The input buffer is therefore three times as wide as the output buffer.
  115. A starting row offset is provided only for the output buffer. The caller
  116. can easily adjust the passed input_buf value to accommodate any row
  117. offset required on that side. }
  118. {METHODDEF}
  119. procedure rgb_ycc_convert (cinfo : j_compress_ptr;
  120. input_buf : JSAMPARRAY;
  121. output_buf : JSAMPIMAGE;
  122. output_row : JDIMENSION;
  123. num_rows : int);
  124. var
  125. cconvert : my_cconvert_ptr;
  126. {register} r, g, b : int;
  127. {register} ctab : INT32_FIELD_PTR;
  128. {register} inptr : JSAMPROW;
  129. {register} outptr0, outptr1, outptr2 : JSAMPROW;
  130. {register} col : JDIMENSION;
  131. num_cols : JDIMENSION;
  132. begin
  133. cconvert := my_cconvert_ptr (cinfo^.cconvert);
  134. ctab := cconvert^.rgb_ycc_tab;
  135. num_cols := cinfo^.image_width;
  136. while (num_rows > 0) do
  137. begin
  138. Dec(num_rows);
  139. inptr := input_buf^[0];
  140. Inc(JSAMPROW_PTR(input_buf));
  141. outptr0 := output_buf^[0]^[output_row];
  142. outptr1 := output_buf^[1]^[output_row];
  143. outptr2 := output_buf^[2]^[output_row];
  144. Inc(output_row);
  145. for col := 0 to pred(num_cols) do
  146. begin
  147. r := GETJSAMPLE(inptr^[RGB_RED]);
  148. g := GETJSAMPLE(inptr^[RGB_GREEN]);
  149. b := GETJSAMPLE(inptr^[RGB_BLUE]);
  150. Inc(JSAMPLE_PTR(inptr), RGB_PIXELSIZE);
  151. { If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  152. must be too; we do not need an explicit range-limiting operation.
  153. Hence the value being shifted is never negative, and we don't
  154. need the general RIGHT_SHIFT macro. }
  155. { Y }
  156. outptr0^[col] := JSAMPLE(
  157. ((ctab^[r+R_Y_OFF] + ctab^[g+G_Y_OFF] + ctab^[b+B_Y_OFF])
  158. shr SCALEBITS) );
  159. { Cb }
  160. outptr1^[col] := JSAMPLE(
  161. ((ctab^[r+R_CB_OFF] + ctab^[g+G_CB_OFF] + ctab^[b+B_CB_OFF])
  162. shr SCALEBITS) );
  163. { Cr }
  164. outptr2^[col] := JSAMPLE(
  165. ((ctab^[r+R_CR_OFF] + ctab^[g+G_CR_OFF] + ctab^[b+B_CR_OFF])
  166. shr SCALEBITS) );
  167. end;
  168. end;
  169. end;
  170. {*************** Cases other than RGB -> YCbCr *************}
  171. { Convert some rows of samples to the JPEG colorspace.
  172. This version handles RGB -> grayscale conversion, which is the same
  173. as the RGB -> Y portion of RGB -> YCbCr.
  174. We assume rgb_ycc_start has been called (we only use the Y tables). }
  175. {METHODDEF}
  176. procedure rgb_gray_convert (cinfo : j_compress_ptr;
  177. input_buf : JSAMPARRAY;
  178. output_buf : JSAMPIMAGE;
  179. output_row : JDIMENSION;
  180. num_rows : int);
  181. var
  182. cconvert : my_cconvert_ptr;
  183. {register} r, g, b : int;
  184. {register} ctab :INT32_FIELD_PTR;
  185. {register} inptr : JSAMPROW;
  186. {register} outptr : JSAMPROW;
  187. {register} col : JDIMENSION;
  188. num_cols : JDIMENSION;
  189. begin
  190. cconvert := my_cconvert_ptr (cinfo^.cconvert);
  191. ctab := cconvert^.rgb_ycc_tab;
  192. num_cols := cinfo^.image_width;
  193. while (num_rows > 0) do
  194. begin
  195. Dec(num_rows);
  196. inptr := input_buf[0];
  197. Inc(JSAMPROW_PTR(input_buf));
  198. outptr := output_buf[0][output_row];
  199. Inc(output_row);
  200. for col := 0 to num_cols - 1 do
  201. begin
  202. r := GETJSAMPLE(inptr[RGB_RED]);
  203. g := GETJSAMPLE(inptr[RGB_GREEN]);
  204. b := GETJSAMPLE(inptr[RGB_BLUE]);
  205. Inc(JSAMPLE_PTR(inptr), RGB_PIXELSIZE);
  206. (* Y *)
  207. // kylix 3 compiler crashes on this
  208. // it also crashes Delphi OSX compiler 9 years later :(
  209. {$IF not (Defined(DCC) and not Defined(MSWINDOWS))}
  210. outptr[col] := JSAMPLE(((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) shr SCALEBITS));
  211. {$IFEND}
  212. end;
  213. end;
  214. end;
  215. { Convert some rows of samples to the JPEG colorspace.
  216. This version handles Adobe-style CMYK -> YCCK conversion,
  217. where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same
  218. conversion as above, while passing K (black) unchanged.
  219. We assume rgb_ycc_start has been called. }
  220. {METHODDEF}
  221. procedure cmyk_ycck_convert (cinfo : j_compress_ptr;
  222. input_buf : JSAMPARRAY;
  223. output_buf : JSAMPIMAGE;
  224. output_row : JDIMENSION;
  225. num_rows : int);
  226. var
  227. cconvert : my_cconvert_ptr;
  228. {register} r, g, b : int;
  229. {register} ctab : INT32_FIELD_PTR;
  230. {register} inptr : JSAMPROW;
  231. {register} outptr0, outptr1, outptr2, outptr3 : JSAMPROW;
  232. {register} col : JDIMENSION;
  233. num_cols : JDIMENSION;
  234. begin
  235. cconvert := my_cconvert_ptr (cinfo^.cconvert);
  236. ctab := cconvert^.rgb_ycc_tab;
  237. num_cols := cinfo^.image_width;
  238. while (num_rows > 0) do
  239. begin
  240. Dec(num_rows);
  241. inptr := input_buf^[0];
  242. Inc(JSAMPROW_PTR(input_buf));
  243. outptr0 := output_buf^[0]^[output_row];
  244. outptr1 := output_buf^[1]^[output_row];
  245. outptr2 := output_buf^[2]^[output_row];
  246. outptr3 := output_buf^[3]^[output_row];
  247. Inc(output_row);
  248. for col := 0 to pred(num_cols) do
  249. begin
  250. r := MAXJSAMPLE - GETJSAMPLE(inptr^[0]);
  251. g := MAXJSAMPLE - GETJSAMPLE(inptr^[1]);
  252. b := MAXJSAMPLE - GETJSAMPLE(inptr^[2]);
  253. { K passes through as-is }
  254. outptr3^[col] := inptr^[3]; { don't need GETJSAMPLE here }
  255. Inc(JSAMPLE_PTR(inptr), 4);
  256. { If the inputs are 0..MAXJSAMPLE, the outputs of these equations
  257. must be too; we do not need an explicit range-limiting operation.
  258. Hence the value being shifted is never negative, and we don't
  259. need the general RIGHT_SHIFT macro. }
  260. { Y }
  261. outptr0^[col] := JSAMPLE (
  262. ((ctab^[r+R_Y_OFF] + ctab^[g+G_Y_OFF] + ctab^[b+B_Y_OFF])
  263. shr SCALEBITS) );
  264. { Cb }
  265. outptr1^[col] := JSAMPLE(
  266. ((ctab^[r+R_CB_OFF] + ctab^[g+G_CB_OFF] + ctab^[b+B_CB_OFF])
  267. shr SCALEBITS) );
  268. { Cr }
  269. outptr2^[col] := JSAMPLE (
  270. ((ctab^[r+R_CR_OFF] + ctab^[g+G_CR_OFF] + ctab^[b+B_CR_OFF])
  271. shr SCALEBITS) );
  272. end;
  273. end;
  274. end;
  275. { Convert some rows of samples to the JPEG colorspace.
  276. This version handles grayscale output with no conversion.
  277. The source can be either plain grayscale or YCbCr (since Y = gray). }
  278. {METHODDEF}
  279. procedure grayscale_convert (cinfo : j_compress_ptr;
  280. input_buf : JSAMPARRAY;
  281. output_buf : JSAMPIMAGE;
  282. output_row : JDIMENSION;
  283. num_rows: int);
  284. var
  285. {register} inptr : JSAMPROW;
  286. {register} outptr : JSAMPROW;
  287. {register} col : JDIMENSION;
  288. num_cols :JDIMENSION;
  289. instride : int;
  290. begin
  291. num_cols := cinfo^.image_width;
  292. instride := cinfo^.input_components;
  293. while (num_rows > 0) do
  294. begin
  295. Dec(num_rows);
  296. inptr := input_buf^[0];
  297. Inc(JSAMPROW_PTR(input_buf));
  298. outptr := output_buf^[0]^[output_row];
  299. Inc(output_row);
  300. for col := 0 to pred(num_cols) do
  301. begin
  302. outptr^[col] := inptr^[0]; { don't need GETJSAMPLE() here }
  303. Inc(JSAMPLE_PTR(inptr), instride);
  304. end;
  305. end;
  306. end;
  307. { Convert some rows of samples to the JPEG colorspace.
  308. This version handles multi-component colorspaces without conversion.
  309. We assume input_components = num_components. }
  310. {METHODDEF}
  311. procedure null_convert (cinfo : j_compress_ptr;
  312. input_buf : JSAMPARRAY;
  313. output_buf : JSAMPIMAGE;
  314. output_row : JDIMENSION;
  315. num_rows : int);
  316. var
  317. {register} inptr : JSAMPROW;
  318. {register} outptr : JSAMPROW;
  319. {register} col : JDIMENSION;
  320. {register} ci : int;
  321. nc : int;
  322. num_cols : JDIMENSION;
  323. begin
  324. nc := cinfo^.num_components;
  325. num_cols := cinfo^.image_width;
  326. while (num_rows > 0) do
  327. begin
  328. Dec(num_rows);
  329. { It seems fastest to make a separate pass for each component. }
  330. for ci := 0 to pred(nc) do
  331. begin
  332. inptr := input_buf^[0];
  333. outptr := output_buf^[ci]^[output_row];
  334. for col := 0 to pred(num_cols) do
  335. begin
  336. outptr^[col] := inptr^[ci]; { don't need GETJSAMPLE() here }
  337. Inc(JSAMPLE_PTR(inptr), nc);
  338. end;
  339. end;
  340. Inc(JSAMPROW_PTR(input_buf));
  341. Inc(output_row);
  342. end;
  343. end;
  344. { Empty method for start_pass. }
  345. {METHODDEF}
  346. procedure null_method (cinfo : j_compress_ptr);
  347. begin
  348. { no work needed }
  349. end;
  350. { Module initialization routine for input colorspace conversion. }
  351. {GLOBAL}
  352. procedure jinit_color_converter (cinfo : j_compress_ptr);
  353. var
  354. cconvert : my_cconvert_ptr;
  355. begin
  356. cconvert := my_cconvert_ptr(
  357. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  358. SIZEOF(my_color_converter)) );
  359. cinfo^.cconvert := jpeg_color_converter_ptr(cconvert);
  360. { set start_pass to null method until we find out differently }
  361. cconvert^.pub.start_pass := null_method;
  362. { Make sure input_components agrees with in_color_space }
  363. case (cinfo^.in_color_space) of
  364. JCS_GRAYSCALE:
  365. if (cinfo^.input_components <> 1) then
  366. ERREXIT(j_common_ptr(cinfo), JERR_BAD_IN_COLORSPACE);
  367. {$ifdef RGB_PIXELSIZE <> 3}
  368. JCS_RGB:
  369. if (cinfo^.input_components <> RGB_PIXELSIZE) then
  370. ERREXIT(j_common_ptr(cinfo), JERR_BAD_IN_COLORSPACE);
  371. {$else} { share code with YCbCr }
  372. JCS_RGB,
  373. {$endif}
  374. JCS_YCbCr:
  375. if (cinfo^.input_components <> 3) then
  376. ERREXIT(j_common_ptr(cinfo), JERR_BAD_IN_COLORSPACE);
  377. JCS_CMYK,
  378. JCS_YCCK:
  379. if (cinfo^.input_components <> 4) then
  380. ERREXIT(j_common_ptr(cinfo), JERR_BAD_IN_COLORSPACE);
  381. else { JCS_UNKNOWN can be anything }
  382. if (cinfo^.input_components < 1) then
  383. ERREXIT(j_common_ptr(cinfo), JERR_BAD_IN_COLORSPACE);
  384. end;
  385. { Check num_components, set conversion method based on requested space }
  386. case (cinfo^.jpeg_color_space) of
  387. JCS_GRAYSCALE:
  388. begin
  389. if (cinfo^.num_components <> 1) then
  390. ERREXIT(j_common_ptr(cinfo), JERR_BAD_J_COLORSPACE);
  391. if (cinfo^.in_color_space = JCS_GRAYSCALE) then
  392. cconvert^.pub.color_convert := grayscale_convert
  393. else
  394. if (cinfo^.in_color_space = JCS_RGB) then
  395. begin
  396. cconvert^.pub.start_pass := rgb_ycc_start;
  397. cconvert^.pub.color_convert := rgb_gray_convert;
  398. end
  399. else
  400. if (cinfo^.in_color_space = JCS_YCbCr) then
  401. cconvert^.pub.color_convert := grayscale_convert
  402. else
  403. ERREXIT(j_common_ptr(cinfo), JERR_CONVERSION_NOTIMPL);
  404. end;
  405. JCS_RGB:
  406. begin
  407. if (cinfo^.num_components <> 3) then
  408. ERREXIT(j_common_ptr(cinfo), JERR_BAD_J_COLORSPACE);
  409. if (cinfo^.in_color_space = JCS_RGB) and (RGB_PIXELSIZE = 3) then
  410. cconvert^.pub.color_convert := null_convert
  411. else
  412. ERREXIT(j_common_ptr(cinfo), JERR_CONVERSION_NOTIMPL);
  413. end;
  414. JCS_YCbCr:
  415. begin
  416. if (cinfo^.num_components <> 3) then
  417. ERREXIT(j_common_ptr(cinfo), JERR_BAD_J_COLORSPACE);
  418. if (cinfo^.in_color_space = JCS_RGB) then
  419. begin
  420. cconvert^.pub.start_pass := rgb_ycc_start;
  421. cconvert^.pub.color_convert := rgb_ycc_convert;
  422. end
  423. else
  424. if (cinfo^.in_color_space = JCS_YCbCr) then
  425. cconvert^.pub.color_convert := null_convert
  426. else
  427. ERREXIT(j_common_ptr(cinfo), JERR_CONVERSION_NOTIMPL);
  428. end;
  429. JCS_CMYK:
  430. begin
  431. if (cinfo^.num_components <> 4) then
  432. ERREXIT(j_common_ptr(cinfo), JERR_BAD_J_COLORSPACE);
  433. if (cinfo^.in_color_space = JCS_CMYK) then
  434. cconvert^.pub.color_convert := null_convert
  435. else
  436. ERREXIT(j_common_ptr(cinfo), JERR_CONVERSION_NOTIMPL);
  437. end;
  438. JCS_YCCK:
  439. begin
  440. if (cinfo^.num_components <> 4) then
  441. ERREXIT(j_common_ptr(cinfo), JERR_BAD_J_COLORSPACE);
  442. if (cinfo^.in_color_space = JCS_CMYK) then
  443. begin
  444. cconvert^.pub.start_pass := rgb_ycc_start;
  445. cconvert^.pub.color_convert := cmyk_ycck_convert;
  446. end
  447. else
  448. if (cinfo^.in_color_space = JCS_YCCK) then
  449. cconvert^.pub.color_convert := null_convert
  450. else
  451. ERREXIT(j_common_ptr(cinfo), JERR_CONVERSION_NOTIMPL);
  452. end;
  453. else { allow null conversion of JCS_UNKNOWN }
  454. begin
  455. if (cinfo^.jpeg_color_space <> cinfo^.in_color_space) or
  456. (cinfo^.num_components <> cinfo^.input_components) then
  457. ERREXIT(j_common_ptr(cinfo), JERR_CONVERSION_NOTIMPL);
  458. cconvert^.pub.color_convert := null_convert;
  459. end;
  460. end;
  461. end;
  462. end.