home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 November / CDVD1105.ISO / Software / Freeware / programare / graphics32 / GR32_Filters.pas < prev    next >
Pascal/Delphi Source File  |  2004-12-19  |  6KB  |  243 lines

  1. unit GR32_Filters;
  2.  
  3. (* ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is Graphics32
  17.  *
  18.  * The Initial Developer of the Original Code is
  19.  * Alex A. Denisov
  20.  *
  21.  * Portions created by the Initial Developer are Copyright (C) 2000-2004
  22.  * the Initial Developer. All Rights Reserved.
  23.  *
  24.  * Contributor(s):
  25.  *
  26.  * ***** END LICENSE BLOCK ***** *)
  27.  
  28. interface
  29.  
  30. {$I GR32.inc}
  31.  
  32. uses
  33.   {$IFDEF CLX}
  34.   Qt, Types, {$IFDEF LINUX}Libc, {$ENDIF}
  35.   {$ELSE}
  36.   Windows,
  37.   {$ENDIF}
  38.   Classes, SysUtils, GR32, GR32_Blend, GR32_ByteMaps;
  39.  
  40. { Basic processing }
  41. type
  42.   TLUT8 = array [Byte] of Byte;
  43.  
  44. procedure AlphaToGrayscale(Dst, Src: TBitmap32);
  45. procedure IntensityToAlpha(Dst, Src: TBitmap32);
  46. procedure Invert(Dst, Src: TBitmap32);
  47. procedure InvertRGB(Dst, Src: TBitmap32);
  48. procedure ColorToGrayscale(Dst, Src: TBitmap32; PreserveAlpha: Boolean = False);
  49. procedure ApplyLUT(Dst, Src: TBitmap32; const LUT: TLUT8; PreserveAlpha: Boolean = False);
  50. procedure CromaKey(ABitmap: TBitmap32; TrColor: TColor32);
  51.  
  52. procedure CheckParams(Dst, Src: TBitmap32);
  53.  
  54. implementation
  55.  
  56. const
  57.   SEmptySource = 'The source is nil';
  58.   SEmptyDestination = 'Destination is nil';
  59.   SNoInPlace = 'In-place operation is unsupported';
  60.  
  61. procedure CheckParams(Dst, Src: TBitmap32);
  62. begin
  63.   if Src = nil then raise Exception.Create(SEmptySource);
  64.   if Dst = nil then raise Exception.Create(SEmptyDestination);
  65.   Dst.SetSize(Src.Width, Src.Height);
  66. end;
  67.  
  68. {rocedure CheckParamsNoInPlace(Dst, Src: TBitmap32);
  69. begin
  70.   if (Src = nil) then
  71.     raise Exception.Create(SEmptySource);
  72.   if Dst = nil then
  73.     raise Exception.Create(SEmptyDestination);
  74.   if Dst = Src then
  75.     raise Exception.Create(SNoInPlace);
  76.   Dst.SetSize(Src);
  77. end;       }
  78.  
  79. procedure AlphaToGrayscale(Dst, Src: TBitmap32);
  80. var
  81.   I: Integer;
  82.   D, S: PColor32;
  83. begin
  84.   CheckParams(Dst, Src);
  85.   Dst.SetSize(Src.Width, Src.Height);
  86.   D := @Dst.Bits[0];
  87.   S := @Src.Bits[0];
  88.   for I := 0 to Src.Width * Src.Height - 1 do
  89.   begin
  90.     D^ := Gray32(AlphaComponent(S^));
  91.     Inc(S); Inc(D);
  92.   end;
  93.   Dst.Changed;
  94. end;
  95.  
  96. procedure IntensityToAlpha(Dst, Src: TBitmap32);
  97. var
  98.   I: Integer;
  99.   D, S: PColor32;
  100. begin
  101.   CheckParams(Dst, Src);
  102.   Dst.SetSize(Src.Width, Src.Height);
  103.   D := @Dst.Bits[0];
  104.   S := @Src.Bits[0];
  105.   for I := 0 to Src.Width * Src.Height - 1 do
  106.   begin
  107.     D^ := SetAlpha(D^, Intensity(S^));
  108.     Inc(S); Inc(D);
  109.   end;
  110.   Dst.Changed;
  111. end;
  112.  
  113. procedure Invert(Dst, Src: TBitmap32);
  114. var
  115.   I: Integer;
  116.   D, S: PColor32;
  117. begin
  118.   CheckParams(Dst, Src);
  119.   Dst.SetSize(Src.Width, Src.Height);
  120.   D := @Dst.Bits[0];
  121.   S := @Src.Bits[0];
  122.   for I := 0 to Src.Width * Src.Height - 1 do
  123.   begin
  124.     D^ := S^ xor $FFFFFFFF;
  125.     Inc(S); Inc(D);
  126.   end;
  127.   Dst.Changed;
  128. end;
  129.  
  130. procedure InvertRGB(Dst, Src: TBitmap32);
  131. var
  132.   I: Integer;
  133.   D, S: PColor32;
  134. begin
  135.   CheckParams(Dst, Src);
  136.   Dst.SetSize(Src.Width, Src.Height);
  137.   D := @Dst.Bits[0];
  138.   S := @Src.Bits[0];
  139.   for I := 0 to Src.Width * Src.Height - 1 do
  140.   begin
  141.     D^ := S^ xor $00FFFFFF;
  142.     Inc(S); Inc(D);
  143.   end;
  144.   Dst.Changed;
  145. end;
  146.  
  147. procedure ColorToGrayscale(Dst, Src: TBitmap32; PreserveAlpha: Boolean = False);
  148. var
  149.   I: Integer;
  150.   D, S: PColor32;
  151. begin
  152.   CheckParams(Dst, Src);
  153.   Dst.SetSize(Src.Width, Src.Height);
  154.   D := @Dst.Bits[0];
  155.   S := @Src.Bits[0];
  156.   
  157.   if PreserveAlpha then
  158.     for I := 0 to Src.Width * Src.Height - 1 do
  159.     begin
  160.       D^ := Gray32(Intensity(S^), AlphaComponent(S^));
  161.       Inc(S); Inc(D);
  162.     end
  163.   else
  164.     for I := 0 to Src.Width * Src.Height - 1 do
  165.     begin
  166.       D^ := Gray32(Intensity(S^));
  167.       Inc(S); Inc(D);
  168.     end;
  169.     
  170.   Dst.Changed;
  171. end;
  172.  
  173. procedure ApplyLUT(Dst, Src: TBitmap32; const LUT: TLUT8; PreserveAlpha: Boolean = False);
  174. var
  175.   I: Integer;
  176.   D, S: PColor32;
  177.   a, r, g, b: TColor32;
  178.   C: TColor32;
  179. begin
  180.   CheckParams(Dst, Src);
  181.   Dst.SetSize(Src.Width, Src.Height);
  182.   D := @Dst.Bits[0];
  183.   S := @Src.Bits[0];
  184.  
  185.   if PreserveAlpha then
  186.     for I := 0 to Src.Width * Src.Height - 1 do
  187.     begin
  188.       C := S^;
  189.       a := C and $FF000000;
  190.       r := C and $00FF0000;
  191.       g := C and $0000FF00;
  192.       a := a shr 24;
  193.       r := r shr 16;
  194.       b := C and $000000FF;
  195.       g := g shr 8;
  196.       r := LUT[r];
  197.       g := LUT[g];
  198.       b := LUT[b];
  199.       D^ := a shl 24 or r shl 16 or g shl 8 or b;
  200.       Inc(S);
  201.       Inc(D);
  202.     end
  203.   else
  204.     for I := 0 to Src.Width * Src.Height - 1 do
  205.     begin
  206.       C := S^;
  207.       r := C and $00FF0000;
  208.       g := C and $0000FF00;
  209.       r := r shr 16;
  210.       b := C and $000000FF;
  211.       g := g shr 8;
  212.       r := LUT[r];
  213.       g := LUT[g];
  214.       b := LUT[b];
  215.       D^ := $FF000000 or r shl 16 or g shl 8 or b;
  216.       Inc(S);
  217.       Inc(D);
  218.     end;
  219.     
  220.   Dst.Changed;
  221. end;
  222.  
  223. procedure CromaKey(ABitmap: TBitmap32; TrColor: TColor32);
  224. var
  225.   P: PColor32;
  226.   C: TColor32;
  227.   I: Integer;
  228. begin
  229.   TrColor := TrColor and $00FFFFFF;
  230.   with ABitmap do
  231.   begin
  232.     P := PixelPtr[0, 0];
  233.     for I := 0 to Width * Height - 1 do
  234.     begin
  235.       C := P^ and $00FFFFFF;
  236.       if C = TrColor then P^ := C;
  237.       Inc(P)
  238.     end;
  239.   end;
  240. end;
  241.  
  242. end.
  243.