Răsfoiți Sursa

* Implement ImageSize in Read handler (Patch by Ondrej Pokorny, bug ID #29988)

git-svn-id: trunk@33474 -
michael 9 ani în urmă
părinte
comite
4b54a82678

+ 25 - 0
packages/fcl-image/src/fphandler.inc

@@ -252,6 +252,31 @@ begin
   end;
 end;
 
+function TFPCustomImageReader.InternalSize(Str: TStream): TPoint;
+
+begin
+  Result.X:=-1;
+  Result.Y:=-1;
+end;
+
+function TFPCustomImageReader.ImageSize(Str: TStream): TPoint;
+var InRead : boolean;
+    P : Int64;
+begin
+  InRead := assigned(FStream);
+  if not assigned(Str) then
+    raise FPImageException.Create(ErrorText[StrNoStream]);
+  try
+    FStream := Str;
+    P:=Str.Position;
+    result := InternalSize (Str);
+    Str.Position:=P;
+  finally
+    if not InRead then
+      FStream := nil;
+  end;
+end;
+
 
 { TFPCustomImageWriter }
 

+ 4 - 1
packages/fcl-image/src/fpimage.pp

@@ -208,12 +208,15 @@ type
     protected
       procedure InternalRead  (Str:TStream; Img:TFPCustomImage); virtual; abstract;
       function  InternalCheck (Str:TStream) : boolean; virtual; abstract;
+      function  InternalSize  (Str:TStream): TPoint; virtual; 
     public
       constructor Create; override;
       function ImageRead (Str:TStream; Img:TFPCustomImage) : TFPCustomImage;
       // reads image
       function CheckContents (Str:TStream) : boolean;
-      // returns the size of image in stream without loading it completely
+      // Returns true if the content is readable
+      function ImageSize(Str:TStream): TPoint;
+      // returns the size of image in stream without loading it completely. -1,-1 means this is not implemented.
       property DefaultImageClass : TFPCustomImageClass read FDefImageClass write FDefImageClass;
       // Image Class to create when no img is given for reading
   end;

+ 33 - 0
packages/fcl-image/src/fpreadjpeg.pas

@@ -64,6 +64,7 @@ type
   protected
     procedure InternalRead(Str: TStream; Img: TFPCustomImage); override;
     function  InternalCheck(Str: TStream): boolean; override;
+    function  InternalSize(Str:TStream): TPoint; override;
   public
     constructor Create; override;
     destructor Destroy; override;
@@ -450,6 +451,38 @@ begin
   end;
 end;
 
+function TFPReaderJPEG.InternalSize(Str: TStream): TPoint;
+var
+  JInfo: jpeg_decompress_struct;
+  JError: jpeg_error_mgr;
+
+  procedure SetSource;
+  begin
+    jpeg_stdio_src(@JInfo, @Str);
+  end;
+
+  procedure ReadHeader;
+  begin
+    jpeg_read_header(@JInfo, TRUE);
+    Result.X := JInfo.image_width;
+    Result.Y := JInfo.image_height;
+  end;
+
+begin
+  FillChar(JInfo,SizeOf(JInfo),0);
+  if Str.Position < Str.Size then begin
+    JError:=jpeg_std_error;
+    JInfo.err := @JError;
+    jpeg_CreateDecompress(@JInfo, JPEG_LIB_VERSION, SizeOf(JInfo));
+    try
+      SetSource;
+      ReadHeader;
+    finally
+      jpeg_Destroy_Decompress(@JInfo);
+    end;
+  end;
+end;
+
 function TFPReaderJPEG.InternalCheck(Str: TStream): boolean;
 var
   Buf: array[0..1] of Byte = (0, 0);