Changeset 472 for GraphicTest/Packages/bgrabitmap/bgralayers.pas
- Timestamp:
- Apr 9, 2015, 9:58:36 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GraphicTest/Packages/bgrabitmap/bgralayers.pas
r452 r472 14 14 TBGRALayeredBitmap = class; 15 15 TBGRALayeredBitmapClass = class of TBGRALayeredBitmap; 16 17 TBGRALayeredBitmapSaveToStreamProc = procedure(AStream: TStream; ALayers: TBGRACustomLayeredBitmap); 18 TBGRALayeredBitmapLoadFromStreamProc = function(AStream: TStream): TBGRALayeredBitmap; 16 19 17 20 { TBGRACustomLayeredBitmap } … … 36 39 function GetLayerName(layer: integer): string; virtual; 37 40 function GetLayerOffset(layer: integer): TPoint; virtual; 38 function GetLayerBitmapDirectly(layer: integer): TBGRABitmap; virtual;39 41 function GetLayerFrozenRange(layer: integer): integer; 40 42 function GetLayerFrozen(layer: integer): boolean; virtual; 43 function GetLayerUniqueId(layer: integer): integer; virtual; 41 44 procedure SetLayerFrozen(layer: integer; AValue: boolean); virtual; 42 45 function RangeIntersect(first1,last1,first2,last2: integer): boolean; … … 50 53 51 54 public 52 procedure SaveToFile(const filename : string); override;55 procedure SaveToFile(const filenameUTF8: string); override; 53 56 procedure SaveToStream(Stream: TStream); override; 54 57 constructor Create; override; 55 58 destructor Destroy; override; 56 59 function ToString: ansistring; override; 60 function GetLayerBitmapDirectly(layer: integer): TBGRABitmap; virtual; 57 61 function GetLayerBitmapCopy(layer: integer): TBGRABitmap; virtual; abstract; 58 62 function ComputeFlatImage: TBGRABitmap; overload; … … 80 84 property LayerOffset[layer: integer]: TPoint read GetLayerOffset; 81 85 property LayerFrozen[layer: integer]: boolean read GetLayerFrozen; 86 property LayerUniqueId[layer: integer]: integer read GetLayerUniqueId; 82 87 property LinearBlend: boolean read GetLinearBlend write SetLinearBlend; //use linear blending unless specified 83 88 property DefaultBlendingOperation: TBlendOperation read GetDefaultBlendingOperation; … … 103 108 FLayers: array of TBGRALayerInfo; 104 109 FWidth,FHeight: integer; 105 function GetLayerUniqueId(layer: integer): integer;106 procedure SetLayerUniqueId(layer: integer; AValue: integer);107 110 108 111 protected … … 122 125 procedure SetLayerName(layer: integer; AValue: string); 123 126 procedure SetLayerFrozen(layer: integer; AValue: boolean); override; 124 function GetLayerBitmapDirectly(layer: integer): TBGRABitmap; override; 127 function GetLayerUniqueId(layer: integer): integer; override; 128 procedure SetLayerUniqueId(layer: integer; AValue: integer); 125 129 126 130 public 127 procedure LoadFromFile(const filename : string); override;131 procedure LoadFromFile(const filenameUTF8: string); override; 128 132 procedure LoadFromStream(stream: TStream); override; 129 133 procedure SetSize(AWidth, AHeight: integer); virtual; … … 155 159 function AddOwnedLayer(ABitmap: TBGRABitmap; BlendOp: TBlendOperation; Opacity: byte = 255): integer; overload; 156 160 destructor Destroy; override; 157 constructor Create; override; 158 constructor Create(AWidth, AHeight: integer); 161 constructor Create; override; overload; 162 constructor Create(AWidth, AHeight: integer); virtual; overload; 163 function GetLayerBitmapDirectly(layer: integer): TBGRABitmap; override; 159 164 function GetLayerBitmapCopy(layer: integer): TBGRABitmap; override; 160 165 function GetLayerIndexFromId(AIdentifier: integer): integer; 161 166 function Duplicate(ASharedLayerIds: boolean = false): TBGRALayeredBitmap; 167 function ProduceLayerUniqueId: integer; 162 168 163 169 procedure RotateCW; … … 180 186 end; 181 187 182 procedure RegisterLayeredBitmapWriter(AExtension: string; AWriter: TBGRALayeredBitmapClass); 183 procedure RegisterLayeredBitmapReader(AExtension: string; AReader: TBGRACustomLayeredBitmapClass); 188 procedure RegisterLayeredBitmapWriter(AExtensionUTF8: string; AWriter: TBGRALayeredBitmapClass); 189 procedure RegisterLayeredBitmapReader(AExtensionUTF8: string; AReader: TBGRACustomLayeredBitmapClass); 190 191 var 192 LayeredBitmapSaveToStreamProc : TBGRALayeredBitmapSaveToStreamProc; 193 LayeredBitmapLoadFromStreamProc : TBGRALayeredBitmapLoadFromStreamProc; 194 195 type 196 TOnLayeredBitmapLoadStartProc = procedure(AFilenameUTF8: string) of object; 197 TOnLayeredBitmapLoadProgressProc = procedure(APercentage: integer) of object; 198 TOnLayeredBitmapLoadedProc = procedure() of object; 199 200 procedure OnLayeredBitmapLoadFromStreamStart; 201 procedure OnLayeredBitmapLoadStart(AFilenameUTF8: string); 202 procedure OnLayeredBitmapLoadProgress(APercentage: integer); 203 procedure OnLayeredBitmapLoaded(); 204 procedure RegisterLoadingHandler(AStart: TOnLayeredBitmapLoadStartProc; AProgress: TOnLayeredBitmapLoadProgressProc; 205 ADone: TOnLayeredBitmapLoadedProc); 206 procedure UnregisterLoadingHandler(AStart: TOnLayeredBitmapLoadStartProc; AProgress: TOnLayeredBitmapLoadProgressProc; 207 ADone: TOnLayeredBitmapLoadedProc); 184 208 185 209 implementation 210 211 uses LCLProc; 212 213 var 214 OnLayeredBitmapLoadStartProc: TOnLayeredBitmapLoadStartProc; 215 OnLayeredBitmapLoadProgressProc: TOnLayeredBitmapLoadProgressProc; 216 OnLayeredBitmapLoadedProc: TOnLayeredBitmapLoadedProc; 186 217 187 218 var … … 380 411 end; 381 412 382 procedure TBGRALayeredBitmap.LoadFromFile(const filename : string);413 procedure TBGRALayeredBitmap.LoadFromFile(const filenameUTF8: string); 383 414 var bmp: TBGRABitmap; 384 415 index: integer; … … 387 418 i: integer; 388 419 begin 389 ext := lowercase(ExtractFileExt(filename));420 ext := UTF8LowerCase(ExtractFileExt(filenameUTF8)); 390 421 for i := 0 to high(LayeredBitmapReaders) do 391 422 if '.'+LayeredBitmapReaders[i].extension = ext then … … 393 424 temp := LayeredBitmapReaders[i].theClass.Create; 394 425 try 395 temp.LoadFromFile(filename );426 temp.LoadFromFile(filenameUTF8); 396 427 Assign(temp); 397 428 finally … … 401 432 end; 402 433 403 bmp := TBGRABitmap.Create(filename );434 bmp := TBGRABitmap.Create(filenameUTF8, True); 404 435 Clear; 405 436 SetSize(bmp.Width,bmp.Height); … … 411 442 var bmp: TBGRABitmap; 412 443 index: integer; 413 begin 444 temp: TBGRALayeredBitmap; 445 begin 446 if Assigned(LayeredBitmapLoadFromStreamProc) then 447 begin 448 temp := LayeredBitmapLoadFromStreamProc(Stream); 449 if temp <> nil then 450 begin 451 Assign(temp); 452 temp.Free; 453 exit; 454 end; 455 end; 414 456 bmp := TBGRABitmap.Create(stream); 415 457 Clear; … … 538 580 FLayers[FNbLayers].Visible := true; 539 581 FLayers[FNbLayers].Frozen := false; 540 FLayers[FNbLayers].UniqueId := InterLockedIncrement(NextLayerUniqueId);582 FLayers[FNbLayers].UniqueId := ProduceLayerUniqueId; 541 583 if Shared then 542 584 begin … … 694 736 result := TBGRALayeredBitmap.Create; 695 737 result.Assign(self, ASharedLayerIds); 738 end; 739 740 function TBGRALayeredBitmap.ProduceLayerUniqueId: integer; 741 begin 742 result := InterLockedIncrement(NextLayerUniqueId); 696 743 end; 697 744 … … 841 888 end; 842 889 result := false; 890 end; 891 892 function TBGRACustomLayeredBitmap.GetLayerUniqueId(layer: integer): integer; 893 begin 894 result := layer; 843 895 end; 844 896 … … 903 955 end; 904 956 905 procedure TBGRACustomLayeredBitmap.SaveToFile(const filename : string);957 procedure TBGRACustomLayeredBitmap.SaveToFile(const filenameUTF8: string); 906 958 var bmp: TBGRABitmap; 907 959 ext: string; … … 909 961 i: integer; 910 962 begin 911 ext := lowercase(ExtractFileExt(filename));963 ext := UTF8LowerCase(ExtractFileExt(filenameUTF8)); 912 964 for i := 0 to high(LayeredBitmapWriters) do 913 965 if '.'+LayeredBitmapWriters[i].extension = ext then … … 916 968 try 917 969 temp.Assign(self); 918 temp.SaveToFile(filename );970 temp.SaveToFile(filenameUTF8); 919 971 finally 920 972 temp.Free; … … 925 977 bmp := ComputeFlatImage; 926 978 try 927 bmp.SaveToFile (filename);979 bmp.SaveToFileUTF8(filenameUTF8); 928 980 finally 929 981 bmp.Free; … … 933 985 procedure TBGRACustomLayeredBitmap.SaveToStream(Stream: TStream); 934 986 begin 935 raise exception.Create('Not implemented'); 987 if Assigned(LayeredBitmapSaveToStreamProc) then 988 LayeredBitmapSaveToStreamProc(Stream, self) 989 else 990 raise exception.Create('Call BGRAStreamLayers.RegisterStreamLayers first'); 936 991 end; 937 992 … … 1188 1243 linear := false; //to avoid hint 1189 1244 for j := firstlayer to lastLayer do 1190 if (BlendOperation[j] in [boTransparent,boLinearBlend]) or (start = 0) then1245 if (BlendOperation[j] in [boTransparent,boLinearBlend]) or (start = 0) or ((firstlayer= 0) and (j=0)) then 1191 1246 begin 1192 1247 nextLinear := (BlendOperation[j] = boLinearBlend) or self.LinearBlend; … … 1239 1294 end; 1240 1295 1241 procedure RegisterLayeredBitmapReader(AExtension : string; AReader: TBGRACustomLayeredBitmapClass);1296 procedure RegisterLayeredBitmapReader(AExtensionUTF8: string; AReader: TBGRACustomLayeredBitmapClass); 1242 1297 begin 1243 1298 setlength(LayeredBitmapReaders,length(LayeredBitmapReaders)+1); 1244 1299 with LayeredBitmapReaders[high(LayeredBitmapReaders)] do 1245 1300 begin 1246 extension:= AExtension;1301 extension:= UTF8LowerCase(AExtensionUTF8); 1247 1302 theClass := AReader; 1248 1303 end; 1249 1304 end; 1250 1305 1251 procedure RegisterLayeredBitmapWriter(AExtension: string; AWriter: TBGRALayeredBitmapClass); 1252 begin 1306 procedure OnLayeredBitmapLoadFromStreamStart; 1307 begin 1308 OnLayeredBitmapLoadStart('<Stream>'); 1309 end; 1310 1311 procedure OnLayeredBitmapLoadStart(AFilenameUTF8: string); 1312 begin 1313 if Assigned(OnLayeredBitmapLoadStartProc) then 1314 OnLayeredBitmapLoadStartProc(AFilenameUTF8); 1315 end; 1316 1317 procedure OnLayeredBitmapLoadProgress(APercentage: integer); 1318 begin 1319 if Assigned(OnLayeredBitmapLoadProgressProc) then 1320 OnLayeredBitmapLoadProgressProc(APercentage); 1321 end; 1322 1323 procedure OnLayeredBitmapLoaded; 1324 begin 1325 if Assigned(OnLayeredBitmapLoadedProc) then 1326 OnLayeredBitmapLoadedProc(); 1327 end; 1328 1329 procedure RegisterLoadingHandler(AStart: TOnLayeredBitmapLoadStartProc; 1330 AProgress: TOnLayeredBitmapLoadProgressProc; ADone: TOnLayeredBitmapLoadedProc 1331 ); 1332 begin 1333 OnLayeredBitmapLoadProgressProc:= AProgress; 1334 OnLayeredBitmapLoadStartProc := AStart; 1335 OnLayeredBitmapLoadedProc:= ADone; 1336 end; 1337 1338 procedure UnregisterLoadingHandler(AStart: TOnLayeredBitmapLoadStartProc; 1339 AProgress: TOnLayeredBitmapLoadProgressProc; ADone: TOnLayeredBitmapLoadedProc); 1340 begin 1341 if OnLayeredBitmapLoadProgressProc = AProgress then OnLayeredBitmapLoadProgressProc := nil; 1342 if OnLayeredBitmapLoadStartProc = AStart then OnLayeredBitmapLoadStartProc := nil; 1343 if OnLayeredBitmapLoadedProc = ADone then OnLayeredBitmapLoadedProc := nil; 1344 end; 1345 1346 procedure RegisterLayeredBitmapWriter(AExtensionUTF8: string; AWriter: TBGRALayeredBitmapClass); 1347 begin 1348 while (length(AExtensionUTF8)>0) and (AExtensionUTF8[1]='.') do delete(AExtensionUTF8,1,1); 1253 1349 setlength(LayeredBitmapWriters,length(LayeredBitmapWriters)+1); 1254 1350 with LayeredBitmapWriters[high(LayeredBitmapWriters)] do 1255 1351 begin 1256 extension:= AExtension;1352 extension:= UTF8LowerCase(AExtensionUTF8); 1257 1353 theClass := AWriter; 1258 1354 end;
Note:
See TracChangeset
for help on using the changeset viewer.