1 | unit BGRALazPaint;
|
---|
2 |
|
---|
3 | {$mode objfpc}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, BGRALayers, BGRABitmapTypes, BGRAReadLzp, BGRAWriteLzp,
|
---|
9 | BGRALzpCommon, FPimage;
|
---|
10 |
|
---|
11 | type
|
---|
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 |
|
---|
58 | procedure RegisterLazPaintFormat;
|
---|
59 |
|
---|
60 | implementation
|
---|
61 |
|
---|
62 | uses BGRAStreamLayers, BGRABitmap, BGRAUTF8;
|
---|
63 |
|
---|
64 | { TBGRALazPaintImage }
|
---|
65 |
|
---|
66 | constructor TBGRALazPaintImage.Create;
|
---|
67 | begin
|
---|
68 | inherited Create;
|
---|
69 | RegisterLazPaintFormat;
|
---|
70 | FSelectedLayerIndex:= 0;
|
---|
71 | end;
|
---|
72 |
|
---|
73 | constructor TBGRALazPaintImage.Create(AWidth, AHeight: integer);
|
---|
74 | begin
|
---|
75 | inherited Create(AWidth, AHeight);
|
---|
76 | RegisterLazPaintFormat;
|
---|
77 | FSelectedLayerIndex:= 0;
|
---|
78 | end;
|
---|
79 |
|
---|
80 | procedure TBGRALazPaintImage.LoadFromStream(AStream: TStream);
|
---|
81 | var
|
---|
82 | {%H-}header: TLazPaintImageHeader;
|
---|
83 | bmp: TBGRACustomBitmap;
|
---|
84 | reader: TBGRAReaderLazPaintWithLayers;
|
---|
85 | begin
|
---|
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;
|
---|
117 | end;
|
---|
118 |
|
---|
119 | procedure TBGRALazPaintImage.LoadFromFile(const filenameUTF8: string);
|
---|
120 | var AStream: TFileStreamUTF8;
|
---|
121 | begin
|
---|
122 | AStream := TFileStreamUTF8.Create(filenameUTF8,fmOpenRead or fmShareDenyWrite);
|
---|
123 | try
|
---|
124 | LoadFromStream(AStream);
|
---|
125 | finally
|
---|
126 | AStream.Free;
|
---|
127 | end;
|
---|
128 | end;
|
---|
129 |
|
---|
130 | procedure TBGRALazPaintImage.SaveToFile(const filenameUTF8: string);
|
---|
131 | var AStream: TFileStreamUTF8;
|
---|
132 | begin
|
---|
133 | AStream := TFileStreamUTF8.Create(filenameUTF8,fmCreate or fmShareDenyWrite);
|
---|
134 | try
|
---|
135 | SaveToStream(AStream);
|
---|
136 | finally
|
---|
137 | AStream.Free;
|
---|
138 | end;
|
---|
139 | end;
|
---|
140 |
|
---|
141 | procedure TBGRALazPaintImage.SaveToStream(AStream: TStream);
|
---|
142 | var
|
---|
143 | writer: TBGRAWriterLazPaint;
|
---|
144 | flat: TBGRACustomBitmap;
|
---|
145 | begin
|
---|
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;
|
---|
171 | end;
|
---|
172 |
|
---|
173 | { TBGRAReaderLazPaintWithLayers }
|
---|
174 |
|
---|
175 | procedure TBGRAReaderLazPaintWithLayers.InternalReadLayers(str: TStream;
|
---|
176 | Img: TFPCustomImage);
|
---|
177 | begin
|
---|
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;
|
---|
186 | end;
|
---|
187 |
|
---|
188 | constructor TBGRAReaderLazPaintWithLayers.Create(ALayers: TBGRALayeredBitmap);
|
---|
189 | begin
|
---|
190 | FLayersLoaded := false;
|
---|
191 | FLayers := ALayers;
|
---|
192 | FSelectedLayerIndex:= -1;
|
---|
193 | end;
|
---|
194 |
|
---|
195 | { TBGRAWriterLazPaintWithLayers }
|
---|
196 |
|
---|
197 | function TBGRAWriterLazPaintWithLayers.GetNbLayers: integer;
|
---|
198 | begin
|
---|
199 | if Assigned(FLayers) then
|
---|
200 | Result:= FLayers.NbLayers
|
---|
201 | else
|
---|
202 | Result := 1;
|
---|
203 | end;
|
---|
204 |
|
---|
205 | function TBGRAWriterLazPaintWithLayers.InternalWriteLayers(Str: TStream;
|
---|
206 | Img: TFPCustomImage): boolean;
|
---|
207 | begin
|
---|
208 | If Assigned(FLayers) then
|
---|
209 | begin
|
---|
210 | SaveLayersToStream(str, FLayers, FSelectedLayerIndex, FCompression);
|
---|
211 | Result:=true;
|
---|
212 | end
|
---|
213 | else result := False;
|
---|
214 | end;
|
---|
215 |
|
---|
216 | constructor TBGRAWriterLazPaintWithLayers.Create(ALayers: TBGRALayeredBitmap);
|
---|
217 | begin
|
---|
218 | inherited Create;
|
---|
219 | FLayers := ALayers;
|
---|
220 | FSelectedLayerIndex:= 0;
|
---|
221 | FCompression:= lzpRLE;
|
---|
222 | IncludeThumbnail:= true;
|
---|
223 | end;
|
---|
224 |
|
---|
225 | var AlreadyRegistered: boolean;
|
---|
226 |
|
---|
227 | procedure RegisterLazPaintFormat;
|
---|
228 | begin
|
---|
229 | if AlreadyRegistered then exit;
|
---|
230 | RegisterLayeredBitmapReader('lzp', TBGRALazPaintImage);
|
---|
231 | RegisterLayeredBitmapWriter('lzp', TBGRALazPaintImage);
|
---|
232 | AlreadyRegistered:= True;
|
---|
233 | end;
|
---|
234 |
|
---|
235 | end.
|
---|
236 |
|
---|