GLS.CompositeImage.pas 6.3 KB

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