-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathzBitmap.pas
More file actions
202 lines (184 loc) · 5.06 KB
/
zBitmap.pas
File metadata and controls
202 lines (184 loc) · 5.06 KB
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
//------------------------------------------------------------------------------
//
// DD_MODEL: DelphiDOOM Procedural Model Editor
// Copyright (C) 2017-2021 by Jim Valavanis
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// 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. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// DESCRIPTION:
// zBitmap image
//
//------------------------------------------------------------------------------
// E-Mail: jimmyvalavanis@yahoo.gr
// Site : https://sourceforge.net/projects/delphidoom-procedural-modeler/
//------------------------------------------------------------------------------
unit zBitmap;
{$P+,S-,W-,R-,T-,X+,H+}
{$C PRELOAD}
interface
uses
Windows, Forms, SysUtils, Classes, Graphics, Math, zLibPas;
type
TZBitmap = class(TBitmap)
private
procedure WriteZStreamData(Stream: TStream);
procedure ReadZStreamData(Stream: TStream);
protected
procedure WriteData(Stream: TStream); override;
procedure ReadData(Stream: TStream); override;
public
CompressionLevel: TCompressionLevel;
constructor Create; override;
procedure SaveToStream(Stream: TStream); override;
procedure LoadFromStream(Stream: TStream); override;
end;
implementation
{ TZBitmap }
constructor TZBitmap.Create;
begin
Inherited;
CompressionLevel := clDefault;
end;
procedure TZBitmap.WriteData(Stream: TStream);
begin
WriteZStreamData(Stream);
end;
procedure TZBitmap.SaveToStream(Stream: TStream);
begin
WriteZStreamData(Stream);
end;
procedure TZBitmap.LoadFromStream(Stream: TStream);
begin
ReadZStreamData(Stream);
end;
procedure TZBitmap.ReadData(Stream: TStream);
begin
ReadZStreamData(Stream);
end;
procedure TZBitmap.ReadZStreamData(Stream: TStream);
var
aBitmap : TBitmap;
f : TDecompressionStream;
m : TMemoryStream;
buf : Array [1..4096] of byte;
size, numread : longint;
pos: longint;
i,j: integer;
P,P1: PByteArray;
begin
pos := Stream.Position;
Stream.Seek(Stream.Size - SizeOf(size), soFromBeginning);
Stream.Read(size, SizeOf(size));
Stream.Seek(pos, soFromBeginning);
m := TMemoryStream.Create;
f := TDecompressionStream.Create(Stream);
try
while f.Position < size do
begin
numread := f.Read(buf, Min(SizeOf(buf), size - f.Position ));
m.Write(buf, numRead);
end;
aBitmap := TBitmap.Create;
m.Seek(0, soFromBeginning);
aBitmap.LoadFromStream(m);
Assign(aBitmap);
aBitmap.Free;
if PixelFormat = pf8bit then
begin
for j := 0 to Height - 1 do
begin
P := Scanline[j];
P1 := aBitmap.Scanline[j];
for i := 1 to Width - 1 do
P[i] := P[i-1] + P1[i];
end
end;
if PixelFormat = pf24bit then
begin
for j := 0 to Height - 1 do
begin
P := Scanline[j];
P1 := aBitmap.Scanline[j];
for i := 1 to Width - 1 do
begin
P[i*3] := P[i*3-3] + P1[i*3];
P[i*3+1] := P[i*3-2] + P1[i*3+1];
P[i*3+2] := P[i*3-1] + P1[i*3+2];
end;
end
end;
finally
f.Free;
m.free;
end;
end;
procedure TZBitmap.WriteZStreamData(Stream: TStream);
var
aBitmap: TBitmap;
f: TCompressionStream;
size: Longint;
i,j: integer;
P,P1: PByteArray;
begin
aBitmap := TBitmap.Create;
f := TCompressionStream.Create(CompressionLevel, Stream);
try
if not (PixelFormat in [pf1bit, pf4bit, pf8bit, pfCustom]) then
PixelFormat := pf24bit;
aBitmap.Assign(self);
if PixelFormat = pf8bit then
begin
for j := 0 to Height - 1 do
begin
P := Scanline[j];
P1 := aBitmap.Scanline[j];
for i := 1 to Width - 1 do
P1[i] := P[i] - P[i-1];
end
end
else if PixelFormat = pf24bit then
begin
for j := 0 to Height - 1 do
begin
P := Scanline[j];
P1 := aBitmap.Scanline[j];
for i := 1 to Width - 1 do
begin
P1[i*3] := P[i*3] - P[i*3-3];
P1[i*3+1] := P[i*3+1] - P[i*3-2];
P1[i*3+2] := P[i*3+2] - P[i*3-1];
end;
end
end;
aBitmap.SaveToStream(f);
// At f.Position is the uncompressed file size
size := f.Position;
finally
aBitmap.Free;
f.Free;
end;
Stream.Seek(0, soFromEnd);
Stream.Write(size, SizeOf(size));
end;
initialization
{ Register the TZBitmap as a new graphic file format
now all the TPicture storage stuff can access our new
Zipped graphic format !
}
TPicture.RegisterFileFormat('BMZ','zBitmap', TZBitmap);
finalization
TPicture.UnregisterGraphicClass(TZBitmap);
end.