| 1 |
{ |
| 2 |
Copyright 2009-2022 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" 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. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Composite (like KTX or DDS) image file format handling (TCompositeImage). } |
| 17 |
unit CastleInternalCompositeImage; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses Classes, CastleImages; |
| 24 |
|
| 25 |
type |
| 26 |
EInvalidCompositeImage = class(EInvalidImageFormat); |
| 27 |
EInvalidDDS = class(EInvalidCompositeImage); |
| 28 |
EInvalidKTX = class(EInvalidCompositeImage); |
| 29 |
|
| 30 |
{ Type of data in a TCompositeImage file. |
| 31 |
This doesn't take into account mipmaps (they are orthogonal to types here). } |
| 32 |
TCompositeType = ( |
| 33 |
ctTexture, |
| 34 |
ctCubeMap, |
| 35 |
ctVolume); |
| 36 |
|
| 37 |
{ Cube map faces. |
| 38 |
Always interpreted in right-handed orientation (like for OpenGL or OpenGL ES) |
| 39 |
by our engine. |
| 40 |
Order matches the order of OpenGL constants |
| 41 |
GL_TEXTURE_CUBE_MAP_POSITIVE/NEGATIVE_X/Y/Z_ARB. } |
| 42 |
TCubeMapSide = ( |
| 43 |
csPositiveX, |
| 44 |
csNegativeX, |
| 45 |
csPositiveY, |
| 46 |
csNegativeY, |
| 47 |
csPositiveZ, |
| 48 |
csNegativeZ); |
| 49 |
TCubeMapSides = set of TCubeMapSide; |
| 50 |
|
| 51 |
TCubeMapImages = array [TCubeMapSide] of TCastleImage; |
| 52 |
|
| 53 |
{ Composite image file (like KTX or DDS). This supports image that can have mipmaps, |
| 54 |
can be 3D, and can be a cubemap. This is something more than our TCastleImage |
| 55 |
(or TEncodedImage), which can only be a single pixel matrix (it can be 3D, |
| 56 |
but it cannot be a cubemap or have mipmaps). |
| 57 |
|
| 58 |
In essence, this is a container for a sequence of simple images |
| 59 |
in the @link(Images) property. |
| 60 |
The interpretation of the image sequence |
| 61 |
depends on other fields: first of all @link(CompositeType) and @link(Mipmaps). |
| 62 |
|
| 63 |
The basic usage of this class is to load a file using LoadFromFile |
| 64 |
or LoadFromStream. |
| 65 |
|
| 66 |
Note that you can write (change) many properties of this class. |
| 67 |
This allows you to create, or load and edit, composite files. |
| 68 |
You can even later save the composite image back to the stream (like a file) by |
| 69 |
SaveToStream or SaveToFile. Be careful though: you're responsible then |
| 70 |
to set all properties to sensible values. For example, the length |
| 71 |
(and interpretation) of @link(Images) list is determined by other properties |
| 72 |
of this class, so be sure to set them all to something sensible. } |
| 73 |
TCompositeImage = class |
| 74 |
private |
| 75 |
type |
| 76 |
// TODO: not OK that cubemap stuff is DDS-specific in this file. |
| 77 |
{ DDS cube map sides. |
| 78 |
|
| 79 |
Compared with TCubeMapSide type, the meaning |
| 80 |
positive/negative Y faces is swapped. |
| 81 |
|
| 82 |
Reason: Cube map sides are named and written in DDS file |
| 83 |
in a way natural for DirectX, and DirectX has left-handed coordinate system, |
| 84 |
which means that one axis seems reverted when you want OpenGL right-handed |
| 85 |
coord system (like |
| 86 |
OpenGL, see http://opengl.org/registry/specs/ARB/texture_cube_map.txt). |
| 87 |
See https://castle-engine.io/dds for more. } |
| 88 |
TDDSCubeMapSide = ( |
| 89 |
dcsPositiveX, |
| 90 |
dcsNegativeX, |
| 91 |
dcsPositiveY, |
| 92 |
dcsNegativeY, |
| 93 |
dcsPositiveZ, |
| 94 |
dcsNegativeZ); |
| 95 |
TDDSCubeMapSides = set of TDDSCubeMapSide; |
| 96 |
const |
| 97 |
{ Convert TDDSCubeMapSide to TCubeMapSide. } |
| 98 |
DDSToCubeMapSide: array [TDDSCubeMapSide] of TCubeMapSide = |
| 99 |
( csPositiveX, |
| 100 |
csNegativeX, |
| 101 |
csNegativeY, |
| 102 |
csPositiveY, |
| 103 |
csPositiveZ, |
| 104 |
csNegativeZ |
| 105 |
); |
| 106 |
{ Convert TCubeMapSide to TDDSCubeMapSide. } |
| 107 |
DDSFromCubeMapSide: array [TCubeMapSide] of TDDSCubeMapSide = |
| 108 |
( dcsPositiveX, |
| 109 |
dcsNegativeX, |
| 110 |
dcsNegativeY, |
| 111 |
dcsPositiveY, |
| 112 |
dcsPositiveZ, |
| 113 |
dcsNegativeZ |
| 114 |
); |
| 115 |
var |
| 116 |
FImages: TEncodedImageList; |
| 117 |
FWidth: Cardinal; |
| 118 |
FHeight: Cardinal; |
| 119 |
FCompositeType: TCompositeType; |
| 120 |
FMipmaps: boolean; |
| 121 |
FMipmapsCount: Cardinal; |
| 122 |
FCubeMapSides: TDDSCubeMapSides; |
| 123 |
FDepth: Cardinal; |
| 124 |
FOwnsFirstImage: boolean; |
| 125 |
function GetImages(const Index: Integer): TEncodedImage; |
| 126 |
function GetCubeMapSides: TCubeMapSides; |
| 127 |
procedure SetCubeMapSides(const Value: TCubeMapSides); |
| 128 |
public |
| 129 |
{ Some DDS files specify unknown GPU texture compression. |
| 130 |
More precisely, the DxgiFormat is equal DXGI_FORMAT_UNKNOWN |
| 131 |
(see https://msdn.microsoft.com/en-us/library/windows/desktop/bb173059(v=vs.85).aspx ). |
| 132 |
This can happen if you used PvrTexTool and compressed to PVRTC2_* formats. |
| 133 |
|
| 134 |
To be able to read such DDS files anyway, |
| 135 |
set AutomaticCompression to true and set AutomaticCompressionType |
| 136 |
as appropriate. } |
| 137 |
class var AutomaticCompression: boolean; |
| 138 |
class var AutomaticCompressionType: TTextureCompression; |
| 139 |
|
| 140 |
constructor Create; |
| 141 |
destructor Destroy; override; |
| 142 |
|
| 143 |
{ Images sequence stored in this composite file. |
| 144 |
|
| 145 |
This has always length > 0 when file is successfully loaded |
| 146 |
(that is, when LoadFromStream method finished without raising any |
| 147 |
exception). } |
| 148 |
property Images: TEncodedImageList read FImages; |
| 149 |
|
| 150 |
property Width: Cardinal read FWidth write FWidth; |
| 151 |
property Height: Cardinal read FHeight write FHeight; |
| 152 |
|
| 153 |
property CompositeType: TCompositeType read FCompositeType write FCompositeType; |
| 154 |
|
| 155 |
{ Does this composite image contain mipmaps. |
| 156 |
If @true, then all @link(Images) are guaranteed to have sizes |
| 157 |
being power of 2. } |
| 158 |
property Mipmaps: boolean read FMipmaps write FMipmaps; |
| 159 |
{ Mipmaps count. |
| 160 |
Always 1 when @link(Mipmaps) = @false, this is usually comfortable. } |
| 161 |
property MipmapsCount: Cardinal read FMipmapsCount write FMipmapsCount; |
| 162 |
|
| 163 |
{ Present cube map sides. |
| 164 |
Valid only when image is loaded and is ctCubeMap. } |
| 165 |
property CubeMapSides: TCubeMapSides read GetCubeMapSides write SetCubeMapSides; |
| 166 |
|
| 167 |
{ Depth of volume (3D) texture. |
| 168 |
Always 1 when CompositeType is not ctVolume, this is usually comfortable. } |
| 169 |
property Depth: Cardinal read FDepth write FDepth; |
| 170 |
|
| 171 |
{ Return given side of cube map. |
| 172 |
Assumes CompositeType = ctCubeMap and CubeMapSides = all. |
| 173 |
|
| 174 |
Level is mipmap level. Pass 0 for base level. |
| 175 |
When not @link(Mipmaps), Level must be 0. } |
| 176 |
function CubeMapImage(const Side: TCubeMapSide; |
| 177 |
const Level: Cardinal = 0): TEncodedImage; |
| 178 |
|
| 179 |
{ Load composite (KTX or DDS) image from any TStream. |
| 180 |
The image type is recognized from the MimeType extension, |
| 181 |
or (if empty) from Url, |
| 182 |
so make sure that you provide at least one of these parameters. |
| 183 |
@raises(EInvalidCompositeImage In case of any error in the file data.) } |
| 184 |
procedure LoadFromStream(Stream: TStream; const Url: String; |
| 185 |
MimeType: string = ''; |
| 186 |
const Options: TLoadImageOptions = []); |
| 187 |
|
| 188 |
{ Load composite (KTX or DDS) image from this Url. } |
| 189 |
procedure LoadFromFile(Url: String; const Options: TLoadImageOptions = []); |
| 190 |
|
| 191 |
procedure SaveToStream(Stream: TStream; const MimeType: string); |
| 192 |
procedure SaveToFile(const Url: String); |
| 193 |
|
| 194 |
{ Close all loaded image data. Effectively, this releases all data |
| 195 |
loaded by LoadFromStream, reverting the object to the state right |
| 196 |
after creation. } |
| 197 |
procedure Close; |
| 198 |
|
| 199 |
{ When @false, then closing this composite image will not free Images[0]. |
| 200 |
Closing happens when you call the @link(Close) method or |
| 201 |
destructor of this object. When this is @false, you're responsible |
| 202 |
to storing and freeing Images[0] later yourself, or you'll get memory |
| 203 |
leaks. } |
| 204 |
property OwnsFirstImage: boolean read FOwnsFirstImage write FOwnsFirstImage |
| 205 |
default true; |
| 206 |
|
| 207 |
{ Convert 3D images in @link(Images) list into a sequences of 2D images. |
| 208 |
Useful utility for 3d (volume) textures. |
| 209 |
|
| 210 |
Normal loading of 3D composite textures creates single TCastleImage (using Depth |
| 211 |
possibly > 1) for each mipmap level. Such TCastleImage with depth |
| 212 |
is comfortable if you want to load this 3d texture into OpenGL |
| 213 |
(as then the image data is just a continuous memory area, |
| 214 |
loadable by glTexImage3d). But it's not comfortable if you want |
| 215 |
to display it using some 2D GUI. For example, it's not comfortable |
| 216 |
for image viewer like castle-image-viewer. |
| 217 |
|
| 218 |
So this method will convert such TCastleImage instances (with Depth > 1) |
| 219 |
into a sequence of TCastleImage instances all with Depth = 1. |
| 220 |
This isn't difficult, memory contents on 3d TCastleImage may be splitted |
| 221 |
into many 2d TCastleImage instances without problems. |
| 222 |
|
| 223 |
Note that it's safe to do this before saving the image. |
| 224 |
SaveToFile/SaveToStream methods accept both layouts of images |
| 225 |
(because, as said, memory contents actually are the same before |
| 226 |
and after splitting). |
| 227 |
|
| 228 |
Note that this may free all Images (possibly even whole Images object), |
| 229 |
disregarding OwnsFirstImage (as it would be difficult, since |
| 230 |
it may or may not replace it with new images). } |
| 231 |
procedure Flatten3d; |
| 232 |
|
| 233 |
{ Decompress texture images (if any) on the @link(Images) list, |
| 234 |
replacing them with uncompressed equivalents. |
| 235 |
This can be used to decompress textures compressed using |
| 236 |
GPU compression algorithms, see @link(TTextureCompression). |
| 237 |
See TGPUCompressedImage.Decompress. |
| 238 |
|
| 239 |
Just like @link(Flatten3d): |
| 240 |
Note that this may free all Images (possibly even whole Images object), |
| 241 |
disregarding OwnsFirstImage (as it would be difficult, since |
| 242 |
it may or may not replace it with new images). |
| 243 |
|
| 244 |
@raises(ECannotDecompressTexture If some image cannot be decompressed |
| 245 |
for any reason.) } |
| 246 |
procedure DecompressTexture; |
| 247 |
|
| 248 |
{ Does this URL look like it contains composite (KTX, DDS...) contents. |
| 249 |
Guesses by processing the URL with @link(ProcessImageUrl) |
| 250 |
and then looking at final filename extension. } |
| 251 |
class function MatchesUrl(Url: String): boolean; |
| 252 |
|
| 253 |
procedure AddCubeMapImages(const AImages: TCubeMapImages); |
| 254 |
end; |
| 255 |
|
| 256 |
const |
| 257 |
AllCubeMapSides = [Low(TCubeMapSide) .. High(TCubeMapSide)]; |
| 258 |
|
| 259 |
CompositeTypeToString: array [TCompositeType] of string = |
| 260 |
( 'Texture', 'CubeMap', 'Volume' ); |
| 261 |
|
| 262 |
implementation |
| 263 |
|
| 264 |
uses SysUtils, Math, |
| 265 |
CastleUtils, CastleClassUtils, CastleLog, CastleStringUtils, |
| 266 |
CastleVectors, CastleDownload, CastleUriUtils; |
| 267 |
|
| 268 |
{$I castleinternalcompositeimage_format_handler.inc} |
| 269 |
{$I castleinternalcompositeimage_dds.inc} |
| 270 |
{$I castleinternalcompositeimage_ktx.inc} |
| 271 |
|
| 272 |
{ TCompositeImage ------------------------------------------------------------------ } |
| 273 |
|
| 274 |
constructor TCompositeImage.Create; |
| 275 |
begin |
| 276 |
inherited; |
| 277 |
FOwnsFirstImage := true; |
| 278 |
FImages := TEncodedImageList.Create(false); |
| 279 |
end; |
| 280 |
|
| 281 |
destructor TCompositeImage.Destroy; |
| 282 |
begin |
| 283 |
Close; |
| 284 |
FreeAndNil(FImages); |
| 285 |
inherited; |
| 286 |
end; |
| 287 |
|
| 288 |
procedure TCompositeImage.Close; |
| 289 |
var |
| 290 |
I: Integer; |
| 291 |
begin |
| 292 |
for I := 0 to Images.Count - 1 do |
| 293 |
if OwnsFirstImage or (I <> 0) then |
| 294 |
begin |
| 295 |
Images[I].Free; |
| 296 |
Images[I] := nil; |
| 297 |
end; |
| 298 |
|
| 299 |
Images.Count := 0; |
| 300 |
end; |
| 301 |
|
| 302 |
function TCompositeImage.GetImages(const Index: Integer): TEncodedImage; |
| 303 |
begin |
| 304 |
Result := FImages[Index]; |
| 305 |
end; |
| 306 |
|
| 307 |
function TCompositeImage.CubeMapImage(const Side: TCubeMapSide; |
| 308 |
const Level: Cardinal): TEncodedImage; |
| 309 |
var |
| 310 |
Index: Integer; |
| 311 |
begin |
| 312 |
Index := Ord(DDSFromCubeMapSide[Side]); |
| 313 |
if Mipmaps then |
| 314 |
Result := FImages[Cardinal(Index) * FMipmapsCount + Level] else |
| 315 |
Result := FImages[Index]; |
| 316 |
end; |
| 317 |
|
| 318 |
procedure TCompositeImage.LoadFromStream(Stream: TStream; const Url: String; |
| 319 |
MimeType: string = ''; |
| 320 |
const Options: TLoadImageOptions = []); |
| 321 |
var |
| 322 |
Handler: TCompositeFormatHandler; |
| 323 |
begin |
| 324 |
Close; |
| 325 |
|
| 326 |
if liFlipVertically in Options then |
| 327 |
WritelnWarning('ImageTexture.flipVertically for DDS/KTX not implemented yet, the image will be inverted'); |
| 328 |
|
| 329 |
if MimeType = '' then |
| 330 |
MimeType := UriMimeType(Url); |
| 331 |
|
| 332 |
if MimeType = 'image/x-dds' then |
| 333 |
Handler := TDDSHandler.Create(Self) |
| 334 |
else |
| 335 |
if MimeType = 'image/ktx' then |
| 336 |
Handler := TKTXHandler.Create(Self) |
| 337 |
else |
| 338 |
raise EInvalidCompositeImage.CreateFmt('Cannot recognize composite image MIME type: %s', |
| 339 |
[MimeType]); |
| 340 |
|
| 341 |
try |
| 342 |
Handler.LoadFromStream(Stream, Url); |
| 343 |
finally FreeAndNil(Handler) end; |
| 344 |
end; |
| 345 |
|
| 346 |
procedure TCompositeImage.LoadFromFile(Url: String; |
| 347 |
const Options: TLoadImageOptions = []); |
| 348 |
var |
| 349 |
S: TStream; |
| 350 |
begin |
| 351 |
Url := ProcessImageUrl(Url); |
| 352 |
|
| 353 |
S := Download(Url, [soForceMemoryStream]); |
| 354 |
try |
| 355 |
LoadFromStream(S, Url, '', Options); |
| 356 |
finally FreeAndNil(S) end; |
| 357 |
end; |
| 358 |
|
| 359 |
procedure TCompositeImage.SaveToStream(Stream: TStream; const MimeType: string); |
| 360 |
var |
| 361 |
Handler: TCompositeFormatHandler; |
| 362 |
begin |
| 363 |
Assert(Images.Count > 0, 'Images count must be > 0 when saving a composite image'); |
| 364 |
|
| 365 |
if MimeType = 'image/x-dds' then |
| 366 |
Handler := TDDSHandler.Create(Self) |
| 367 |
else |
| 368 |
if MimeType = 'image/ktx' then |
| 369 |
raise EInvalidCompositeImage.Create('Cannot save to the KTX format now') |
| 370 |
else |
| 371 |
raise EInvalidCompositeImage.CreateFmt('Cannot recognize composite image MIME type: %s', |
| 372 |
[MimeType]); |
| 373 |
|
| 374 |
try |
| 375 |
Handler.SaveToStream(Stream); |
| 376 |
finally FreeAndNil(Handler) end; |
| 377 |
end; |
| 378 |
|
| 379 |
procedure TCompositeImage.SaveToFile(const Url: String); |
| 380 |
var |
| 381 |
S: TStream; |
| 382 |
begin |
| 383 |
S := UrlSaveStream(Url); |
| 384 |
try |
| 385 |
SaveToStream(S, UriMimeType(Url)); |
| 386 |
finally FreeAndNil(S) end; |
| 387 |
end; |
| 388 |
|
| 389 |
class function TCompositeImage.MatchesUrl(Url: String): boolean; |
| 390 |
begin |
| 391 |
Url := ProcessImageUrl(Url); |
| 392 |
|
| 393 |
Result := |
| 394 |
(UriMimeType(Url) = 'image/x-dds') or |
| 395 |
(UriMimeType(Url) = 'image/ktx'); |
| 396 |
end; |
| 397 |
|
| 398 |
procedure TCompositeImage.Flatten3d; |
| 399 |
var |
| 400 |
NewImages: TEncodedImageList; |
| 401 |
OldImage, NewImage: TCastleImage; |
| 402 |
I, J: Integer; |
| 403 |
begin |
| 404 |
if (CompositeType = ctVolume) and (Depth > 1) then |
| 405 |
begin |
| 406 |
NewImages := TEncodedImageList.Create(false); |
| 407 |
|
| 408 |
for I := 0 to Images.Count - 1 do |
| 409 |
begin |
| 410 |
if not (Images[I] is TCastleImage) then |
| 411 |
raise Exception.CreateFmt('Cannot do Flatten3d on this image class: %s', |
| 412 |
[Images[I].ClassName]); |
| 413 |
OldImage := TCastleImage(Images[I]); |
| 414 |
|
| 415 |
for J := 0 to OldImage.Depth - 1 do |
| 416 |
begin |
| 417 |
NewImage := TCastleImageClass(OldImage.ClassType).Create( |
| 418 |
OldImage.Width, OldImage.Height, 1); |
| 419 |
Move(OldImage.PixelPtr(0, 0, J)^, NewImage.RawPixels^, |
| 420 |
OldImage.Width * OldImage.Height * OldImage.PixelSize); |
| 421 |
NewImages.Add(NewImage); |
| 422 |
end; |
| 423 |
end; |
| 424 |
|
| 425 |
Close; |
| 426 |
FreeAndNil(FImages); |
| 427 |
FImages := NewImages; |
| 428 |
end; |
| 429 |
end; |
| 430 |
|
| 431 |
procedure TCompositeImage.DecompressTexture; |
| 432 |
var |
| 433 |
OldImage: TGPUCompressedImage; |
| 434 |
I: Integer; |
| 435 |
begin |
| 436 |
for I := 0 to Images.Count - 1 do |
| 437 |
if Images[I] is TGPUCompressedImage then |
| 438 |
begin |
| 439 |
OldImage := TGPUCompressedImage(Images[I]); |
| 440 |
Images[I] := OldImage.Decompress; |
| 441 |
FreeAndNil(OldImage); |
| 442 |
end; |
| 443 |
end; |
| 444 |
|
| 445 |
function TCompositeImage.GetCubeMapSides: TCubeMapSides; |
| 446 |
var |
| 447 |
DDSSide: TDDSCubeMapSide; |
| 448 |
begin |
| 449 |
Result := []; |
| 450 |
for DDSSide in FCubeMapSides do |
| 451 |
Include(Result, DDSToCubeMapSide[DDSSide]); |
| 452 |
end; |
| 453 |
|
| 454 |
procedure TCompositeImage.SetCubeMapSides(const Value: TCubeMapSides); |
| 455 |
var |
| 456 |
Side: TCubeMapSide; |
| 457 |
begin |
| 458 |
FCubeMapSides := []; |
| 459 |
for Side in Value do |
| 460 |
Include(FCubeMapSides, DDSFromCubeMapSide[Side]); |
| 461 |
end; |
| 462 |
|
| 463 |
procedure TCompositeImage.AddCubeMapImages(const AImages: TCubeMapImages); |
| 464 |
var |
| 465 |
StartIndex: Integer; |
| 466 |
begin |
| 467 |
StartIndex := Images.Count; |
| 468 |
Images.Count := StartIndex + 6; |
| 469 |
{ note that we use DDSFromCubeMapSide on the left side, |
| 470 |
so that Images order is like in TDDSCubeMapSide enum. } |
| 471 |
Images[StartIndex + Ord(DDSFromCubeMapSide[csPositiveX])] := AImages[csPositiveX]; |
| 472 |
Images[StartIndex + Ord(DDSFromCubeMapSide[csNegativeX])] := AImages[csNegativeX]; |
| 473 |
Images[StartIndex + Ord(DDSFromCubeMapSide[csPositiveY])] := AImages[csPositiveY]; |
| 474 |
Images[StartIndex + Ord(DDSFromCubeMapSide[csNegativeY])] := AImages[csNegativeY]; |
| 475 |
Images[StartIndex + Ord(DDSFromCubeMapSide[csPositiveZ])] := AImages[csPositiveZ]; |
| 476 |
Images[StartIndex + Ord(DDSFromCubeMapSide[csNegativeZ])] := AImages[csNegativeZ]; |
| 477 |
end; |
| 478 |
|
| 479 |
end. |