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/bgraopenraster.pas

    r452 r472  
    3838    function GetMemoryStreamAsString(AFilename: string): string;
    3939    procedure UnzipFromStream(AStream: TStream);
    40     procedure UnzipFromFile(AFilename: string);
    41     procedure ZipToFile(AFilename: string);
     40    procedure UnzipFromFile(AFilenameUTF8: string);
     41    procedure ZipToFile(AFilenameUTF8: string);
     42    procedure ZipToStream(AStream: TStream);
    4243    procedure CopyThumbnailToMemoryStream(AMaxWidth, AMaxHeight: integer);
    4344    procedure AnalyzeZip;
     45    procedure PrepareZipToSave;
    4446    function GetMimeType: string; override;
    4547
    4648  public
    47     constructor Create; override;
     49    constructor Create; override; overload;
     50    constructor Create(AWidth, AHeight: integer); override; overload;
    4851    procedure Clear; override;
     52    function CheckMimeType(AStream: TStream): boolean;
    4953    procedure LoadFromStream(AStream: TStream); override;
    50     procedure LoadFromFile(const filename: string); override;
    51     procedure SaveToFile(const filename: string); override;
     54    procedure LoadFromFile(const filenameUTF8: string); override;
     55    procedure SaveToFile(const filenameUTF8: string); override;
     56    procedure SaveToStream(AStream: TStream); override;
    5257    property MimeType : string read GetMimeType write SetMimeType;
    5358    property StackXML : TXMLDocument read FStackXML;
     
    5762
    5863  TFPReaderOpenRaster = class(TFPCustomImageReader)
     64    private
     65      FWidth,FHeight,FNbLayers: integer;
    5966    protected
    6067      function InternalCheck(Stream: TStream): boolean; override;
    6168      procedure InternalRead(Stream: TStream; Img: TFPCustomImage); override;
     69    public
     70      property Width: integer read FWidth;
     71      property Height: integer read FHeight;
     72      property NbLayers: integer read FNbLayers;
     73  end;
     74
     75  { TFPWriterOpenRaster }
     76
     77  TFPWriterOpenRaster = class(TFPCustomImageWriter)
     78    protected
     79      procedure InternalWrite (Str:TStream; Img:TFPCustomImage); override;
    6280  end;
    6381
     
    6684implementation
    6785
    68 uses Graphics, XMLRead, XMLWrite, FPReadPNG, dialogs, BGRABitmapTypes, zstream;
     86uses Graphics, XMLRead, XMLWrite, FPReadPNG, dialogs, BGRABitmapTypes, zstream, lazutf8classes,
     87  UnzipperExt;
    6988
    7089function IsZipStream(stream: TStream): boolean;
     
    89108end;
    90109
     110{ TFPWriterOpenRaster }
     111
     112procedure TFPWriterOpenRaster.InternalWrite(Str: TStream; Img: TFPCustomImage);
     113var doc: TBGRAOpenRasterDocument;
     114  tempBmp: TBGRABitmap;
     115  x,y: integer;
     116
     117begin
     118  doc := TBGRAOpenRasterDocument.Create;
     119  if Img is TBGRABitmap then doc.AddLayer(Img as TBGRABitmap) else
     120  begin
     121    tempBmp := TBGRABitmap.Create(img.Width,img.Height);
     122    for y := 0 to Img.Height-1 do
     123      for x := 0 to img.Width-1 do
     124        tempBmp.SetPixel(x,y, FPColorToBGRA(img.Colors[x,y]));
     125    doc.AddOwnedLayer(tempBmp);
     126  end;
     127  doc.SaveToStream(Str);
     128  doc.Free;
     129end;
     130
    91131{ TFPReaderOpenRaster }
    92132
    93133function TFPReaderOpenRaster.InternalCheck(Stream: TStream): boolean;
    94 begin
    95  result := IsZipStream(Stream);
     134var {%h-}magic: packed array[0..3] of byte;
     135  OldPos,BytesRead: Int64;
     136  doc : TBGRAOpenRasterDocument;
     137begin
     138  Result:=false;
     139  if Stream=nil then exit;
     140  oldPos := stream.Position;
     141  BytesRead := Stream.Read({%h-}magic,sizeof(magic));
     142  stream.Position:= OldPos;
     143  if BytesRead<>sizeof(magic) then exit;
     144  if (magic[0] = $50) and (magic[1] = $4b) and (magic[2] = $03) and (magic[3] = $04) then
     145  begin
     146    doc := TBGRAOpenRasterDocument.Create;
     147    result := doc.CheckMimeType(Stream);
     148    doc.Free;
     149  end;
    96150end;
    97151
     
    102156  x,y: integer;
    103157begin
     158  FWidth := 0;
     159  FHeight:= 0;
     160  FNbLayers:= 0;
    104161  layeredImage := TBGRAOpenRasterDocument.Create;
    105162  try
     
    107164    flat := layeredImage.ComputeFlatImage;
    108165    try
    109       Img.SetSize(flat.Width,flat.Height);
    110       for y := 0 to flat.Height-1 do
    111         for x := 0 to flat.Width-1 do
    112           Img.Colors[x,y] := BGRAToFPColor(flat.GetPixel(x,y));
     166      FWidth:= layeredImage.Width;
     167      FHeight:= layeredImage.Height;
     168      FNbLayers:= layeredImage.NbLayers;
     169      if Img is TBGRACustomBitmap then
     170        TBGRACustomBitmap(img).Assign(flat)
     171      else
     172      begin
     173        Img.SetSize(flat.Width,flat.Height);
     174        for y := 0 to flat.Height-1 do
     175          for x := 0 to flat.Width-1 do
     176            Img.Colors[x,y] := BGRAToFPColor(flat.GetPixel(x,y));
     177      end;
    113178    finally
    114179      flat.free;
     
    173238  for i := stackNode.ChildNodes.Length-1 downto 0 do
    174239  begin
     240    OnLayeredBitmapLoadProgress((stackNode.ChildNodes.Length-i)*100 div stackNode.ChildNodes.Length);
    175241    layerNode:= stackNode.ChildNodes[i];
    176242    if (layerNode.NodeName = 'layer') and Assigned(layerNode.Attributes) then
     
    291357end;
    292358
    293 procedure TBGRAOpenRasterDocument.LoadFromFile(const filename: string);
    294 begin
    295   UnzipFromFile(filename);
    296   AnalyzeZip;
    297 end;
    298 
    299 procedure TBGRAOpenRasterDocument.SaveToFile(const filename: string);
     359procedure TBGRAOpenRasterDocument.PrepareZipToSave;
    300360var i: integer;
    301361    imageNode,stackNode,layerNode: TDOMElement;
     
    375435  WriteXMLFile(StackXML, StackStream);
    376436  SetMemoryStream('stack.xml',StackStream);
    377 
    378   ZipToFile(filename);
     437end;
     438
     439procedure TBGRAOpenRasterDocument.LoadFromFile(const filenameUTF8: string);
     440var AStream: TFileStreamUTF8;
     441begin
     442  AStream := TFileStreamUTF8.Create(filenameUTF8,fmOpenRead or fmShareDenyWrite);
     443  try
     444    LoadFromStream(AStream);
     445  finally
     446    AStream.Free;
     447  end;
     448end;
     449
     450procedure TBGRAOpenRasterDocument.SaveToFile(const filenameUTF8: string);
     451begin
     452  PrepareZipToSave;
     453  ZipToFile(filenameUTF8);
     454end;
     455
     456procedure TBGRAOpenRasterDocument.SaveToStream(AStream: TStream);
     457begin
     458  PrepareZipToSave;
     459  ZipToStream(AStream);
    379460end;
    380461
     
    390471begin
    391472  inherited Create;
     473  RegisterOpenRasterFormat;
     474end;
     475
     476constructor TBGRAOpenRasterDocument.Create(AWidth, AHeight: integer);
     477begin
     478  inherited Create(AWidth, AHeight);
    392479  RegisterOpenRasterFormat;
    393480end;
     
    514601  finally
    515602    FZipInputStream := nil;
    516   end;
    517   unzip.Free;
    518 end;
    519 
    520 procedure TBGRAOpenRasterDocument.UnzipFromFile(AFilename: string);
     603    unzip.Free;
     604  end;
     605end;
     606
     607procedure TBGRAOpenRasterDocument.UnzipFromFile(AFilenameUTF8: string);
    521608var unzip: TUnZipper;
    522609begin
     
    524611  unzip := TUnZipper.Create;
    525612  try
    526     unzip.FileName := AFilename;
     613    unzip.FileName := Utf8ToAnsi(AFilenameUTF8);
    527614    unzip.OnCreateStream := @ZipOnCreateStream;
    528615    unzip.OnDoneStream := @ZipOnDoneStream;
    529616    unzip.UnZipAllFiles;
    530617  finally
    531   end;
    532   unzip.Free;
    533 end;
    534 
    535 procedure TBGRAOpenRasterDocument.ZipToFile(AFilename: string);
     618    unzip.Free;
     619  end;
     620end;
     621
     622procedure TBGRAOpenRasterDocument.ZipToFile(AFilenameUTF8: string);
     623var
     624  stream: TFileStreamUTF8;
     625begin
     626  stream := TFileStreamUTF8.Create(AFilenameUTF8, fmCreate);
     627  try
     628    ZipToStream(stream);
     629  finally
     630    stream.Free;
     631  end;
     632end;
     633
     634procedure TBGRAOpenRasterDocument.ZipToStream(AStream: TStream);
    536635var zip: TZipper;
    537636  i: integer;
     
    539638  zip := TZipper.Create;
    540639  try
    541     zip.FileName := AFilename;
    542640    for i := 0 to high(FFiles) do
    543641    begin
     
    545643      zip.Entries.AddFileEntry(FFiles[i].Stream,FFiles[i].Filename).CompressionLevel := clnone;
    546644    end;
    547     zip.ZipAllFiles;
     645    zip.SaveToStream(AStream);
    548646  finally
    549647    zip.Free;
     
    557655  if (Width = 0) or (Height = 0) then exit;
    558656  thumbnail := ComputeFlatImage;
     657  CopyBitmapToMemoryStream(thumbnail,'mergedimage.png');
    559658  if (thumbnail.Width > AMaxWidth) or
    560659   (thumbnail.Height > AMaxHeight) then
     
    586685end;
    587686
     687function TBGRAOpenRasterDocument.CheckMimeType(AStream: TStream): boolean;
     688var unzip: TUnzipperStreamUtf8;
     689  mimeTypeFound: string;
     690  oldPos: int64;
     691begin
     692  result := false;
     693  unzip := TUnzipperStreamUtf8.Create;
     694  oldPos := AStream.Position;
     695  try
     696    unzip.InputStream := AStream;
     697    mimeTypeFound := unzip.UnzipFileToString('mimetype');
     698    if mimeTypeFound = OpenRasterMimeType then result := true;
     699  except
     700  end;
     701  unzip.Free;
     702  astream.Position:= OldPos;
     703end;
     704
    588705procedure TBGRAOpenRasterDocument.LoadFromStream(AStream: TStream);
    589706begin
    590   UnzipFromStream(AStream);
    591   AnalyzeZip;
     707  OnLayeredBitmapLoadFromStreamStart;
     708  try
     709    UnzipFromStream(AStream);
     710    AnalyzeZip;
     711  finally
     712    OnLayeredBitmapLoaded;
     713  end;
    592714end;
    593715
     
    673795  RegisterLayeredBitmapWriter('ora', TBGRAOpenRasterDocument);
    674796  //TPicture.RegisterFileFormat('ora', 'OpenRaster', TBGRAOpenRasterDocument);
     797  DefaultBGRAImageReader[ifOpenRaster] := TFPReaderOpenRaster;
     798  DefaultBGRAImageWriter[ifOpenRaster] := TFPWriterOpenRaster;
    675799  AlreadyRegistered:= True;
    676800end;
Note: See TracChangeset for help on using the changeset viewer.