-
Notifications
You must be signed in to change notification settings - Fork 12
/
MatrixImageLists.pas
491 lines (421 loc) · 15.9 KB
/
MatrixImageLists.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// ###################################################################
// #### This file is part of the mrimageutils project, depends on
// #### the mathematics library project and is
// #### offered under the licence agreement described on
// #### http://www.mrsoft.org/
// ####
// #### Copyright:(c) 2012, Michael R. . All rights reserved.
// ####
// #### Unless required by applicable law or agreed to in writing, software
// #### distributed under the License is distributed on an "AS IS" BASIS,
// #### WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// #### See the License for the specific language governing permissions and
// #### limitations under the License.
// ###################################################################
unit MatrixImageLists;
// #########################################################
// #### Loading and converting of image lists
// #########################################################
interface
uses SysUtils, Classes, contnrs, Matrix, ImageMatrixConv;
// #########################################################
// #### Base class
type
TCustomMtxImageList = class(TObject)
private
fNumImages : integer;
fImgWidth : integer;
fImgHeight : integer;
fImgResize: TOnImgResizeEvent;
fRecursive: boolean;
protected
property NumImages : integer read fNumImages;
procedure OnImageLoad(mtx : TDoubleMatrix; actNum : integer; const fileName : string); virtual; abstract;
public
property OnImgResizeEvent : TOnImgResizeEvent read fImgResize write fImgResize;
property Recursive : boolean read fRecursive write fRecursive;
procedure ReadListFromDirectory(const Directory : string; convType : TImageConvType); virtual;
procedure ReadListFromDirectoryRaw(const Directory : string; convType : TImageConvType); virtual;
end;
// #########################################################
// #### complete list as a whole matrix
type
TOneMtxImageList = class(TCustomMtxImageList)
private
fImages : TDoubleMatrix;
protected
procedure OnImageLoad(mtx : TDoubleMatrix; actNum : integer; const fileName : string); override;
public
property Images : TDoubleMatrix read fImages;
constructor Create;
destructor Destroy; override;
end;
// #########################################################
// #### Complete list
type
TMtxImageList = class(TCustomMtxImageList)
private
fImages : TObjectList;
function GetImage(index: integer): TDoubleMatrix;
function GetImageCount: integer;
protected
procedure OnImageLoad(mtx : TDoubleMatrix; actNum : integer; const FileName : string); override;
public
property Images[index : integer] : TDoubleMatrix read GetImage;
property ImageCount : integer read GetImageCount;
constructor Create;
destructor Destroy; override;
end;
// #########################################################
// #### list for incremental building - note the procedure does not store
// the images
type
TImageLoadEvent = procedure(Sender : TObject; mtx : TDoubleMatrix; actNum, NumImags : integer; const FileName : string) of Object;
TImageLoadEventEx = function(Sender : TObject; mtx : TDoubleMatrix; actNum, NumImags : integer; const FileName : string) : boolean of Object;
TIncrementalImageList = class(TCustomMtxImageList)
private
fOnImageLoad : TImageLoadEvent;
fOnImageLoadEx : TImageLoadEventEx;
protected
procedure OnImageLoad(mtx : TDoubleMatrix; actNum : integer; const FileName : string); override;
public
property OnImageStep : TImageLoadEvent read fOnImageLoad write fOnImageLoad;
property OnImageStepEx : TImageLoadEventEx read fOnImageLoadEx write fOnImageLoadEx;
procedure ReadListFromDirectory(const Directory : string; convType : TImageConvType); override;
procedure ReadListFromDirectoryRaw(const Directory : string; convType : TImageConvType); override;
end;
implementation
uses Graphics;
{ TMtxImageList }
constructor TMtxImageList.Create;
begin
inherited Create;
fImages := TObjectList.Create(True);
end;
destructor TMtxImageList.Destroy;
begin
fImages.Free;
inherited;
end;
function TMtxImageList.GetImage(index: integer): TDoubleMatrix;
begin
Result := TDoubleMatrix(fImages[index]);
end;
function TMtxImageList.GetImageCount: integer;
begin
Result := fImages.Count;
end;
procedure TMtxImageList.OnImageLoad(mtx: TDoubleMatrix; actNum : integer; const fileName : string);
var img : TDoubleMatrix;
begin
if actNum = 0 then
begin
fImages.Clear;
fImages.Capacity := NumImages;
end;
// ############################################
// #### copy image
img := TDoubleMatrix.Create;
img.Assign(mtx);
fImages.Add(img);
end;
{ TCustomMtxImageList }
procedure TCustomMtxImageList.ReadListFromDirectory(const Directory: string;
convType: TImageConvType);
var registeredExtensions : TStringList;
nonRegExtensions : TStringList;
fileNames : TStringList;
i : Integer;
pict : TPicture;
bmp : TBitmap;
converter : TMatrixImageConverter;
mtx : TDoubleMatrix;
procedure EnumFilesInDirRecursive(const dir : string; ResList : TStringList);
var imgFiles : TSearchRec;
ext : string;
pict : TPicture;
begin
// note it would be easier if we would have access to the function Graphics.GetFileFormats
// function. Se I have to build it myself
if FindFirst(IncludeTrailingPathDelimiter(dir) + '*.*', faAnyFile, imgFiles) = 0 then
begin
repeat
if (imgFiles.Attr and faDirectory) = faDirectory then
begin
// recursively check for new files
if (imgFiles.Name[1] <> '.') and fRecursive then
EnumFilesInDirRecursive(IncludeTrailingPathDelimiter(dir) + imgFiles.Name, ResList);
continue;
end;
ext := ExtractFileExt(imgFiles.Name);
if nonRegExtensions.IndexOf(ext) >= 0
then
continue
else if registeredExtensions.IndexOf(ext) >= 0
then
ResList.Add(IncludeTrailingPathDelimiter(dir) + imgFiles.Name)
else
begin
// test load the file -> if successfull then it's good
try
pict := TPicture.Create;
try
pict.LoadFromFile(IncludeTrailingPathDelimiter(dir) + imgFiles.Name);
if fImgWidth = -1 then
begin
fImgWidth := pict.Width;
fImgHeight := pict.Height;
end;
registeredExtensions.Add(ExtractFileExt(imgFiles.Name));
ResList.Add(IncludeTrailingPathDelimiter(dir) + imgFiles.Name)
finally
pict.Free;
end;
except
nonRegExtensions.Add(ExtractFileExt(imgFiles.Name));
end;
end;
until FindNext(imgFiles) <> 0;
end;
FindClose(imgFiles);
end;
function EnumFilesInDir(const dir : string) : TStringList;
begin
Result := TStringList.Create;
EnumFilesInDirRecursive(dir, Result);
end;
begin
fImgWidth := -1;
fImgHeight := -1;
registeredExtensions := TStringList.Create;
nonRegExtensions := TStringList.Create;
try
filenames := EnumFilesInDir(Directory);
try
fNumImages := fileNames.Count;
if fNumImages = 0 then
exit;
mtx := TDoubleMatrix.Create(fImgWidth, fImgHeight);
try
converter := TMatrixImageConverter.Create(convType, False, True, fImgWidth, fImgHeight);
try
// #####################################################
// #### read the images
i := 0;
while i < fNumImages do
begin
pict := TPicture.Create;
try
pict.LoadFromFile(filenames[i]);
bmp := TBitmap.Create;
try
bmp.SetSize(pict.Width, pict.Height);
bmp.Canvas.Draw(0, 0, pict.Graphic);
if (bmp.Width <> fImgWidth) or (bmp.Height <> fImgHeight) and Assigned(fImgResize) then
fImgResize(Self, bmp, fImgWidth, fImgHeight);
if (bmp.Width <> fImgWidth) or (bmp.Height <> fImgHeight) then
raise Exception.Create('Error image properties do not match the expected ones');
// ########################################################
// #### convert and store images
converter.ImageToMatrix(mtx, bmp);
OnImageLoad(mtx, i, fileNames[i]);
finally
bmp.Free;
end;
finally
pict.Free;
end;
inc(i);
end;
finally
converter.Free;
end;
finally
mtx.Free;
end;
finally
filenames.Free;
end;
finally
registeredExtensions.Free;
nonRegExtensions.Free;
end;
end;
procedure TCustomMtxImageList.ReadListFromDirectoryRaw(const Directory: string;
convType: TImageConvType);
var registeredExtensions : TStringList;
nonRegExtensions : TStringList;
fileNames : TStringList;
i : Integer;
pict : TPicture;
bmp : TBitmap;
mtx : TDoubleMatrix;
procedure EnumFilesInDirRecursive(const dir : string; ResList : TStringList);
var imgFiles : TSearchRec;
ext : string;
pict : TPicture;
begin
// note it would be easier if we would have access to the function Graphics.GetFileFormats
// function. Se I have to build it myself
if FindFirst(IncludeTrailingPathDelimiter(dir) + '*.*', faAnyFile, imgFiles) = 0 then
begin
repeat
if (imgFiles.Attr and faDirectory) = faDirectory then
begin
// recursively check for new files
if (imgFiles.Name <> '') and (imgFiles.Name[1] <> '.') and fRecursive then
EnumFilesInDirRecursive(IncludeTrailingPathDelimiter(dir) + imgFiles.Name, ResList);
continue;
end;
ext := ExtractFileExt(imgFiles.Name);
if nonRegExtensions.IndexOf(ext) >= 0
then
continue
else if registeredExtensions.IndexOf(ext) >= 0
then
ResList.Add(IncludeTrailingPathDelimiter(dir) + imgFiles.Name)
else
begin
// test load the file -> if successfull then it's good
try
pict := TPicture.Create;
try
pict.LoadFromFile(IncludeTrailingPathDelimiter(dir) + imgFiles.Name);
if fImgWidth = -1 then
begin
fImgWidth := pict.Width;
fImgHeight := pict.Height;
end;
registeredExtensions.Add(ExtractFileExt(imgFiles.Name));
ResList.Add(IncludeTrailingPathDelimiter(dir) + imgFiles.Name)
finally
pict.Free;
end;
except
nonRegExtensions.Add(ExtractFileExt(imgFiles.Name));
end;
end;
until FindNext(imgFiles) <> 0;
end;
FindClose(imgFiles);
end;
function EnumFilesInDir(const dir : string) : TStringList;
begin
Result := TStringList.Create;
EnumFilesInDirRecursive(dir, Result);
end;
begin
fImgWidth := -1;
fImgHeight := -1;
registeredExtensions := TStringList.Create;
nonRegExtensions := TStringList.Create;
try
filenames := EnumFilesInDir(Directory);
try
fNumImages := fileNames.Count;
if fNumImages = 0 then
exit;
// #####################################################
// #### read the images
i := 0;
while i < fNumImages do
begin
pict := TPicture.Create;
try
pict.LoadFromFile(filenames[i]);
bmp := TBitmap.Create;
try
bmp.SetSize(pict.Width, pict.Height);
bmp.Canvas.Draw(0, 0, pict.Graphic);
mtx := TMatrixImageConverter.ConvertImage(bmp, convType);
try
// ########################################################
// #### convert and store images
OnImageLoad(mtx, i, filenames[i]);
finally
mtx.Free
end;
finally
bmp.Free;
end;
finally
pict.Free;
end;
inc(i);
end;
finally
filenames.Free;
end;
finally
registeredExtensions.Free;
nonRegExtensions.Free;
end;
end;
{ TOneMtxImageList }
type
EIteratorHalt = class(Exception);
constructor TOneMtxImageList.Create;
begin
inherited Create;
fImages := TDoubleMatrix.Create;
end;
destructor TOneMtxImageList.Destroy;
begin
fImages.Free;
inherited;
end;
procedure TOneMtxImageList.OnImageLoad(mtx: TDoubleMatrix; actNum : integer; const fileName : string);
var oldWidth, oldHeight : integer;
begin
if actNum = 0 then
begin
fImages.Free;
fImages := TDoubleMatrix.Create(NumImages, mtx.Width*mtx.Height);
end;
oldWidth := mtx.Width;
oldHeight := mtx.Height;
mtx.ReshapeInPlace(1, oldWidth*oldHeight);
fImages.SetRow(actNum, mtx);
mtx.ReshapeInPlace(oldWidth, oldHeight);
end;
{ TIncrementalImageList }
procedure TIncrementalImageList.OnImageLoad(mtx: TDoubleMatrix;
actNum: integer; const FileName : string);
begin
if Assigned(fOnImageLoad) then
fOnImageLoad(Self, mtx, actNum, NumImages, FileName);
if Assigned(fOnImageLoadEx) then
begin
if not fOnImageLoadEx(Self, mtx, actNum, NumImages, FileName) then
raise EIteratorHalt.Create('User canceled file iteration.');
end;
end;
procedure TIncrementalImageList.ReadListFromDirectory(const Directory: string;
convType: TImageConvType);
begin
try
inherited;
except
on E : EIteratorHalt do
begin
// swallow
end;
else
raise;
end;
end;
procedure TIncrementalImageList.ReadListFromDirectoryRaw(
const Directory: string; convType: TImageConvType);
begin
try
inherited;
except
on E : EIteratorHalt do
begin
// swallow
end;
else
raise;
end;
end;
end.