MainUnit.pas 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. unit MainUnit;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  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
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is PixelCombine Example
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Alex A. Denisov
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2000-2005
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. *
  32. * ***** END LICENSE BLOCK ***** *)
  33. interface
  34. {$include GR32.inc}
  35. uses
  36. {$IFNDEF FPC} Windows, {$ELSE} LCLIntf, LCLType, LResources, {$ENDIF}
  37. SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
  38. GR32,
  39. GR32_Image,
  40. GR32_Layers;
  41. type
  42. TFormPixelCombine = class(TForm)
  43. ImgView: TImgView32;
  44. RadioGroup: TRadioGroup;
  45. procedure FormCreate(Sender: TObject);
  46. procedure RadioGroupClick(Sender: TObject);
  47. protected
  48. procedure PC_Add(F: TColor32; var B: TColor32; M: Cardinal);
  49. procedure PC_Sub(F: TColor32; var B: TColor32; M: Cardinal);
  50. procedure PC_Modulate(F: TColor32; var B: TColor32; M: Cardinal);
  51. procedure PC_Min(F: TColor32; var B: TColor32; M: Cardinal);
  52. procedure PC_Max(F: TColor32; var B: TColor32; M: Cardinal);
  53. procedure PC_Screen(F: TColor32; var B: TColor32; M: Cardinal);
  54. procedure PC_ColorBurn(F: TColor32; var B: TColor32; M: Cardinal);
  55. procedure PC_ColorDodge(F: TColor32; var B: TColor32; M: Cardinal);
  56. procedure PC_Difference(F: TColor32; var B: TColor32; M: Cardinal);
  57. procedure PC_Exclusion(F: TColor32; var B: TColor32; M: Cardinal);
  58. procedure PC_Pattern(F: TColor32; var B: TColor32; M: Cardinal);
  59. procedure PC_Blend(F: TColor32; var B: TColor32; M: Cardinal);
  60. procedure PC_BlendAdd(F: TColor32; var B: TColor32; M: Cardinal);
  61. procedure PC_BlendModulate(F: TColor32; var B: TColor32; M: Cardinal);
  62. private
  63. FPatCount: Integer;
  64. FLayer1: TBitmapLayer;
  65. FLayer2: TBitmapLayer;
  66. end;
  67. var
  68. FormPixelCombine: TFormPixelCombine;
  69. implementation
  70. {$R *.dfm}
  71. uses
  72. Types,
  73. GR32_Blend,
  74. GR32_RangeBars,
  75. GR32.ImageFormats.JPG;
  76. { TFormPixelCombine }
  77. procedure TFormPixelCombine.FormCreate(Sender: TObject);
  78. procedure GenerateBitmap(Bitmap: TBitmap32);
  79. var
  80. X, Y: Integer;
  81. SinY, SinX: Double;
  82. Color: TColor32;
  83. begin
  84. // Just a pattern with some variation
  85. for Y := 0 to Bitmap.Height-1 do
  86. begin
  87. SinY := Sin(Y * 0.1);
  88. for X := 0 to Bitmap.Width-1 do
  89. begin
  90. SinX := Sin(X * 0.1);
  91. Color := Gray32(Round(((SinX + SinY) * 0.25 + 0.5) * 255));
  92. // Alpha gradient
  93. Color := SetAlpha(Color, MulDiv(255, Y, Bitmap.Height-1));
  94. Bitmap[X, Y] := Color;
  95. end;
  96. end;
  97. end;
  98. var
  99. RubberbandLayer: TRubberbandLayer;
  100. r: TRect;
  101. Viewport: TRect;
  102. Location: TFloatRect;
  103. const
  104. BitmapSize = 200;
  105. BitmapOffset = 20;
  106. begin
  107. // Load background picture 'Runner'
  108. ImgView.Bitmap.LoadFromResourceName(HInstance, 'Runner', RT_RCDATA);
  109. // Create foreground bitmap layers
  110. // First layer is unscaled
  111. FLayer1 := TBitmapLayer.Create(ImgView.Layers);
  112. FLayer1.Visible := False;
  113. FLayer1.Bitmap.SetSize(BitmapSize, BitmapSize);
  114. FLayer1.Bitmap.DrawMode := dmCustom;
  115. GenerateBitmap(FLayer1.Bitmap);
  116. FLayer1.Scaled := False;
  117. // Position top-left
  118. r := FLayer1.Bitmap.BoundsRect;
  119. r.Offset(BitmapOffset, BitmapOffset);
  120. if (FLayer1.Scaled) then
  121. // Location is relative to bitmap
  122. Location := ImgView.ControlToBitmap(r)
  123. else
  124. // Location is relative to viewport
  125. Location := FloatRect(r);
  126. FLayer1.Location := Location;
  127. // Second layer is scaled
  128. FLayer2 := TBitmapLayer.Create(ImgView.Layers);
  129. FLayer2.Visible := False;
  130. FLayer2.Bitmap.Assign(FLayer1.Bitmap);
  131. FLayer2.Scaled := True;
  132. // Position bottom-right
  133. r := FLayer1.Bitmap.BoundsRect;
  134. Viewport := ImgView.GetViewportRect;
  135. r.Offset(Viewport.Width-r.Width-BitmapOffset, Viewport.Height-r.Height-BitmapOffset);
  136. if (FLayer2.Scaled) then
  137. // Location is relative to bitmap
  138. Location := ImgView.ControlToBitmap(r)
  139. else
  140. // Location is relative to viewport
  141. Location := FloatRect(r);
  142. FLayer2.Location := Location;
  143. // Create rubberband layers so we can move the foreground layers around
  144. RubberbandLayer := TRubberbandLayer.Create(ImgView.Layers);
  145. RubberbandLayer.Visible := False;
  146. RubberbandLayer.ChildLayer := FLayer1;
  147. RubberbandLayer.Handles := [rhCenter, rhFrame, rhCorners];
  148. RubberbandLayer.ChildLayer.Visible := True;
  149. RubberbandLayer.Visible := True;
  150. RubberbandLayer := TRubberbandLayer.Create(ImgView.Layers);
  151. RubberbandLayer.Visible := False;
  152. RubberbandLayer.ChildLayer := FLayer2;
  153. RubberbandLayer.Handles := [rhCenter, rhFrame, rhCorners];
  154. RubberbandLayer.ChildLayer.Visible := True;
  155. RubberbandLayer.Visible := True;
  156. end;
  157. procedure TFormPixelCombine.PC_Add(F: TColor32; var B: TColor32; M: Cardinal);
  158. begin
  159. B := ColorAdd(F, B);
  160. end;
  161. procedure TFormPixelCombine.PC_Max(F: TColor32; var B: TColor32; M: Cardinal);
  162. begin
  163. B := ColorMax(F, B);
  164. end;
  165. procedure TFormPixelCombine.PC_Min(F: TColor32; var B: TColor32; M: Cardinal);
  166. begin
  167. B := ColorMin(F, B);
  168. end;
  169. procedure TFormPixelCombine.PC_Modulate(F: TColor32; var B: TColor32; M: Cardinal);
  170. begin
  171. B := ColorModulate(F, B);
  172. end;
  173. procedure TFormPixelCombine.PC_Pattern(F: TColor32; var B: TColor32; M: Cardinal);
  174. begin
  175. FPatCount := 1 - FPatCount;
  176. if FPatCount = 0 then
  177. B := F;
  178. end;
  179. procedure TFormPixelCombine.PC_Sub(F: TColor32; var B: TColor32; M: Cardinal);
  180. begin
  181. B := ColorSub(F, B);
  182. end;
  183. procedure TFormPixelCombine.PC_Screen(F: TColor32; var B: TColor32; M: Cardinal);
  184. begin
  185. B := ColorScreen(F, B);
  186. end;
  187. procedure TFormPixelCombine.PC_ColorDodge(F: TColor32; var B: TColor32; M: Cardinal);
  188. begin
  189. B := ColorDodge(F, B);
  190. end;
  191. procedure TFormPixelCombine.PC_ColorBurn(F: TColor32; var B: TColor32; M: Cardinal);
  192. begin
  193. B := ColorBurn(F, B);
  194. end;
  195. procedure TFormPixelCombine.PC_Difference(F: TColor32; var B: TColor32; M: Cardinal);
  196. begin
  197. B := ColorDifference(F, B);
  198. end;
  199. procedure TFormPixelCombine.PC_Exclusion(F: TColor32; var B: TColor32; M: Cardinal);
  200. begin
  201. B := ColorExclusion(F, B);
  202. end;
  203. procedure TFormPixelCombine.PC_Blend(F: TColor32; var B: TColor32; M: Cardinal);
  204. begin
  205. B := BlendReg(F, B);
  206. end;
  207. procedure TFormPixelCombine.PC_BlendAdd(F: TColor32; var B: TColor32; M: Cardinal);
  208. begin
  209. B := BlendColorAdd(F, B);
  210. end;
  211. procedure TFormPixelCombine.PC_BlendModulate(F: TColor32; var B: TColor32; M: Cardinal);
  212. begin
  213. B := BlendColorModulate(F, B);
  214. end;
  215. procedure TFormPixelCombine.RadioGroupClick(Sender: TObject);
  216. begin
  217. case RadioGroup.ItemIndex of
  218. 0:
  219. FLayer1.Bitmap.OnPixelCombine := nil;
  220. 1:
  221. FLayer1.Bitmap.OnPixelCombine := PC_Add;
  222. 2:
  223. FLayer1.Bitmap.OnPixelCombine := PC_Sub;
  224. 3:
  225. FLayer1.Bitmap.OnPixelCombine := PC_Modulate;
  226. 4:
  227. FLayer1.Bitmap.OnPixelCombine := PC_Min;
  228. 5:
  229. FLayer1.Bitmap.OnPixelCombine := PC_Max;
  230. 6:
  231. FLayer1.Bitmap.OnPixelCombine := PC_Screen;
  232. 7:
  233. FLayer1.Bitmap.OnPixelCombine := PC_ColorDodge;
  234. 8:
  235. FLayer1.Bitmap.OnPixelCombine := PC_ColorBurn;
  236. 9:
  237. FLayer1.Bitmap.OnPixelCombine := PC_Difference;
  238. 10:
  239. FLayer1.Bitmap.OnPixelCombine := PC_Exclusion;
  240. 11:
  241. FLayer1.Bitmap.OnPixelCombine := PC_Pattern;
  242. 12:
  243. FLayer1.Bitmap.OnPixelCombine := PC_Blend;
  244. 13:
  245. FLayer1.Bitmap.OnPixelCombine := PC_BlendAdd;
  246. 14:
  247. FLayer1.Bitmap.OnPixelCombine := PC_BlendModulate;
  248. end;
  249. FLayer2.Bitmap.OnPixelCombine := FLayer1.Bitmap.OnPixelCombine;
  250. FLayer1.Changed;
  251. FLayer2.Changed;
  252. end;
  253. end.