| 1 |
{%MainUnit castleimages.pas} |
| 2 |
{ |
| 3 |
Copyright 2001-2024 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 |
{ Image formats. } |
| 18 |
|
| 19 |
type |
| 20 |
{ Possible image file format. |
| 21 |
|
| 22 |
The order on this list matters --- it determines the order of filters |
| 23 |
for open/save dialogs. |
| 24 |
First we list most adviced and well-known formats, starting from lossless. } |
| 25 |
TImageFormat = ( |
| 26 |
{ We have full PNG support, for both reading and writing. |
| 27 |
We can use LibPng (if available on given system), (FPC) FpImage, |
| 28 |
Vampyre Imaging Library, or (Delphi) PngImage. } |
| 29 |
ifPNG, |
| 30 |
|
| 31 |
{ Image formats below are supported by FPImage and/or Vampyre Imaging Library. } |
| 32 |
ifBMP, |
| 33 |
ifJPEG, |
| 34 |
ifGIF, |
| 35 |
ifTGA, |
| 36 |
ifXPM, |
| 37 |
ifPSD, |
| 38 |
ifPCX, |
| 39 |
ifPNM, |
| 40 |
|
| 41 |
{ We handle fully DDS (DirectDraw Surface) and KTX image formats. |
| 42 |
They provide images in formats optimal for GPU. } |
| 43 |
ifDDS, |
| 44 |
ifKTX, |
| 45 |
|
| 46 |
{ Reading ASTC image format } |
| 47 |
ifASTC, |
| 48 |
|
| 49 |
{ High-dynamic range image format, originally used by Radiance. |
| 50 |
See e.g. the pfilt and ximage programs from the Radiance package |
| 51 |
for processing such images. |
| 52 |
|
| 53 |
The float color values are encoded smartly as 4 bytes: |
| 54 |
3 mantisas for RGB and 1 byte for an Exponent. |
| 55 |
This is the Greg Ward's RGBE color encoding described in the |
| 56 |
"Graphic Gems" (gem II.5). This allows high floating-point-like precision, |
| 57 |
and possibility to encode any value >= 0 (not necessarily <= 1), |
| 58 |
keeping the pixel only 4 bytes long. |
| 59 |
|
| 60 |
Encoding a color values with float precision is very useful. |
| 61 |
Otherwise, when synthesized / photographed images are |
| 62 |
very dark / very bright, simply encoding them in traditional fixed-point |
| 63 |
pixel format looses color precision. So potentially important but small |
| 64 |
differences are lost in fixed-point formats. |
| 65 |
And color values are clamped to [0..1] range. |
| 66 |
On the other hand, keeping colors as floats preserves |
| 67 |
everything, and allows to process images later. |
| 68 |
|
| 69 |
It's most useful and natural to load/save these files as TRGBFloatImage, |
| 70 |
this way you keep the floating-point precision inside memory. |
| 71 |
However, you can also load/convert such image format |
| 72 |
to normal 8-bits image formats (like TRGBImage), |
| 73 |
if you're Ok with losing some of the precision. } |
| 74 |
ifRGBE |
| 75 |
|
| 76 |
{$ifdef CASTLE_ENABLE_PLATFORM_SPECIFIC_IMAGE_FORMATS} |
| 77 |
, |
| 78 |
ifTIFF, |
| 79 |
ifJP2 |
| 80 |
{$endif} |
| 81 |
); |
| 82 |
TImageFormats = set of TImageFormat; |
| 83 |
|
| 84 |
{ Loading image (format-specific) --------------------------------------- |
| 85 |
|
| 86 |
Load image from Stream. |
| 87 |
|
| 88 |
Image loaders may look at AllowedImageClasses (to return a data type matching required |
| 89 |
AllowedImageClasses) but they don't have to. A wrapper (LoadEncodedImage) will always |
| 90 |
fix the class to satisfy AllowedImageClasses anyway (albeit doing it inside the particular |
| 91 |
loader may be faster, e.g. it's better to use LibPng transformations than LoadEncodedImage |
| 92 |
FixImageClass, as it avoids unnecessarily copying the data is wrong format, only to fix it later.) |
| 93 |
|
| 94 |
An appropriate descendant of EImageLoadError will be raised |
| 95 |
in case of error when reading from Stream or when Stream will not |
| 96 |
contain correct data. } |
| 97 |
|
| 98 |
type |
| 99 |
EInvalidBMP = class(EInvalidImageFormat); |
| 100 |
EInvalidPNG = class(EInvalidImageFormat); |
| 101 |
EInvalidIPL = class(EInvalidImageFormat); |
| 102 |
EInvalidASTC = class(EInvalidImageFormat); |
| 103 |
|
| 104 |
function LoadPNG(const Stream: TStream; |
| 105 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; forward; |
| 106 |
|
| 107 |
function LoadBMP(const Stream: TStream; |
| 108 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; forward; |
| 109 |
|
| 110 |
function LoadGIF(const Stream: TStream; |
| 111 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; forward; |
| 112 |
|
| 113 |
function LoadTGA(const Stream: TStream; |
| 114 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; forward; |
| 115 |
|
| 116 |
{function LoadSGI(const Stream: TStream; |
| 117 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; forward;} |
| 118 |
|
| 119 |
{$ifdef CASTLE_ENABLE_PLATFORM_SPECIFIC_IMAGE_FORMATS} |
| 120 |
function LoadTIFF(const Stream: TStream; |
| 121 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; forward; |
| 122 |
function LoadJP2(const Stream: TStream; |
| 123 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; forward; |
| 124 |
{$endif} |
| 125 |
|
| 126 |
{function LoadEXR(const Stream: TStream; |
| 127 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; forward;} |
| 128 |
|
| 129 |
function LoadJPEG(const Stream: TStream; |
| 130 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; forward; |
| 131 |
|
| 132 |
function LoadXPM(const Stream: TStream; |
| 133 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; forward; |
| 134 |
|
| 135 |
function LoadPSD(const Stream: TStream; |
| 136 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; forward; |
| 137 |
|
| 138 |
function LoadPCX(const Stream: TStream; |
| 139 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; forward; |
| 140 |
|
| 141 |
{ Load PNM image (PNM, PGM, PBM, PPM). } |
| 142 |
function LoadPNM(const Stream: TStream; |
| 143 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; forward; |
| 144 |
|
| 145 |
{ Load RGBE image. } |
| 146 |
function LoadRGBE(const Stream: TStream; |
| 147 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; forward; |
| 148 |
|
| 149 |
{ Load DDS image file into a single 2D image. This simply returns the first |
| 150 |
image found in DDS file, which should be the main image. |
| 151 |
If you want to investigate other images in DDS, you have to use TCompositeImage |
| 152 |
class. } |
| 153 |
function LoadDDS(const Stream: TStream; |
| 154 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; forward; |
| 155 |
function LoadKTX(const Stream: TStream; |
| 156 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; forward; |
| 157 |
|
| 158 |
function LoadASTC(const Stream: TStream; |
| 159 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; forward; |
| 160 |
|
| 161 |
{ Saving image (format-specific) -------------------------------------------- |
| 162 |
|
| 163 |
SaveXxx. Each file format may have specialized SaveXxx that allows |
| 164 |
you to give some parameters special for given format. |
| 165 |
|
| 166 |
Each format must also have procedure with two parameters |
| 167 |
(Img: TEncodedImage; Stream: TStream), this will be used with |
| 168 |
ImageFormatsInfo[]. |
| 169 |
This means that below we must use overloading instead of |
| 170 |
default parameters, since pointers to given procedures must be |
| 171 |
compatible with @link(TImageSaveFunc). |
| 172 |
|
| 173 |
SaveXxx should |
| 174 |
raise EImageSaveError.CreateFmt('Saving to XXX image class %s not possible', [Img.ClassName]); |
| 175 |
when Img doesn't have acceptable class. |
| 176 |
} |
| 177 |
|
| 178 |
{ } |
| 179 |
procedure SaveBMP(const Img: TEncodedImage; const Stream: TStream); forward; |
| 180 |
procedure SavePNG(const Img: TEncodedImage; const Stream: TStream); forward; |
| 181 |
procedure SaveGIF(const Img: TEncodedImage; const Stream: TStream); forward; |
| 182 |
procedure SaveTGA(const Img: TEncodedImage; const Stream: TStream); forward; |
| 183 |
procedure SaveXPM(const Img: TEncodedImage; const Stream: TStream); forward; |
| 184 |
procedure SavePNM(const Img: TEncodedImage; const Stream: TStream); forward; |
| 185 |
procedure SaveJPEG(const Img: TEncodedImage; const Stream: TStream); forward; |
| 186 |
procedure SaveRGBE(const Img: TEncodedImage; const Stream: TStream); forward; |
| 187 |
procedure SaveDDS(const Img: TEncodedImage; const Stream: TStream); forward; |
| 188 |
{$ifdef CASTLE_ENABLE_PLATFORM_SPECIFIC_IMAGE_FORMATS} |
| 189 |
procedure SaveTIFF(const Img: TEncodedImage; const Stream: TStream); forward; |
| 190 |
procedure SaveJP2(const Img: TEncodedImage; const Stream: TStream); forward; |
| 191 |
{$endif} |
| 192 |
|
| 193 |
{ Detecting class ------------------------------------------------------------ } |
| 194 |
|
| 195 |
function DetectClassJPG(const Stream: TStream): TEncodedImageClass; |
| 196 |
begin |
| 197 |
{ JPG format cannot express grayscale or alpha. } |
| 198 |
Result := TRGBImage; |
| 199 |
end; |
| 200 |
|
| 201 |
function InternalDetectClassPNG(const Stream: TStream): TEncodedImageClass; |
| 202 |
|
| 203 |
function ChunkTypeCode(const ChunkName: String): UInt32; |
| 204 |
begin |
| 205 |
Assert(Length(ChunkName) = SizeOf(UInt32)); |
| 206 |
Result := |
| 207 |
{$ifdef CASTLE_LITTLE_ENDIAN} |
| 208 |
UInt32(Ord(ChunkName[4])) shl 24 + |
| 209 |
UInt32(Ord(ChunkName[3])) shl 16 + |
| 210 |
UInt32(Ord(ChunkName[2])) shl 8 + |
| 211 |
Ord(ChunkName[1]) |
| 212 |
{$else} |
| 213 |
UInt32(Ord(ChunkName[1])) shl 24 + |
| 214 |
UInt32(Ord(ChunkName[2])) shl 16 + |
| 215 |
UInt32(Ord(ChunkName[3])) shl 8 + |
| 216 |
Ord(ChunkName[4]) |
| 217 |
{$endif} |
| 218 |
; |
| 219 |
end; |
| 220 |
|
| 221 |
const |
| 222 |
{ Color type masks and types. |
| 223 |
Copied from castleinternalpng_dynamic.inc, so this compiles regardless of libpng unit. } |
| 224 |
PNG_COLOR_MASK_PALETTE = 1; |
| 225 |
PNG_COLOR_MASK_COLOR = 2; |
| 226 |
PNG_COLOR_MASK_ALPHA = 4; |
| 227 |
|
| 228 |
PNG_COLOR_TYPE_GRAY = 0; |
| 229 |
PNG_COLOR_TYPE_PALETTE = (PNG_COLOR_MASK_COLOR or PNG_COLOR_MASK_PALETTE); |
| 230 |
PNG_COLOR_TYPE_RGB = (PNG_COLOR_MASK_COLOR); |
| 231 |
PNG_COLOR_TYPE_RGB_ALPHA = (PNG_COLOR_MASK_COLOR or PNG_COLOR_MASK_ALPHA); |
| 232 |
PNG_COLOR_TYPE_GRAY_ALPHA = (PNG_COLOR_MASK_ALPHA); |
| 233 |
var |
| 234 |
ReadHeader, ChunkType, ChunkSize, IWidth, IHeight: UInt32; |
| 235 |
ChunkTypeIHDR, ChunkTypetRNS, ChunkTypeIEND: UInt32; |
| 236 |
Bits, ColorType: Byte; |
| 237 |
begin |
| 238 |
try |
| 239 |
{ See https://en.wikipedia.org/wiki/Portable_Network_Graphics about PNG header } |
| 240 |
|
| 241 |
ChunkTypeIHDR := ChunkTypeCode('IHDR'); |
| 242 |
ChunkTypetRNS := ChunkTypeCode('tRNS'); |
| 243 |
ChunkTypeIEND := ChunkTypeCode('IEND'); |
| 244 |
|
| 245 |
Stream.ReadBuffer(ReadHeader, SizeOf(ReadHeader)); |
| 246 |
if BEtoN(ReadHeader) <> $89504E47 then |
| 247 |
Exit(nil); |
| 248 |
|
| 249 |
Stream.ReadBuffer(ReadHeader, SizeOf(ReadHeader)); |
| 250 |
if BEtoN(ReadHeader) <> $0D0A1A0A then |
| 251 |
Exit(nil); |
| 252 |
|
| 253 |
Stream.ReadBuffer(ChunkSize, SizeOf(ChunkSize)); |
| 254 |
if BEtoN(ChunkSize) <> 13 then |
| 255 |
Exit(nil); |
| 256 |
|
| 257 |
Stream.ReadBuffer(ChunkType, SizeOf(ChunkType)); |
| 258 |
if LEtoN(ChunkType) <> ChunkTypeIHDR then // chunk IHDR must be first |
| 259 |
Exit(nil); |
| 260 |
|
| 261 |
Stream.ReadBuffer(IWidth, SizeOf(IWidth)); |
| 262 |
// IWidth := BEtoN(IWidth); // not needed, as we don't actually use it |
| 263 |
Stream.ReadBuffer(IHeight, SizeOf(IHeight)); |
| 264 |
// IHeight := BEtoN(IHeight); // not needed, as we don't actually use it |
| 265 |
Stream.ReadBuffer(Bits, SizeOf(Bits)); |
| 266 |
Stream.ReadBuffer(ColorType, SizeOf(ColorType)); |
| 267 |
|
| 268 |
case ColorType of |
| 269 |
PNG_COLOR_TYPE_GRAY : Result := TGrayscaleImage; |
| 270 |
PNG_COLOR_TYPE_RGB : Result := TRGBImage; |
| 271 |
PNG_COLOR_TYPE_RGB_ALPHA : Result := TRGBAlphaImage; |
| 272 |
PNG_COLOR_TYPE_GRAY_ALPHA: Result := TGrayscaleAlphaImage; |
| 273 |
else Result := nil; // unknown, possible in case of PNG_COLOR_TYPE_PALETTE |
| 274 |
end; |
| 275 |
|
| 276 |
// if Result indicates non-alpha image, scan for PNG tRNS chunk |
| 277 |
if (Result = TRGBImage) or |
| 278 |
(Result = TGrayscaleImage) then |
| 279 |
begin |
| 280 |
{ skip 3 remaining bytes of IHDR chunk, + 4 for CRC } |
| 281 |
Stream.Position := Stream.Position + 3 + 4; |
| 282 |
|
| 283 |
{ We need to detect whether we have tRNS chunk. |
| 284 |
Libpng implementation does this by |
| 285 |
|
| 286 |
png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) <> 0 |
| 287 |
|
| 288 |
In this case, we have alpha information, |
| 289 |
even though ColorType doesn't indicate it. |
| 290 |
See png_is_alpha implementation in castleinternalpng.pas. |
| 291 |
} |
| 292 |
|
| 293 |
while ChunkType <> ChunkTypeIEND do |
| 294 |
begin |
| 295 |
Stream.ReadBuffer(ChunkSize, SizeOf(ChunkSize)); |
| 296 |
ChunkSize := BEtoN(ChunkSize); |
| 297 |
|
| 298 |
Stream.ReadBuffer(ChunkType, SizeOf(ChunkType)); |
| 299 |
|
| 300 |
// if tRNS, then update Result class and break |
| 301 |
if LEtoN(ChunkType) = ChunkTypetRNS then |
| 302 |
begin |
| 303 |
if Result = TRGBImage then |
| 304 |
begin |
| 305 |
Result := TRGBAlphaImage; |
| 306 |
Break; |
| 307 |
end else |
| 308 |
if Result = TGrayscaleImage then |
| 309 |
begin |
| 310 |
Result := TGrayscaleAlphaImage; |
| 311 |
Break; |
| 312 |
end; |
| 313 |
end; |
| 314 |
|
| 315 |
Stream.Position := Stream.Position + ChunkSize + 4 { CRC }; |
| 316 |
end; |
| 317 |
end; |
| 318 |
finally |
| 319 |
Stream.Position := 0; |
| 320 |
end; |
| 321 |
end; |
| 322 |
|
| 323 |
{ File formats list ---------------------------------------------------------- } |
| 324 |
|
| 325 |
type |
| 326 |
{ See TImageFormatInfo.Load } |
| 327 |
TImageLoadFunc = function (const Stream: TStream; |
| 328 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; |
| 329 |
|
| 330 |
{ See TImageFormatInfo.Save } |
| 331 |
TImageSaveFunc = procedure (const Img: TEncodedImage; const Stream: TStream); |
| 332 |
|
| 333 |
{ See TImageFormatInfo.DetectClass } |
| 334 |
TImageDetectClassFunc = function (const Stream: TStream): TEncodedImageClass; |
| 335 |
|
| 336 |
{ Possible TEncodedImage classes supported by Save method of this file format. } |
| 337 |
TImageSaveHandledClasses = ( |
| 338 |
scRGB, |
| 339 |
scG_GA_RGB_RGBA, |
| 340 |
scG_GA_RGB_RGBA_GPUCompressed, |
| 341 |
scRGB_RGBFloat, |
| 342 |
scAnything |
| 343 |
); |
| 344 |
|
| 345 |
{ Index of TImageFormatInfo.MimeTypes array and |
| 346 |
type for TImageFormatInfo.MimeTypesCount. |
| 347 |
Implies that TImageFormatInfo.MimeTypes is indexed from 1, |
| 348 |
TImageFormatInfo.MimeTypesCount must be >= 1, |
| 349 |
so each file format must have at least one |
| 350 |
(treated as "default" in some cases) MIME type. } |
| 351 |
TImageFormatInfoMimeTypesCount = 1..7; |
| 352 |
|
| 353 |
{ A type to index TImageFormatInfo.Exts array and also for TImageFormatInfo.ExtsCount. |
| 354 |
So TImageFormatInfo.Exts array is indexed from 1, |
| 355 |
and TImageFormatInfo.ExtsCount must be >= 1, so each file format must have at least one |
| 356 |
(treated as "default" in some cases) file extension. } |
| 357 |
TImageFormatInfoExtsCount = 1..4; |
| 358 |
|
| 359 |
{ Record with information specific to TImageFormat. } |
| 360 |
TImageFormatInfo = record |
| 361 |
{ Human-readable format name. |
| 362 |
|
| 363 |
Note that this is supposed to be shown to normal user, |
| 364 |
in save dialog boxes etc. So it should be short and concise. I used to |
| 365 |
have here long format names like @code(JFIF, JPEG File Interchange Format) or |
| 366 |
@code(PNG, Portable Network Graphic), but they are too ugly, and unnecessarily |
| 367 |
resolving format abbrevs. For example, most users probably used JPEG, |
| 368 |
but not many have to know, or understand, that actually this is image format JFIF; |
| 369 |
these are technical and historical details that are not needed for normal usage of image |
| 370 |
operations. |
| 371 |
|
| 372 |
Saying it directly, I want to keep this FormatName short and concise. |
| 373 |
This is not a place to educate users what some abbrev means. |
| 374 |
This is a place to "name" each file format in the most natural way, which |
| 375 |
usually means to only slightly rephrase typical file format extension. |
| 376 |
|
| 377 |
In practice, I now copy descriptions from English GIMP open dialog. } |
| 378 |
FormatName: string; |
| 379 |
|
| 380 |
MimeTypesCount: TImageFormatInfoMimeTypesCount; |
| 381 |
|
| 382 |
{ MIME types recognized as this image file format. |
| 383 |
First MIME type is the default for this file format |
| 384 |
(some procedures make use of it). } |
| 385 |
MimeTypes: array [TImageFormatInfoMimeTypesCount] of string; |
| 386 |
|
| 387 |
ExtsCount: TImageFormatInfoExtsCount; |
| 388 |
|
| 389 |
{ File extensions for this image type. |
| 390 |
First file extension is default, which is used for some routines. |
| 391 |
Must be lowercase. |
| 392 |
|
| 393 |
This is used e.g. to construct file filters in open/save dialogs. |
| 394 |
Together with MimeTypes it is also used by UriMimeType to map |
| 395 |
file extension into a MIME type. An extension matching one of Exts |
| 396 |
values implicates the default MIME type for this format (MimeTypes[1]). |
| 397 |
|
| 398 |
Note that to cooperate nicely with network URLs |
| 399 |
(when server may report MIME type) and data URIs, most of the code |
| 400 |
should operate using MIME types instead of file extensions. |
| 401 |
So usually you are more interested in MimeTypes than Exts. } |
| 402 |
Exts: array [TImageFormatInfoExtsCount] of string; |
| 403 |
|
| 404 |
{ Load method for this file format. |
| 405 |
@nil if cannot be loaded. } |
| 406 |
Load: TImageLoadFunc; |
| 407 |
|
| 408 |
{ Save method for this file format. |
| 409 |
@nil if cannot be saved. } |
| 410 |
Save: TImageSaveFunc; |
| 411 |
|
| 412 |
{ Detect the class to which load image, based on Stream. |
| 413 |
Return @nil if auto-detection failed. |
| 414 |
|
| 415 |
If this function actually reads some Stream contents, |
| 416 |
it @italic(must) reset Stream.Position to 0 afterwards |
| 417 |
(we don't do it automatically, to avoid exceptions in case this function |
| 418 |
doesn't read from Stream, and stream doesn't support "Stream.Position := 0"). |
| 419 |
} |
| 420 |
DetectClass: TImageDetectClassFunc; |
| 421 |
end; |
| 422 |
|
| 423 |
const |
| 424 |
{ Information about supported image formats. } |
| 425 |
ImageFormatInfos: array [TImageFormat] of TImageFormatInfo = |
| 426 |
( |
| 427 |
|
| 428 |
{ Portable Network Graphic } { } |
| 429 |
( FormatName: 'PNG image'; |
| 430 |
MimeTypesCount: 1; |
| 431 |
MimeTypes: ('image/png', '', '', '', '', '', ''); |
| 432 |
ExtsCount: 1; Exts: ('png', '', '', ''); |
| 433 |
{ Regardless of using libpng or not, our PNG load/save can handle |
| 434 |
all possibilities. } |
| 435 |
Load: {$ifdef FPC}@{$endif}LoadPNG; |
| 436 |
Save: {$ifdef FPC}@{$endif}SavePNG; |
| 437 |
DetectClass: {$ifdef FPC}@{$endif}InternalDetectClassPNG; |
| 438 |
), |
| 439 |
( FormatName: 'Windows BMP image'; |
| 440 |
MimeTypesCount: 1; |
| 441 |
MimeTypes: ('image/bmp', '', '', '', '', '', ''); |
| 442 |
ExtsCount: 1; Exts: ('bmp', '', '', ''); |
| 443 |
Load: {$ifdef FPC}@{$endif}LoadBMP; |
| 444 |
Save: {$ifdef FPC}@{$endif}SaveBMP; |
| 445 |
DetectClass: nil; |
| 446 |
), |
| 447 |
{ JFIF, JPEG File Interchange Format } { } |
| 448 |
( FormatName: 'JPEG image'; |
| 449 |
MimeTypesCount: 2; |
| 450 |
MimeTypes: ('image/jpeg', 'image/jpg', '', '', '', '', ''); |
| 451 |
ExtsCount: 3; Exts: ('jpg', 'jpeg', 'jpe', ''); |
| 452 |
Load: {$ifdef FPC}@{$endif}LoadJPEG; |
| 453 |
Save: {$ifdef FPC}@{$endif}SaveJPEG; |
| 454 |
DetectClass: {$ifdef FPC}@{$endif}DetectClassJPG; |
| 455 |
), |
| 456 |
{ Graphics Interchange Format } { } |
| 457 |
( FormatName: 'GIF image'; |
| 458 |
MimeTypesCount: 1; |
| 459 |
MimeTypes: ('image/gif', '', '', '', '', '', ''); |
| 460 |
ExtsCount: 1; Exts: ('gif', '', '', ''); |
| 461 |
Load: {$ifdef FPC}@{$endif}LoadGIF; |
| 462 |
Save: {$ifdef FPC}@{$endif}SaveGIF; |
| 463 |
DetectClass: nil; |
| 464 |
), |
| 465 |
( FormatName: 'TarGA image'; |
| 466 |
MimeTypesCount: 2; |
| 467 |
MimeTypes: ('image/x-targa', 'image/x-tga', '', '', '', '', ''); |
| 468 |
ExtsCount: 2; Exts: ('tga', 'tpic', '', ''); |
| 469 |
Load: {$ifdef FPC}@{$endif}LoadTGA; |
| 470 |
Save: {$ifdef FPC}@{$endif}SaveTGA; |
| 471 |
DetectClass: nil; |
| 472 |
), |
| 473 |
( FormatName: 'XPM image'; |
| 474 |
MimeTypesCount: 1; |
| 475 |
MimeTypes: ('image/x-xpixmap', '', '', '', '', '', ''); |
| 476 |
ExtsCount: 1; Exts: ('xpm', '', '', ''); |
| 477 |
Load: {$ifdef FPC}@{$endif}LoadXPM; |
| 478 |
Save: {$ifdef FPC}@{$endif}SaveXPM; |
| 479 |
DetectClass: nil; |
| 480 |
), |
| 481 |
( FormatName: 'PSD image'; |
| 482 |
MimeTypesCount: 4; |
| 483 |
MimeTypes: ('image/photoshop', 'image/x-photoshop', 'image/psd', 'application/photoshop', '', '', ''); |
| 484 |
ExtsCount: 1; Exts: ('psd', '', '', ''); |
| 485 |
Load: {$ifdef FPC}@{$endif}LoadPSD; |
| 486 |
Save: nil; |
| 487 |
DetectClass: nil; |
| 488 |
), |
| 489 |
( FormatName: 'ZSoft PCX image'; |
| 490 |
MimeTypesCount: 5; |
| 491 |
MimeTypes: ('image/pcx', 'application/pcx', 'application/x-pcx', 'image/x-pc-paintbrush', 'image/x-pcx', '', ''); |
| 492 |
ExtsCount: 1; Exts: ('pcx', '', '', ''); |
| 493 |
Load: {$ifdef FPC}@{$endif}LoadPCX; |
| 494 |
Save: nil; |
| 495 |
DetectClass: nil; |
| 496 |
), |
| 497 |
( FormatName: 'PNM image'; |
| 498 |
MimeTypesCount: 7; |
| 499 |
MimeTypes: ('image/x-portable-anymap', 'image/x-portable-graymap', 'image/x-pgm', 'image/x-portable-bitmap', 'image/pbm', 'image/x-pbm', 'image/x-portable-pixmap'); |
| 500 |
ExtsCount: 4; Exts: ('pnm', 'pgm', 'pbm', 'ppm'); |
| 501 |
Load: {$ifdef FPC}@{$endif}LoadPNM; |
| 502 |
Save: {$ifdef FPC}@{$endif}SavePNM; |
| 503 |
DetectClass: nil; |
| 504 |
), |
| 505 |
|
| 506 |
{ Direct Draw Surface } { } |
| 507 |
( FormatName: 'DDS image'; |
| 508 |
MimeTypesCount: 1; |
| 509 |
MimeTypes: ('image/x-dds', '', '', '', '', '', ''); |
| 510 |
ExtsCount: 1; Exts: ('dds', '', '', ''); |
| 511 |
Load: {$ifdef FPC}@{$endif}LoadDDS; |
| 512 |
Save: {$ifdef FPC}@{$endif}SaveDDS; |
| 513 |
DetectClass: nil; |
| 514 |
), |
| 515 |
|
| 516 |
{ Khronos KTX } { } |
| 517 |
( FormatName: 'Khronos KTX image'; |
| 518 |
MimeTypesCount: 1; |
| 519 |
MimeTypes: ('image/ktx', '', '', '', '', '', ''); |
| 520 |
ExtsCount: 1; Exts: ('ktx', '', '', ''); |
| 521 |
Load: {$ifdef FPC}@{$endif}LoadKTX; |
| 522 |
Save: nil; |
| 523 |
DetectClass: nil; |
| 524 |
), |
| 525 |
{ ASTC } { } |
| 526 |
( FormatName: 'ASTC image'; |
| 527 |
MimeTypesCount: 1; |
| 528 |
MimeTypes: ('image/astc', '', '', '', '', '', ''); |
| 529 |
ExtsCount: 1; Exts: ('astc', '', '', ''); |
| 530 |
Load: {$ifdef FPC}@{$endif}LoadASTC; |
| 531 |
Save: nil; |
| 532 |
DetectClass: nil; |
| 533 |
), |
| 534 |
( FormatName: 'RGBE (Radiance, HDR) image'; |
| 535 |
MimeTypesCount: 1; |
| 536 |
MimeTypes: ('image/vnd.radiance', '', '', '', '', '', ''); |
| 537 |
ExtsCount: 3; Exts: ('rgbe', 'pic', 'hdr', ''); |
| 538 |
Load: {$ifdef FPC}@{$endif}LoadRGBE; |
| 539 |
Save: {$ifdef FPC}@{$endif}SaveRGBE; |
| 540 |
DetectClass: nil; |
| 541 |
) |
| 542 |
|
| 543 |
{$ifdef CASTLE_ENABLE_PLATFORM_SPECIFIC_IMAGE_FORMATS} |
| 544 |
, |
| 545 |
( FormatName: 'TIFF image'; |
| 546 |
MimeTypesCount: 1; |
| 547 |
MimeTypes: ('image/tiff', '', '', '', '', '', ''); |
| 548 |
ExtsCount: 2; Exts: ('tiff', 'tif', '', ''); |
| 549 |
Load: {$ifdef FPC}@{$endif}LoadTIFF; |
| 550 |
// Saving to TIFF always seems to cause SEGFAULT; TODO: test and submit to Vampyre |
| 551 |
//Save: {$ifdef FPC}@{$endif}SaveTIFF; |
| 552 |
Save: nil; |
| 553 |
DetectClass: nil; |
| 554 |
), |
| 555 |
( FormatName: 'JPEG 2000 image'; |
| 556 |
MimeTypesCount: 4; |
| 557 |
MimeTypes: ('image/jp2', 'image/jpeg2000', 'image/jpeg2000-image', 'image/x-jpeg2000-image', '', '', ''); |
| 558 |
ExtsCount: 1; Exts: ('jp2', '', '', ''); |
| 559 |
Load: {$ifdef FPC}@{$endif}LoadJP2; |
| 560 |
Save: {$ifdef FPC}@{$endif}SaveJP2; |
| 561 |
DetectClass: nil; |
| 562 |
) |
| 563 |
{$endif CASTLE_ENABLE_PLATFORM_SPECIFIC_IMAGE_FORMATS} |
| 564 |
|
| 565 |
(* |
| 566 |
( FormatName: 'EXR image'; |
| 567 |
MimeTypesCount: 1; |
| 568 |
MimeTypes: ('image/x-exr', '', '', '', '', '', ''); |
| 569 |
ExtsCount: 1; Exts: ('exr', '', '', ''); |
| 570 |
Load: {$ifdef FPC}@{$endif}LoadEXR; |
| 571 |
Save: nil; |
| 572 |
DetectClass: nil; |
| 573 |
) |
| 574 |
*) |
| 575 |
); |
| 576 |
|
| 577 |
{ image loading utilities --------------------------------------------------- } |
| 578 |
|
| 579 |
{ Helper methods for implementing LoadEncodedImage. } |
| 580 |
|
| 581 |
function ClassAllowed(ImageClass: TEncodedImageClass; |
| 582 |
const AllowedImageClasses: array of TEncodedImageClass): boolean; |
| 583 |
begin |
| 584 |
Result := (High(AllowedImageClasses) = -1) or |
| 585 |
InImageClasses(ImageClass, AllowedImageClasses); |
| 586 |
end; |
| 587 |
|
| 588 |
function LoadEncodedImageParams( |
| 589 |
const AllowedImageClasses: array of TEncodedImageClass): string; |
| 590 |
|
| 591 |
function ImageClassesToStr(const AllowedImageClasses: array of TEncodedImageClass): string; |
| 592 |
var |
| 593 |
I: Integer; |
| 594 |
begin |
| 595 |
if High(AllowedImageClasses) = -1 then |
| 596 |
Result := 'all' else |
| 597 |
begin |
| 598 |
Result := ''; |
| 599 |
for I := 0 to High(AllowedImageClasses) do |
| 600 |
begin |
| 601 |
if Result <> '' then Result := Result + ', '; |
| 602 |
Result := Result + AllowedImageClasses[I].ClassName; |
| 603 |
end; |
| 604 |
end; |
| 605 |
end; |
| 606 |
|
| 607 |
begin |
| 608 |
Result := 'required class [' + ImageClassesToStr(AllowedImageClasses) + ']'; |
| 609 |
end; |
| 610 |
|
| 611 |
{ Exposing ImageFormatInfos mimo/exts info ----------------------------------- } |
| 612 |
|
| 613 |
function ImageExtToMimeType(Ext: string): string; |
| 614 |
var |
| 615 |
I: TImageFormat; |
| 616 |
E: TImageFormatInfoExtsCount; |
| 617 |
begin |
| 618 |
Ext := LowerCase(Ext); |
| 619 |
for I := Low(I) to High(I) do |
| 620 |
for E := Low(E) to ImageFormatInfos[I].ExtsCount do |
| 621 |
if Ext = '.' + ImageFormatInfos[I].Exts[E] then |
| 622 |
Exit(ImageFormatInfos[I].MimeTypes[1]); |
| 623 |
Result := ''; |
| 624 |
end; |
| 625 |
|
| 626 |
procedure RegisterMimeTypes; |
| 627 |
var |
| 628 |
I: TImageFormat; |
| 629 |
E: TImageFormatInfoExtsCount; |
| 630 |
begin |
| 631 |
for I := Low(I) to High(I) do |
| 632 |
for E := Low(E) to ImageFormatInfos[I].ExtsCount do |
| 633 |
URIMimeExtensions.AddOrSetValue( |
| 634 |
'.' + ImageFormatInfos[I].Exts[E], |
| 635 |
ImageFormatInfos[I].MimeTypes[1]); |
| 636 |
end; |
| 637 |
|
| 638 |
function MimeTypeToImageFormat(const MimeType: string; |
| 639 |
const OnlyLoadable, OnlySaveable: boolean; out ImgFormat: TImageFormat): boolean; |
| 640 |
var |
| 641 |
I: TImageFormat; |
| 642 |
M: TImageFormatInfoMimeTypesCount; |
| 643 |
begin |
| 644 |
for I := Low(I) to High(I) do |
| 645 |
begin |
| 646 |
if ((not OnlyLoadable) or Assigned(ImageFormatInfos[I].Load)) and |
| 647 |
((not OnlySaveable) or Assigned(ImageFormatInfos[I].Save)) then |
| 648 |
for M := 1 to ImageFormatInfos[I].MimeTypesCount do |
| 649 |
if MimeType = ImageFormatInfos[I].MimeTypes[M] then |
| 650 |
begin |
| 651 |
ImgFormat := I; |
| 652 |
Exit(true); |
| 653 |
end; |
| 654 |
end; |
| 655 |
Result := false; |
| 656 |
end; |
| 657 |
|
| 658 |
function ImageClassBestForSavingToFormatCore(const Format: TImageFormat): TCastleImageClass; |
| 659 |
begin |
| 660 |
if Format = ifRGBE then |
| 661 |
Result := TRGBFloatImage else |
| 662 |
Result := TRGBImage; |
| 663 |
end; |
| 664 |
|
| 665 |
function ImageClassBestForSavingToFormat(const Url: String): TCastleImageClass; |
| 666 |
var |
| 667 |
Format: TImageFormat; |
| 668 |
begin |
| 669 |
if not MimeTypeToImageFormat(UriMimeType(Url), false, true, Format) then |
| 670 |
Exit(TRGBImage); |
| 671 |
Result := ImageClassBestForSavingToFormatCore(Format); |
| 672 |
end; |
| 673 |
|
| 674 |
function IsImageMimeType(const MimeType: string; |
| 675 |
const OnlyLoadable, OnlySaveable: boolean): boolean; |
| 676 |
var |
| 677 |
ImgFormat: TImageFormat; |
| 678 |
begin |
| 679 |
Result := MimeTypeToImageFormat(MimeType, OnlyLoadable, OnlySaveable, ImgFormat); |
| 680 |
end; |
| 681 |
|
| 682 |
procedure AddImageMimeTypes(const MimeTypes: TStrings; |
| 683 |
const OnlyLoadable, OnlySaveable: boolean); |
| 684 |
var |
| 685 |
I: TImageFormat; |
| 686 |
M: TImageFormatInfoMimeTypesCount; |
| 687 |
MimeType: String; |
| 688 |
begin |
| 689 |
for I := Low(I) to High(I) do |
| 690 |
begin |
| 691 |
if ((not OnlyLoadable) or Assigned(ImageFormatInfos[I].Load)) and |
| 692 |
((not OnlySaveable) or Assigned(ImageFormatInfos[I].Save)) then |
| 693 |
for M := 1 to ImageFormatInfos[I].MimeTypesCount do |
| 694 |
begin |
| 695 |
MimeType := ImageFormatInfos[I].MimeTypes[M]; |
| 696 |
// all MIME types within ImageFormatInfos[I].MimeTypesCount should be OK |
| 697 |
Assert(MimeType <> ''); |
| 698 |
{$warnings off} // this assertion is guaranteed by types now, ignore warning |
| 699 |
// at least one MIME type must be defined |
| 700 |
Assert(ImageFormatInfos[I].MimeTypesCount >= 1); |
| 701 |
{$warnings on} |
| 702 |
if MimeTypes.IndexOf(MimeType) = -1 then |
| 703 |
MimeTypes.Add(MimeType); |
| 704 |
end; |
| 705 |
end; |
| 706 |
end; |
| 707 |
|
| 708 |
procedure AddImageExtensions(const Extensions: TStrings; |
| 709 |
const OnlyLoadable, OnlySaveable: boolean); |
| 710 |
var |
| 711 |
I: TImageFormat; |
| 712 |
ExtIndex: TImageFormatInfoExtsCount; |
| 713 |
Ext: String; |
| 714 |
begin |
| 715 |
for I := Low(I) to High(I) do |
| 716 |
begin |
| 717 |
if ((not OnlyLoadable) or Assigned(ImageFormatInfos[I].Load)) and |
| 718 |
((not OnlySaveable) or Assigned(ImageFormatInfos[I].Save)) then |
| 719 |
for ExtIndex := 1 to ImageFormatInfos[I].ExtsCount do |
| 720 |
begin |
| 721 |
// extensions in ImageFormatInfos[I].Exts don't include dot |
| 722 |
Assert(not IsPrefix('.', ImageFormatInfos[I].Exts[ExtIndex], false)); |
| 723 |
Ext := '.' + ImageFormatInfos[I].Exts[ExtIndex]; |
| 724 |
// all extensions within ImageFormatInfos[I].ExtsCount should be OK |
| 725 |
Assert(Ext <> ''); |
| 726 |
{$warnings off} // this assertion is guaranteed by types now, ignore warning |
| 727 |
// at least one extension must be defined |
| 728 |
Assert(ImageFormatInfos[I].ExtsCount >= 1); |
| 729 |
{$warnings on} |
| 730 |
if Extensions.IndexOf(Ext) = -1 then |
| 731 |
Extensions.Add(Ext); |
| 732 |
end; |
| 733 |
end; |
| 734 |
end; |
| 735 |
|
| 736 |
function ListImageExtsLong(OnlyLoadable, OnlySaveable: boolean; |
| 737 |
const LinePrefix: string): string; |
| 738 |
var |
| 739 |
Format: TImageFormat; |
| 740 |
I: integer; |
| 741 |
begin |
| 742 |
result := ''; |
| 743 |
|
| 744 |
for Format := Low(Format) to High(Format) do |
| 745 |
if ((not OnlyLoadable) or Assigned(ImageFormatInfos[Format].Load)) and |
| 746 |
((not OnlySaveable) or Assigned(ImageFormatInfos[Format].Save)) then |
| 747 |
begin |
| 748 |
{ Note that we do not want to add new line at the end because there |
| 749 |
it would not be very convenient to recognize whether we are at the last |
| 750 |
line or not (due to OnlySaveable / OnlyLoadable it cannot be recognized |
| 751 |
by simply checking Format <High (Format)). } |
| 752 |
if Result <> '' then |
| 753 |
Result := Result + NL; |
| 754 |
|
| 755 |
Result := Result + LinePrefix + ImageFormatInfos[Format].Exts[1]; |
| 756 |
for I := 2 to ImageFormatInfos[Format].ExtsCount do |
| 757 |
Result := Result + ', ' + ImageFormatInfos[Format].Exts[I]; |
| 758 |
Result := Result + ' - ' + ImageFormatInfos[Format].FormatName; |
| 759 |
end; |
| 760 |
end; |
| 761 |
|
| 762 |
function ListImageExtsShort(OnlyLoadable, OnlySaveable: boolean): string; |
| 763 |
var |
| 764 |
Format: TImageFormat; |
| 765 |
I: integer; |
| 766 |
begin |
| 767 |
Result := ''; |
| 768 |
|
| 769 |
for Format := Low(Format) to High(Format) do |
| 770 |
if ((not OnlyLoadable) or Assigned(ImageFormatInfos[Format].Load)) and |
| 771 |
((not OnlySaveable) or Assigned(ImageFormatInfos[Format].Save)) then |
| 772 |
begin |
| 773 |
for I := 1 to ImageFormatInfos[Format].ExtsCount do |
| 774 |
begin |
| 775 |
if Result <> '' then |
| 776 |
Result := Result + ', '; |
| 777 |
Result := Result + ImageFormatInfos[Format].Exts[I]; |
| 778 |
end; |
| 779 |
end; |
| 780 |
end; |
| 781 |
|
| 782 |
procedure InitializeImagesFileFilters; |
| 783 |
|
| 784 |
function CreateImagesFilters: TFileFilterList; |
| 785 |
begin |
| 786 |
Result := TFileFilterList.Create(true); |
| 787 |
Result.AddFilter('All Files', ['*']); |
| 788 |
Result.AddFilter('All Images', []); |
| 789 |
Result.DefaultFilter := 1; |
| 790 |
end; |
| 791 |
|
| 792 |
procedure AddImageFormat(Filters: TFileFilterList; Format: TImageFormatInfo); |
| 793 |
var |
| 794 |
F: TFileFilter; |
| 795 |
ExtIndex: Integer; |
| 796 |
Pattern: string; |
| 797 |
begin |
| 798 |
F := TFileFilter.Create; |
| 799 |
Filters.Add(F); |
| 800 |
F.Name := Format.FormatName + ' ('; |
| 801 |
|
| 802 |
for ExtIndex := 1 to Format.ExtsCount do |
| 803 |
begin |
| 804 |
Pattern := '*.' + Format.Exts[ExtIndex]; |
| 805 |
|
| 806 |
{ add to "All images" filter } |
| 807 |
Filters[Filters.DefaultFilter].Patterns.Append(Pattern); |
| 808 |
|
| 809 |
{ add to this filter } |
| 810 |
F.Patterns.Append(Pattern); |
| 811 |
|
| 812 |
{ add to this filter visible name } |
| 813 |
if ExtIndex <> 1 then F.Name := F.Name + ', '; |
| 814 |
F.Name := F.Name + Pattern; |
| 815 |
end; |
| 816 |
|
| 817 |
F.Name := F.Name + ')'; |
| 818 |
end; |
| 819 |
|
| 820 |
var |
| 821 |
Format: TImageFormat; |
| 822 |
begin |
| 823 |
LoadImage_FileFilters := CreateImagesFilters; |
| 824 |
SaveImage_FileFilters := CreateImagesFilters; |
| 825 |
|
| 826 |
for Format := Low(Format) to High(Format) do |
| 827 |
begin |
| 828 |
if Assigned(ImageFormatInfos[Format].Load) then |
| 829 |
AddImageFormat(LoadImage_FileFilters, ImageFormatInfos[Format]); |
| 830 |
if Assigned(ImageFormatInfos[Format].Save) then |
| 831 |
AddImageFormat(SaveImage_FileFilters, ImageFormatInfos[Format]); |
| 832 |
end; |
| 833 |
end; |