-
Notifications
You must be signed in to change notification settings - Fork 18
/
cGraphLoader.RainDrop.pas
338 lines (250 loc) · 7.45 KB
/
cGraphLoader.RainDrop.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
UNIT cGraphLoader.RainDrop;
{=============================================================================================================
Gabriel Moraru
2023.08.05
See Copyright.txt
--------------------------------------------------------------------------------------------------------------
Reads/writes files in RainDrop format (own format).
It saves itself to disk to a binaray file that contains:
* The original image as a JPG stream
* A 2D array of bits (TPixelMap)
See editor:
cGraphLoader.RainEditor.pas
c:\Myprojects\BIONIX\Projects\Project - Effects RainDrop\RainDropEditor\RainDropEditor.dpr
-----------------------------------------------------------------------------------------------------------------------}
INTERFACE
USES
System.SysUtils, System.Classes, vcl.Graphics, Vcl.Imaging.jpeg, ccStreamBuff;
TYPE
TPixelMap= array of array of Boolean; { Map of bits. True bits correspond to a pink pixel (where we draw water) }
CONST
RainDropExt = '.RainDrop';
RainDropFlt = 'RainDrop animations|*.RainDrop';
RainDropEditor= 'RainDropEditor.exe';
RainMagicNo = 'Shelter';
TYPE
TWaterDamping = 1..99;
PRaindropParams= ^RRaindropParams;
RRaindropParams= record
private
FDamping: TWaterDamping;
procedure SetDamping(Value: TWaterDamping);
public
TargetFPS : Integer;
WaveAplitude : Integer;
WaveTravelDist: Integer;
DropInterval : Integer;
property Damping: TWaterDamping read FDamping write SetDamping;
procedure Reset;
procedure Load(Stream: TCubicBuffStream);
procedure Save(Stream: TCubicBuffStream);
end;
TRainShelter = class(TObject)
private
procedure Clear;
public
Params : RRaindropParams;
OrigImage : TBitmap;
FileName : string;
PixelMap : TPixelMap;
function ImportImage (aFileName: string): Boolean;
function LoadFromFile(aFileName: string): Boolean;
procedure SaveToFile (aFileName: string; Mask: TBitmap);
procedure Save(Mask: TBitmap);
destructor Destroy; override;
end;
function IsRainDrop (CONST FileName: string): Boolean;
function DetectRainShelter(CONST FileName: string): Boolean;
function LoadRainShelter (CONST FileName: string): TBitmap;
IMPLEMENTATION
USES
cGraphConvert, cGraphLoader;
CONST
Verification= $50505050;
function DetectRainShelter(CONST FileName: string): Boolean; { Unused }
VAR
Stream: TCubicBuffStream;
begin
Stream:= TCubicBuffStream.CreateRead(FileName);
TRY
Result:= Stream.ReadHeadertry(RainMagicNo, 1);
FINALLY
FreeAndNil(Stream);
END;
end;
function IsRainDrop(CONST FileName: string): Boolean;
VAR sExtension: string;
begin
sExtension:= ExtractFileExt(FileName);
Result:= SameText(sExtension, RainDropExt);
end;
function LoadRainShelter(CONST FileName: string): TBitmap;
VAR
Obj: TRainShelter;
begin
Result:= TBitmap.Create;
Obj:= TRainShelter.Create;
TRY
Obj.LoadFromFile(FileName);
Result.Assign(Obj.OrigImage);
FINALLY
FreeAndNil(Obj);
END;
end;
destructor TRainShelter.Destroy;
begin
Clear;
inherited;
end;
procedure TRainShelter.Clear;
begin
FileName:= '';
FreeAndNil(OrigImage);
end;
function TRainShelter.ImportImage(aFileName: string): Boolean;
begin
Clear;
FileName:= aFileName;
OrigImage:= cGraphLoader.LoadGraph(aFileName, TRUE, TRUE);
Result:= OrigImage <> NIL;
end;
procedure TRainShelter.Save(Mask: TBitmap);
begin
if NOT IsRainDrop(FileName)
then SaveToFile(ChangeFileExt(FileName, RainDropExt), Mask)
else SaveToFile(FileName, Mask);
end;
procedure TRainShelter.SaveToFile(aFileName: string; Mask: TBitmap);
VAR
x, y, w, h: Integer;
b: Boolean;
Stream: TCubicBuffStream;
begin
Assert(Mask <> NIL);
Assert(OrigImage <> NIL);
FileName:= aFileName;
{ Prepare output stream }
Stream:= TCubicBuffStream.CreateWrite(aFileName);
TRY
Stream.WriteHeader(RainMagicNo, 1);
{ Write img resolution }
w:= OrigImage.Width;
h:= OrigImage.Height;
Stream.WriteCardinal(w);
Stream.WriteCardinal(h);
Params.Save(Stream);
Stream.WritePadding(940);
{ Write original image to the output stream }
VAR JpgStream:= cGraphConvert.Bmp2JpgStream(OrigImage);
TRY
JpgStream.Position:= 0;
{ Write size and JPG }
VAR Count:= JpgStream.Size;
Stream.WriteInteger(Count); // Write the size of the original input image
Stream.CopyFrom(JpgStream, Count); // Write the whole JPG stream
FINALLY
FreeAndNil(JpgStream);
END;
{ Write extras }
Stream.WriteCardinal(Verification); // Verification
{ Write pink pixels }
for x := 0 to w-1 do
for y := 0 to h-1 do
begin
b:= Mask.Canvas.Pixels[x, y]= clFuchsia;
Stream.WriteBoolean(b);
end;
FINALLY
FreeAndNil(Stream);
END;
end;
function TRainShelter.LoadFromFile(aFileName: string): Boolean;
VAR
Count: Integer;
Version: Word;
x, y: Integer;
w, h: Integer;
JpegImg: TJpegImage;
Stream: TCubicBuffStream;
begin
Result:= FALSE;
Clear;
FileName:= aFileName;
{ Open inp stream }
Stream:= TCubicBuffStream.CreateRead(FileName);
TRY
Version:= Stream.ReadHeader(RainMagicNo);
case Version of
0: Result:= FALSE; //'Invalid magic number!'
1: begin
{ Read size }
w:= Stream.ReadCardinal;
h:= Stream.ReadCardinal;
Params.Load(Stream);
Stream.ReadPadding(940);
Count:= Stream.ReadInteger;
OrigImage:= TBitmap.Create;
VAR JpgStream:= TMemoryStream.Create;
JpegImg:= TJpegImage.Create;
TRY
//Assert(Count = 111791);
JpgStream.CopyFrom(Stream, Count);
JpgStream.Position := 0;
JpegImg.LoadFromStream(JpgStream);
//JpgStream.SaveToFile(AppData.CurFolder+ '1.jpg');
OrigImage.Assign(JpegImg); { it must be a bitmap otherwise we get "Can only modify an image if it contains a bitmap": https://stackoverflow.com/questions/35703863/why-cant-i-draw-on-my-images-canvas }
if (w <> OrigImage.Width) OR (h <> OrigImage.Height)
then RAISE Exception.Create('Invalid BMP size!');
FINALLY
FreeAndNil(JpegImg);
FreeAndNil(JpgStream);
END;
// Verification
if Stream.ReadCardinal <> Verification
then RAISE Exception.Create('Invalid verification!');
{ Read pink pixels }
SetLength(PixelMap, w, h);
for x := 0 to w-1 do
for y := 0 to h-1 do
PixelMap[x, y]:= Stream.ReadBoolean;
Result:= TRUE;
end;
end;
FINALLY
freeAndNil(Stream);
END;
end;
{ PARAMETERS }
procedure RRaindropParams.Reset;
begin
TargetFPS := 24;
FDamping := 15; { Default damping = 15 }
WaveAplitude := 1;
WaveTravelDist := 50;
DropInterval := 150;
end;
procedure RRaindropParams.Save(Stream: TCubicBuffStream);
begin
Stream.WriteInteger(FDamping);
Stream.WriteInteger(TargetFPS);
Stream.WriteInteger(WaveAplitude);
Stream.WriteInteger(WaveTravelDist);
Stream.WriteInteger(DropInterval);
Stream.WritePadding(64);
end;
procedure RRaindropParams.Load(Stream: TCubicBuffStream);
begin
Damping := Stream.ReadInteger;
TargetFPS := Stream.ReadInteger;
WaveAplitude := Stream.ReadInteger;
WaveTravelDist:= Stream.ReadInteger;
DropInterval := Stream.ReadInteger;
Stream.ReadPadding(64);
end;
procedure RRaindropParams.SetDamping(Value: TWaterDamping);
begin
if (Value >= Low(TWaterDamping))
and (Value <= High(TWaterDamping))
then FDamping := Value;
end;
end.