| 1 |
{%MainUnit castleimages.pas} |
| 2 |
{ |
| 3 |
Copyright 2001-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 |
function LoadGIF(const Stream: TStream; |
| 17 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; |
| 18 |
begin |
| 19 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 20 |
Result := Load_VampyreImaging(Stream, AllowedImageClasses); |
| 21 |
{$elseif defined(FPC)} |
| 22 |
Result := Load_FpImage(Stream, AllowedImageClasses, TFPReaderGIF.Create, ifGIF); |
| 23 |
{$else} |
| 24 |
raise EImageLoadError.Create('Cannot read GIF (neither FpImage nor Vampyre Imaging Library available at compile-time)'); |
| 25 |
{$endif} |
| 26 |
end; |
| 27 |
|
| 28 |
function LoadTGA(const Stream: TStream; |
| 29 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; |
| 30 |
begin |
| 31 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 32 |
Result := Load_VampyreImaging(Stream, AllowedImageClasses); |
| 33 |
{$elseif defined(FPC)} |
| 34 |
Result := Load_FpImage(Stream, AllowedImageClasses, TFPReaderTarga.Create, ifTGA); |
| 35 |
{$else} |
| 36 |
raise EImageLoadError.Create('Cannot read TGA (neither FpImage nor Vampyre Imaging Library available at compile-time)'); |
| 37 |
{$endif} |
| 38 |
end; |
| 39 |
|
| 40 |
{$ifdef CASTLE_ENABLE_PLATFORM_SPECIFIC_IMAGE_FORMATS} |
| 41 |
function LoadTIFF(const Stream: TStream; |
| 42 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; |
| 43 |
begin |
| 44 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 45 |
Result := Load_VampyreImaging(Stream, AllowedImageClasses); |
| 46 |
{$else} |
| 47 |
raise EImageLoadError.Create('Cannot read TIFF (Vampyre Imaging Library not available at compile-time)'); |
| 48 |
Result := nil; // just silence FPC warning |
| 49 |
{$endif} |
| 50 |
end; |
| 51 |
|
| 52 |
function LoadJP2(const Stream: TStream; |
| 53 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; |
| 54 |
begin |
| 55 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 56 |
Result := Load_VampyreImaging(Stream, AllowedImageClasses); |
| 57 |
{$else} |
| 58 |
raise EImageLoadError.Create('Cannot read JP2 (Vampyre Imaging Library not available at compile-time)'); |
| 59 |
Result := nil; // just silence FPC warning |
| 60 |
{$endif} |
| 61 |
end; |
| 62 |
{$endif CASTLE_ENABLE_PLATFORM_SPECIFIC_IMAGE_FORMATS} |
| 63 |
|
| 64 |
function LoadJPEG(const Stream: TStream; |
| 65 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; |
| 66 |
begin |
| 67 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 68 |
Result := Load_VampyreImaging(Stream, AllowedImageClasses); |
| 69 |
{$elseif defined(FPC)} |
| 70 |
Result := Load_FpImage(Stream, AllowedImageClasses, TFPReaderJPEG.Create, ifJPEG); |
| 71 |
{$else} |
| 72 |
raise EImageLoadError.Create('Cannot read JPEG (neither FpImage nor Vampyre Imaging Library available at compile-time)'); |
| 73 |
{$endif} |
| 74 |
end; |
| 75 |
|
| 76 |
function LoadXPM(const Stream: TStream; |
| 77 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; |
| 78 |
begin |
| 79 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 80 |
Result := Load_VampyreImaging(Stream, AllowedImageClasses); |
| 81 |
// FpImage XPM reader crashes on examples/images_videos/image_display/data/images/test_texture.xpm |
| 82 |
// {$elseif defined(FPC)} |
| 83 |
// Result := Load_FpImage(Stream, AllowedImageClasses, TFPReaderXPM.Create, ifXPM); |
| 84 |
{$else} |
| 85 |
raise EImageLoadError.Create('Cannot read XPM (Vampyre Imaging Library not available at compile-time)'); |
| 86 |
Result := nil; // silence FPC 3.2.2 warning |
| 87 |
{$endif} |
| 88 |
end; |
| 89 |
|
| 90 |
function LoadPSD(const Stream: TStream; |
| 91 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; |
| 92 |
begin |
| 93 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 94 |
Result := Load_VampyreImaging(Stream, AllowedImageClasses); |
| 95 |
{$elseif defined(FPC)} |
| 96 |
Result := Load_FpImage(Stream, AllowedImageClasses, TFPReaderPSD.Create, ifPSD); |
| 97 |
{$else} |
| 98 |
raise EImageLoadError.Create('Cannot read PSD (neither FpImage nor Vampyre Imaging Library available at compile-time)'); |
| 99 |
{$endif} |
| 100 |
end; |
| 101 |
|
| 102 |
function LoadPCX(const Stream: TStream; |
| 103 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; |
| 104 |
begin |
| 105 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 106 |
Result := Load_VampyreImaging(Stream, AllowedImageClasses); |
| 107 |
{$elseif defined(FPC)} |
| 108 |
Result := Load_FpImage(Stream, AllowedImageClasses, TFPReaderPCX.Create, ifPCX); |
| 109 |
{$else} |
| 110 |
raise EImageLoadError.Create('Cannot read PCX (neither FpImage nor Vampyre Imaging Library available at compile-time)'); |
| 111 |
{$endif} |
| 112 |
end; |
| 113 |
|
| 114 |
{ Load PNM image (PNM, PGM, PBM, PPM). } |
| 115 |
function LoadPNM(const Stream: TStream; |
| 116 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; |
| 117 |
begin |
| 118 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 119 |
Result := Load_VampyreImaging(Stream, AllowedImageClasses); |
| 120 |
{$elseif defined(FPC)} |
| 121 |
// Known to fail on PPM like examples/images_videos/image_display/data/images/sky_red_left.ppm |
| 122 |
// TODO: submit FPC bug |
| 123 |
Result := Load_FpImage(Stream, AllowedImageClasses, TFPReaderPNM.Create, ifPNM); |
| 124 |
{$else} |
| 125 |
raise EImageLoadError.Create('Cannot read PNM (neither FpImage nor Vampyre Imaging Library available at compile-time)'); |
| 126 |
{$endif} |
| 127 |
end; |
| 128 |
|
| 129 |
function LoadBMP(const Stream: TStream; |
| 130 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; |
| 131 |
begin |
| 132 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 133 |
Result := Load_VampyreImaging(Stream, AllowedImageClasses); |
| 134 |
{$elseif defined(FPC)} |
| 135 |
Result := Load_FpImage(Stream, AllowedImageClasses, TFPReaderBMP.Create, ifBMP); |
| 136 |
{$else} |
| 137 |
raise EImageLoadError.Create('Cannot read BMP (neither FpImage nor Vampyre Imaging Library available at compile-time)'); |
| 138 |
{$endif} |
| 139 |
end; |
| 140 |
|
| 141 |
function LoadRGBE(const Stream: TStream; |
| 142 |
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage; |
| 143 |
begin |
| 144 |
{$if defined(USE_VAMPYRE_IMAGING)} |
| 145 |
Result := Load_VampyreImaging(Stream, AllowedImageClasses); |
| 146 |
{$else} |
| 147 |
raise EImageLoadError.Create('Cannot read RGBE (Vampyre Imaging Library not available at compile-time)'); |
| 148 |
{$endif} |
| 149 |
end; |
| 150 |
|
| 151 |
{ Saving image (format-specific) -------------------------------------------- |
| 152 |
|
| 153 |
Each file format may have specialized SaveXxx that allows |
| 154 |
you to give some parameters special for given format. |
| 155 |
|
| 156 |
Each format must also have procedure with two parameters |
| 157 |
(Img: TEncodedImage; Stream: TStream), this will be used with |
| 158 |
ImageFormatsInfo[]. |
| 159 |
This means that below we must use overloading instead of |
| 160 |
default parameters, since pointers to given procedures must be |
| 161 |
compatible with @link(TImageSaveFunc). |
| 162 |
|
| 163 |
SaveXxx should |
| 164 |
raise EImageSaveError.CreateFmt('Saving to XXX image class %s not possible', [Img.ClassName]); |
| 165 |
when Img doesn't have acceptable class. |
| 166 |
Also, list of handled image classes should be reflected in SavedClasses |
| 167 |
in ImageFormatsInfo[] for this format. |
| 168 |
} |
| 169 |
|
| 170 |
procedure SaveJPEG(const Img: TEncodedImage; const Stream: TStream); |
| 171 |
begin |
| 172 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 173 |
Save_VampyreImaging(Img, Stream, 'jpg'); |
| 174 |
{$elseif defined(FPC)} |
| 175 |
Save_FpImage(Img, Stream, TFPWriterJPEG.Create); |
| 176 |
{$else} |
| 177 |
raise Exception.Create('Cannot save JPEG (neither FpImage nor Vampyre Imaging Library available at compile-time)'); |
| 178 |
{$endif} |
| 179 |
end; |
| 180 |
|
| 181 |
procedure SaveGIF(const Img: TEncodedImage; const Stream: TStream); |
| 182 |
begin |
| 183 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 184 |
Save_VampyreImaging(Img, Stream, 'gif'); |
| 185 |
{$else} |
| 186 |
raise EImageSaveError.Create('Cannot save GIF (Vampyre Imaging Library not available at compile-time)'); |
| 187 |
{$endif} |
| 188 |
end; |
| 189 |
|
| 190 |
procedure SaveTGA(const Img: TEncodedImage; const Stream: TStream); |
| 191 |
begin |
| 192 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 193 |
Save_VampyreImaging(Img, Stream, 'tga'); |
| 194 |
{$else} |
| 195 |
raise EImageSaveError.Create('Cannot save TGA (Vampyre Imaging Library not available at compile-time)'); |
| 196 |
{$endif} |
| 197 |
end; |
| 198 |
|
| 199 |
procedure SaveXPM(const Img: TEncodedImage; const Stream: TStream); |
| 200 |
begin |
| 201 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 202 |
Save_VampyreImaging(Img, Stream, 'xpm'); |
| 203 |
{$else} |
| 204 |
raise EImageSaveError.Create('Cannot save XPM (Vampyre Imaging Library not available at compile-time)'); |
| 205 |
{$endif} |
| 206 |
end; |
| 207 |
|
| 208 |
procedure SavePNM(const Img: TEncodedImage; const Stream: TStream); |
| 209 |
begin |
| 210 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 211 |
Save_VampyreImaging(Img, Stream, 'pnm'); |
| 212 |
{$elseif defined(FPC)} |
| 213 |
Save_FpImage(Img, Stream, TFPWriterPNM.Create); |
| 214 |
{$else} |
| 215 |
raise EImageSaveError.Create('Cannot save BMP (neither FpImage nor Vampyre Imaging Library available at compile-time)'); |
| 216 |
{$endif} |
| 217 |
end; |
| 218 |
|
| 219 |
procedure SaveBMP(const Img: TEncodedImage; const Stream: TStream); |
| 220 |
begin |
| 221 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 222 |
Save_VampyreImaging(Img, Stream, 'bmp'); |
| 223 |
{$elseif defined(FPC)} |
| 224 |
Save_FpImage(Img, Stream, TFPWriterBMP.Create); |
| 225 |
{$else} |
| 226 |
raise EImageSaveError.Create('Cannot save BMP (neither FpImage nor Vampyre Imaging Library available at compile-time)'); |
| 227 |
{$endif} |
| 228 |
end; |
| 229 |
|
| 230 |
{$ifdef CASTLE_ENABLE_PLATFORM_SPECIFIC_IMAGE_FORMATS} |
| 231 |
procedure SaveTIFF(const Img: TEncodedImage; const Stream: TStream); |
| 232 |
begin |
| 233 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 234 |
Save_VampyreImaging(Img, Stream, 'tif'); |
| 235 |
{$else} |
| 236 |
raise EImageSaveError.Create('Cannot save TIFF (Vampyre Imaging Library not available at compile-time)'); |
| 237 |
{$endif} |
| 238 |
end; |
| 239 |
|
| 240 |
procedure SaveJP2(const Img: TEncodedImage; const Stream: TStream); |
| 241 |
begin |
| 242 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 243 |
Save_VampyreImaging(Img, Stream, 'jp2'); |
| 244 |
{$else} |
| 245 |
raise EImageSaveError.Create('Cannot save JP2 (Vampyre Imaging Library not available at compile-time)'); |
| 246 |
{$endif} |
| 247 |
end; |
| 248 |
{$endif CASTLE_ENABLE_PLATFORM_SPECIFIC_IMAGE_FORMATS} |
| 249 |
|
| 250 |
procedure SaveRGBE(const Img: TEncodedImage; const Stream: TStream); |
| 251 |
begin |
| 252 |
{$if defined(USE_VAMPYRE_IMAGING)} // if available, prefer to use Vampyre |
| 253 |
Save_VampyreImaging(Img, Stream, 'hdr'); |
| 254 |
{$else} |
| 255 |
raise EImageSaveError.Create('Cannot save RGBE (Vampyre Imaging Library not available at compile-time)'); |
| 256 |
{$endif} |
| 257 |
end; |