Ignore:
Timestamp:
Apr 9, 2015, 9:58:36 PM (10 years ago)
Author:
chronos
Message:
  • Fixed: Use csOpaque control style also to Image, PaintBox and OpenGLControl.
  • Modified: Change size of test frame with SpinEdits as delayed using timer.
  • Updated: BRGABitmap package to version 8.1.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • GraphicTest/Packages/bgrabitmap/bgrapaintnet.pas

    r452 r472  
    2727  TPaintDotNetFile = class(TBGRACustomLayeredBitmap)
    2828  public
    29     procedure LoadFromFile(const filename: string); override;
     29    procedure LoadFromFile(const filenameUTF8: string); override;
    3030    procedure LoadFromStream(stream: TStream); override;
    3131    procedure Clear; override;
     
    3434    constructor Create; override;
    3535  protected
     36    procedure InternalLoadFromStream(stream: TStream);
    3637    function GetWidth: integer; override;
    3738    function GetHeight: integer; override;
     
    4243    function GetLayerName(layer: integer): string; override;
    4344  private
    44     XmlHeader: string;
    45     ThumbNail: TBGRABitmap;
    4645    Content:   TDotNetDeserialization;
    4746    Document:  TSerializedClass;
     
    6059
    6160  TFPReaderPaintDotNet = class(TFPCustomImageReader)
     61    private
     62      FWidth,FHeight,FNbLayers: integer;
    6263    protected
    6364      function InternalCheck(Stream: TStream): boolean; override;
    6465      procedure InternalRead(Stream: TStream; Img: TFPCustomImage); override;
     66    public
     67      property Width: integer read FWidth;
     68      property Height: integer read FHeight;
     69      property NbLayers: integer read FNbLayers;
    6570  end;
    6671
    6772function IsPaintDotNetFile(filename: string): boolean;
     73function IsPaintDotNetFileUTF8(filenameUTF8: string): boolean;
    6874function IsPaintDotNetStream(stream: TStream): boolean;
    6975function LoadPaintDotNetFile(filename: string): TBGRABitmap;
     76function LoadPaintDotNetFileUTF8(filenameUTF8: string): TBGRABitmap;
    7077
    7178procedure RegisterPaintNetFormat;
     
    7380implementation
    7481
    75 uses zstream, Math, graphtype, Graphics;
     82uses zstream, Math, graphtype, Graphics, lazutf8classes, FileUtil;
    7683
    7784{$hints off}
     
    99106  begin
    100107    stream := TFileStream.Create(filename, fmOpenRead);
     108    Result := IsPaintDotNetStream(stream);
     109    stream.Free;
     110  end;
     111end;
     112
     113function IsPaintDotNetFileUTF8(filenameUTF8: string): boolean;
     114var
     115  stream: TFileStreamUTF8;
     116begin
     117  Result := False;
     118  if FileExistsUTF8(filenameUTF8) then
     119  begin
     120    stream := TFileStreamUTF8.Create(filenameUTF8, fmOpenRead);
    101121    Result := IsPaintDotNetStream(stream);
    102122    stream.Free;
     
    127147
    128148function LoadPaintDotNetFile(filename: string): TBGRABitmap;
     149begin
     150  result := LoadPaintDotNetFileUTF8(SysToUTF8(filename));
     151end;
     152
     153function LoadPaintDotNetFileUTF8(filenameUTF8: string): TBGRABitmap;
    129154var
    130155  pdn: TPaintDotNetFile;
     
    133158  Result := nil;
    134159  try
    135     pdn.LoadFromFile(filename);
     160    pdn.LoadFromFile(filenameUTF8);
    136161    Result := pdn.ComputeFlatImage;
    137162    pdn.Free;
     
    181206  x,y: integer;
    182207begin
     208  FWidth := 0;
     209  FHeight:= 0;
     210  FNbLayers:= 0;
    183211  pdn    := TPaintDotNetFile.Create;
    184212  try
     
    186214    flat := pdn.ComputeFlatImage;
    187215    try
    188       Img.SetSize(pdn.Width,pdn.Height);
    189       for y := 0 to pdn.Height-1 do
    190         for x := 0 to pdn.Width-1 do
    191           Img.Colors[x,y] := BGRAToFPColor(flat.GetPixel(x,y));
     216      FWidth:= pdn.Width;
     217      FHeight:= pdn.Height;
     218      FNbLayers:= pdn.NbLayers;
     219
     220      if Img is TBGRACustomBitmap then
     221        TBGRACustomBitmap(Img).Assign(flat) else
     222      begin
     223        Img.SetSize(pdn.Width,pdn.Height);
     224        for y := 0 to pdn.Height-1 do
     225          for x := 0 to pdn.Width-1 do
     226            Img.Colors[x,y] := BGRAToFPColor(flat.GetPixel(x,y));
     227      end;
    192228    finally
    193229      flat.free;
     
    205241{ TPaintDotNetFile }
    206242
    207 procedure TPaintDotNetFile.LoadFromFile(const filename: string);
    208 var
    209   stream: TFileStream;
    210 begin
    211   stream := TFileStream.Create(filename, fmOpenRead);
     243procedure TPaintDotNetFile.LoadFromFile(const filenameUTF8: string);
     244var
     245  stream: TFileStreamUTF8;
     246begin
     247  stream := TFileStreamUTF8.Create(filenameUTF8, fmOpenRead);
     248  OnLayeredBitmapLoadStart(filenameUTF8);
    212249  try
    213     LoadFromStream(stream);
     250    InternalLoadFromStream(stream);
    214251  finally
     252    OnLayeredBitmapLoaded;
    215253    stream.Free;
    216254  end;
     
    218256
    219257procedure TPaintDotNetFile.LoadFromStream(stream: TStream);
     258begin
     259  OnLayeredBitmapLoadFromStreamStart;
     260  try
     261    InternalLoadFromStream(stream);
     262  finally
     263    OnLayeredBitmapLoaded;
     264  end;
     265end;
     266
     267procedure TPaintDotNetFile.InternalLoadFromStream(stream: TStream);
    220268var
    221269  header: packed array[0..3] of char;
     
    233281  stream.Read(XmlHeaderSize, 3);
    234282  XmlheaderSize := LEtoN(XmlheaderSize);
    235   setlength(XmlHeader, XmlHeaderSize);
    236   if stream.Read(XmlHeader[1], XmlHeaderSize) <> XmlHeaderSize then
     283  if Stream.Position + XmlHeaderSize > stream.Size then
    237284    raise Exception.Create('Xml header size error');
    238   XmlHeader := Utf8ToAnsi(XmlHeader);
     285  Stream.Position:= Stream.Position + XmlHeaderSize;
    239286     {$hints off}
    240287  stream.Read(CompressionFormat, sizeof(CompressionFormat));
     
    255302  for i := 0 to NbLayers - 1 do
    256303  begin
     304    OnLayeredBitmapLoadProgress((i+1)*100 div NbLayers);
    257305    LayerData[i] := TMemoryStream.Create;
    258306    LoadLayer(LayerData[i], Stream, LayerDataSize(i));
     
    266314begin
    267315  Result := 'Paint.Net document' + LineEnding + LineEnding;
    268   if length(XmlHeader) > 255 then
    269     Result += copy(XmlHeader, 1, 255) + '...'
    270   else
    271     Result += XmlHeader;
    272   Result += LineEnding + LineEnding + Content.ToString;
     316  Result += Content.ToString;
    273317  for i := 0 to NbLayers - 1 do
    274318  begin
     
    297341  inherited Create;
    298342  Content   := nil;
    299   ThumbNail := nil;
    300343  Document  := nil;
    301344  Layers    := nil;
     
    308351  i: integer;
    309352begin
    310   XmlHeader := '';
    311353  FreeAndNil(content);
    312   FreeAndNil(thumbNail);
    313354  document := nil;
    314355  Layers   := nil;
     
    610651  RegisterLayeredBitmapReader('pdn', TPaintDotNetFile);
    611652  //TPicture.RegisterFileFormat('pdn', 'Paint.NET image', TPaintDotNetFile);
     653  DefaultBGRAImageReader[ifPaintDotNet] := TFPReaderPaintDotNet;
    612654  AlreadyRegistered := true;
    613655end;
Note: See TracChangeset for help on using the changeset viewer.