Changeset 472 for GraphicTest/Packages/bgrabitmap/bgrapaintnet.pas
- Timestamp:
- Apr 9, 2015, 9:58:36 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GraphicTest/Packages/bgrabitmap/bgrapaintnet.pas
r452 r472 27 27 TPaintDotNetFile = class(TBGRACustomLayeredBitmap) 28 28 public 29 procedure LoadFromFile(const filename : string); override;29 procedure LoadFromFile(const filenameUTF8: string); override; 30 30 procedure LoadFromStream(stream: TStream); override; 31 31 procedure Clear; override; … … 34 34 constructor Create; override; 35 35 protected 36 procedure InternalLoadFromStream(stream: TStream); 36 37 function GetWidth: integer; override; 37 38 function GetHeight: integer; override; … … 42 43 function GetLayerName(layer: integer): string; override; 43 44 private 44 XmlHeader: string;45 ThumbNail: TBGRABitmap;46 45 Content: TDotNetDeserialization; 47 46 Document: TSerializedClass; … … 60 59 61 60 TFPReaderPaintDotNet = class(TFPCustomImageReader) 61 private 62 FWidth,FHeight,FNbLayers: integer; 62 63 protected 63 64 function InternalCheck(Stream: TStream): boolean; override; 64 65 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; 65 70 end; 66 71 67 72 function IsPaintDotNetFile(filename: string): boolean; 73 function IsPaintDotNetFileUTF8(filenameUTF8: string): boolean; 68 74 function IsPaintDotNetStream(stream: TStream): boolean; 69 75 function LoadPaintDotNetFile(filename: string): TBGRABitmap; 76 function LoadPaintDotNetFileUTF8(filenameUTF8: string): TBGRABitmap; 70 77 71 78 procedure RegisterPaintNetFormat; … … 73 80 implementation 74 81 75 uses zstream, Math, graphtype, Graphics ;82 uses zstream, Math, graphtype, Graphics, lazutf8classes, FileUtil; 76 83 77 84 {$hints off} … … 99 106 begin 100 107 stream := TFileStream.Create(filename, fmOpenRead); 108 Result := IsPaintDotNetStream(stream); 109 stream.Free; 110 end; 111 end; 112 113 function IsPaintDotNetFileUTF8(filenameUTF8: string): boolean; 114 var 115 stream: TFileStreamUTF8; 116 begin 117 Result := False; 118 if FileExistsUTF8(filenameUTF8) then 119 begin 120 stream := TFileStreamUTF8.Create(filenameUTF8, fmOpenRead); 101 121 Result := IsPaintDotNetStream(stream); 102 122 stream.Free; … … 127 147 128 148 function LoadPaintDotNetFile(filename: string): TBGRABitmap; 149 begin 150 result := LoadPaintDotNetFileUTF8(SysToUTF8(filename)); 151 end; 152 153 function LoadPaintDotNetFileUTF8(filenameUTF8: string): TBGRABitmap; 129 154 var 130 155 pdn: TPaintDotNetFile; … … 133 158 Result := nil; 134 159 try 135 pdn.LoadFromFile(filename );160 pdn.LoadFromFile(filenameUTF8); 136 161 Result := pdn.ComputeFlatImage; 137 162 pdn.Free; … … 181 206 x,y: integer; 182 207 begin 208 FWidth := 0; 209 FHeight:= 0; 210 FNbLayers:= 0; 183 211 pdn := TPaintDotNetFile.Create; 184 212 try … … 186 214 flat := pdn.ComputeFlatImage; 187 215 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; 192 228 finally 193 229 flat.free; … … 205 241 { TPaintDotNetFile } 206 242 207 procedure TPaintDotNetFile.LoadFromFile(const filename: string); 208 var 209 stream: TFileStream; 210 begin 211 stream := TFileStream.Create(filename, fmOpenRead); 243 procedure TPaintDotNetFile.LoadFromFile(const filenameUTF8: string); 244 var 245 stream: TFileStreamUTF8; 246 begin 247 stream := TFileStreamUTF8.Create(filenameUTF8, fmOpenRead); 248 OnLayeredBitmapLoadStart(filenameUTF8); 212 249 try 213 LoadFromStream(stream);250 InternalLoadFromStream(stream); 214 251 finally 252 OnLayeredBitmapLoaded; 215 253 stream.Free; 216 254 end; … … 218 256 219 257 procedure TPaintDotNetFile.LoadFromStream(stream: TStream); 258 begin 259 OnLayeredBitmapLoadFromStreamStart; 260 try 261 InternalLoadFromStream(stream); 262 finally 263 OnLayeredBitmapLoaded; 264 end; 265 end; 266 267 procedure TPaintDotNetFile.InternalLoadFromStream(stream: TStream); 220 268 var 221 269 header: packed array[0..3] of char; … … 233 281 stream.Read(XmlHeaderSize, 3); 234 282 XmlheaderSize := LEtoN(XmlheaderSize); 235 setlength(XmlHeader, XmlHeaderSize); 236 if stream.Read(XmlHeader[1], XmlHeaderSize) <> XmlHeaderSize then 283 if Stream.Position + XmlHeaderSize > stream.Size then 237 284 raise Exception.Create('Xml header size error'); 238 XmlHeader := Utf8ToAnsi(XmlHeader);285 Stream.Position:= Stream.Position + XmlHeaderSize; 239 286 {$hints off} 240 287 stream.Read(CompressionFormat, sizeof(CompressionFormat)); … … 255 302 for i := 0 to NbLayers - 1 do 256 303 begin 304 OnLayeredBitmapLoadProgress((i+1)*100 div NbLayers); 257 305 LayerData[i] := TMemoryStream.Create; 258 306 LoadLayer(LayerData[i], Stream, LayerDataSize(i)); … … 266 314 begin 267 315 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; 273 317 for i := 0 to NbLayers - 1 do 274 318 begin … … 297 341 inherited Create; 298 342 Content := nil; 299 ThumbNail := nil;300 343 Document := nil; 301 344 Layers := nil; … … 308 351 i: integer; 309 352 begin 310 XmlHeader := '';311 353 FreeAndNil(content); 312 FreeAndNil(thumbNail);313 354 document := nil; 314 355 Layers := nil; … … 610 651 RegisterLayeredBitmapReader('pdn', TPaintDotNetFile); 611 652 //TPicture.RegisterFileFormat('pdn', 'Paint.NET image', TPaintDotNetFile); 653 DefaultBGRAImageReader[ifPaintDotNet] := TFPReaderPaintDotNet; 612 654 AlreadyRegistered := true; 613 655 end;
Note:
See TracChangeset
for help on using the changeset viewer.