surfacei.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 TPTCSurface.Create(_width, _height : Integer; Const _format : TPTCFormat);
  18. Var
  19. size : Integer;
  20. Begin
  21. m_pixels := Nil;
  22. m_copy := Nil;
  23. m_clear := Nil;
  24. m_palette := Nil;
  25. m_format := Nil;
  26. m_area := Nil;
  27. m_clip := Nil;
  28. m_locked := False;
  29. LOG('creating surface');
  30. LOG('width', _width);
  31. LOG('height', _height);
  32. LOG('format', _format);
  33. m_width := _width;
  34. m_height := _height;
  35. m_format := TPTCFormat.Create(_format);
  36. m_area := TPTCArea.Create(0, 0, width, height);
  37. m_clip := TPTCArea.Create(m_area);
  38. m_pitch := width * _format.bytes;
  39. size := width * height * _format.bytes;
  40. If size = 0 Then
  41. Raise TPTCError.Create('zero surface size');
  42. m_pixels := GetMem(size);
  43. If m_pixels = Nil Then
  44. Raise TPTCError.Create('could not allocate surface pixels');
  45. m_copy := TPTCCopy.Create;
  46. m_clear := TPTCClear.Create;
  47. m_palette := TPTCPalette.Create;
  48. clear;
  49. End;
  50. Destructor TPTCSurface.Destroy;
  51. Begin
  52. If m_locked Then
  53. Raise TPTCError.Create('surface is still locked');
  54. m_copy.Free;
  55. m_clear.Free;
  56. m_palette.Free;
  57. m_clip.Free;
  58. m_area.Free;
  59. m_format.Free;
  60. If m_pixels <> Nil Then
  61. FreeMem(m_pixels);
  62. Inherited Destroy;
  63. End;
  64. Procedure TPTCSurface.copy(Var surface : TPTCBaseSurface);
  65. Begin
  66. surface.load(m_pixels, m_width, m_height, m_pitch, m_format, m_palette);
  67. End;
  68. Procedure TPTCSurface.copy(Var surface : TPTCBaseSurface;
  69. Const source, destination : TPTCArea);
  70. Begin
  71. surface.load(m_pixels, m_width, m_height, m_pitch, m_format, m_palette,
  72. source, destination);
  73. End;
  74. Function TPTCSurface.lock : Pointer;
  75. Begin
  76. If m_locked Then
  77. Raise TPTCError.Create('surface is already locked');
  78. m_locked := True;
  79. lock := m_pixels;
  80. End;
  81. Procedure TPTCSurface.unlock;
  82. Begin
  83. If Not m_locked Then
  84. Raise TPTCError.Create('surface is not locked');
  85. m_locked := False;
  86. End;
  87. Procedure TPTCSurface.load(Const pixels : Pointer;
  88. _width, _height, _pitch : Integer;
  89. Const _format : TPTCFormat;
  90. Const _palette : TPTCPalette);
  91. Var
  92. Area_ : TPTCArea;
  93. Begin
  94. If m_clip.Equals(m_area) Then
  95. Begin
  96. m_copy.request(_format, m_format);
  97. m_copy.palette(_palette, m_palette);
  98. m_copy.copy(pixels, 0, 0, _width, _height, _pitch, m_pixels, 0, 0,
  99. m_width, m_height, m_pitch);
  100. End
  101. Else
  102. Begin
  103. Area_ := TPTCArea.Create(0, 0, _width, _height);
  104. Try
  105. load(pixels, _width, _height, _pitch, _format, _palette, Area_, m_area);
  106. Finally
  107. Area_.Free;
  108. End;
  109. End;
  110. End;
  111. Procedure TPTCSurface.load(Const pixels : Pointer;
  112. _width, _height, _pitch : Integer;
  113. Const _format : TPTCFormat;
  114. Const _palette : TPTCPalette;
  115. Const source, destination : TPTCArea);
  116. Var
  117. clipped_source, clipped_destination : TPTCArea;
  118. area_ : TPTCArea;
  119. Begin
  120. clipped_source := Nil;
  121. clipped_destination := Nil;
  122. area_ := Nil;
  123. Try
  124. clipped_source := TPTCArea.Create;
  125. clipped_destination := TPTCArea.Create;
  126. area_ := TPTCArea.Create(0, 0, _width, _height);
  127. TPTCClipper.clip(source, area_, clipped_source, destination, m_clip,
  128. clipped_destination);
  129. m_copy.request(_format, m_format);
  130. m_copy.palette(_palette, m_palette);
  131. m_copy.copy(pixels, clipped_source.left, clipped_source.top,
  132. clipped_source.width, clipped_source.height, _pitch,
  133. m_pixels, clipped_destination.left, clipped_destination.top,
  134. clipped_destination.width, clipped_destination.height, m_pitch);
  135. Finally
  136. clipped_source.Free;
  137. clipped_destination.Free;
  138. area_.Free;
  139. End;
  140. End;
  141. Procedure TPTCSurface.save(pixels : Pointer;
  142. _width, _height, _pitch : Integer;
  143. Const _format : TPTCFormat;
  144. Const _palette : TPTCPalette);
  145. Var
  146. area_ : TPTCArea;
  147. Begin
  148. If m_clip.Equals(m_area) Then
  149. Begin
  150. m_copy.request(m_format, _format);
  151. m_copy.palette(m_palette, _palette);
  152. m_copy.copy(m_pixels, 0, 0, m_width, m_height, m_pitch, pixels, 0, 0,
  153. _width, _height, _pitch);
  154. End
  155. Else
  156. Begin
  157. area_ := TPTCArea.Create(0, 0, width, height);
  158. Try
  159. save(pixels, _width, _height, _pitch, _format, _palette, m_area, area_);
  160. Finally
  161. area_.Free;
  162. End;
  163. End;
  164. End;
  165. Procedure TPTCSurface.save(pixels : Pointer;
  166. _width, _height, _pitch : Integer;
  167. Const _format : TPTCFormat;
  168. Const _palette : TPTCPalette;
  169. Const source, destination : TPTCArea);
  170. Var
  171. clipped_source, clipped_destination : TPTCArea;
  172. area_ : TPTCArea;
  173. Begin
  174. clipped_source := Nil;
  175. clipped_destination := Nil;
  176. area_ := Nil;
  177. Try
  178. clipped_source := TPTCArea.Create;
  179. clipped_destination := TPTCArea.Create;
  180. area_ := TPTCArea.Create(0, 0, _width, _height);
  181. TPTCClipper.clip(source, m_clip, clipped_source, destination, area_,
  182. clipped_destination);
  183. m_copy.request(m_format, _format);
  184. m_copy.palette(m_palette, _palette);
  185. m_copy.copy(m_pixels, clipped_source.left, clipped_source.top,
  186. clipped_source.width, clipped_source.height, m_pitch,
  187. pixels, clipped_destination.left, clipped_destination.top,
  188. clipped_destination.width, clipped_destination.height, _pitch);
  189. Finally
  190. clipped_source.Free;
  191. clipped_destination.Free;
  192. area_.Free;
  193. End;
  194. End;
  195. Procedure TPTCSurface.clear;
  196. Var
  197. Color : TPTCColor;
  198. Begin
  199. If format.direct Then
  200. Color := TPTCColor.Create(0, 0, 0, 0)
  201. Else
  202. Color := TPTCColor.Create(0);
  203. Try
  204. clear(Color);
  205. Finally
  206. Color.Free;
  207. End;
  208. End;
  209. Procedure TPTCSurface.clear(Const color : TPTCColor);
  210. Begin
  211. clear(color, m_area);
  212. End;
  213. Procedure TPTCSurface.clear(Const color : TPTCColor; Const _area : TPTCArea);
  214. Var
  215. clipped_area : TPTCArea;
  216. Begin
  217. clipped_area := TPTCClipper.clip(_area, m_clip);
  218. Try
  219. m_clear.request(m_format);
  220. m_clear.clear(m_pixels, clipped_area.left, clipped_area.top,
  221. clipped_area.width, clipped_area.height, m_pitch, color);
  222. Finally
  223. clipped_area.Free;
  224. End;
  225. End;
  226. Procedure TPTCSurface.palette(Const _palette : TPTCPalette);
  227. Begin
  228. m_palette.load(_palette.data^);
  229. End;
  230. Function TPTCSurface.palette : TPTCPalette;
  231. Begin
  232. palette := m_palette;
  233. End;
  234. Procedure TPTCSurface.clip(Const _area : TPTCArea);
  235. Var
  236. tmp : TPTCArea;
  237. Begin
  238. tmp := TPTCClipper.clip(_area, m_area);
  239. Try
  240. m_clip.ASSign(tmp);
  241. Finally
  242. tmp.Free;
  243. End;
  244. End;
  245. Function TPTCSurface.width : Integer;
  246. Begin
  247. width := m_width;
  248. End;
  249. Function TPTCSurface.height : Integer;
  250. Begin
  251. height := m_height;
  252. End;
  253. Function TPTCSurface.pitch : Integer;
  254. Begin
  255. pitch := m_pitch;
  256. End;
  257. Function TPTCSurface.area : TPTCArea;
  258. Begin
  259. area := m_area;
  260. End;
  261. Function TPTCSurface.clip : TPTCArea;
  262. Begin
  263. clip := m_clip;
  264. End;
  265. Function TPTCSurface.format : TPTCFormat;
  266. Begin
  267. format := m_format;
  268. End;
  269. Function TPTCSurface.option(Const _option : String) : Boolean;
  270. Begin
  271. option := m_copy.option(_option);
  272. End;