forked from edivando-fpc/BGRABitmap
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bgralazpaint.pas
270 lines (228 loc) · 8.14 KB
/
bgralazpaint.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
{ ***************************************************************************
* *
* This file is part of BGRABitmap library which is distributed under the *
* modified LGPL. *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
************************* BGRABitmap library ******************************
- Drawing routines with transparency and antialiasing with Lazarus.
Offers also various transforms.
- These routines allow to manipulate 32bit images in BGRA format or RGBA
format (depending on the platform).
- This code is under modified LGPL (see COPYING.modifiedLGPL.txt).
This means that you can link this library inside your programs for any purpose.
Only the included part of the code must remain LGPL.
- If you make some improvements to this library, please notify here:
http://www.lazarus.freepascal.org/index.php/topic,12037.0.html
********************* Contact : Circular at operamail.com *******************
******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | mailedivando@gmail.com
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGRALazPaint;
{$i bgrabitmap.inc}{$H+}
interface
uses
Classes, SysUtils, BGRATypes, {$IFNDEF FPC}Types, GraphType, BGRAGraphics,{$ENDIF} BGRALayers, BGRABitmapTypes, BGRAReadLzp, BGRAWriteLzp,
{$IFDEF FPC} BGRALzpCommon,{$ENDIF} FPImage;
type
TLzpCompression = BGRALzpCommon.TLzpCompression;
{ TBGRALazPaintImage }
TBGRALazPaintImage = class(TBGRALayeredBitmap)
private
FSelectedLayerIndex: integer;
public
constructor Create; overload; override;
constructor Create(AWidth, AHeight: integer); overload; override;
procedure LoadFromStream(AStream: TStream); override;
procedure LoadFromFile(const filenameUTF8: string); override;
procedure SaveToFile(const filenameUTF8: string); override;
procedure SaveToStream(AStream: TStream); override;
property SelectedLayerIndex: integer read FSelectedLayerIndex write FSelectedLayerIndex;
end;
{ TBGRAWriterLazPaintWithLayers }
TBGRAWriterLazPaintWithLayers = class(TBGRAWriterLazPaint)
protected
FLayers: TBGRALayeredBitmap;
FSelectedLayerIndex: integer;
FCompression: TLzpCompression;
function GetNbLayers: integer; override;
function InternalWriteLayers(Str: TStream; {%H-}Img: TFPCustomImage): boolean; override;
public
constructor Create(ALayers: TBGRALayeredBitmap); overload;
property SelectedLayerIndex: integer read FSelectedLayerIndex write FSelectedLayerIndex;
property Compression: TLzpCompression read FCompression write FCompression;
end;
{ TBGRAReaderLazPaintWithLayers }
TBGRAReaderLazPaintWithLayers = class(TBGRAReaderLazPaint)
protected
FLayers: TBGRALayeredBitmap;
FLayersLoaded: boolean;
FSelectedLayerIndex: integer;
procedure InternalReadLayers(str: TStream; {%H-}Img: TFPCustomImage); override;
public
constructor Create(ALayers: TBGRALayeredBitmap); overload;
property LayersLoaded: boolean read FLayersLoaded;
property SelectedLayerIndex: integer read FSelectedLayerIndex;
end;
procedure RegisterLazPaintFormat;
implementation
uses BGRAStreamLayers, BGRABitmap, BGRAUTF8;
{ TBGRALazPaintImage }
constructor TBGRALazPaintImage.Create;
begin
inherited Create;
RegisterLazPaintFormat;
FSelectedLayerIndex:= 0;
end;
constructor TBGRALazPaintImage.Create(AWidth, AHeight: integer);
begin
inherited Create(AWidth, AHeight);
RegisterLazPaintFormat;
FSelectedLayerIndex:= 0;
end;
procedure TBGRALazPaintImage.LoadFromStream(AStream: TStream);
var
{%H-}header: TLazPaintImageHeader;
bmp: TBGRACustomBitmap;
reader: TBGRAReaderLazPaintWithLayers;
begin
AStream.ReadBuffer({%H-}header, sizeof(header));
LazPaintImageHeader_SwapEndianIfNeeded(header);
AStream.Position:= AStream.Position-sizeof(header);
//use shortcut if possible
if (header.magic = LAZPAINT_MAGIC_HEADER) and (header.zero1 = 0)
and (header.layersOffset >= sizeof(header)) then
begin
AStream.Position:= AStream.Position+header.layersOffset;
LoadLayersFromStream(AStream, FSelectedLayerIndex, false, self);
end else
begin
reader := TBGRAReaderLazPaintWithLayers.Create(self);
try
bmp := BGRABitmapFactory.Create;
bmp.LoadFromStream(AStream, reader);
if reader.LayersLoaded then
begin
bmp.Free;
end else
begin
Clear;
SetSize(bmp.Width,bmp.Height);
AddOwnedLayer(bmp as TBGRABitmap);
LayerName[0] := reader.Caption;
end;
SelectedLayerIndex:= reader.SelectedLayerIndex;
finally
reader.Free;
end;
end;
end;
procedure TBGRALazPaintImage.LoadFromFile(const filenameUTF8: string);
var AStream: TFileStreamUTF8;
begin
AStream := TFileStreamUTF8.Create(filenameUTF8,fmOpenRead or fmShareDenyWrite);
try
LoadFromStream(AStream);
finally
AStream.Free;
end;
end;
procedure TBGRALazPaintImage.SaveToFile(const filenameUTF8: string);
var AStream: TFileStreamUTF8;
begin
AStream := TFileStreamUTF8.Create(filenameUTF8,fmCreate or fmShareDenyWrite);
try
SaveToStream(AStream);
finally
AStream.Free;
end;
end;
procedure TBGRALazPaintImage.SaveToStream(AStream: TStream);
var
writer: TBGRAWriterLazPaint;
flat: TBGRACustomBitmap;
begin
if NbLayers = 0 then
raise exception.Create('File cannot be empty');
writer := nil;
flat := nil;
try
if (NbLayers > 1) or (LayerOpacity[0] <> 255) or not LayerVisible[0] or (BlendOperation[0]<>boTransparent) then
begin
writer := TBGRAWriterLazPaintWithLayers.Create(self);
writer.Caption := 'Preview';
TBGRAWriterLazPaintWithLayers(writer).SelectedLayerIndex := self.SelectedLayerIndex;
end else
begin
writer := TBGRAWriterLazPaint.Create;
writer.Caption := LayerName[0];
end;
writer.IncludeThumbnail:= true;
flat := ComputeFlatImage;
flat.SaveToStream(AStream, writer);
finally
writer.Free;
flat.Free;
end;
end;
{ TBGRAReaderLazPaintWithLayers }
procedure TBGRAReaderLazPaintWithLayers.InternalReadLayers(str: TStream;
Img: TFPCustomImage);
begin
if Assigned(FLayers) then
begin
if CheckStreamForLayers(str) then
begin
LoadLayersFromStream(str, FSelectedLayerIndex, false, FLayers);
FLayersLoaded := true;
end;
end;
end;
constructor TBGRAReaderLazPaintWithLayers.Create(ALayers: TBGRALayeredBitmap);
begin
FLayersLoaded := false;
FLayers := ALayers;
FSelectedLayerIndex:= -1;
end;
{ TBGRAWriterLazPaintWithLayers }
function TBGRAWriterLazPaintWithLayers.GetNbLayers: integer;
begin
if Assigned(FLayers) then
Result:= FLayers.NbLayers
else
Result := 1;
end;
function TBGRAWriterLazPaintWithLayers.InternalWriteLayers(Str: TStream;
Img: TFPCustomImage): boolean;
begin
If Assigned(FLayers) then
begin
SaveLayersToStream(str, FLayers, FSelectedLayerIndex, FCompression);
Result:=true;
end
else result := False;
end;
constructor TBGRAWriterLazPaintWithLayers.Create(ALayers: TBGRALayeredBitmap);
begin
inherited Create;
FLayers := ALayers;
FSelectedLayerIndex:= 0;
FCompression:= lzpRLE;
IncludeThumbnail:= true;
end;
var AlreadyRegistered: boolean;
procedure RegisterLazPaintFormat;
begin
if AlreadyRegistered then exit;
RegisterLayeredBitmapReader('lzp', TBGRALazPaintImage);
RegisterLayeredBitmapWriter('lzp', TBGRALazPaintImage);
AlreadyRegistered:= True;
end;
end.