pixtools.pp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2003 by the Free Pascal development team
  5. Pixel drawing routines.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$mode objfpc}{$h+}
  13. unit PixTools;
  14. interface
  15. uses classes, FPCanvas, clipping, FPimage;
  16. procedure DrawSolidPolyline (Canv : TFPCustomCanvas; points:array of TPoint; close:boolean);
  17. procedure DrawSolidLine (Canv : TFPCustomCanvas; x1,y1, x2,y2:integer);
  18. procedure FillRectangleColor (Canv:TFPCustomCanvas; x1,y1, x2,y2:integer);
  19. procedure DrawPatternLine (Canv:TFPCustomCanvas; x1,y1, x2,y2:integer; pattern:word);
  20. implementation
  21. procedure FillRectangleColor (Canv:TFPCustomCanvas; x1,y1, x2,y2:integer);
  22. var x,y : integer;
  23. c : TFPColor;
  24. begin
  25. writeln ('FillREctangleColor, sorting rec');
  26. SortRect (x1,y1, x2,y2);
  27. with Canv do
  28. begin
  29. writeln ('FillRectangleColor(',x1,',',y1,', ',x2,',',y2);
  30. c := brush.color;
  31. for x := x1 to x2 do
  32. for y := y1 to y2 do
  33. colors[x,y] := c;
  34. end;
  35. end;
  36. procedure DrawSolidPolyLine (Canv : TFPCustomCanvas; points:array of TPoint; close:boolean);
  37. var i,a, r : integer;
  38. p : TPoint;
  39. begin
  40. i := low(points);
  41. a := high(points);
  42. p := points[i];
  43. with Canv do
  44. begin
  45. for r := i+1 to a do
  46. begin
  47. Line (p.x, p.y, points[r].x, points[r].y);
  48. p := points[r];
  49. end;
  50. if close then
  51. Line (p.x,p.y, points[i].x,points[i].y);
  52. end;
  53. end;
  54. type
  55. TPutPixelProc = procedure (Canv:TFPCustomCanvas; x,y:integer; color:TFPColor);
  56. procedure PutPixelCopy(Canv:TFPCustomCanvas; x,y:integer; color:TFPColor);
  57. begin
  58. with Canv do
  59. Colors[x,y] := color;
  60. end;
  61. procedure PutPixelXor(Canv:TFPCustomCanvas; x,y:integer; color:TFPColor);
  62. begin
  63. with Canv do
  64. Colors[x,y] := Colors[x,y] xor color;
  65. end;
  66. procedure PutPixelOr(Canv:TFPCustomCanvas; x,y:integer; color:TFPColor);
  67. begin
  68. with Canv do
  69. Colors[x,y] := Colors[x,y] or color;
  70. end;
  71. procedure PutPixelAnd(Canv:TFPCustomCanvas; x,y:integer; color:TFPColor);
  72. begin
  73. with Canv do
  74. Colors[x,y] := Colors[x,y] and color;
  75. end;
  76. procedure DrawSolidLine (Canv : TFPCustomCanvas; x1,y1, x2,y2:integer);
  77. var PutPixelProc : TPutPixelProc;
  78. procedure HorizontalLine (x1,x2,y:integer);
  79. var x : integer;
  80. c : TFPColor;
  81. begin
  82. c := Canv.pen.color;
  83. for x := x1 to x2 do
  84. PutPixelProc (Canv, x,y, c);
  85. end;
  86. procedure VerticalLine (x,y1,y2:integer);
  87. var y : integer;
  88. c : TFPColor;
  89. begin
  90. c := Canv.pen.color;
  91. for y := y1 to y2 do
  92. PutPixelProc (Canv, x,y, c);
  93. end;
  94. procedure SlopedLine;
  95. var npixels,xinc1,yinc1,xinc2,yinc2,dx,dy,d,dinc1,dinc2 : integer;
  96. procedure initialize;
  97. begin // precalculations
  98. dx := abs(x2-x1);
  99. dy := abs(y2-y1);
  100. if dx > dy then // determining independent variable
  101. begin // x is independent
  102. npixels := dx + 1;
  103. d := (2 * dy) - dx;
  104. dinc1 := dy * 2;
  105. dinc2:= (dy - dx) * 2;
  106. xinc1 := 1;
  107. xinc2 := 1;
  108. yinc1 := 0;
  109. yinc2 := 1;
  110. end
  111. else
  112. begin // y is independent
  113. npixels := dy + 1;
  114. d := (2 * dx) - dy;
  115. dinc1 := dx * 2;
  116. dinc2:= (dx - dy) * 2;
  117. xinc1 := 0;
  118. xinc2 := 1;
  119. yinc1 := 1;
  120. yinc2 := 1;
  121. end;
  122. // going into the correct direction
  123. if x1 > x2 then
  124. begin
  125. xinc1 := - xinc1;
  126. xinc2 := - xinc2;
  127. end;
  128. if y1 > y2 then
  129. begin
  130. yinc1 := - yinc1;
  131. yinc2 := - yinc2;
  132. end;
  133. end;
  134. var r,x,y : integer;
  135. c : TFPColor;
  136. begin
  137. initialize;
  138. x := x1;
  139. y := y1;
  140. c := canv.pen.color;
  141. for r := 1 to nPixels do
  142. begin
  143. PutPixelProc (Canv, x,y, c);
  144. if d < 0 then
  145. begin
  146. d := d + dinc1;
  147. x := x + xinc1;
  148. y := y + yinc1;
  149. end
  150. else
  151. begin
  152. d := d + dinc2;
  153. x := x + xinc2;
  154. y := y + yinc2;
  155. end;
  156. end;
  157. end;
  158. begin
  159. with canv.pen do
  160. case mode of
  161. pmAnd : PutPixelProc := @PutPixelAnd;
  162. pmOr : PutPixelProc := @PutPixelOr;
  163. pmXor : PutPixelProc := @PutPixelXor;
  164. else PutPixelProc := @PutPixelCopy;
  165. end;
  166. if x1 = x2 then // vertical line
  167. if y1 < y2 then
  168. VerticalLine (x1, y1, y2)
  169. else
  170. VerticalLine (x1, y2, y1)
  171. else if y1 = y2 then
  172. if x1 < x2 then
  173. HorizontalLine (x1, x2, y1)
  174. else
  175. HorizontalLine (x2, x1, y1)
  176. else // sloped line
  177. SlopedLine;
  178. end;
  179. procedure DrawPatternLine (Canv:TFPCustomCanvas; x1,y1, x2,y2:integer; pattern:word);
  180. begin
  181. end;
  182. end.