Repository navigation
Expand file tree
/
Copy pathconsolecommands.cpp
More file actions
648 lines (519 loc) · 18 KB
/
Copy pathconsolecommands.cpp
File metadata and controls
648 lines (519 loc) · 18 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
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#include "consolecommands.hpp"
#include "eeiv.hpp"
static std::string createSavePath( const std::string& oriPath, Uint32 width, Uint32 height,
Image::SaveType saveType ) {
Image::SaveType type = saveType == Image::SaveType::Unknown
? Image::extensionToSaveType( FileSystem::fileExtension( oriPath ) )
: saveType;
if ( Image::SaveType::Unknown == type ) {
type = Image::SaveType::PNG;
}
return FileSystem::fileRemoveExtension( oriPath ) + "-" + String::toString( width ) + "x" +
String::toString( height ) + "." + Image::saveTypeToExtension( type );
}
Image::SaveType ConsoleCommands::getPathSaveType( const std::string& path ) {
return Image::extensionToSaveType( FileSystem::fileExtension( path ) );
}
void ConsoleCommands::scaleImg( const std::string& Path, const Float& Scale,
const bool& overridePath, Image::SaveType saveType ) {
int w, h, c;
if ( Image::getInfo( Path, &w, &h, &c ) && Scale > 0.f ) {
Int32 new_width = static_cast<Int32>( w * Scale );
Int32 new_height = static_cast<Int32>( h * Scale );
std::string outputPath( Path );
if ( !overridePath ) {
outputPath = createSavePath( Path, new_width, new_height, saveType );
}
resizeImg( Path, outputPath, new_width, new_height, saveType );
} else {
Con->pushText( "Images does not exists." );
}
}
void ConsoleCommands::resizeImg( const std::string& Path, const std::string& outputPath,
const Uint32& NewWidth, const Uint32& NewHeight,
Image::SaveType saveType ) {
if ( App::isImage( Path ) ) {
Image::SaveType type =
Image::SaveType::Unknown != saveType ? saveType : getPathSaveType( outputPath );
Image img( Path );
img.resize( NewWidth, NewHeight );
img.saveToFile( outputPath, type );
} else {
Con->pushText( "Images does not exists." );
}
}
void ConsoleCommands::thumbnailImg( const std::string& Path, const Uint32& MaxWidth,
const Uint32& MaxHeight, Image::SaveType saveType ) {
if ( App::isImage( Path ) ) {
Image img( Path );
Image* thumb = img.thumbnail( MaxWidth, MaxHeight );
if ( NULL != thumb ) {
std::string newPath(
createSavePath( Path, thumb->getWidth(), thumb->getHeight(), saveType ) );
Image::SaveType type =
Image::SaveType::Unknown != saveType ? saveType : getPathSaveType( newPath );
thumb->saveToFile( newPath, type );
eeSAFE_DELETE( thumb );
}
} else {
Con->pushText( "Images does not exists." );
}
}
void ConsoleCommands::centerCropImg( const std::string& Path, const Uint32& Width,
const Uint32& Height, Image::SaveType saveType ) {
if ( App::isImage( Path ) ) {
Image img( Path );
Sizei nSize;
double scale = 1.f;
scale = eemax( (double)Width / (double)img.getWidth(),
(double)Height / (double)img.getHeight() );
nSize.x = Math::round( img.getWidth() * scale );
nSize.y = Math::round( img.getHeight() * scale );
if ( nSize.getWidth() == (int)Width - 1 || nSize.getWidth() == (int)Width + 1 ) {
nSize.x = (int)Width;
}
if ( nSize.getHeight() == (int)Height - 1 || nSize.getHeight() == (int)Height + 1 ) {
nSize.y = (int)Height;
}
img.resize( nSize.getWidth(), nSize.getHeight() );
Image* croppedImg = NULL;
Rect rect;
if ( img.getWidth() > Width ) {
rect.Left = ( img.getWidth() - Width ) / 2;
rect.Right = rect.Left + Width;
rect.Top = 0;
rect.Bottom = Height;
} else {
rect.Top = ( img.getHeight() - Height ) / 2;
rect.Bottom = rect.Top + Height;
rect.Left = 0;
rect.Right = Width;
}
croppedImg = img.crop( rect );
if ( NULL != croppedImg ) {
std::string newPath(
createSavePath( Path, croppedImg->getWidth(), croppedImg->getHeight(), saveType ) );
Image::SaveType type =
Image::SaveType::Unknown != saveType ? saveType : getPathSaveType( newPath );
croppedImg->saveToFile( newPath, type );
eeSAFE_DELETE( croppedImg );
} else {
std::string newPath(
createSavePath( Path, img.getWidth(), img.getHeight(), saveType ) );
Image::SaveType type =
Image::SaveType::Unknown != saveType ? saveType : getPathSaveType( newPath );
img.saveToFile( newPath, type );
}
}
}
void ConsoleCommands::batchImgScale( const std::string& Path, const Float& Scale,
const bool& overridePath ) {
std::string iPath = Path;
std::vector<std::string> tmpFiles = FileSystem::filesGetInPath( iPath );
if ( iPath[iPath.size() - 1] != '/' )
iPath += "/";
for ( Int32 i = 0; i < (Int32)tmpFiles.size(); i++ ) {
std::string fPath = iPath + tmpFiles[i];
scaleImg( fPath, Scale, overridePath );
}
}
void ConsoleCommands::batchImgThumbnail( Sizei size, std::string dir, bool recursive ) {
FileSystem::dirAddSlashAtEnd( dir );
std::vector<std::string> files = FileSystem::filesGetInPath( dir );
for ( size_t i = 0; i < files.size(); i++ ) {
std::string fpath( dir + files[i] );
if ( FileSystem::isDirectory( fpath ) ) {
if ( recursive ) {
batchImgThumbnail( size, fpath, recursive );
}
} else {
int w, h, c;
if ( Image::getInfo( fpath, &w, &h, &c ) ) {
if ( w > size.getWidth() || h > size.getHeight() ) {
Image img( fpath );
Image* thumb = img.thumbnail( size.getWidth(), size.getHeight() );
if ( NULL != thumb ) {
thumb->saveToFile( fpath, Image::extensionToSaveType(
FileSystem::fileExtension( fpath ) ) );
Con->pushText(
"Thumbnail created for '%s'. Old size %dx%d. New size %dx%d.",
fpath.c_str(), img.getWidth(), img.getHeight(), thumb->getWidth(),
thumb->getHeight() );
eeSAFE_DELETE( thumb );
} else {
Con->pushText( "Thumbnail %s failed to create.", fpath.c_str() );
}
}
}
}
}
}
void ConsoleCommands::cmdSlideShow( const std::vector<String>& params ) {
String Error( "Usage example: slideshow slide_time_in_ms" );
if ( params.size() >= 2 ) {
Uint32 time = 0;
bool Res = String::fromString( time, params[1] );
if ( Res ) {
if ( !mApp->getSlideShow() ) {
mApp->createSlideShow( time );
} else {
if ( 0 == time ) {
mApp->setSlideShow( false );
}
}
} else {
Con->pushText( Error );
}
} else {
Con->pushText( Error );
}
}
void ConsoleCommands::cmdImgResize( const std::vector<String>& params ) {
String Error(
"Usage example: imgresize new_width new_height path_to_img format override_image_path" );
if ( params.size() >= 3 ) {
Uint32 nWidth = 0;
Uint32 nHeight = 0;
Image::SaveType saveType = Image::SaveType::Unknown;
Uint32 override = 0;
bool Res1 = String::fromString( nWidth, params[1] );
bool Res2 = String::fromString( nHeight, params[2] );
std::string myPath;
if ( params.size() >= 4 ) {
myPath = params[3].toUtf8();
if ( params.size() > 4 ) {
saveType = Image::extensionToSaveType( params[4].toUtf8() );
}
if ( params.size() > 5 ) {
String::fromString( override, params[5] );
}
} else {
myPath = mApp->getFilePath();
}
if ( Res1 && Res2 ) {
std::string savePath =
override != 0 ? myPath : createSavePath( myPath, nWidth, nHeight, saveType );
resizeImg( myPath, savePath, nWidth, nHeight, saveType );
} else {
Con->pushText( Error );
}
} else {
Con->pushText( Error );
}
}
void ConsoleCommands::cmdImgThumbnail( const std::vector<String>& params ) {
String Error( "Usage example: imgthumbnail max_width max_height path_to_img format" );
if ( params.size() >= 3 ) {
Uint32 nWidth = 0;
Uint32 nHeight = 0;
Image::SaveType saveType = Image::SaveType::Unknown;
bool Res1 = String::fromString( nWidth, params[1] );
bool Res2 = String::fromString( nHeight, params[2] );
std::string myPath;
if ( params.size() >= 4 ) {
myPath = params[3].toUtf8();
if ( params.size() > 4 ) {
saveType = Image::extensionToSaveType( params[4].toUtf8() );
}
} else {
myPath = mApp->getFilePath();
}
if ( Res1 && Res2 ) {
thumbnailImg( myPath, nWidth, nHeight, saveType );
} else {
Con->pushText( Error );
}
} else {
Con->pushText( Error );
}
}
void ConsoleCommands::cmdImgCenterCrop( const std::vector<String>& params ) {
String Error( "Usage example: imgcentercrop width height path_to_img format" );
if ( params.size() >= 3 ) {
Uint32 nWidth = 0;
Uint32 nHeight = 0;
Image::SaveType saveType = Image::SaveType::Unknown;
bool Res1 = String::fromString( nWidth, params[1] );
bool Res2 = String::fromString( nHeight, params[2] );
std::string myPath;
if ( params.size() >= 4 ) {
myPath = params[3].toUtf8();
if ( params.size() > 4 ) {
saveType = Image::extensionToSaveType( params[4].toUtf8() );
}
} else {
myPath = mApp->getFilePath();
}
if ( Res1 && Res2 )
centerCropImg( myPath, nWidth, nHeight, saveType );
else
Con->pushText( Error );
} else {
Con->pushText( Error );
}
}
void ConsoleCommands::cmdImgScale( const std::vector<String>& params ) {
String Error( "Usage example: imgscale scale path_to_img format override_path" );
if ( params.size() >= 2 ) {
Float Scale = 0;
Image::SaveType saveType = Image::SaveType::Unknown;
Uint32 override = 0;
bool Res = String::fromString( Scale, params[1] );
std::string myPath;
if ( params.size() >= 3 ) {
myPath = params[2].toUtf8();
if ( params.size() > 3 ) {
saveType = Image::extensionToSaveType( params[3].toUtf8() );
}
if ( params.size() > 4 ) {
String::fromString( override, params[4] );
}
} else {
myPath = mApp->getFilePath();
}
if ( Res )
scaleImg( myPath, Scale, 0 != override, saveType );
else
Con->pushText( Error );
} else {
Con->pushText( Error );
}
}
void ConsoleCommands::cmdBatchImgScale( const std::vector<String>& params ) {
String Error(
"Usage example: batchimgscale scale_value override_img_path ( default disabled ) "
"directory_to_resize_img ( if no dir is passed, it will use the current dir opened )" );
if ( params.size() >= 2 ) {
Float Scale = 0;
Uint32 override = 0;
bool Res = String::fromString( Scale, params[1] );
override = String::fromString( override, params[2] );
std::string myPath = params.size() >= 4 ? params[3].toUtf8() : mApp->getFileDirectoryPath();
if ( Res ) {
if ( FileSystem::isDirectory( myPath ) ) {
batchImgScale( myPath, Scale, 0 != override );
} else {
Con->pushText( "Second argument is not a directory!" );
}
} else {
Con->pushText( Error );
}
} else {
Con->pushText( Error );
}
}
void ConsoleCommands::cmdBatchImgThumbnail( const std::vector<String>& params ) {
String Error(
"Usage example: batchimgthumbnail max_width max_height directory_to_create_thumbs "
"recursive ( if no dir is passed, it will use the current dir opened )" );
if ( params.size() >= 3 ) {
Uint32 max_width = 0, max_height = 0;
bool recursive = false;
bool Res1 = String::fromString( max_width, params[1] );
bool Res2 = String::fromString( max_height, params[2] );
std::string myPath = params.size() >= 4 ? params[3].toUtf8() : mApp->getFileDirectoryPath();
if ( params.size() > 4 && params[4] == "recursive" ) {
recursive = true;
}
if ( Res1 && Res2 ) {
if ( FileSystem::isDirectory( myPath ) ) {
batchImgThumbnail( Sizei( max_width, max_height ), myPath, recursive );
} else {
Con->pushText( "Third argument is not a directory!" );
}
} else {
Con->pushText( Error );
}
} else {
Con->pushText( Error );
}
}
void ConsoleCommands::cmdImgChangeFormat( const std::vector<String>& params ) {
String Error( "Usage example: imgchangeformat to_format image_to_reformat ( if null will use "
"the current loaded image )" );
if ( params.size() >= 2 ) {
std::string toFormat = params[1].toUtf8();
std::string myPath;
if ( params.size() >= 3 ) {
myPath = params[2].toUtf8();
} else {
myPath = mApp->getFilePath();
}
std::string fromFormat = FileSystem::fileExtension( myPath );
if ( Image::isImage( myPath ) ) {
std::string fPath = myPath;
std::string fExt = FileSystem::fileExtension( fPath );
if ( fExt == fromFormat ) {
std::string fName;
if ( fExt != toFormat )
fName = fPath.substr( 0, fPath.find_last_of( "." ) + 1 ) + toFormat;
else
fName = fPath + "." + toFormat;
Image::SaveType saveType = Image::extensionToSaveType( toFormat );
if ( Image::SaveType::Unknown != saveType ) {
Image* img = eeNew( Image, ( fPath ) );
img->saveToFile( fName, saveType );
eeSAFE_DELETE( img );
Con->pushText( fName + " created." );
}
}
} else {
Con->pushText( "Third argument is not a directory! Argument: " + myPath );
}
} else {
Con->pushText( Error );
}
}
void ConsoleCommands::cmdBatchImgChangeFormat( const std::vector<String>& params ) {
String Error( "Usage example: batchimgchangeformat from_format to_format directory_to_reformat "
"( if no dir is passed, it will use the current dir opened )" );
if ( params.size() >= 4 ) {
std::string fromFormat = params[1].toUtf8();
std::string toFormat = params[2].toUtf8();
std::string myPath = params.size() >= 4 ? params[3].toUtf8() : mApp->getFileDirectoryPath();
if ( FileSystem::isDirectory( myPath ) ) {
std::vector<std::string> tmpFiles = FileSystem::filesGetInPath( myPath );
if ( myPath[myPath.size() - 1] != '/' )
myPath += "/";
for ( Int32 i = 0; i < (Int32)tmpFiles.size(); i++ ) {
std::string fPath = myPath + tmpFiles[i];
std::string fExt = FileSystem::fileExtension( fPath );
if ( App::isImage( fPath ) && fExt == fromFormat ) {
std::string fName;
if ( fExt != toFormat )
fName = fPath.substr( 0, fPath.find_last_of( "." ) + 1 ) + toFormat;
else
fName = fPath + "." + toFormat;
Image::SaveType saveType = Image::extensionToSaveType( toFormat );
if ( Image::SaveType::Unknown != saveType ) {
Image* img = eeNew( Image, ( fPath ) );
img->saveToFile( fPath, saveType );
eeSAFE_DELETE( img );
Con->pushText( fName + " created." );
}
}
}
} else {
Con->pushText( "Third argument is not a directory! Argument: " + myPath );
}
} else {
Con->pushText( Error );
}
}
void ConsoleCommands::cmdMoveTo( const std::vector<String>& params ) {
if ( params.size() >= 2 && mApp->getFileCount() > 0 ) {
Int32 tInt = 0;
bool Res = String::fromString( tInt, params[1] );
if ( tInt )
tInt--;
if ( Res && tInt >= 0 && tInt < (Int32)mApp->getFileCount() ) {
Con->pushText( "moveto: moving to image number " + String::toString( tInt + 1 ) );
mApp->fastLoadImage( tInt );
} else if ( params[1] == "last" ) {
Con->pushText( "moveto: moving to last" );
mApp->fastLoadImage( mApp->getFileCount() - 1 );
} else if ( params[1] == "first" ) {
Con->pushText( "moveto: moving to first" );
mApp->fastLoadImage( 0 );
} else {
Con->pushText( "moveto: image number does not exists" );
}
} else {
Con->pushText( "Expected some parameter" );
}
}
void ConsoleCommands::cmdSetBlockWheel( const std::vector<String>& params ) {
if ( params.size() >= 2 ) {
Int32 tInt = 0;
bool Res = String::fromString( tInt, params[1] );
if ( Res && ( tInt == 0 || tInt == 1 ) ) {
mApp->getConfig().BlockWheelSpeed = tInt ? true : false;
Con->pushText( "setblockwheel " + String::toString( tInt ) );
} else
Con->pushText( "Valid parameters are 0 or 1." );
} else
Con->pushText( "Expected some parameter" );
}
void ConsoleCommands::cmdSetLateLoading( const std::vector<String>& params ) {
if ( params.size() >= 2 ) {
Int32 tInt = 0;
bool Res = String::fromString( tInt, params[1] );
if ( Res && ( tInt == 0 || tInt == 1 ) ) {
mApp->getConfig().LateLoading = tInt ? true : false;
Con->pushText( "setlateloading " + String::toString( tInt ) );
} else
Con->pushText( "Valid parameters are 0 or 1." );
} else
Con->pushText( "Expected some parameter" );
}
void ConsoleCommands::cmdSetImgFade( const std::vector<String>& params ) {
if ( params.size() >= 2 ) {
Int32 tInt = 0;
bool Res = String::fromString( tInt, params[1] );
if ( Res && ( tInt == 0 || tInt == 1 ) ) {
mApp->getConfig().Fade = tInt ? true : false;
Con->pushText( "setimgfade " + String::toString( tInt ) );
} else
Con->pushText( "Valid parameters are 0 or 1." );
} else
Con->pushText( "Expected some parameter" );
}
void ConsoleCommands::cmdSetBackColor( const std::vector<String>& params ) {
const String error(
"Usage example: setbackcolor 255 255 255 (RGB Color, numbers between 0 and 255)" );
if ( params.size() == 2 ) {
mApp->setBackgroundColor( Color::fromString( params[1].toUtf8() ) );
Con->pushText( "setbackcolor applied" );
return;
}
if ( params.size() >= 4 ) {
Int32 red;
Int32 green;
Int32 blue;
if ( String::fromString( red, params[1] ) && String::fromString( green, params[2] ) &&
String::fromString( blue, params[3] ) && red >= 0 && red <= 255 && green >= 0 &&
green <= 255 && blue >= 0 && blue <= 255 ) {
mApp->setBackgroundColor( Color( static_cast<Uint8>( red ), static_cast<Uint8>( green ),
static_cast<Uint8>( blue ) ) );
Con->pushText( "setbackcolor applied" );
return;
}
}
Con->pushText( error );
}
void ConsoleCommands::cmdLoadImg( const std::vector<String>& params ) {
if ( params.size() >= 2 ) {
std::string myPath = params[1].toUtf8();
if ( App::isImage( myPath ) || App::isHttpUrl( myPath ) ) {
mApp->loadDir( myPath );
} else
Con->pushText( "\"" + myPath +
"\" is not an image path or the image is not supported." );
}
}
void ConsoleCommands::cmdLoadDir( const std::vector<String>& params ) {
if ( params.size() >= 2 ) {
std::string myPath = params[1].toUtf8();
if ( params.size() > 2 ) {
for ( Uint32 i = 2; i < params.size(); i++ )
myPath += " " + params[i].toUtf8();
}
if ( FileSystem::isDirectory( myPath ) ) {
mApp->loadDir( myPath );
} else
Con->pushText( "If you want to load an image use loadimg. \"" + myPath +
"\" is not a directory path." );
}
}
void ConsoleCommands::cmdSetZoom( const std::vector<String>& params ) {
if ( params.size() >= 2 ) {
Float tFloat = 0;
bool Res = String::fromString( tFloat, params[1] );
if ( Res && tFloat >= 0 && tFloat <= 10 ) {
Con->pushText( "setzoom: zoom level " + String::toString( tFloat ) );
mApp->setImgScale( tFloat );
} else
Con->pushText( "setzoom: value out of range" );
} else
Con->pushText( "Expected some parameter" );
}