source: trunk/Packages/bgrabitmap/bgralazpaint.pas

Last change on this file was 2, checked in by chronos, 5 years ago
File size: 6.2 KB
Line 
1unit BGRALazPaint;
2
3{$mode objfpc}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, BGRALayers, BGRABitmapTypes, BGRAReadLzp, BGRAWriteLzp,
9 BGRALzpCommon, FPimage;
10
11type
12 TLzpCompression = BGRALzpCommon.TLzpCompression;
13
14 { TBGRALazPaintImage }
15
16 TBGRALazPaintImage = class(TBGRALayeredBitmap)
17 private
18 FSelectedLayerIndex: integer;
19 public
20 constructor Create; overload; override;
21 constructor Create(AWidth, AHeight: integer); overload; override;
22 procedure LoadFromStream(AStream: TStream); override;
23 procedure LoadFromFile(const filenameUTF8: string); override;
24 procedure SaveToFile(const filenameUTF8: string); override;
25 procedure SaveToStream(AStream: TStream); override;
26 property SelectedLayerIndex: integer read FSelectedLayerIndex write FSelectedLayerIndex;
27 end;
28
29 { TBGRAWriterLazPaintWithLayers }
30
31 TBGRAWriterLazPaintWithLayers = class(TBGRAWriterLazPaint)
32 protected
33 FLayers: TBGRALayeredBitmap;
34 FSelectedLayerIndex: integer;
35 FCompression: TLzpCompression;
36 function GetNbLayers: integer; override;
37 function InternalWriteLayers(Str: TStream; {%H-}Img: TFPCustomImage): boolean; override;
38 public
39 constructor Create(ALayers: TBGRALayeredBitmap); overload;
40 property SelectedLayerIndex: integer read FSelectedLayerIndex write FSelectedLayerIndex;
41 property Compression: TLzpCompression read FCompression write FCompression;
42 end;
43
44 { TBGRAReaderLazPaintWithLayers }
45
46 TBGRAReaderLazPaintWithLayers = class(TBGRAReaderLazPaint)
47 protected
48 FLayers: TBGRALayeredBitmap;
49 FLayersLoaded: boolean;
50 FSelectedLayerIndex: integer;
51 procedure InternalReadLayers(str: TStream; {%H-}Img: TFPCustomImage); override;
52 public
53 constructor Create(ALayers: TBGRALayeredBitmap); overload;
54 property LayersLoaded: boolean read FLayersLoaded;
55 property SelectedLayerIndex: integer read FSelectedLayerIndex;
56 end;
57
58procedure RegisterLazPaintFormat;
59
60implementation
61
62uses BGRAStreamLayers, BGRABitmap, BGRAUTF8;
63
64{ TBGRALazPaintImage }
65
66constructor TBGRALazPaintImage.Create;
67begin
68 inherited Create;
69 RegisterLazPaintFormat;
70 FSelectedLayerIndex:= 0;
71end;
72
73constructor TBGRALazPaintImage.Create(AWidth, AHeight: integer);
74begin
75 inherited Create(AWidth, AHeight);
76 RegisterLazPaintFormat;
77 FSelectedLayerIndex:= 0;
78end;
79
80procedure TBGRALazPaintImage.LoadFromStream(AStream: TStream);
81var
82 {%H-}header: TLazPaintImageHeader;
83 bmp: TBGRACustomBitmap;
84 reader: TBGRAReaderLazPaintWithLayers;
85begin
86 AStream.ReadBuffer({%H-}header, sizeof(header));
87 LazPaintImageHeader_SwapEndianIfNeeded(header);
88 AStream.Position:= AStream.Position-sizeof(header);
89
90 //use shortcut if possible
91 if (header.magic = LAZPAINT_MAGIC_HEADER) and (header.zero1 = 0)
92 and (header.layersOffset >= sizeof(header)) then
93 begin
94 AStream.Position:= AStream.Position+header.layersOffset;
95 LoadLayersFromStream(AStream, FSelectedLayerIndex, false, self);
96 end else
97 begin
98 reader := TBGRAReaderLazPaintWithLayers.Create(self);
99 try
100 bmp := BGRABitmapFactory.Create;
101 bmp.LoadFromStream(AStream, reader);
102 if reader.LayersLoaded then
103 begin
104 bmp.Free;
105 end else
106 begin
107 Clear;
108 SetSize(bmp.Width,bmp.Height);
109 AddOwnedLayer(bmp as TBGRABitmap);
110 LayerName[0] := reader.Caption;
111 end;
112 SelectedLayerIndex:= reader.SelectedLayerIndex;
113 finally
114 reader.Free;
115 end;
116 end;
117end;
118
119procedure TBGRALazPaintImage.LoadFromFile(const filenameUTF8: string);
120var AStream: TFileStreamUTF8;
121begin
122 AStream := TFileStreamUTF8.Create(filenameUTF8,fmOpenRead or fmShareDenyWrite);
123 try
124 LoadFromStream(AStream);
125 finally
126 AStream.Free;
127 end;
128end;
129
130procedure TBGRALazPaintImage.SaveToFile(const filenameUTF8: string);
131var AStream: TFileStreamUTF8;
132begin
133 AStream := TFileStreamUTF8.Create(filenameUTF8,fmCreate or fmShareDenyWrite);
134 try
135 SaveToStream(AStream);
136 finally
137 AStream.Free;
138 end;
139end;
140
141procedure TBGRALazPaintImage.SaveToStream(AStream: TStream);
142var
143 writer: TBGRAWriterLazPaint;
144 flat: TBGRACustomBitmap;
145begin
146 if NbLayers = 0 then
147 raise exception.Create('File cannot be empty');
148
149 writer := nil;
150 flat := nil;
151 try
152 if (NbLayers > 1) or (LayerOpacity[0] <> 255) or not LayerVisible[0] or (BlendOperation[0]<>boTransparent)
153 or (OriginalCount <> 0) then
154 begin
155 writer := TBGRAWriterLazPaintWithLayers.Create(self);
156 writer.Caption := 'Preview';
157 TBGRAWriterLazPaintWithLayers(writer).SelectedLayerIndex := self.SelectedLayerIndex;
158 end else
159 begin
160 writer := TBGRAWriterLazPaint.Create;
161 writer.Caption := LayerName[0];
162 end;
163
164 writer.IncludeThumbnail:= true;
165 flat := ComputeFlatImage;
166 flat.SaveToStream(AStream, writer);
167 finally
168 writer.Free;
169 flat.Free;
170 end;
171end;
172
173{ TBGRAReaderLazPaintWithLayers }
174
175procedure TBGRAReaderLazPaintWithLayers.InternalReadLayers(str: TStream;
176 Img: TFPCustomImage);
177begin
178 if Assigned(FLayers) then
179 begin
180 if CheckStreamForLayers(str) then
181 begin
182 LoadLayersFromStream(str, FSelectedLayerIndex, false, FLayers);
183 FLayersLoaded := true;
184 end;
185 end;
186end;
187
188constructor TBGRAReaderLazPaintWithLayers.Create(ALayers: TBGRALayeredBitmap);
189begin
190 FLayersLoaded := false;
191 FLayers := ALayers;
192 FSelectedLayerIndex:= -1;
193end;
194
195{ TBGRAWriterLazPaintWithLayers }
196
197function TBGRAWriterLazPaintWithLayers.GetNbLayers: integer;
198begin
199 if Assigned(FLayers) then
200 Result:= FLayers.NbLayers
201 else
202 Result := 1;
203end;
204
205function TBGRAWriterLazPaintWithLayers.InternalWriteLayers(Str: TStream;
206 Img: TFPCustomImage): boolean;
207begin
208 If Assigned(FLayers) then
209 begin
210 SaveLayersToStream(str, FLayers, FSelectedLayerIndex, FCompression);
211 Result:=true;
212 end
213 else result := False;
214end;
215
216constructor TBGRAWriterLazPaintWithLayers.Create(ALayers: TBGRALayeredBitmap);
217begin
218 inherited Create;
219 FLayers := ALayers;
220 FSelectedLayerIndex:= 0;
221 FCompression:= lzpRLE;
222 IncludeThumbnail:= true;
223end;
224
225var AlreadyRegistered: boolean;
226
227procedure RegisterLazPaintFormat;
228begin
229 if AlreadyRegistered then exit;
230 RegisterLayeredBitmapReader('lzp', TBGRALazPaintImage);
231 RegisterLayeredBitmapWriter('lzp', TBGRALazPaintImage);
232 AlreadyRegistered:= True;
233end;
234
235end.
236
Note: See TracBrowser for help on using the repository browser.