drawtest.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. {
  2. Test for the basic graph operations - PutPixel, GetPixel and HLine/VLine
  3. drawing with different colours and write modes
  4. Test draws random pixels and H/V lines with the graph unit and performs the
  5. same operations in memory. Finally it reads the whole resulting image, pixel
  6. by pixel, via GetPixel and compares the result with the expected value from
  7. the PixArray
  8. Useful for testing the platform-specific parts of the FPC graph unit (in
  9. various modes and operating systems)
  10. This test works also with TP7
  11. }
  12. program DrawTest;
  13. uses
  14. Graph;
  15. type
  16. TTestParams = record
  17. Driver: Integer;
  18. Mode: Integer;
  19. NumberOfObjectsToDraw: Integer;
  20. ProbabilityPixel: Integer;
  21. ProbabilityHLine: Integer;
  22. ProbabilityVLine: Integer;
  23. end;
  24. TPixelColor = Word;
  25. PRow = ^TRow;
  26. TRow = array [0..1279] of TPixelColor;
  27. var
  28. XRes, YRes: Integer;
  29. PixArray: array [0..1023] of PRow;
  30. procedure InitPixArray(AXRes, AYRes: Integer);
  31. var
  32. Y: Integer;
  33. begin
  34. XRes := AXRes;
  35. YRes := AYRes;
  36. for Y := 0 to AYRes - 1 do
  37. begin
  38. GetMem(PixArray[Y], AXRes * SizeOf(TPixelColor));
  39. FillChar(PixArray[Y]^, AXRes * SizeOf(TPixelColor), 0);
  40. end;
  41. end;
  42. procedure FreePixArray;
  43. var
  44. Y: Integer;
  45. begin
  46. for Y := 0 to YRes - 1 do
  47. FreeMem(PixArray[Y], XRes * SizeOf(TPixelColor));
  48. end;
  49. procedure TestFinalResult;
  50. var
  51. X, Y: Integer;
  52. begin
  53. for Y := 0 to YRes - 1 do
  54. for X := 0 to XRes - 1 do
  55. if GetPixel(X, Y) <> PixArray[Y]^[X] then
  56. begin
  57. CloseGraph;
  58. Writeln('Error at X = ', X, ', Y = ', Y);
  59. Halt(1);
  60. end;
  61. end;
  62. procedure TestPutPixel(X, Y: Integer; Color: TPixelColor);
  63. begin
  64. PutPixel(X, Y, Color);
  65. PixArray[Y]^[X] := Color;
  66. end;
  67. procedure DirectPutPixel(X, Y: Integer; Color: TPixelColor; WriteMode: Integer);
  68. begin
  69. case WriteMode of
  70. NormalPut, OrPut, NotPut: PixArray[Y]^[X] := Color;
  71. XORPut, AndPut: PixArray[Y]^[X] := PixArray[Y]^[X] xor Color;
  72. { TODO: add some sort of SetWriteModeExtended to the FPC graph unit, so
  73. we can test these as well: }
  74. { OrPut: PixArray[Y]^[X] := PixArray[Y]^[X] or Color;}
  75. { AndPut: PixArray[Y]^[X] := PixArray[Y]^[X] and Color;}
  76. { NotPut: PixArray[Y]^[X] := Color xor GetMaxColor;}
  77. end;
  78. end;
  79. procedure TestHLine(Y, X1, X2: Integer; Color: TPixelColor; WriteMode: Integer);
  80. var
  81. tmp, X: Integer;
  82. begin
  83. SetWriteMode(WriteMode);
  84. SetColor(Color);
  85. Line(X1, Y, X2, Y);
  86. if X1 > X2 then
  87. begin
  88. tmp := X1;
  89. X1 := X2;
  90. X2 := tmp;
  91. end;
  92. for X := X1 to X2 do
  93. begin
  94. DirectPutPixel(X, Y, Color, WriteMode);
  95. end;
  96. SetWriteMode(NormalPut);
  97. end;
  98. procedure TestVLine(X, Y1, Y2: Integer; Color: TPixelColor; WriteMode: Integer);
  99. var
  100. tmp, Y: Integer;
  101. begin
  102. SetWriteMode(WriteMode);
  103. SetColor(Color);
  104. Line(X, Y1, X, Y2);
  105. if Y1 > Y2 then
  106. begin
  107. tmp := Y1;
  108. Y1 := Y2;
  109. Y2 := tmp;
  110. end;
  111. for Y := Y1 to Y2 do
  112. begin
  113. DirectPutPixel(X, Y, Color, WriteMode);
  114. end;
  115. SetWriteMode(NormalPut);
  116. end;
  117. procedure TestDraw(const TestParams: TTestParams);
  118. var
  119. I: Integer;
  120. R: Integer;
  121. begin
  122. for I := 1 to TestParams.NumberOfObjectsToDraw do
  123. begin
  124. R := Random(TestParams.ProbabilityPixel + TestParams.ProbabilityHLine + TestParams.ProbabilityVLine);
  125. if R < TestParams.ProbabilityPixel then
  126. TestPutPixel(Random(XRes), Random(YRes), Random(GetMaxColor + 1))
  127. else
  128. if (R >= TestParams.ProbabilityPixel) and (R < TestParams.ProbabilityPixel + TestParams.ProbabilityHLine) then
  129. TestHLine(Random(YRes), Random(XRes), Random(XRes), Random(GetMaxColor + 1), Random(NotPut + 1))
  130. else
  131. TestVLine(Random(XRes), Random(YRes), Random(YRes), Random(GetMaxColor + 1), Random(NotPut + 1));
  132. end;
  133. end;
  134. procedure PerformTest(const TestParams: TTestParams);
  135. var
  136. GraphDriver, GraphMode: Integer;
  137. begin
  138. GraphDriver := TestParams.Driver;
  139. GraphMode := TestParams.Mode;
  140. InitGraph(GraphDriver, GraphMode, 'C:\TP\BGI');
  141. InitPixArray(GetMaxX + 1, GetMaxY + 1);
  142. TestDraw(TestParams);
  143. TestFinalResult;
  144. FreePixArray;
  145. CloseGraph;
  146. Writeln('Ok');
  147. end;
  148. var
  149. TestsCount: Integer;
  150. TestParams: TTestParams;
  151. Code: Integer;
  152. I: Integer;
  153. begin
  154. if ParamCount <> 3 then
  155. begin
  156. Writeln('Usage: ', ParamStr(0), ' <driver number> <mode number> <tests count>');
  157. Writeln;
  158. Writeln('For example: ', ParamStr(0), ' 9 2 20');
  159. Writeln('performs 20 tests in 640x480x16 VGA mode (VGA = 9, VGAHi = 2)');
  160. Halt;
  161. end;
  162. Val(ParamStr(1), TestParams.Driver, Code);
  163. Val(ParamStr(2), TestParams.Mode, Code);
  164. Val(ParamStr(3), TestsCount, Code);
  165. Randomize;
  166. for I := 1 to TestsCount do
  167. begin
  168. TestParams.NumberOfObjectsToDraw := Random(30000);
  169. TestParams.ProbabilityPixel := Random(10);
  170. TestParams.ProbabilityHLine := Random(2);
  171. TestParams.ProbabilityVLine := Random(2);
  172. PerformTest(TestParams);
  173. end;
  174. end.