/usr/src/castle-game-engine-5.0.0/images/images_fpimage.inc is in castle-game-engine-src 5.0.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | {
Copyright 2001-2014 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
}
{ Image loaders using FPImage.
FPImage docs:
http://lazarus-ccr.sourceforge.net/docs/fcl/fpimage/index.html
http://wiki.freepascal.org/fcl-image
Also http://free-pascal-general.1045716.n5.nabble.com/Why-is-FPImage-dog-slow-in-reading-jpeg-s-td4439450.html
is informative, explains UsePalette stuff.
However, for some formats palette is needed --- e.g. reading XPM
reader segfaults when UsePalette = false, tested on
/usr/share/pixmaps/EasyTAG_icon.xpm
/usr/share/pixmaps/flamerobin.xpm
}
function LoadFpImage(Stream: TStream;
const AllowedImageClasses: array of TCastleImageClass;
Reader: TFPCustomImageReader;
const AllowPalette: boolean = false): TCastleImage;
var
FPImage: TFPMemoryImage;
{ FPImage doesn't provide us any information does the image contain useful
alpha channel. So just iterate over pixels, and check it ourselves. }
function DetectAlpha: boolean;
var
X, Y: Integer;
begin
for Y := FPImage.Height - 1 downto 0 do
for X := 0 to FPImage.Width - 1 do
if (FPImage.Colors[X, Y].Alpha shr 8) <> $FF then
Exit(true);
Result := false;
end;
procedure ReadRGB;
var
X, Y: Integer;
ResultPixels: PVector3Byte;
Color: TFPColor;
begin
Result := TRGBImage.Create(FPImage.Width, FPImage.Height);
try
ResultPixels := TRGBImage(Result).RGBPixels;
for Y := FPImage.Height - 1 downto 0 do
for X := 0 to FPImage.Width - 1 do
begin
Color := FPImage.Colors[X, Y];
ResultPixels^[0] := Color.Red shr 8;
ResultPixels^[1] := Color.Green shr 8;
ResultPixels^[2] := Color.Blue shr 8;
Inc(ResultPixels);
end;
except FreeAndNil(Result) end;
end;
procedure ReadRGBA;
var
X, Y: Integer;
ResultPixels: PVector4Byte;
Color: TFPColor;
begin
Result := TRGBAlphaImage.Create(FPImage.Width, FPImage.Height);
try
ResultPixels := TRGBAlphaImage(Result).AlphaPixels;
for Y := FPImage.Height - 1 downto 0 do
for X := 0 to FPImage.Width - 1 do
begin
Color := FPImage.Colors[X, Y];
ResultPixels^[0] := Color.Red shr 8;
ResultPixels^[1] := Color.Green shr 8;
ResultPixels^[2] := Color.Blue shr 8;
ResultPixels^[3] := Color.Alpha shr 8;
Inc(ResultPixels);
end;
except FreeAndNil(Result) end;
end;
begin
FPImage := TFPMemoryImage.Create(0, 0);
try
FPImage.UsePalette := AllowPalette;
try
FPImage.LoadFromStream(Stream, Reader);
{ FPImage.LoadFromStream may raise any kind of Exception (for example,
in case of invalid JPEG header it just raises generic Exception class).
Catch it and convert to our EImageLoadError. }
except on E: Exception do raise EImageLoadError.Create(E.Message) end;
if ClassAllowed(TRGBAlphaImage, AllowedImageClasses) and DetectAlpha then
ReadRGBA else
ReadRGB;
finally
FreeAndNil(FPImage);
FreeAndNil(Reader);
end;
end;
procedure SaveFpImage(Img: TCastleImage; Stream: TStream;
Writer: TFPCustomImageWriter;
const AllowPalette: boolean = false);
var
FPImage: TFPMemoryImage;
procedure WriteRGB;
var
X, Y: Integer;
ImgPixels: PVector3Byte;
Color: TFPColor;
begin
ImgPixels := TRGBImage(Img).RGBPixels;
for Y := FPImage.Height - 1 downto 0 do
for X := 0 to FPImage.Width - 1 do
begin
{ It doesn't matter what you put in least-significat
byte of color component, FPWriteJPEG will do "shr 8" on these values
anyway (JPEG can only save 8-bit RGB). }
Color.Red := Word(ImgPixels^[0]) shl 8;
Color.Green := Word(ImgPixels^[1]) shl 8;
Color.Blue := Word(ImgPixels^[2]) shl 8;
Color.Alpha := $FFFF;
FPImage.Colors[X, Y] := Color;
Inc(ImgPixels);
end;
end;
procedure WriteRGBA;
var
X, Y: Integer;
ImgPixels: PVector4Byte;
Color: TFPColor;
begin
ImgPixels := TRGBAlphaImage(Img).AlphaPixels;
for Y := FPImage.Height - 1 downto 0 do
for X := 0 to FPImage.Width - 1 do
begin
{ It doesn't matter what you put in least-significat
byte of color component, FPWriteJPEG will do "shr 8" on these values
anyway (JPEG can only save 8-bit RGB). }
Color.Red := Word(ImgPixels^[0]) shl 8;
Color.Green := Word(ImgPixels^[1]) shl 8;
Color.Blue := Word(ImgPixels^[2]) shl 8;
Color.Alpha := Word(ImgPixels^[3]) shl 8;
FPImage.Colors[X, Y] := Color;
Inc(ImgPixels);
end;
end;
begin
FPImage := TFPMemoryImage.Create(Img.Width, Img.Height);
try
FPImage.UsePalette := AllowPalette;
if Img is TRGBAlphaImage then
WriteRGBA else
if Img is TRGBImage then
WriteRGB else
raise EImageSaveError.Create('Cannot save non-RGB image');
try
FPImage.SaveToStream(Stream, Writer);
{ FPImage.SaveToStream may raise any kind of Exception.
Catch it and convert to our EImageSaveError. }
except on E: Exception do raise EImageSaveError.Create(E.Message) end;
finally
FreeAndNil(FPImage);
FreeAndNil(Writer);
end;
end;
function LoadGIF(Stream: TStream;
const AllowedImageClasses: array of TCastleImageClass): TCastleImage;
begin
{$ifndef VER2_2}
Result := LoadFpImage(Stream, AllowedImageClasses, TFPReaderGIF.Create);
{$else}
raise EImageLoadError.Create('Cannot load GIF when compiled with old FPC <= 2.2.x');
{$endif}
end;
function LoadTGA(Stream: TStream;
const AllowedImageClasses: array of TCastleImageClass): TCastleImage;
begin
Result := LoadFpImage(Stream, AllowedImageClasses, TFPReaderTarga.Create);
end;
{
TIFF reader from FPReadTiff fails on ~/images/test_images/lena.tif
with "missing RowsPerStrip.."
AllowPalette = true or false, doesn't matter.
function LoadTIFF(Stream: TStream;
const AllowedImageClasses: array of TCastleImageClass): TCastleImage;
begin
Result := LoadFpImage(Stream, AllowedImageClasses, TFPReaderTIFF.Create, true);
end;
}
function LoadXPM(Stream: TStream;
const AllowedImageClasses: array of TCastleImageClass): TCastleImage;
begin
Result := LoadFpImage(Stream, AllowedImageClasses, TFPReaderXPM.Create, true);
end;
function LoadPSD(Stream: TStream;
const AllowedImageClasses: array of TCastleImageClass): TCastleImage;
begin
{$ifndef VER2_2}
Result := LoadFpImage(Stream, AllowedImageClasses, TFPReaderPSD.Create);
{$else}
raise EImageLoadError.Create('Cannot load PSD when compiled with old FPC <= 2.2.x');
{$endif}
end;
function LoadPCX(Stream: TStream;
const AllowedImageClasses: array of TCastleImageClass): TCastleImage;
begin
Result := LoadFpImage(Stream, AllowedImageClasses, TFPReaderPCX.Create);
end;
function LoadJPEG(Stream: TStream;
const AllowedImageClasses: array of TCastleImageClass): TCastleImage;
begin
Result := LoadFpImage(Stream, AllowedImageClasses, TFPReaderJPEG.Create);
end;
{$ifdef CASTLE_PNG_USING_FCL_IMAGE}
function LoadPNG(Stream: TStream;
const AllowedImageClasses: array of TCastleImageClass): TCastleImage;
begin
Result := LoadFpImage(Stream, AllowedImageClasses, TFPReaderPNG.Create);
end;
{$endif}
function LoadPNM(Stream: TStream;
const AllowedImageClasses: array of TCastleImageClass): TCastleImage;
begin
Result := LoadFpImage(Stream, AllowedImageClasses, TFPReaderPNM.Create);
end;
procedure SaveJPEG(Img: TCastleImage; Stream: TStream);
begin
SaveFpImage(Img, Stream, TFPWriterJPEG.Create);
end;
{$ifdef CASTLE_PNG_USING_FCL_IMAGE}
procedure SavePNG(Img: TCastleImage; Stream: TStream);
begin
SaveFpImage(Img, Stream, TFPWriterPNG.Create);
end;
{$endif}
|