IMG grafika kezelése

Top  Previous  Next

A *.IMG fájlok 4 byte/pixel formátumú grafikát tartalmaznak.

Ezt memória bitmapba töltjük ill. ezt mentjük IMG formátumban.

 

// Load an *.IMG (4 bytes/pixel) image file into BMP

procedure LoadIMGFile( fName : string; var BMP: TBitmap);

Type

    TRGB_Rec = packed record

      R,G,B : byte;

    end;

 

    TRGBRec = packed record

      rgbTriple : TRGB_Rec;

      dummy     : byte;

    end;

 

var f       : TFileStream;

   RGBRec  : TRGBRec;

   fSize,w : integer;

   Row     : ^TRGB_Rec;

   pix     : Longint;

   x,y     : integer;

begin

Try

    f := TFileStream.Create(fName,fmOpenRead) ;

    fSize := f.Size div 4;          // Pixel count in file (file size = 4 * byte count)

    w := Trunc(SQRT(fSize));        // Width of rectangular bitmap

    BMP.PixelFormat := pf24bit;

    BMP.Width  := w;

    BMP.Height := w;

       for y:=0 to BMP.Width-1 do begin

           Row:=BMP.ScanLine[y];

           for x:=0 to BMP.Height-1 do begin

               f.Read(RGBRec,SizeOf(TRGBRec));

               Row^ := RGBRec.rgbTriple;

               Inc(Row);

           end;

       end;

finally

    f.Free;

end;

end;

 

// Save BMP as *.IMG (4 bytes/pixel) image file

procedure SaveIMGFile( fName : string; BMP: TBitmap);

Type

    TRGB_Rec = packed record

      R,G,B : byte;

    end;

 

    TRGBRec = packed record

      rgbTriple : TRGB_Rec;

      dummy     : byte;

    end;

 

var f       : TFileStream;

   RGBRec  : TRGBRec;

   fSize,w : integer;

   Row     : ^TRGB_Rec;

   pix     : Longint;

   x,y     : integer;

begin

Try

    f := TFileStream.Create(fName,fmCreate) ;

       for y:=0 to BMP.Width-1 do begin

           Row:=BMP.ScanLine[y];

           for x:=0 to BMP.Height-1 do begin

               RGBRec.rgbTriple := Row^;

//                RGBRec.rgbTriple.B := 255;

               RGBRec.dummy := $FF;

               f.Write(RGBRec,SizeOf(TRGBRec));

               Inc(Row);

           end;

       end;

finally

    f.Free;

end;

end;