/usr/src/castle-game-engine-5.2.0/images/images_ppm.inc is in castle-game-engine-src 5.2.0-2.
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 | {
Copyright 2002-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.
----------------------------------------------------------------------------
}
{ PPM save/load support. }
{ TODO: When ReadPPMToken is a nested procedure inside LoadPPM (as it should be),
then loading ppms causes segfaults, only under Windows (not on Linux),
only with FPC 2.6.2 (not with 2.6.0 or earlier). Probably some FPC 2.6.2 bug.
Investigate, check if it's still there with FPC trunk, and eventually report.
The segfault occurs in weird places, and seems to have something to do
with how ReadPPMToken(Stream) returns a string.
Occurs with both debug and release build.
To reproduce, just try to open
castle_game_engine/tests/data/images/rgb.ppm
with glViewImage. Or just castle_game_engine/tests/
(the CastleImage tests there also open rgb.ppm).
}
{ Reads next token in ppm - skips any blanks that it stands on ,
and reads everything up to the ending whitespace (or to the end of stream).
Returns token without those surrounding whitespaces.
Returns '' if we're standing at the end of file.
Ignores PPM comments (# ... to the nearest #10 or #13) before token. }
function ReadPPMToken(Stream: TStream): string;
var c: char;
begin
try
result:='';
{read whitespaces and comments}
repeat
c := StreamReadChar(Stream);
if c = '#' then { omit comment }
repeat c := StreamReadChar(Stream) until c in [#10, #13];
until not (c in WhiteSpaces);
{read token, until whitespace}
result := c;
repeat
c := StreamReadChar(Stream);
if c in WhiteSpaces then
break else
result := result+c;
until false;
except
on EReadError do {silent exception, just exit from this function};
end;
end;
function LoadPPM(Stream: TStream;
const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage;
var magic: array[0..1]of char;
IsBinary: boolean;
MaxColVal: integer;
x, y: Cardinal;
line: PArray_Vector3Byte;
ppmline: PByteArray;
ColSize, LineSize: Cardinal;
AllocateWidth, AllocateHeight: Cardinal;
begin
try
Stream.ReadBuffer(magic, 2);
if (magic[0]<>'P') or ((magic[1]<>'3') and (magic[1]<>'6')) then
raise ECheckFailed.CreateFmt('Not a PPM beginning : signature is %s%s (#%d #%d)',
[magic[0], magic[1], Ord(magic[0]), Ord(magic[1])]);
IsBinary := magic[1] = '6';
Check(StreamReadChar(Stream) in Whitespaces, 'no whitespace after PPM signature');
AllocateWidth := StrToInt(ReadPPMToken(Stream));
AllocateHeight := StrToInt(ReadPPMToken(Stream));
result := TRGBImage.Create(AllocateWidth, AllocateHeight);
try
{ one whitespace after MaxColVal is already read by ReadPPMToken }
MaxColVal := StrToInt(ReadPPMToken(Stream));
if IsBinary then
begin
if MaxColVal < 256 then ColSize := 1 else ColSize := 2;
LineSize := ColSize * TCastleImage(Result).PixelSize * Result.Width;
{osobny kod dla ColSize =1 i =2 (dla szybkosci).}
if ColSize = 1 then
begin
for y := result.Height-1 downto 0 do
begin
line := TCastleImage(Result).RowPtr(y);
Stream.ReadBuffer(line^, LineSize);
if MaxColVal <> 255 then
begin
for x := 0 to (Result.Width*3)-1 do
PByteArray(line)^[x] := Integer(PByteArray(line)^[x])*255 div MaxColVal;
end;
end;
end else
begin
ppmline := GetMem(LineSize);
try
for y := result.Height-1 downto 0 do
begin
line := TCastleImage(Result).RowPtr(y);
Stream.ReadBuffer(ppmline[0], LineSize);
for x := 0 to result.Width-1 do
begin
{Zawsze odczytany wynik rzutujemy na Integer zeby pomnozony * 255 nie wyszedl
poza swoj zakres. Zamieniamy go na zakres 0..255, dzielac przez MaxColVal. }
line^[x, 0]:=(Integer(ppmline^[x*6 ])*256+ppmline^[x*6+1])*255 div MaxColVal;
line^[x, 1]:=(Integer(ppmline^[x*6+2])*256+ppmline^[x*6+3])*255 div MaxColVal;
line^[x, 2]:=(Integer(ppmline^[x*6+4])*256+ppmline^[x*6+5])*255 div MaxColVal;
end;
end;
finally FreeMem(ppmline) end;
end;
end else
begin
for y := result.Height-1 downto 0 do
begin
line := TCastleImage(Result).RowPtr(y);
for x := 0 to result.Width-1 do
begin
line^[x, 0] := StrToInt(ReadPPMToken(Stream))*255 div MaxColVal;
line^[x, 1] := StrToInt(ReadPPMToken(Stream))*255 div MaxColVal;
line^[x, 2] := StrToInt(ReadPPMToken(Stream))*255 div MaxColVal;
end;
end;
end;
except Result.Free; raise end;
except
on E: EReadError do raise EInvalidPPM.Create('Read error : '+E.Message);
on E: ECheckFailed do raise EInvalidPPM.Create('Wrong PPM file : '+E.Message);
on E: EConvertError do raise EInvalidPPM.Create('Convert error : '+E.Message);
end;
end;
procedure SavePPM(Img: TEncodedImage; Stream: TStream; Binary: boolean);
var line: PArray_Vector3Byte;
x, y: cardinal;
begin
if not (Img is TRGBImage) then
raise EImageSaveError.CreateFmt('Saving to PPM image class %s not possible', [Img.ClassName]);
{najbezpieczniej w ppm uzywac znakow konca linii #10.
To dlatego ze po MaxColVal wymagany jest DOKLADNIE jeden bialy znak. }
if binary then
WriteStr(Stream,'P6') else
WriteStr(Stream,'P3');
WriteStr(Stream, Format(#10'%d %d'#10'%d'#10, [img.Width, img.Height, 255]));
for y := img.Height-1 downto 0 do
begin
line := TCastleImage(Img).RowPtr(y);
if binary then
Stream.WriteBuffer(line^, img.Width*SizeOf(TVector3Byte)) else
begin
for x := 0 to img.Width-1 do
begin
WriteStr(Stream, Format('%d %d %d ', [line^[x, 0], line^[x, 1], line^[x, 2]]));
{linia powinna miec maksymalnie 70 znakow. Acha.
Kazdy nasz pixel to max 4(liczba 0..255 + spacja) * 3 = 12 znakow.
Wiec piszmy 5 pixeli w linii - maksymalnie linia bedzie miala 5*12 = 60 znakow. }
if (x+1) mod 5 = 0 then WriteStr(Stream, #10);
end;
end;
end;
end;
procedure SavePPM(Img: TEncodedImage; Stream: TStream); { binary = true }
begin SavePPM(img, Stream, true) end;
|