Browse Source

+ Small improvements. More cosmetic in nature

michael 21 years ago
parent
commit
2e410c3c8a
1 changed files with 49 additions and 28 deletions
  1. 49 28
      fcl/image/fpreadbmp.pp

+ 49 - 28
fcl/image/fpreadbmp.pp

@@ -14,7 +14,10 @@
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 }
 }
 {*****************************************************************************}
 {*****************************************************************************}
-{$mode objfpc}{$h+}
+
+{$mode objfpc}
+{$h+}
+
 unit FPReadBMP;
 unit FPReadBMP;
 
 
 interface
 interface
@@ -24,15 +27,17 @@ uses FPImage, classes, sysutils, BMPcomn;
 type
 type
   TFPReaderBMP = class (TFPCustomImageReader)
   TFPReaderBMP = class (TFPCustomImageReader)
     Private
     Private
-      Procedure FreeBufs;
+      Procedure FreeBufs;       // Free (and nil) buffers.
     protected
     protected
-      ReadSize:Integer;
-      BFI:TBitMapInfoHeader;
-      FPalette : PFPcolor;
-      LineBuf : PByte; // Byte , TColorRGB or TColorRGBA
+      ReadSize : Integer;       // Size (in bytes) of 1 scanline.
+      BFI : TBitMapInfoHeader;  // The header as read from the stream.
+      FPalette : PFPcolor;      // Buffer with Palette entries. 
+      LineBuf : PByte;          // Buffer for 1 scanline. Can be Byte, TColorRGB or TColorRGBA
+      
+      // SetupRead will allocate the needed buffers, and read the colormap if needed.
+      procedure SetupRead(nPalette, nRowBits: Integer; Stream : TStream); virtual;
       procedure ReadScanLine(Row : Integer; Stream : TStream);
       procedure ReadScanLine(Row : Integer; Stream : TStream);
       procedure WriteScanLine(Row : Integer; Img : TFPCustomImage);
       procedure WriteScanLine(Row : Integer; Img : TFPCustomImage);
-      procedure SetupRead(nPalette, nRowBits: Integer; Stream : TStream); virtual;
       procedure InternalRead  (Stream:TStream; Img:TFPCustomImage); override;
       procedure InternalRead  (Stream:TStream; Img:TFPCustomImage); override;
       function  InternalCheck (Stream:TStream) : boolean; override;
       function  InternalCheck (Stream:TStream) : boolean; override;
     public
     public
@@ -43,7 +48,7 @@ type
 implementation
 implementation
 
 
 
 
-function MakeFpColor(RGBA: TColorRGBA):TFPcolor;
+function RGBAToFPColor(Const RGBA: TColorRGBA) : TFPcolor;
 
 
 begin
 begin
   with Result, RGBA do 
   with Result, RGBA do 
@@ -51,7 +56,19 @@ begin
     Red   :=(R shl 8) or R;
     Red   :=(R shl 8) or R;
     Green :=(G shl 8) or G;
     Green :=(G shl 8) or G;
     Blue  :=(B shl 8) or B;
     Blue  :=(B shl 8) or B;
-    alpha :=AlphaOpaque;
+    alpha :=A; //!! MVC: Used to be AlphaOpaque ???
+    end;
+end;
+
+Function RGBToFPColor(Const RGB : TColorRGB) : TFPColor;
+
+begin
+  with Result,RGB do
+    begin  {Use only the high byte to convert the color}
+    Red   := (R shl 8) + R;
+    Green := (G shl 8) + G;
+    Blue  := (B shl 8) + B;
+    Alpha := AlphaOpaque;
     end;
     end;
 end;
 end;
 
 
@@ -99,7 +116,7 @@ begin
     else // Seems to me that this is dangerous. 
     else // Seems to me that this is dangerous. 
       Stream.Read(ColInfo[0],nPalette*SizeOf(TColorRGBA));
       Stream.Read(ColInfo[0],nPalette*SizeOf(TColorRGBA));
     for i := 0 to High(ColInfo) do
     for i := 0 to High(ColInfo) do
-      FPalette[i] := MakeFpColor(ColInfo[i]);
+      FPalette[i] := RGBAToFPColor(ColInfo[i]);
     end 
     end 
   else if BFI.ClrUsed>0 then { Skip palette }
   else if BFI.ClrUsed>0 then { Skip palette }
     Stream.Position := Stream.Position + BFI.ClrUsed*SizeOf(TColorRGBA);
     Stream.Position := Stream.Position + BFI.ClrUsed*SizeOf(TColorRGBA);
@@ -118,6 +135,8 @@ begin
   Stream.Position:=Stream.Position-SizeOf(BFI)+BFI.Size;
   Stream.Position:=Stream.Position-SizeOf(BFI)+BFI.Size;
   with BFI do
   with BFI do
     begin
     begin
+    if (Compression<>0) then
+      Raise FPImageException.Create('Compressed bitmaps not supported');
     Img.Width:=Width;
     Img.Width:=Width;
     Img.Height:=Height;
     Img.Height:=Height;
     end;
     end;
@@ -129,23 +148,29 @@ begin
     8 : 
     8 : 
       SetupRead(256,Img.Width*8,Stream);
       SetupRead(256,Img.Width*8,Stream);
     16 :
     16 :
-      Raise Exception.Create('16 bpp bitmaps not supported');
+      Raise FPImageException.Create('16 bpp bitmaps not supported');
     24:
     24:
       SetupRead(0,Img.Width*8*3,Stream);
       SetupRead(0,Img.Width*8*3,Stream);
     32:
     32:
       SetupRead(0,Img.Width*8*4,Stream);
       SetupRead(0,Img.Width*8*4,Stream);
   end;
   end;
-  for Row:=Img.Height-1 downto 0 do 
-    begin
-    ReadScanLine(Row,Stream);
-    WriteScanLine(Row,Img);
-    end;
+  Try
+    for Row:=Img.Height-1 downto 0 do 
+      begin
+      ReadScanLine(Row,Stream); // Scanline in LineBuf with Size ReadSize.
+      WriteScanLine(Row,Img);
+      end;
+  finally
+    FreeBufs;
+  end;     
 end;
 end;
     
     
 procedure TFPReaderBMP.ReadScanLine(Row : Integer; Stream : TStream);
 procedure TFPReaderBMP.ReadScanLine(Row : Integer; Stream : TStream);
 
 
 begin
 begin
-  // Add here support for compressed lines. The 'readsize' is the same
+  {
+    Add here support for compressed lines. The 'readsize' is the same in the end.
+  }   
   Stream.Read(LineBuf[0],ReadSize);
   Stream.Read(LineBuf[0],ReadSize);
 end;
 end;
 
 
@@ -170,20 +195,13 @@ begin
       for Column:=0 to img.Width-1 do
       for Column:=0 to img.Width-1 do
         img.colors[Column,Row]:=FPalette[LineBuf[Column]];
         img.colors[Column,Row]:=FPalette[LineBuf[Column]];
    16 :
    16 :
-      Raise Exception.Create('16 bpp bitmaps not supported');
+      Raise FPImageException.Create('16 bpp bitmaps not supported');
    24 :
    24 :
       for Column:=0 to img.Width-1 do
       for Column:=0 to img.Width-1 do
-         with PColorRGB(LineBuf)[Column],aColor do
-           begin  {Use only the high byte to convert the color}
-           Red := (R shl 8) + R;
-           Green := (G shl 8) + G;
-           Blue := (B shl 8) + B;
-           alpha := AlphaOpaque;
-           img.colors[Column,Row]:=aColor;
-           end;
+        img.colors[Column,Row]:=RGBToFPColor(PColorRGB(LineBuf)[Column]);
    32 :
    32 :
       for Column:=0 to img.Width-1 do
       for Column:=0 to img.Width-1 do
-        img.colors[Column,Row]:=MakeFpColor(PColorRGBA(LineBuf)[Column]);
+        img.colors[Column,Row]:=RGBAToFPColor(PColorRGBA(LineBuf)[Column]);
     end;
     end;
 end;
 end;
 
 
@@ -202,7 +220,10 @@ initialization
 end.
 end.
 {
 {
 $Log$
 $Log$
-Revision 1.7  2004-02-20 22:42:44  michael
+Revision 1.8  2004-02-20 23:00:35  michael
++ Small improvements. More cosmetic in nature
+
+Revision 1.7  2004/02/20 22:42:44  michael
 + More modular reading of BMP for easier overriding in descendents
 + More modular reading of BMP for easier overriding in descendents
 
 
 Revision 1.6  2004/02/15 20:59:06  michael
 Revision 1.6  2004/02/15 20:59:06  michael