* Fix bug ID #32025

git-svn-id: trunk@36704 -
This commit is contained in:
michael 2017-07-09 09:10:53 +00:00
parent f247a66c90
commit 31d9d345e1
10 changed files with 110 additions and 54 deletions

View File

@ -1,7 +1,8 @@
{$mode objfpc}{$h+} {$mode objfpc}{$h+}
program Drawing; program Drawing;
uses cwstring,classes, sysutils, FPImage, FPCanvas, FPImgCanv, FPWritePNG, FPReadPNG; uses
{$IFDEF UNIX}cwstring,{$ENDIF} classes, sysutils, FPImage, FPCanvas, FPImgCanv, FPWritePNG, FPReadPNG;
const const
MyColor : TFPColor = (Red: $7FFF; Green: $0000; Blue: $FFFF; Alpha: alphaOpaque); MyColor : TFPColor = (Red: $7FFF; Green: $0000; Blue: $FFFF; Alpha: alphaOpaque);

View File

@ -3,7 +3,7 @@
program textout; program textout;
uses uses
cwstring,classes, sysutils, FPImage, FPCanvas, FPImgCanv, ftFont, FPWritePNG, freetype; {$IFDEF UNIX}cwstring, {$ENDIF} classes, sysutils, FPImage, FPCanvas, FPImgCanv, ftFont, FPWritePNG, freetype;
const const
MyColor : TFPColor = (Red: $7FFF; Green: $0000; Blue: $FFFF; Alpha: alphaOpaque); MyColor : TFPColor = (Red: $7FFF; Green: $0000; Blue: $FFFF; Alpha: alphaOpaque);

View File

@ -124,7 +124,6 @@ begin
try try
if CheckContents (str) then if CheckContents (str) then
try try
str.Position := startPos;
FStream := str; FStream := str;
FImage := self; FImage := self;
InternalRead (str, self); InternalRead (str, self);

View File

@ -224,7 +224,7 @@ begin
end; end;
procedure TFPReaderBMP.InternalRead(Stream:TStream; Img:TFPCustomImage); procedure TFPReaderBMP.InternalRead(Stream:TStream; Img:TFPCustomImage);
// NOTE: Assumes that BMP header already has been read
Var Var
Row, i, pallen : Integer; Row, i, pallen : Integer;
BadCompression : boolean; BadCompression : boolean;
@ -504,16 +504,23 @@ begin
end; end;
function TFPReaderBMP.InternalCheck (Stream:TStream) : boolean; function TFPReaderBMP.InternalCheck (Stream:TStream) : boolean;
// NOTE: Does not rewind the stream!
var var
BFH:TBitMapFileHeader; BFH:TBitMapFileHeader;
n: Int64;
begin begin
stream.Read(BFH,SizeOf(BFH)); Result:=False;
{$IFDEF ENDIAN_BIG} if Stream=nil then
SwapBMPFileHeader(BFH); exit;
{$ENDIF} n:=SizeOf(BFH);
With BFH do Result:=Stream.Read(BFH,n)=n;
Result:=(bfType=BMmagic); // Just check magic number if Result then
begin
{$IFDEF ENDIAN_BIG}
SwapBMPFileHeader(BFH);
{$ENDIF}
Result := BFH.bfType = BMmagic; // Just check magic number
end;
end; end;
initialization initialization

View File

@ -473,17 +473,23 @@ begin
end; end;
function TFPReaderGif.InternalCheck(Stream: TStream): boolean; function TFPReaderGif.InternalCheck(Stream: TStream): boolean;
var var
OldPos: Int64; OldPos: Int64;
n: Int64;
begin begin
Result:=False;
if Stream = nil then
exit;
OldPos:=Stream.Position;
try try
OldPos:=Stream.Position; n := SizeOf(FHeader);
Stream.Read(FHeader,SizeOf(FHeader)); Result:=(Stream.Read(FHeader,n)=n)
Result:=(FHeader.Signature = 'GIF') and and (FHeader.Signature = 'GIF')
((FHeader.Version = '87a') or (FHeader.Version = '89a')); and ((FHeader.Version = '87a') or (FHeader.Version = '89a'));
Stream.Position:=OldPos; finally
except Stream.Position := OldPos;
Result:=False;
end; end;
end; end;

View File

@ -301,10 +301,28 @@ begin
end; end;
function TFPReaderPCX.InternalCheck(Stream: TStream): boolean; function TFPReaderPCX.InternalCheck(Stream: TStream): boolean;
var
hdr: TPcxHeader;
n: Integer;
oldPos: Int64;
begin begin
Result := True; Result:=False;
if Stream = nil then
exit;
oldPos := Stream.Position;
try
n:=SizeOf(hdr);
Result:=(Stream.Read(hdr, n)=n)
and (hdr.FileID in [$0A, $0C])
and (hdr.ColorPlanes in [1, 3, 4])
and (hdr.Version in [0, 2, 3, 5])
and (hdr.PaletteType in [1, 2]);
finally
Stream.Position := oldPos;
end;
end; end;
initialization initialization
ImageHandlers.RegisterImageReader('PCX Format', 'pcx', TFPReaderPCX); ImageHandlers.RegisterImageReader('PCX Format', 'pcx', TFPReaderPCX);
end. end.

View File

@ -805,6 +805,7 @@ begin
raise PNGImageException.Create('Critical chunk '+chunk.readtype+' not recognized'); raise PNGImageException.Create('Critical chunk '+chunk.readtype+' not recognized');
end; end;
// NOTE: It is assumed that signature and IDHDR chunk already have been read.
procedure TFPReaderPNG.InternalRead (Str:TStream; Img:TFPCustomImage); procedure TFPReaderPNG.InternalRead (Str:TStream; Img:TFPCustomImage);
begin begin
{$ifdef FPC_Debug_Image} {$ifdef FPC_Debug_Image}
@ -872,33 +873,34 @@ begin
Result.Y := Height; Result.Y := Height;
end; end;
// NOTE: Stream does not rewind here!
function TFPReaderPNG.InternalCheck (Str:TStream) : boolean; function TFPReaderPNG.InternalCheck (Str:TStream) : boolean;
var SigCheck : array[0..7] of byte; var SigCheck : array[0..7] of byte;
r : integer; r : integer;
begin begin
try Result:=False;
// Check Signature if Str=Nil then
Str.Read(SigCheck, SizeOf(SigCheck)); exit;
for r := 0 to 7 do // Check Signature
if Str.Read(SigCheck, SizeOf(SigCheck)) <> SizeOf(SigCheck) then
Exit;
for r := 0 to 7 do
begin begin
If SigCheck[r] <> Signature[r] then If SigCheck[r] <> Signature[r] then
Exit(false); Exit;
end;
// Check IHDR
ReadChunk;
move (chunk.data^, FHeader, sizeof(Header));
with header do
begin
{$IFDEF ENDIAN_LITTLE}
Width := swap(width);
height := swap (height);
{$ENDIF}
result :=(width > 0) and (height > 0) and (compression = 0)
and (filter = 0) and (Interlace in [0,1]);
end; end;
// Check IHDR
ReadChunk;
move (chunk.data^, FHeader, sizeof(Header));
with header do
begin
{$IFDEF ENDIAN_LITTLE}
Width := swap(width);
height := swap (height);
{$ENDIF}
result := (width > 0) and (height > 0) and (compression = 0)
and (filter = 0) and (Interlace in [0,1]);
end;
except
result := false;
end;
end; end;
initialization initialization

View File

@ -48,14 +48,32 @@ type
implementation implementation
function TFPReaderPNM.InternalCheck(Stream:TStream):boolean;
begin
InternalCheck:=True;
end;
const const
WhiteSpaces=[#9,#10,#13,#32]; {Whitespace (TABs, CRs, LFs, blanks) are separators in the PNM Headers} WhiteSpaces=[#9,#10,#13,#32];
{Whitespace (TABs, CRs, LFs, blanks) are separators in the PNM Headers}
{ The magic number at the beginning of a pnm file is 'P1', 'P2', ..., 'P7'
followed by a WhiteSpace character }
function TFPReaderPNM.InternalCheck(Stream:TStream):boolean;
var
hdr: array[0..2] of char;
oldPos: Int64;
n: Integer;
begin
Result:=False;
if Stream = nil then
exit;
oldPos := Stream.Position;
try
n := SizeOf(hdr);
Result:=(Stream.Read(hdr[0], n) = n)
and (hdr[0] = 'P')
and (hdr[1] in ['1'..'7'])
and (hdr[2] in WhiteSpaces);
finally
Stream.Position := oldPos;
end;
end;
function DropWhiteSpaces(Stream : TStream) :Char; function DropWhiteSpaces(Stream : TStream) :Char;

View File

@ -588,14 +588,19 @@ end;
function TFPReaderPSD.InternalCheck(Stream: TStream): boolean; function TFPReaderPSD.InternalCheck(Stream: TStream): boolean;
var var
OldPos: Int64; OldPos: Int64;
n: Integer;
begin begin
Result:=False;
if Stream=Nil then
exit;
OldPos := Stream.Position;
try try
OldPos:=Stream.Position; n := SizeOf(FHeader);
Stream.Read(FHeader,SizeOf(FHeader)); Result:=(Stream.Read(FHeader, n) = n)
Result:=(FHeader.Signature = '8BPS'); and (FHeader.Signature = '8BPS')
Stream.Position:=OldPos; finally
except Stream.Position := OldPos;
Result:=False;
end; end;
end; end;

View File

@ -39,7 +39,7 @@ type
implementation implementation
const const
WhiteSpace = ' '#8#10#13; WhiteSpace = ' '#9#10#13;
constructor TFPReaderXPM.create; constructor TFPReaderXPM.create;
begin begin