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.

246 lines
7.7 KiB

3 years ago
  1. {
  2. Vampyre Imaging Library
  3. by Marek Mauder
  4. http://imaginglib.sourceforge.net
  5. The contents of this file are used with permission, subject to the Mozilla
  6. Public License Version 1.1 (the "License"); you may not use this file except
  7. in compliance with the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/MPL-1.1.html
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  11. the specific language governing rights and limitations under the License.
  12. Alternatively, the contents of this file may be used under the terms of the
  13. GNU Lesser General Public License (the "LGPL License"), in which case the
  14. provisions of the LGPL License are applicable instead of those above.
  15. If you wish to allow use of your version of this file only under the terms
  16. of the LGPL License and not to allow others to use your version of this file
  17. under the MPL, indicate your decision by deleting the provisions above and
  18. replace them with the notice and other provisions required by the LGPL
  19. License. If you do not delete the provisions above, a recipient may use
  20. your version of this file under either the MPL or the LGPL License.
  21. For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html
  22. }
  23. { This unit contains functions for manipulating and converting color values.}
  24. unit ImagingColors;
  25. interface
  26. {$I ImagingOptions.inc}
  27. uses
  28. SysUtils, ImagingTypes, ImagingUtility;
  29. { Converts RGB color to YUV.}
  30. procedure RGBToYUV(R, G, B: Byte; var Y, U, V: Byte);
  31. { Converts YIV to RGB color.}
  32. procedure YUVToRGB(Y, U, V: Byte; var R, G, B: Byte);
  33. { Converts RGB color to YCbCr as used in JPEG.}
  34. procedure RGBToYCbCr(R, G, B: Byte; var Y, Cb, Cr: Byte);
  35. { Converts YCbCr as used in JPEG to RGB color.}
  36. procedure YCbCrToRGB(Y, Cb, Cr: Byte; var R, G, B: Byte);
  37. { Converts RGB color to YCbCr as used in JPEG.}
  38. procedure RGBToYCbCr16(R, G, B: Word; var Y, Cb, Cr: Word);
  39. { Converts YCbCr as used in JPEG to RGB color.}
  40. procedure YCbCrToRGB16(Y, Cb, Cr: Word; var R, G, B: Word);
  41. { Converts RGB color to CMY.}
  42. procedure RGBToCMY(R, G, B: Byte; var C, M, Y: Byte);
  43. { Converts CMY to RGB color.}
  44. procedure CMYToRGB(C, M, Y: Byte; var R, G, B: Byte);
  45. { Converts RGB color to CMY.}
  46. procedure RGBToCMY16(R, G, B: Word; var C, M, Y: Word);
  47. { Converts CMY to RGB color.}
  48. procedure CMYToRGB16(C, M, Y: Word; var R, G, B: Word);
  49. { Converts RGB color to CMYK.}
  50. procedure RGBToCMYK(R, G, B: Byte; var C, M, Y, K: Byte);
  51. { Converts CMYK to RGB color.}
  52. procedure CMYKToRGB(C, M, Y, K: Byte; var R, G, B: Byte);
  53. { Converts RGB color to CMYK.}
  54. procedure RGBToCMYK16(R, G, B: Word; var C, M, Y, K: Word);
  55. { Converts CMYK to RGB color.}
  56. procedure CMYKToRGB16(C, M, Y, K: Word; var R, G, B: Word);
  57. { Converts RGB color to YCoCg.}
  58. procedure RGBToYCoCg(R, G, B: Byte; var Y, Co, Cg: Byte);
  59. { Converts YCoCg to RGB color.}
  60. procedure YCoCgToRGB(Y, Co, Cg: Byte; var R, G, B: Byte);
  61. //procedure RGBToHSL(R, G, B: Byte; var H, S, L: Byte);
  62. //procedure HSLToRGB(H, S, L: Byte; var R, G, B: Byte);
  63. implementation
  64. procedure RGBToYUV(R, G, B: Byte; var Y, U, V: Byte);
  65. begin
  66. Y := ClampToByte(Round( 0.257 * R + 0.504 * G + 0.098 * B) + 16);
  67. V := ClampToByte(Round( 0.439 * R - 0.368 * G - 0.071 * B) + 128);
  68. U := ClampToByte(Round(-0.148 * R - 0.291 * G + 0.439 * B) + 128);
  69. end;
  70. procedure YUVToRGB(Y, U, V: Byte; var R, G, B: Byte);
  71. var
  72. CY, CU, CV: LongInt;
  73. begin
  74. CY := Y - 16;
  75. CU := U - 128;
  76. CV := V - 128;
  77. R := ClampToByte(Round(1.164 * CY - 0.002 * CU + 1.596 * CV));
  78. G := ClampToByte(Round(1.164 * CY - 0.391 * CU - 0.813 * CV));
  79. B := ClampToByte(Round(1.164 * CY + 2.018 * CU - 0.001 * CV));
  80. end;
  81. procedure RGBToYCbCr(R, G, B: Byte; var Y, Cb, Cr: Byte);
  82. begin
  83. Y := ClampToByte(Round( 0.29900 * R + 0.58700 * G + 0.11400 * B));
  84. Cb := ClampToByte(Round(-0.16874 * R - 0.33126 * G + 0.50000 * B + 128));
  85. Cr := ClampToByte(Round( 0.50000 * R - 0.41869 * G - 0.08131 * B + 128));
  86. end;
  87. procedure YCbCrToRGB(Y, Cb, Cr: Byte; var R, G, B: Byte);
  88. begin
  89. R := ClampToByte(Round(Y + 1.40200 * (Cr - 128)));
  90. G := ClampToByte(Round(Y - 0.34414 * (Cb - 128) - 0.71414 * (Cr - 128)));
  91. B := ClampToByte(Round(Y + 1.77200 * (Cb - 128)));
  92. end;
  93. procedure RGBToYCbCr16(R, G, B: Word; var Y, Cb, Cr: Word);
  94. begin
  95. Y := ClampToWord(Round( 0.29900 * R + 0.58700 * G + 0.11400 * B));
  96. Cb := ClampToWord(Round(-0.16874 * R - 0.33126 * G + 0.50000 * B + 32768));
  97. Cr := ClampToWord(Round( 0.50000 * R - 0.41869 * G - 0.08131 * B + 32768));
  98. end;
  99. procedure YCbCrToRGB16(Y, Cb, Cr: Word; var R, G, B: Word);
  100. begin
  101. R := ClampToWord(Round(Y + 1.40200 * (Cr - 32768)));
  102. G := ClampToWord(Round(Y - 0.34414 * (Cb - 32768) - 0.71414 * (Cr - 32768)));
  103. B := ClampToWord(Round(Y + 1.77200 * (Cb - 32768)));
  104. end;
  105. procedure RGBToCMY(R, G, B: Byte; var C, M, Y: Byte);
  106. begin
  107. C := 255 - R;
  108. M := 255 - G;
  109. Y := 255 - B;
  110. end;
  111. procedure CMYToRGB(C, M, Y: Byte; var R, G, B: Byte);
  112. begin
  113. R := 255 - C;
  114. G := 255 - M;
  115. B := 255 - Y;
  116. end;
  117. procedure RGBToCMY16(R, G, B: Word; var C, M, Y: Word);
  118. begin
  119. C := 65535 - R;
  120. M := 65535 - G;
  121. Y := 65535 - B;
  122. end;
  123. procedure CMYToRGB16(C, M, Y: Word; var R, G, B: Word);
  124. begin
  125. R := 65535 - C;
  126. G := 65535 - M;
  127. B := 65535 - Y;
  128. end;
  129. procedure RGBToCMYK(R, G, B: Byte; var C, M, Y, K: Byte);
  130. begin
  131. RGBToCMY(R, G, B, C, M, Y);
  132. K := Min(C, Min(M, Y));
  133. if K = 255 then
  134. begin
  135. C := 0;
  136. M := 0;
  137. Y := 0;
  138. end
  139. else
  140. begin
  141. C := ClampToByte(Round((C - K) / (255 - K) * 255));
  142. M := ClampToByte(Round((M - K) / (255 - K) * 255));
  143. Y := ClampToByte(Round((Y - K) / (255 - K) * 255));
  144. end;
  145. end;
  146. procedure CMYKToRGB(C, M, Y, K: Byte; var R, G, B: Byte);
  147. begin
  148. R := (255 - (C - MulDiv(C, K, 255) + K));
  149. G := (255 - (M - MulDiv(M, K, 255) + K));
  150. B := (255 - (Y - MulDiv(Y, K, 255) + K));
  151. end;
  152. procedure RGBToCMYK16(R, G, B: Word; var C, M, Y, K: Word);
  153. begin
  154. RGBToCMY16(R, G, B, C, M, Y);
  155. K := Min(C, Min(M, Y));
  156. if K = 65535 then
  157. begin
  158. C := 0;
  159. M := 0;
  160. Y := 0;
  161. end
  162. else
  163. begin
  164. C := ClampToWord(Round((C - K) / (65535 - K) * 65535));
  165. M := ClampToWord(Round((M - K) / (65535 - K) * 65535));
  166. Y := ClampToWord(Round((Y - K) / (65535 - K) * 65535));
  167. end;
  168. end;
  169. procedure CMYKToRGB16(C, M, Y, K: Word; var R, G, B: Word);
  170. begin
  171. R := 65535 - (C - MulDiv(C, K, 65535) + K);
  172. G := 65535 - (M - MulDiv(M, K, 65535) + K);
  173. B := 65535 - (Y - MulDiv(Y, K, 65535) + K);
  174. end;
  175. procedure RGBToYCoCg(R, G, B: Byte; var Y, Co, Cg: Byte);
  176. begin
  177. // C and Delphi's SHR behaviour differs for negative numbers, use div instead.
  178. Y := ClampToByte(( R + G shl 1 + B + 2) div 4);
  179. Co := ClampToByte(( R shl 1 - B shl 1 + 2) div 4 + 128);
  180. Cg := ClampToByte((-R + G shl 1 - B + 2) div 4 + 128);
  181. end;
  182. procedure YCoCgToRGB(Y, Co, Cg: Byte; var R, G, B: Byte);
  183. var
  184. CoInt, CgInt: Integer;
  185. begin
  186. CoInt := Co - 128;
  187. CgInt := Cg - 128;
  188. R := ClampToByte(Y + CoInt - CgInt);
  189. G := ClampToByte(Y + CgInt);
  190. B := ClampToByte(Y - CoInt - CgInt);
  191. end;
  192. {
  193. File Notes:
  194. -- TODOS ----------------------------------------------------
  195. - nothing now
  196. -- 0.26.3 Changes/Bug Fixes ---------------------------------
  197. - Added RGB<>YCoCg conversion functions.
  198. - Fixed RGB>>CMYK conversions.
  199. -- 0.23 Changes/Bug Fixes -----------------------------------
  200. - Added RGB<>CMY(K) converion functions for 16 bit channels
  201. (needed by PSD loading code).
  202. -- 0.21 Changes/Bug Fixes -----------------------------------
  203. - Added some color space conversion functions and LUTs
  204. (RGB/YUV/YCrCb/CMY/CMYK).
  205. -- 0.17 Changes/Bug Fixes -----------------------------------
  206. - unit created (empty!)
  207. }
  208. end.