GXS.CompositeImage.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //
  2. // The graphics engine GLXEngine. The unit of GXScene for Delphi
  3. //
  4. unit GXS.CompositeImage;
  5. (*
  6. This class is required for loading images such classes as in DDSImage,
  7. TgxO3TCImage, TgxHDRImage etc.
  8. *)
  9. interface
  10. uses
  11. System.Classes,
  12. FMX.Graphics,
  13. Stage.TextureFormat,
  14. GXS.Graphics,
  15. GXS.Texture,
  16. GXS.Context;
  17. type
  18. TgxCompositeImage = class(TgxTextureImage)
  19. private
  20. FBitmap: TgxBitmap32;
  21. FWidth, FHeight, FDepth: integer;
  22. protected
  23. procedure SetWidth(val: Integer);
  24. procedure SetHeight(val: Integer);
  25. procedure SetDepth(val: Integer);
  26. function GetWidth: Integer; override;
  27. function GetHeight: Integer; override;
  28. function GetDepth: Integer; override;
  29. function GetTextureTarget: TglTextureTarget; override;
  30. public
  31. constructor Create(AOwner: TPersistent); override;
  32. destructor Destroy; override;
  33. procedure Assign(Source: TPersistent); override;
  34. function GetBitmap32: TgxBitmap32; override;
  35. procedure ReleaseBitmap32; override;
  36. procedure SaveToFile(const fileName: string); override;
  37. procedure LoadFromFile(const fileName: string); override;
  38. procedure LoadFromStream(const AStream: TStream);
  39. class function FriendlyName: string; override;
  40. class function FriendlyDescription: string; override;
  41. property NativeTextureTarget;
  42. published
  43. property Width: Integer read GetWidth write SetWidth;
  44. property Height: Integer read GetHeight write SetHeight;
  45. property Depth: Integer read GetDepth write SetDepth;
  46. end;
  47. implementation //------------------------------------------------------------
  48. // ------------------
  49. // ------------------ TgxCompositeImage ------------------
  50. // ------------------
  51. constructor TgxCompositeImage.Create(AOwner: TPersistent);
  52. begin
  53. inherited;
  54. FWidth := 256;
  55. FHeight := 256;
  56. FDepth := 0;
  57. end;
  58. destructor TgxCompositeImage.Destroy;
  59. begin
  60. ReleaseBitmap32;
  61. inherited Destroy;
  62. end;
  63. procedure TgxCompositeImage.Assign(Source: TPersistent);
  64. begin
  65. if Assigned(Source) then
  66. begin
  67. if not Assigned(FBitmap) then
  68. FBitmap := TgxBitmap32.Create;
  69. if (Source is TgxCompositeImage) then
  70. begin
  71. FBitmap.Assign(TgxCompositeImage(Source).FBitmap);
  72. end
  73. else
  74. FBitmap.Assign(Source);
  75. FWidth := FBitmap.Width;
  76. FHeight := FBitmap.Height;
  77. FDepth := FBitmap.Depth;
  78. FResourceFile := FBitmap.ResourceName;
  79. // Composite image always rewrite texture format
  80. if Assigned(FOwnerTexture) then
  81. TgxTexture(FOwnerTexture).TextureFormatEx := FBitmap.InternalFormat;
  82. NotifyChange(Self);
  83. end
  84. else
  85. inherited;
  86. end;
  87. procedure TgxCompositeImage.SetWidth(val: Integer);
  88. begin
  89. if val <> FWidth then
  90. begin
  91. if val < 1 then
  92. val := 1;
  93. FWidth := val;
  94. Invalidate;
  95. end;
  96. end;
  97. function TgxCompositeImage.GetWidth: Integer;
  98. begin
  99. Result := FWidth;
  100. end;
  101. procedure TgxCompositeImage.SetHeight(val: Integer);
  102. begin
  103. if val <> FHeight then
  104. begin
  105. if val < 1 then
  106. val := 1;
  107. FHeight := val;
  108. Invalidate;
  109. end;
  110. end;
  111. function TgxCompositeImage.GetHeight: Integer;
  112. begin
  113. Result := FHeight;
  114. end;
  115. procedure TgxCompositeImage.SetDepth(val: Integer);
  116. begin
  117. if val <> FDepth then
  118. begin
  119. if val < 0 then
  120. val := 0;
  121. FDepth := val;
  122. Invalidate;
  123. end;
  124. end;
  125. function TgxCompositeImage.GetDepth: Integer;
  126. begin
  127. Result := FDepth;
  128. end;
  129. function TgxCompositeImage.GetBitmap32: TgxBitmap32;
  130. begin
  131. if not Assigned(FBitmap) then
  132. begin
  133. FBitmap := TgxBitmap32.Create;
  134. FBitmap.Blank := true;
  135. FWidth := 256;
  136. FHeight := 256;
  137. FDepth := 0;
  138. FBitmap.Width := FWidth;
  139. FBitmap.Height := FHeight;
  140. FBitmap.Depth := FDepth;
  141. end;
  142. Result := FBitmap;
  143. end;
  144. procedure TgxCompositeImage.ReleaseBitmap32;
  145. begin
  146. if Assigned(FBitmap) then
  147. begin
  148. FBitmap.Free;
  149. FBitmap := nil;
  150. end;
  151. end;
  152. procedure TgxCompositeImage.SaveToFile(const fileName: string);
  153. var
  154. BaseImageClass: TgxBaseImageClass;
  155. tempImage: TgxBaseImage;
  156. LOwner: TgxTexture;
  157. begin
  158. if filename = '' then
  159. exit;
  160. BaseImageClass := GetRasterFileFormats.FindFromFileName(filename);
  161. tempImage := BaseImageClass.Create;
  162. if Assigned(FOwnerTexture) then
  163. begin
  164. LOwner := TgxTexture(FOwnerTexture);
  165. if not tempImage.AssignFromTexture(
  166. LOwner.TextureHandle, False, LOwner.TextureFormatEx) then
  167. tempImage.Assign(fBitmap);
  168. end
  169. else
  170. tempImage.Assign(fBitmap);
  171. try
  172. tempImage.SaveToFile(fileName);
  173. FResourceFile := fileName;
  174. finally
  175. tempImage.Free;
  176. end;
  177. end;
  178. procedure TgxCompositeImage.LoadFromFile(const fileName: string);
  179. var
  180. BaseImageClass: TgxBaseImageClass;
  181. tempImage: TgxBaseImage;
  182. begin
  183. if filename = '' then
  184. exit;
  185. BaseImageClass := GetRasterFileFormats.FindFromFileName(filename);
  186. tempImage := BaseImageClass.Create;
  187. try
  188. tempImage.LoadFromFile(fileName);
  189. if not Assigned(FBitmap) then
  190. FBitmap := TgxBitmap32.Create;
  191. FBitmap.Assign(tempImage);
  192. FWidth := FBitmap.Width;
  193. FHeight := FBitmap.Height;
  194. FDepth := FBitmap.Depth;
  195. FResourceFile := FBitmap.ResourceName;
  196. // Internal image always rewrite texture format
  197. if Assigned(FOwnerTexture) then
  198. TgxTexture(FOwnerTexture).TextureFormatEx := FBitmap.InternalFormat;
  199. NotifyChange(Self);
  200. finally
  201. tempImage.Free;
  202. end;
  203. end;
  204. procedure TgxCompositeImage.LoadFromStream(const AStream: TStream);
  205. var
  206. tempImage: TgxBaseImage;
  207. begin
  208. if (not Assigned(AStream)) or (AStream.Size - AStream.Position < 200) then
  209. exit;
  210. with GetRasterFileFormats do
  211. tempImage := FindFromStream(AStream).Create;
  212. try
  213. tempImage.LoadFromStream(AStream);
  214. if not Assigned(FBitmap) then
  215. FBitmap := TgxBitmap32.Create;
  216. FBitmap.Assign(tempImage);
  217. FWidth := FBitmap.Width;
  218. FHeight := FBitmap.Height;
  219. FDepth := FBitmap.Depth;
  220. FResourceFile := '';
  221. if Assigned(FOwnerTexture) then
  222. TgxTexture(FOwnerTexture).TextureFormatEx := FBitmap.InternalFormat;
  223. NotifyChange(Self);
  224. finally
  225. tempImage.Free;
  226. end;
  227. end;
  228. class function TgxCompositeImage.FriendlyName: string;
  229. begin
  230. Result := 'Composite Image';
  231. end;
  232. class function TgxCompositeImage.FriendlyDescription: string;
  233. begin
  234. Result := 'Image contained any internal formats of OpenGL textures';
  235. end;
  236. function TgxCompositeImage.GetTextureTarget: TglTextureTarget;
  237. begin
  238. if Assigned(fBitmap) then
  239. Result := fBitmap.GetTextureTarget
  240. else
  241. Result := ttNoShape;
  242. end;
  243. initialization //-------------------------------------------------------------
  244. RegisterTextureImageClass(TgxCompositeImage);
  245. end.