-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagepicker.hpp
525 lines (493 loc) · 18.7 KB
/
imagepicker.hpp
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#include <iostream>
#include <fstream>
#include <filesystem>
#include <opencv2/opencv.hpp>
#include <opencv2/core/utils/logger.hpp>
#define KEY_CODE_QUIT 27
#define KEY_CODE_ENTER 13
#define KEY_CODE_PAGE_UP -10
#define KEY_CODE_PAGE_DOWN +10
#define KEY_CODE_ARROW_LEFT -1
#define KEY_CODE_ARROW_UP -2
#define KEY_CODE_ARROW_RIGHT +1
#define KEY_CODE_ARROW_DOWN +2
#define KEY_CODE_HOME 5
#define KEY_CODE_MODE_IMAGE 100
#define KEY_CODE_MODE_FOLDER -100
#define NB_LIG 5
#define NB_COL 4
#define EPAISSEUR 2
#define THUMBMAIL_SIZE 150
class ImagePicker {
public:
~ImagePicker()
{
cv::destroyWindow(windowName_);
}
ImagePicker(std::string repDefaut=".", int nbLig=NB_LIG, int nbCol=NB_COL, int larImg=THUMBMAIL_SIZE, int hauImg= THUMBMAIL_SIZE) : repDefaut_(repDefaut), nbLig_(nbLig), nbCol_(nbCol),
widthImage_(larImg), heightImage_(hauImg)
{
init_ = false;
fileSelec_ = false;
windowName_ = "Album";
}
bool run()
{
auto actIdx = std::ref(idxDir_);
if (!init_)
init();
if (modeImage_)
{
actIdx = std::ref(idxImg_);
actIdx.get().setSize(album_.size());
}
else
{
actIdx = std::ref(idxDir_);
actIdx.get().setSize(folder_.size());
}
int keyCode = 0;
selecSouris_ = false;
while (keyCode != KEY_CODE_QUIT &&
!(modeImage_ && (keyCode == KEY_CODE_ENTER || selecSouris_)))
{
int resVis = int(cv::getWindowProperty(windowName_, cv::WND_PROP_VISIBLE));
if (!resVis)
{
if (verbose_)
std::cout << "Windows " << windowName_ << " closed" << std::endl;
cv::namedWindow(windowName_);
cv::setMouseCallback(windowName_, mouseManager, this);
}
updateSelection();
cv::imshow(windowName_, fileBrowser_);
int code = cv::waitKeyEx(20);
if (platformKeyCode_.find(code) == platformKeyCode_.end())
keyCode = 0;
else
keyCode = platformKeyCode_[code];
if (verbose_ && code > 0)
std::cout << "key " << std::hex << code << " -> keycode "<< keyCode << std::endl;
if (keyCode != 0)
{
step_ = 0;
switch (keyCode) {
case KEY_CODE_ARROW_DOWN:
step_ = actIdx.get().nbCol_;
actIdx.get().selec2_ = actIdx.get().selec1_ + step_;
if (actIdx.get().selec2_ >= actIdx.get().size_)
actIdx.get().selec2_ = actIdx.get().selec1_;
break;
case KEY_CODE_ARROW_UP:
step_ = -actIdx.get().nbCol_;
actIdx.get().selec2_ = actIdx.get().selec1_ + step_;
if (actIdx.get().selec2_ < 0)
actIdx.get().selec2_ = actIdx.get().selec1_;
break;
case KEY_CODE_ARROW_RIGHT:
step_ = 1;
actIdx.get().selec2_ = actIdx.get().selec1_ + step_;
if (actIdx.get().selec2_ >= actIdx.get().size_)
actIdx.get().selec2_ = actIdx.get().selec1_;
break;
case KEY_CODE_ARROW_LEFT:
step_ = -1;
actIdx.get().selec2_ = actIdx.get().selec1_ + step_;
if (actIdx.get().selec2_ < 0)
actIdx.get().selec2_ = actIdx.get().selec1_;
break;
case KEY_CODE_PAGE_UP:
step_ = -actIdx.get().nbCol_ * actIdx.get().nbLig_;
actIdx.get().selec2_ = actIdx.get().selec1_ + step_;
if (actIdx.get().selec2_ < 0)
actIdx.get().selec2_= actIdx.get().selec1_;
break;
case KEY_CODE_PAGE_DOWN:
step_ = actIdx.get().nbCol_ * actIdx.get().nbLig_;
actIdx.get().selec2_ = actIdx.get().selec1_ + step_;
if (actIdx.get().selec2_ >= actIdx.get().size_)
actIdx.get().selec2_ = actIdx.get().selec1_;
break;
case KEY_CODE_MODE_IMAGE:
modeImage_ = true;
actIdx = std::ref(idxImg_);
drawStoreImages();
break;
case KEY_CODE_MODE_FOLDER:
modeImage_ = false;
actIdx = std::ref(idxDir_);
drawStoreImages();
break;
case KEY_CODE_HOME:
{
auto path = std::filesystem::path(repDefaut_);
repDefaut_ = absolute(path).parent_path().generic_string();
if (verbose_)
std::cout << "Folder " << repDefaut_ << std::endl;
selecSouris_ = false;
keyCode = 0;
init();
}
break;
default:
break;
}
}
if (!modeImage_ && (keyCode == KEY_CODE_ENTER || selecSouris_))
{
int idxFolder = actIdx.get().selec1_;
repDefaut_ = actIdx.get().album_[idxFolder].second;
if (verbose_ )
std::cout << "Folder " << repDefaut_ << std::endl;
selecSouris_ = false;
keyCode = 0;
init();
}
}
if (modeImage_)
{
if (keyCode == KEY_CODE_ENTER || selecSouris_)
{
fileName_ = album_[actIdx.get().selec1_].second;
fileSelec_ = true;
}
else
{
fileName_ = "";
fileSelec_ = false;
}
}
else
if (keyCode == KEY_CODE_ENTER || selecSouris_)
{
fileName_ = album_[actIdx.get().selec1_].second;
fileSelec_ = false;
}
else
{
fileName_ = "";
fileSelec_ = false;
}
return fileSelec_;
}
std::string path()
{
if (fileSelec_)
return fileName_;
return "";
}
void setVerbose(bool b)
{
verbose_ = b;
if (!verbose_)
cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_SILENT);
else
cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_INFO);
}
protected:
void updateSelection(void)
{
auto actIdx = std::ref(idxDir_);
if (modeImage_)
{
actIdx = std::ref(idxImg_);
}
else
actIdx = std::ref(idxDir_);
if (actIdx.get().selec2_ < actIdx.get().first_ || actIdx.get().selec2_ >= actIdx.get().last__)
{
if (actIdx.get().selec2_ < actIdx.get().first_)
{
actIdx.get().first_ = std::max(actIdx.get().first_ + step_, 0);
actIdx.get().last__ = std::min(int(actIdx.get().size_), actIdx.get().first_ + actIdx.get().nbCol_ * actIdx.get().nbLig_);
}
if (actIdx.get().selec2_ >= actIdx.get().last__)
{
actIdx.get().last__ = std::min(actIdx.get().last__ + step_, int(actIdx.get().size_));
actIdx.get().first_ = std::max(0, actIdx.get().last__ - actIdx.get().nbCol_ * actIdx.get().nbLig_);
}
drawStoreImages();
}
if (actIdx.get().selec1_ != actIdx.get().selec2_)
{
cv::Mat dst(actIdx.get().heightImage_, actIdx.get().widthImage_, CV_8UC3, cv::Scalar::all(0));
cv::Mat src = actIdx.get().album_[actIdx.get().selec1_].first;
int idxImage = actIdx.get().selec1_ - actIdx.get().first_;
if (idxImage >= 0 && idxImage < actIdx.get().nbLig_ * actIdx.get().nbCol_)
{
src.copyTo(dst);
dst.copyTo(fileBrowser_(cv::Rect(actIdx.get().widthImage_ * (idxImage % actIdx.get().nbCol_), actIdx.get().heightImage_ * (idxImage / actIdx.get().nbCol_), dst.cols, dst.rows)));
}
actIdx.get().selec1_ = actIdx.get().selec2_;
rectangleSelectedImage();
}
}
void mouseManager(int event, int x, int y, int flags)
{
auto actIdx = std::ref(idxDir_);
if (modeImage_)
{
actIdx = std::ref(idxImg_);
}
else
actIdx = std::ref(idxDir_);
if (x < 0 || x >= actIdx.get().widthImage_ * actIdx.get().nbCol_)
return;
if (y < 0 || y >= actIdx.get().heightImage_ * actIdx.get().nbLig_)
return;
switch (event) {
case cv::EVENT_LBUTTONDOWN:
actIdx.get().selec2_ = x / actIdx.get().widthImage_ + actIdx.get().nbCol_ * (y / actIdx.get().heightImage_) + actIdx.get().first_;
if (actIdx.get().selec2_ >= actIdx.get().size_)
actIdx.get().selec2_ = actIdx.get().selec1_;
else
updateSelection();
break;
case cv::EVENT_LBUTTONDBLCLK:
actIdx.get().selec2_ = x / actIdx.get().widthImage_ + actIdx.get().nbCol_ * (y / actIdx.get().heightImage_) + actIdx.get().first_;
if (actIdx.get().selec2_ >= actIdx.get().size_)
actIdx.get().selec2_ = actIdx.get().selec1_;
else
{
updateSelection();
selecSouris_ = true;
}
break;
case cv::EVENT_MOUSEWHEEL:
if (flags > 0)
{
step_ = -actIdx.get().nbCol_;
actIdx.get().selec2_ = actIdx.get().selec1_ + step_;
if (actIdx.get().selec2_ < 0)
actIdx.get().selec2_ = actIdx.get().selec1_;
}
else
{
step_ = actIdx.get().nbCol_;
actIdx.get().selec2_ = actIdx.get().selec1_ + step_;
if (actIdx.get().selec2_ >= actIdx.get().size_)
actIdx.get().selec2_ = actIdx.get().selec1_;
}
break;
case cv::EVENT_MOUSEMOVE:
break;
default:
if (verbose_)
std::cout << "Event " << event << "\n";
}
}
static void mouseManager(int event, int x, int y, int flags, void* param)
{
ImagePicker* obj = (ImagePicker*)param;
obj->mouseManager(event, x, y, flags);
}
void initKeyCodeMap()
{
std::string gui = cv::currentUIFramework();
if (gui == "WIN32")
{
platformKeyCode_[0x210000] = KEY_CODE_PAGE_UP;
platformKeyCode_[0x220000] = KEY_CODE_PAGE_DOWN;
platformKeyCode_[0x250000] = KEY_CODE_ARROW_LEFT;
platformKeyCode_[0x260000] = KEY_CODE_ARROW_UP;
platformKeyCode_[0x270000] = KEY_CODE_ARROW_RIGHT;
platformKeyCode_[0x280000] = KEY_CODE_ARROW_DOWN;
platformKeyCode_[0x240000] = KEY_CODE_HOME;
platformKeyCode_[27] = KEY_CODE_QUIT;
platformKeyCode_[int('\t')] = KEY_CODE_ENTER;
platformKeyCode_[0xd] = KEY_CODE_ENTER;
}
else if (gui == "GTK3")
{
platformKeyCode_[0x10ff55] = KEY_CODE_PAGE_UP;
platformKeyCode_[0x10ff56] = KEY_CODE_PAGE_DOWN;
platformKeyCode_[0x10ff51] = KEY_CODE_ARROW_LEFT;
platformKeyCode_[0x10ff52] = KEY_CODE_ARROW_UP;
platformKeyCode_[0x10ff53] = KEY_CODE_ARROW_RIGHT;
platformKeyCode_[0x10ff54] = KEY_CODE_ARROW_DOWN;
platformKeyCode_[0x10001b] = KEY_CODE_QUIT;
platformKeyCode_[0x10000d] = KEY_CODE_ENTER;
platformKeyCode_[0x100066] = KEY_CODE_MODE_FOLDER;
platformKeyCode_[0x100069] = KEY_CODE_MODE_IMAGE;
platformKeyCode_[0x10ff50] = KEY_CODE_HOME;
}
else if (gui == "COCOA")
{
platformKeyCode_[0x10ff55] = KEY_CODE_PAGE_UP;
platformKeyCode_[0x10ff56] = KEY_CODE_PAGE_DOWN;
platformKeyCode_[0xf702] = KEY_CODE_ARROW_LEFT;
platformKeyCode_[0xf700] = KEY_CODE_ARROW_UP;
platformKeyCode_[0xf703] = KEY_CODE_ARROW_RIGHT;
platformKeyCode_[0xf701] = KEY_CODE_ARROW_DOWN;
platformKeyCode_[0xd] = KEY_CODE_ENTER;
platformKeyCode_[0xf72c] = KEY_CODE_HOME;
platformKeyCode_[int('i')] = KEY_CODE_MODE_IMAGE;
platformKeyCode_[0x66] = KEY_CODE_MODE_FOLDER;
}
else
{
if (verbose_)
std::cout << gui << " Unknown HighGUI backend\n";
platformKeyCode_[0x10001b] = KEY_CODE_QUIT;
if (verbose_)
std::cout << gui << " HighGUI backend\n";
}
}
void rectangleSelectedImage()
{
auto actIdx = std::ref(idxDir_);
if (modeImage_)
{
actIdx = std::ref(idxImg_);
}
else
actIdx = std::ref(idxDir_);
if (actIdx.get().album_.size() == 0)
return;
int idxImage = actIdx.get().selec1_ - actIdx.get().first_;
if (idxImage < 0 || idxImage >= actIdx.get().nbCol_ * actIdx.get().nbLig_)
return;
cv::Mat src = actIdx.get().album_[actIdx.get().selec1_].first;
cv::Mat dst(actIdx.get().heightImage_, actIdx.get().widthImage_, CV_8UC3, cv::Scalar::all(0));
src.copyTo(dst);
cv::rectangle(dst, cv::Rect(0, 0, dst.cols, dst.rows), cv::Scalar(0, 0, 255), EPAISSEUR);
dst.copyTo(fileBrowser_(cv::Rect(actIdx.get().widthImage_ * (idxImage % actIdx.get().nbCol_), actIdx.get().heightImage_ * (idxImage / actIdx.get().nbCol_), dst.cols, dst.rows)));
}
void drawStoreImages()
{
auto actIdx = std::ref(idxDir_);
if (modeImage_)
{
actIdx = std::ref(idxImg_);
}
else
actIdx = std::ref(idxDir_);
fileBrowser_.setTo(cv::Scalar::all(0));
for (int idx = actIdx.get().first_; idx < actIdx.get().last__; idx++)
{
cv::Mat dst = actIdx.get().album_[idx].first;
int idxImage = idx - actIdx.get().first_;
dst.copyTo(fileBrowser_(cv::Rect(actIdx.get().widthImage_ * (idxImage % actIdx.get().nbCol_), actIdx.get().heightImage_ * (idxImage / actIdx.get().nbCol_), dst.cols, dst.rows)));
}
rectangleSelectedImage();
}
void initAlbum()
{
album_.clear();
folder_.clear();
idxImg_.first_ = 0;
idxImg_.selec1_ = 0;
idxImg_.selec2_ = 0;
idxImg_.nbLig_ = nbLig_;
idxImg_.nbCol_ = nbCol_;
idxImg_.heightImage_ = heightImage_;
idxImg_.widthImage_ = widthImage_;
idxDir_.first_ = 0;
idxDir_.selec1_ = 0;
idxDir_.selec2_ = 0;
idxDir_.heightImage_ = 40;
idxDir_.nbLig_ = (nbLig_ * heightImage_) / idxDir_.heightImage_;
idxDir_.nbCol_ = 1;
idxDir_.widthImage_ = nbCol_ * widthImage_;
fileBrowser_ = cv::Mat(nbLig_ * heightImage_, nbCol_ * widthImage_, CV_8UC3, cv::Scalar::all(0));
const auto& entry = std::filesystem::directory_iterator(repDefaut_);
int idxImage = 0;
for (const auto& entry : std::filesystem::directory_iterator(repDefaut_))
{
auto pathStr = entry.path();
try
{
cv::Mat img = cv::imread(pathStr.generic_string(), cv::IMREAD_COLOR);
if (!img.empty())
{
int lMax = std::max(img.rows, img.cols);
double f = double(THUMBMAIL_SIZE) / lMax;
cv::Mat dst;
cv::resize(img, dst, cv::Size(), f, f);
album_.push_back(std::make_pair(dst, pathStr.generic_string()));
idxImage++;
if (verbose_ && idxImage % 100 == 0)
std::cout << "*";
}
else if (std::filesystem::is_directory(pathStr))
{
cv::Mat myText(idxDir_.heightImage_, idxDir_.widthImage_, CV_8UC3, cv::Scalar::all(0));
cv::putText(myText, pathStr.generic_string(), cv::Point(0, (3 * idxDir_.heightImage_) / 4), cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 0, 255), 2);
folder_.push_back(std::make_pair(myText, pathStr.generic_string()));
}
}
catch (std::filesystem::filesystem_error& e)
{
if (verbose_ )
std::cout << e.what() << "\n";
}
}
if (verbose_)
{
std::cout << "\n Dossier " << repDefaut_ << " contenant:\n";
std::cout << album_.size() << " images.\n";
std::cout << folder_.size() << " dossiers.\n";
}
int c = 0;
idxDir_.album_ = folder_;
idxImg_.album_ = album_;
idxDir_.setSize(folder_.size());
idxImg_.setSize(album_.size());
idxImg_.last__ = std::min(nbCol_ * nbLig_, int(album_.size()));
idxDir_.last__ = std::min(idxDir_.nbLig_, int(folder_.size()));
drawStoreImages();
}
void init()
{
if (!init_)
{
modeImage_ = true;
cv::namedWindow(windowName_);
cv::setMouseCallback(windowName_, mouseManager, this);
initKeyCodeMap();
init_ = true;
}
initAlbum();
}
private:
struct IndiceSelect {
int selec1_;
int selec2_;
int first_;
int last__;
int nbLig_;
int nbCol_;
int widthImage_;
int heightImage_;
size_t size_;
std::vector<std::pair<cv::Mat, std::string>> album_;
IndiceSelect(int a=0, int b=0, int c=0, int d=0) : selec1_(a), selec2_(b), first_(c), last__(d)
{
size_ = 0;
};
size_t setSize(size_t x)
{
size_ = x;
return size_;
}
};
std::string repDefaut_;
std::string fileName_;
std::string windowName_;
std::map<int, int> platformKeyCode_;
bool fileSelec_;
bool modeImage_; // true browse image, false browse folder
int nbLig_;
int nbCol_;
int step_;
int widthImage_;
int heightImage_;
cv::Mat fileBrowser_;
std::vector<std::pair<cv::Mat, std::string>> album_;
std::vector<std::pair<cv::Mat, std::string>> folder_;
IndiceSelect idxImg_;
IndiceSelect idxDir_;
bool selecSouris_;
bool init_;
bool verbose_;
};