Changeset 472 for GraphicTest/Packages/bgrabitmap/bgraopenraster.pas
- Timestamp:
- Apr 9, 2015, 9:58:36 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GraphicTest/Packages/bgrabitmap/bgraopenraster.pas
r452 r472 38 38 function GetMemoryStreamAsString(AFilename: string): string; 39 39 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); 42 43 procedure CopyThumbnailToMemoryStream(AMaxWidth, AMaxHeight: integer); 43 44 procedure AnalyzeZip; 45 procedure PrepareZipToSave; 44 46 function GetMimeType: string; override; 45 47 46 48 public 47 constructor Create; override; 49 constructor Create; override; overload; 50 constructor Create(AWidth, AHeight: integer); override; overload; 48 51 procedure Clear; override; 52 function CheckMimeType(AStream: TStream): boolean; 49 53 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; 52 57 property MimeType : string read GetMimeType write SetMimeType; 53 58 property StackXML : TXMLDocument read FStackXML; … … 57 62 58 63 TFPReaderOpenRaster = class(TFPCustomImageReader) 64 private 65 FWidth,FHeight,FNbLayers: integer; 59 66 protected 60 67 function InternalCheck(Stream: TStream): boolean; override; 61 68 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; 62 80 end; 63 81 … … 66 84 implementation 67 85 68 uses Graphics, XMLRead, XMLWrite, FPReadPNG, dialogs, BGRABitmapTypes, zstream; 86 uses Graphics, XMLRead, XMLWrite, FPReadPNG, dialogs, BGRABitmapTypes, zstream, lazutf8classes, 87 UnzipperExt; 69 88 70 89 function IsZipStream(stream: TStream): boolean; … … 89 108 end; 90 109 110 { TFPWriterOpenRaster } 111 112 procedure TFPWriterOpenRaster.InternalWrite(Str: TStream; Img: TFPCustomImage); 113 var doc: TBGRAOpenRasterDocument; 114 tempBmp: TBGRABitmap; 115 x,y: integer; 116 117 begin 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; 129 end; 130 91 131 { TFPReaderOpenRaster } 92 132 93 133 function TFPReaderOpenRaster.InternalCheck(Stream: TStream): boolean; 94 begin 95 result := IsZipStream(Stream); 134 var {%h-}magic: packed array[0..3] of byte; 135 OldPos,BytesRead: Int64; 136 doc : TBGRAOpenRasterDocument; 137 begin 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; 96 150 end; 97 151 … … 102 156 x,y: integer; 103 157 begin 158 FWidth := 0; 159 FHeight:= 0; 160 FNbLayers:= 0; 104 161 layeredImage := TBGRAOpenRasterDocument.Create; 105 162 try … … 107 164 flat := layeredImage.ComputeFlatImage; 108 165 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; 113 178 finally 114 179 flat.free; … … 173 238 for i := stackNode.ChildNodes.Length-1 downto 0 do 174 239 begin 240 OnLayeredBitmapLoadProgress((stackNode.ChildNodes.Length-i)*100 div stackNode.ChildNodes.Length); 175 241 layerNode:= stackNode.ChildNodes[i]; 176 242 if (layerNode.NodeName = 'layer') and Assigned(layerNode.Attributes) then … … 291 357 end; 292 358 293 procedure TBGRAOpenRasterDocument.LoadFromFile(const filename: string); 294 begin 295 UnzipFromFile(filename); 296 AnalyzeZip; 297 end; 298 299 procedure TBGRAOpenRasterDocument.SaveToFile(const filename: string); 359 procedure TBGRAOpenRasterDocument.PrepareZipToSave; 300 360 var i: integer; 301 361 imageNode,stackNode,layerNode: TDOMElement; … … 375 435 WriteXMLFile(StackXML, StackStream); 376 436 SetMemoryStream('stack.xml',StackStream); 377 378 ZipToFile(filename); 437 end; 438 439 procedure TBGRAOpenRasterDocument.LoadFromFile(const filenameUTF8: string); 440 var AStream: TFileStreamUTF8; 441 begin 442 AStream := TFileStreamUTF8.Create(filenameUTF8,fmOpenRead or fmShareDenyWrite); 443 try 444 LoadFromStream(AStream); 445 finally 446 AStream.Free; 447 end; 448 end; 449 450 procedure TBGRAOpenRasterDocument.SaveToFile(const filenameUTF8: string); 451 begin 452 PrepareZipToSave; 453 ZipToFile(filenameUTF8); 454 end; 455 456 procedure TBGRAOpenRasterDocument.SaveToStream(AStream: TStream); 457 begin 458 PrepareZipToSave; 459 ZipToStream(AStream); 379 460 end; 380 461 … … 390 471 begin 391 472 inherited Create; 473 RegisterOpenRasterFormat; 474 end; 475 476 constructor TBGRAOpenRasterDocument.Create(AWidth, AHeight: integer); 477 begin 478 inherited Create(AWidth, AHeight); 392 479 RegisterOpenRasterFormat; 393 480 end; … … 514 601 finally 515 602 FZipInputStream := nil; 516 end;517 unzip.Free;518 end; 519 520 procedure TBGRAOpenRasterDocument.UnzipFromFile(AFilename : string);603 unzip.Free; 604 end; 605 end; 606 607 procedure TBGRAOpenRasterDocument.UnzipFromFile(AFilenameUTF8: string); 521 608 var unzip: TUnZipper; 522 609 begin … … 524 611 unzip := TUnZipper.Create; 525 612 try 526 unzip.FileName := AFilename;613 unzip.FileName := Utf8ToAnsi(AFilenameUTF8); 527 614 unzip.OnCreateStream := @ZipOnCreateStream; 528 615 unzip.OnDoneStream := @ZipOnDoneStream; 529 616 unzip.UnZipAllFiles; 530 617 finally 531 end; 532 unzip.Free; 533 end; 534 535 procedure TBGRAOpenRasterDocument.ZipToFile(AFilename: string); 618 unzip.Free; 619 end; 620 end; 621 622 procedure TBGRAOpenRasterDocument.ZipToFile(AFilenameUTF8: string); 623 var 624 stream: TFileStreamUTF8; 625 begin 626 stream := TFileStreamUTF8.Create(AFilenameUTF8, fmCreate); 627 try 628 ZipToStream(stream); 629 finally 630 stream.Free; 631 end; 632 end; 633 634 procedure TBGRAOpenRasterDocument.ZipToStream(AStream: TStream); 536 635 var zip: TZipper; 537 636 i: integer; … … 539 638 zip := TZipper.Create; 540 639 try 541 zip.FileName := AFilename;542 640 for i := 0 to high(FFiles) do 543 641 begin … … 545 643 zip.Entries.AddFileEntry(FFiles[i].Stream,FFiles[i].Filename).CompressionLevel := clnone; 546 644 end; 547 zip. ZipAllFiles;645 zip.SaveToStream(AStream); 548 646 finally 549 647 zip.Free; … … 557 655 if (Width = 0) or (Height = 0) then exit; 558 656 thumbnail := ComputeFlatImage; 657 CopyBitmapToMemoryStream(thumbnail,'mergedimage.png'); 559 658 if (thumbnail.Width > AMaxWidth) or 560 659 (thumbnail.Height > AMaxHeight) then … … 586 685 end; 587 686 687 function TBGRAOpenRasterDocument.CheckMimeType(AStream: TStream): boolean; 688 var unzip: TUnzipperStreamUtf8; 689 mimeTypeFound: string; 690 oldPos: int64; 691 begin 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; 703 end; 704 588 705 procedure TBGRAOpenRasterDocument.LoadFromStream(AStream: TStream); 589 706 begin 590 UnzipFromStream(AStream); 591 AnalyzeZip; 707 OnLayeredBitmapLoadFromStreamStart; 708 try 709 UnzipFromStream(AStream); 710 AnalyzeZip; 711 finally 712 OnLayeredBitmapLoaded; 713 end; 592 714 end; 593 715 … … 673 795 RegisterLayeredBitmapWriter('ora', TBGRAOpenRasterDocument); 674 796 //TPicture.RegisterFileFormat('ora', 'OpenRaster', TBGRAOpenRasterDocument); 797 DefaultBGRAImageReader[ifOpenRaster] := TFPReaderOpenRaster; 798 DefaultBGRAImageWriter[ifOpenRaster] := TFPWriterOpenRaster; 675 799 AlreadyRegistered:= True; 676 800 end;
Note:
See TracChangeset
for help on using the changeset viewer.