| 1 |
{%MainUnit castleimages.pas} |
| 2 |
{ |
| 3 |
Copyright 2018-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
function LoadComposite(Stream: TStream; |
| 18 |
const AllowedImageClasses: array of TEncodedImageClass; |
| 19 |
const MimeType: string): TEncodedImage; |
| 20 |
var |
| 21 |
Composite: TCompositeImage; |
| 22 |
begin |
| 23 |
Composite := TCompositeImage.Create; |
| 24 |
try |
| 25 |
Composite.LoadFromStream(Stream, '', MimeType); |
| 26 |
|
| 27 |
{ After successfully loading, Composite should always contain at least one image } |
| 28 |
Assert(Composite.Images.Count >= 1); |
| 29 |
|
| 30 |
{ This way I don't have to make a copy of Composite.Images[0] for Result, |
| 31 |
which would be a waste of time. } |
| 32 |
Composite.OwnsFirstImage := false; |
| 33 |
|
| 34 |
{ TODO: make sure to honor AllowedImageClasses. |
| 35 |
For now, this just returns whatever Composite set, without converting... } |
| 36 |
Result := Composite.Images[0]; |
| 37 |
finally FreeAndNil(Composite) end; |
| 38 |
end; |
| 39 |
|
| 40 |
function LoadDDS(const Stream: TStream; |
| 41 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; |
| 42 |
begin |
| 43 |
Result := LoadComposite(Stream, AllowedImageClasses, 'image/x-dds'); |
| 44 |
end; |
| 45 |
|
| 46 |
function LoadKTX(const Stream: TStream; |
| 47 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; |
| 48 |
begin |
| 49 |
Result := LoadComposite(Stream, AllowedImageClasses, 'image/ktx'); |
| 50 |
end; |
| 51 |
|
| 52 |
procedure SaveComposite(Img: TEncodedImage; Stream: TStream; const MimeType: string); |
| 53 |
var |
| 54 |
Composite: TCompositeImage; |
| 55 |
begin |
| 56 |
Composite := TCompositeImage.Create; |
| 57 |
try |
| 58 |
Composite.Width := Img.Width; |
| 59 |
Composite.Height := Img.Height; |
| 60 |
Composite.CompositeType := ctTexture; |
| 61 |
Composite.Mipmaps := false; |
| 62 |
Composite.MipmapsCount := 1; |
| 63 |
Composite.Images.Count := 1; |
| 64 |
Composite.Images[0] := Img; |
| 65 |
|
| 66 |
Composite.OwnsFirstImage := false; |
| 67 |
|
| 68 |
Composite.SaveToStream(Stream, MimeType); |
| 69 |
finally FreeAndNil(Composite) end; |
| 70 |
end; |
| 71 |
|
| 72 |
procedure SaveDDS(const Img: TEncodedImage; const Stream: TStream); |
| 73 |
begin |
| 74 |
SaveComposite(Img, Stream, 'image/x-dds'); |
| 75 |
end; |