cleari.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. {
  2. Free Pascal port of the OpenPTC C++ library.
  3. Copyright (C) 2001-2003 Nikolay Nikolov ([email protected])
  4. Original C++ version by Glenn Fiedler ([email protected])
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. }
  17. Constructor TPTCClear.Create;
  18. Begin
  19. Fformat := Nil;
  20. { initialize hermes }
  21. If Not Hermes_Init Then
  22. Raise TPTCError.Create('could not initialize hermes');
  23. { default current format }
  24. Fformat := TPTCFormat.Create;
  25. { create hermes clearer instance }
  26. Fhandle := Hermes_ClearerInstance;
  27. { check hermes clearer instance }
  28. If Fhandle = 0 Then
  29. Raise TPTCError.Create('could not create hermes clearer instance');
  30. End;
  31. Destructor TPTCClear.Destroy;
  32. Begin
  33. { return the clearer instance }
  34. Hermes_ClearerReturn(Fhandle);
  35. Fformat.Free;
  36. { free hermes }
  37. Hermes_Done;
  38. Inherited Destroy;
  39. End;
  40. Procedure TPTCClear.request(Const format : TPTCFormat);
  41. Var
  42. hermes_format : PHermesFormat;
  43. Begin
  44. hermes_format := @format.Fformat;
  45. { request surface clear for this format }
  46. If Not Hermes_ClearerRequest(Fhandle, hermes_format) Then
  47. Raise TPTCError.Create('unsupported clear format');
  48. { update current format }
  49. Fformat.Assign(format);
  50. End;
  51. Procedure TPTCClear.clear(pixels : Pointer; x, y, width, height, pitch : Integer; Const color : TPTCColor);
  52. Var
  53. r, g, b, a : LongInt;
  54. index : LongInt;
  55. Begin
  56. {$IFDEF DEBUG}
  57. {
  58. This checking is performed only when DEBUG is defined,
  59. and can be used to track down errors early caused by passing
  60. nil pointers to the clear function.
  61. Even though technically clear should never receive a nil
  62. pointer, we provide a check here to assist in debugging
  63. just in case it ever does!
  64. }
  65. If pixels = Nil Then
  66. Raise TPTCError.Create('nil pixels pointer in clear');
  67. {$ELSE}
  68. { In release build no checking is performed for the sake of efficiency. }
  69. {$ENDIF}
  70. { check format type }
  71. If Fformat.direct Then
  72. Begin
  73. { check color type }
  74. If Not color.direct Then
  75. Raise TPTCError.Create('direct pixel formats can only be cleared with direct color');
  76. { setup clear color }
  77. r := Trunc(color.r * 255);
  78. g := Trunc(color.g * 255);
  79. b := Trunc(color.b * 255);
  80. a := Trunc(color.a * 255);
  81. { clamp red }
  82. If r > 255 Then
  83. r := 255
  84. Else
  85. If r < 0 Then
  86. r := 0;
  87. { clamp green }
  88. If g > 255 Then
  89. g := 255
  90. Else
  91. If g < 0 Then
  92. g := 0;
  93. { clamp blue }
  94. If b > 255 Then
  95. b := 255
  96. Else
  97. If b < 0 Then
  98. b := 0;
  99. { clamp alpha }
  100. If a > 255 Then
  101. a := 255
  102. Else
  103. If a < 0 Then
  104. a := 0;
  105. { perform the clearing }
  106. Hermes_ClearerClear(Fhandle,pixels,x,y,width,height,pitch,r,g,b,a);
  107. End
  108. Else
  109. Begin
  110. { check color type }
  111. If Not color.indexed Then
  112. Raise TPTCError.Create('indexed pixel formats can only be cleared with indexed color');
  113. { setup clear index }
  114. index := color.index;
  115. { clamp color index }
  116. If index > 255 Then
  117. index := 255
  118. Else
  119. If index < 0 Then
  120. index := 0;
  121. { perform the clearing }
  122. Hermes_ClearerClear(Fhandle,pixels,x,y,width,height,pitch,0,0,0,index);
  123. End;
  124. End;