ImagingColors.pas 6.5 KB

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