-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpicwoodpecker.pl
1917 lines (1777 loc) · 69.7 KB
/
picwoodpecker.pl
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long;
use Pod::Usage;
use Tk;
use Tk qw(:eventtypes);
use Tk::JPEG;
use Tk::Pane;
use GD;
use Image::ExifTool qw(:Public);
use MIME::Base64;
use File::Spec;
use File::Copy;
use Fcntl;
use Time::HiRes;
###
my $VERSION=110;
$|++;
# from GD docs:
# For backwards compatibility with scripts previous versions of GD, new
# images created from scratch (width, height) are palette based by default.
# To change this default to create true color images use:
# (somewhere before creating new images.)
# [it must unneeded for jpg but if you comment the following line you'll
# get color noise]
GD::Image->trueColor(1);
################################################################################
# default configuration (that can be modified via command line parameters)
################################################################################
# source folder or glob expression to find jpg images
my $glob = './*.jpg';
# destination folder used when copying files
my $dest = '.';
# debug
my $debug = 0;
# ratio of the photo and of photo window
my $win_ratio=0.25;
my $ph_ratio= 0.25;
# index of last column and row(starting from 0(but -1 applpied after getopts!))
# used to display thumbs in a grid
my $grid_col = 6;
my $grid_row = 4;
# how many photos to preload after and before current one
my $preload = 1;
# do not load thumbnails at all!
my $nothumbs = 0;
# jpeg quality
my $jpeg_quality;
# output extension (jpg as commodity alias of jpeg)
my $out_ext = 'jpg';
# png compression factor (0-9)
my $png_compression;
# date format used by Image::ExifTool
my $date_format = '%Y_%m_%d_%H_%M_%S';
################################################################################
# Getoptions
################################################################################
unless (GetOptions (
"source|src|s=s" => \$glob,
"destination|dest|d=s" => \$dest,
"debug!" => \$debug,
"phratio|pr=f" => \$ph_ratio,
"winratio|wr=f" => \$win_ratio,
"gridx|x=i" => \$grid_col,
"gridy|y=i" => \$grid_row,
"preload|p=i" => \$preload,
"nothumbs!" => \$nothumbs,
"jpegquality|quality=i"=> \$jpeg_quality,
"extension|e=s" => \$out_ext,
"pngcompression=i"=> \$png_compression,
"dateformat|df=s" => \$date_format,
)
) {
print "GetOpt::Long returned errors (see above),".
"review available options:";
pod2usage(-verbose => 1);
}
# sanitize destination path
$dest = &sanitize_dest($dest);
# adjust x and y for the grid (which is zero based)
$grid_col -= 1;
$grid_row -= 1;
################################################################################
# other global variables
################################################################################
# @files is ArrayOfArray
# each element contains pic data as follow:
# 0 path
# 1 x
# 2 y
# 3 orientation
# 4 datetime joined with underscores
# 5 GD object of THUMB
# 6 [ GD object of PHOTO]
# the last field [6] will be filled only for current file ( which index is hold in $ph_index)
# and for elelments to be preloaded: from ($ph_index - $preload) to ($ph_index + $preload)
# thumb data [5] will be empty if $nothumbs is defined via -nothumbs commandline switch
my @files;
# @prepost is filled and cleared by next_pic
# it holds indexes of file preloaded
my @prepost;
# is the current index of photo list (@files)
my $ph_index = 0;
# display mode
my $display_mode = $nothumbs ? 'photo' : 'thumbs';
# status of loading and copying operations
my $status = "- status informations -";
# used to jump to a photo
my $gotonum;
# autoplay
my $toggle_autoplay = 0;
# seconds interval during autoplay
my $autoplay_sleep_interval = 3.0;
# the timer to do autoplay
my $tk_timer;
#
my $mw = new MainWindow ();
# output window used for big photos and thumbnails
# see http://www.perlmonks.org/?node_id=1172209
# to know why secondary windows is not yet created
my $phwin;
# the frame used in the photo window
my $scrolledframe;
# main big photo Tk::Photo object
my $tk_ph_image ;
# the label container of the current photo
my $photo_label;
# rows of thumbnails
my @temp_frames;
# curent thumbnails
my @temp_thumbs;
# help text window
my $hw;
# ADVANCED OPTIONS USED TO COPY AND POSTPROCESS PHOTOS
# advanced copy options TopLevel window
my $advw;
# allow files copied to replace file already present
my $allow_overwrite = 1;
# dump exif tags on the console if debug is enabled
my $dump_exif = 0;
# dump exif label
my $dump_exif_lbl;
# dump exif check
my $dump_exif_chk;
# bypass original file GD elaboration, simply copying it
my $bypass_orig_el = 0;
# jpeg quality label
my $jpeg_quality_lbl;
# jpeg quality entry
my $jpeg_quality_ent;
# enable multi copies off by default
my $enable_multiple_copies = 0;
# skip original image
my $skip_orig = 0;
# skip original label
my $skip_orig_lbl;
# checkbutton associated to the above
my $skip_orig_chk;
# the pattern used to have multiple copies (800x600 1024x768 ..)
my $multi_pattern = '';
# pattern label
my $multi_pattern_lbl;
# entry widget for the above
my $multi_pattern_ent;
# enable post process of copied images off by default
my $enable_postprocess = 0;
my $exiftool_path = '';
my $exiftool_args = '';
# widget used by the above
my ($post_prog_lbl, $post_prog_btn,
$post_prog_ent, $post_prog_arg_lbl, $post_prog_arg_ent);
# a lookup table for all global bindings that use chars:
# used to prevent Entry widgets to invoke callbacks when inappropriate
# see http://www.perlmonks.org/?node_id=1173808
my %bind_table = (
'<space>' => sub{©_with_name},
'<KeyRelease-question>' => \&help_me,
'<KeyRelease-p>' => \&autoplay,
);
################################################################################
# build immediatley the file list
@files = &build_list($glob);
################################################################################
$mw->Icon(-image => $mw->Pixmap(-data => &woodpecker_icon));
$mw->geometry("850x480+0+0");
$mw->title(" Pic Wood Pecker ");
$mw->optionAdd('*font', 'Courier 10');
$mw->optionAdd('*Label.font', 'Courier 10');
$mw->optionAdd( '*Entry.background', 'lavender' );
$mw->optionAdd( '*Entry.font', 'Courier 12 bold' );
# title frame
my $fr0 = $mw->Frame(-borderwidth => 2, -relief => 'groove'
)->pack(-side=>'top',-padx=>5,-pady=>5,-fill=>'x');
$fr0->Label(-text => "$0"
)->pack(-fill=>'x',-expand=>1,-side=>'left',-pady=>10);
$fr0->Button(-text => "?",-borderwidth => 4,
-command => \&help_me,
)->pack(-side=>'right',-padx=>10);
# list options frame
my $fr1 = $mw->Frame(-borderwidth => 2, -relief => 'groove'
)->pack(-side=>'top',-padx=>5,-pady=>5,-fill=>'x');
$fr1->Label(-text => "source")->pack(-side => 'left');
$fr1->Entry(-width => 20,-borderwidth => 4, -textvariable => \$glob
)->pack(-side => 'left', -expand => 1,-padx=>5);
# 10.9 addition
$fr1->Button(-padx=> 5,-text => "browse",-borderwidth => 4,
-command => sub{
$glob = $mw->chooseDirectory(-initialdir => '~',
-title => 'Choose a folder');
}
)->pack(-side => 'left',-expand => 1,-padx=>5);
# end of 10.9 addition
$fr1->Button(-padx=> 5,-text => "new list",-borderwidth => 4,
-command => sub{
&clear_current;
$ph_index = 0;
$display_mode = 'photo';
@files=&build_list($glob,undef);
&setup_draw_area;
next_pic(0);
}
)->pack(-side => 'left',-expand => 1,-padx=>5);
$fr1->Button( -padx=> 5,-text => "view list",-borderwidth => 4,
-command => sub{
if(@files){
print "Current files:\n",
map{"\t$_->[0]\n"}@files;
}
else{
print "No files in the list\n";
return;
}
}
)->pack(-side => 'left',-expand => 1,-padx=>5);
$fr1->Button( -padx=> 5,-text => "add to list",-borderwidth => 4,
-command => sub{
&clear_current;
if (scalar @files){
@files = (@files,&build_list($glob,'add'));
}
else{
@files = &build_list($glob);
$ph_index = 0;
}
$ph_index = 0 if $ph_index > $#files;
$display_mode = 'photo';
&setup_draw_area;
next_pic(0);
}
)->pack(-side => 'left',-expand => 1,-padx=>5);
$fr1->Button( -padx=> 5,-text => "clear list",-borderwidth => 4,
-command => sub{
&clear_current;
$ph_index = 0;
$display_mode = 'photo';
@files=();
}
)->pack(-side => 'left',-expand => 1,-padx=>5);
# photos destination folder options frame
my $fr1b = $mw->Frame(-borderwidth => 2, -relief => 'groove'
)->pack(-side=>'top',-padx=>5,-pady=>5,-fill=>'x');
$fr1b->Label(-text => "destination"
)->pack(-side => 'left');
$fr1b->Entry( -width => 50,-borderwidth => 4,
-textvariable => \$dest
)->pack(-side => 'left', -expand => 1,-padx=>5);
$fr1b->Button( -padx=> 5,-text => "set",
-borderwidth => 4,
-command => sub{$mw->focus}
)->pack(-side => 'right',-expand => 1,-padx=>5);
# photo ratio and window ratio options frame
my $fr2 = $mw->Frame(-borderwidth => 2, -relief => 'groove'
)->pack(-side=>'top',-padx=>5,-pady=>5,-fill=>'x');
$fr2->Label(-text => "photo ratio"
)->pack(-side => 'left',-expand => 1);
$fr2->Entry(-width => 4,-borderwidth => 4,
-textvariable => \$ph_ratio,
)->pack(-side => 'left', -expand => 1,-padx=>5);
$fr2->Label(-text => "window ratio"
)->pack(-side => 'left',-expand => 1);
$fr2->Entry(-width => 4,-borderwidth => 4,
-textvariable => \$win_ratio
)->pack(-side => 'left', -expand => 1,-padx=>5);
$fr2->Button( -padx=> 5,-text => "set ratios",-borderwidth => 4,
-command => sub{&set_ratio}
)->pack(-side => 'left',-expand => 1,-padx=>5);
$fr2->Label(-text => "debug"
)->pack(-side => 'left',-expand => 1);
$fr2->Checkbutton(-variable =>\$debug,
-command => sub { status('DarkGreen',
"debug informations to the console ".
($debug ? 'ENABLED' : 'DISABLED'));
map{ $_->configure(-state => $debug ? 'normal' : 'disabled') if defined $_ and $_->Exists
}($dump_exif_lbl,$dump_exif_chk);
}
)->pack();
# current photo exif information frame
my $label_exif_txt = "too soon to have photo data..";
my $fr3 = $mw->Frame(-borderwidth => 2, -relief => 'groove'
)->pack(-side=>'top',-padx=>5,-pady=>5,-fill=>'x');
$fr3->Label( -justify => 'left',-foreground => 'black',
-textvariable => \$label_exif_txt
)->pack;
# new name frame
my $newname;
my $suffix='';
my $fr4 = $mw->Frame(-borderwidth => 2, -relief => 'groove'
)->pack(-side=>'top',-padx=>5,-pady=>5,-fill=>'x');
$fr4->Label(-text => "copy name"
)->pack(-side => 'left',-expand => 1);
my $entryname=$fr4->Entry(-width => 30,-borderwidth => 4,
-textvariable => \$newname
)->pack(-side => 'left', -expand => 1,-padx=>5);
$fr4->Label(-text => "suffix name"
)->pack(-side => 'left',-expand => 1);
$fr4->Entry(-width => 20,-borderwidth => 4,
-textvariable => \$suffix
)->pack(-side => 'left', -expand => 1,-padx=>5,-fill=>'x');
my $fr4b = $mw->Frame(-borderwidth => 2, -relief => 'groove'
)->pack(-side=>'top',-padx=>5,-pady=>5,-fill=>'x');
my $entrynamebutton = $fr4b->Button(-borderwidth => 4,
-text => ' copy this photo ',
-command => \©_with_name,
)->pack(-expand=>1,-side=>'left',-padx=>5);#
$fr4b->Button(-text => "advanced",-borderwidth => 4,
-command => \&advanced_options,
)->pack(-side=>'right',-padx=>10);
# status frame
my $fr4c = $mw->Frame(-borderwidth => 2, -relief => 'groove'
)->pack(-side=>'top',-padx=>5,-pady=>5,-fill=>'x');
my $statuslabel = $fr4c->Label( -justify => 'left',-foreground => 'black',
-textvariable => \$status
)->pack;
# commands frame
my $fr5 = $mw->Frame(-borderwidth => 2, -relief => 'groove'
)->pack(-side=>'top',-padx=>5,-pady=>5,-fill=>'x');
my $butrwd = $fr5->Button(-borderwidth => 4,
-text => ' < ',
-command => sub {
if ($display_mode eq 'photo'){
&next_pic(-1);
}
else{
&prev_thumbs;
}
} )->pack(-side => 'left',-expand => 1,-padx=>5);
my $butfwd = $fr5->Button(-borderwidth => 4,
-text => ' > ',
-command => sub {
if ($display_mode eq 'photo'){
&next_pic(1);
}
else{
&next_thumbs;
}
})->pack(-side => 'left',-expand => 1,-padx=>5);
### these packed right side are in reverse order!
$fr5->Label(-text => "seconds"
)->pack(-side => 'right',-expand => 1);
$fr5->Entry(-width => 4,-borderwidth => 4,
-textvariable => \$autoplay_sleep_interval
)->pack(-side => 'right', -expand => 1,-padx=>5,-fill=>'x');
$fr5->Label(-text => "each"
)->pack(-side => 'right',-expand => 1);
$fr5->Button( -borderwidth => 4,
-text => 'autoplay',
-command => \&autoplay
)->pack(-side => 'right',-expand => 1,-padx=>5);
#
$fr5->Entry(-width => 3,-borderwidth => 4,
-textvariable => \$gotonum
)->pack(-side => 'right', -padx=>5);
$fr5->Button( -padx=> 5,-text => "go to photo number",-borderwidth => 4,
-command => sub{&gotonum($gotonum)}
)->pack(-side => 'right',-padx=>5);
# bindings valid for all display modes
# see http://www.perlmonks.org/?node_id=1173808
foreach my $bind (keys %bind_table){
$mw->bind($bind => $bind_table{$bind});
# remove bindings for all Tk::Entry in all windows
$mw->bind('Tk::Entry', $bind,sub{Tk->break});
}
# bind Return to for Tk::Entry to give the focus to parent
$mw->bind('Tk::Entry','<Return>' => sub{$_[0]->parent->focus});
if (@files){
print "Starting with $files[$ph_index]->[0]\n";
$butrwd->configure( -text => 'not available',-state => 'disabled' );
# see http://www.perlmonks.org/?node_id=1172209
$mw->update;
&setup_draw_area;
$nothumbs ? &next_pic(0) : &draw_thumbs;
$phwin->focus();
}
else{ print "No file to process\n" }
$mw->MainLoop;
################################################################################
# SUBS
################################################################################
sub autoplay {
if ($toggle_autoplay == 0){
$toggle_autoplay = 1 ;
$display_mode = 'photo';
&setup_draw_area();
$ph_index--;
$tk_timer = $phwin->repeat(
1000 * $autoplay_sleep_interval,
[\&next_pic,1]
);
}
else {
$toggle_autoplay = 0;
$tk_timer->cancel if $tk_timer;
}
if ($ph_index == $#files){ $tk_timer->cancel;}
}
################################################################################
sub setup_draw_area {
print "\tsetup_draw_area called\n" if $debug ;
# chek if the window exists
if (! Exists($phwin)) {
$phwin = $mw->Toplevel();
my $icon = &woodpecker_icon;
$icon =~ s/#296D17/#5c6998/;
$phwin->Icon(-image=>$mw->Pixmap(-data => $icon ));
# photo window starts just right (+865) of command window
$phwin->geometry("0x0+865+0");
$scrolledframe = $phwin->Scrolled('Frame',
-background=>'black',
-scrollbars => 'osoe',
)->pack(-expand => 1, -fill => 'both');
$photo_label = $scrolledframe->Label(-image => $tk_ph_image,
-background =>'black'
)->pack(-side => 'top',
-anchor => 'n',
-fill => 'both',
-expand => 1,
); # was just pack
$tk_ph_image = $phwin->Photo(-file => '' ) or die $!;
# see again: http://www.perlmonks.org/?node_id=1172209
# even if here is not used
# $phwin->protocol( 'WM_DELETE_WINDOW', [sub{shift->withdraw}, $phwin], );
}
# window Exists
else {
$phwin->deiconify( ) if $phwin->state() eq 'iconic';
$phwin->raise( ) if $phwin->state() eq 'withdrawn';
}
$phwin->focus;
# THUMBS display
if ($display_mode eq 'thumbs') {
return if $nothumbs;
return unless @files;
# stops autoplay when in thumb view
$toggle_autoplay=1;
# that set it to 0 and cancel the timer
&autoplay;
#
# tadàààà packForget !!
$photo_label->packForget();
my $max_x = 164 * ($grid_col + 1) + 50;
my $max_y = 164 * ($grid_row + 1) + 50;
$phwin->geometry($max_x."x".$max_y.""
);
$phwin->update();
# enable buttons
$butfwd->configure( -text => ' > ',-state => 'normal' );
$butrwd->configure( -text => ' < ',-state => 'normal' );
$entrynamebutton->configure(-state=>'disabled');
# BINDINGS
$phwin->bind('<KeyRelease-Down>' => sub {&next_thumbs()} );
$phwin->bind('<KeyRelease-Up>' => sub {&prev_thumbs()} );
#
$phwin->bind('<KeyRelease-Left>' => sub {$_[0]->focusPrev;} );
$phwin->bind('<KeyRelease-Right>' => sub {$_[0]->focusNext;});
#
$mw->bind('<KeyRelease-Right>' => sub {&next_thumbs()} );
$mw->bind('<KeyRelease-Left>' => sub {&prev_thumbs()} );
}
# PHOTO display
else{
&clear_thumbs();
$photo_label->pack();
return unless @files;
$phwin->geometry( int($files[$ph_index]->[1]*$win_ratio+30) .
"x".
int($files[$ph_index]->[2]*$win_ratio+30).
""
);
$phwin->title( &file_name($files[$ph_index]->[0]) );
$entrynamebutton->configure(-state=>'normal');
# PHOTO display BINDINGS
$phwin->bind('<KeyRelease-Down>' =>
sub {
return if $nothumbs;
$display_mode = 'thumbs';
$tk_ph_image->delete if $tk_ph_image->blank;
&clear_current();
&setup_draw_area();
&draw_thumbs()
} );
$phwin->bind('<KeyRelease-Up>' =>
sub {
return if $nothumbs;
$display_mode = 'thumbs';
$tk_ph_image->delete if $tk_ph_image->blank;
&clear_current();
&setup_draw_area();
&draw_thumbs();
} );
$phwin->bind('<KeyRelease-Left>' => sub {&next_pic(-1) if $ph_index > 0} );
$phwin->bind('<KeyRelease-Right>' => sub {&next_pic(1) if $ph_index < $#files} );
#
$mw->bind('<KeyRelease-Left>' => sub {&next_pic(-1) if $ph_index > 0} );
$mw->bind('<KeyRelease-Right>' => sub {&next_pic(1) if $ph_index < $#files} );
}# end of PHOTO display setup
# bindings valid for all display modes
# see http://www.perlmonks.org/?node_id=1173808
foreach my $bind (keys %bind_table){
$phwin->bind($bind => $bind_table{$bind});
}
}
################################################################################
sub draw_thumbs {
print "\tdraw_thumbs called\n" if $debug;
next_pic(0) if $nothumbs;
my $row = 0;
my $col = 0;
&clear_thumbs();
# just get ids of which thums to load
my @cur_thumbs = &which_thumbs();
$phwin->title( 'thumbnails '.($#cur_thumbs >= 1 ?
(join '-',@cur_thumbs[0,-1]):
$cur_thumbs[0]).
" of 0-$#files (use TAB to navigate,".
" RETURN to view, UP and DOWN to load other thumbnails) " );
my $temp_frame = $scrolledframe->Frame(
-background => 'black', -borderwidth => 0,
)->pack(-side=>'top',-fill=>'x');
push @temp_frames,$temp_frame;
foreach my $th_ind (@cur_thumbs){
my $ph_thumb = $scrolledframe->Photo(-file => '' ) or warn $!;
$ph_thumb->configure( -file => undef,
-data => MIME::Base64::encode($files[$th_ind]->[5]->jpeg())
);
push @temp_thumbs,$ph_thumb;
my $canv = $temp_frame->Canvas(
-background =>'black',
-borderwidth => 0,
# no white border when not selected
-highlightbackground => 'black',
# do not allow scrolling pics inside..
-scrollregion => [0,0,160,160],
-highlightcolor => 'red3',
-takefocus => 1,
-height => 160,
-width => 160 ,
)->pack(-side => 'left',-expand => 1,-padx=>5);
$canv->createImage( 81,81,
-image => $ph_thumb,
-tags => ["$th_ind"],
);
# bind the selected canvas
$canv->CanvasBind('<Return>', sub{&choice_thumb($th_ind) } );
# highlight the first one of the current grid
$canv->focusForce if $th_ind == $ph_index;
# grid mamagement
if ($col > 0 and ($col % ($grid_col || 1)) == 0){
$col = 0;
$row++;
$temp_frame = $scrolledframe->Frame(
-background => 'black', -borderwidth => 0,
)->pack(-side=>'top',-fill=>'x');
push @temp_frames,$temp_frame;
}
else{$col++}
} # end of foreach my $th_ind (@cur_thumbs)
$phwin->focus;
}
################################################################################
sub draw_photo {
my $ph_index = shift;
print "\tdraw_photo got:\n\t",(join '|',map{defined $_ ? $_ : 'undef'}
(@{$files[$ph_index]}[0..4],
$files[$ph_index]->[5]?'THUMB':'NO DATA',
$files[$ph_index]->[6]?'PHOTO':'NO DATA',
)),"\n" if $debug;
$tk_ph_image->delete if $tk_ph_image->blank;
$phwin->geometry( int($files[$ph_index]->[1]*$win_ratio+30) .
"x".int($files[$ph_index]->[2]*$win_ratio+30));
$phwin->title( &file_name($files[$ph_index]->[0]) );
my $small_w = int($files[$ph_index]->[1] * $ph_ratio);
my $small_h = int($files[$ph_index]->[2] * $ph_ratio);
# create the resized but still empty GD image
my $resized = GD::Image->new($small_w,$small_h);
# copy from source into resized on
$resized->copyResampled($files[$ph_index]->[6],0,0,0,0,
$small_w,
$small_h,
$files[$ph_index]->[6]->width,
$files[$ph_index]->[6]->height);
$tk_ph_image->configure( -file => undef,
-data => MIME::Base64::encode($resized->jpeg())
);
# configure the Tk::Label to use the Tk::Photo as image
$photo_label->configure(-image => $tk_ph_image );
# update exif text
my @times=split /_/,($files[$ph_index]->[4] || '');
$label_exif_txt = "file ".($ph_index+1)." of ".($#files+1)." ".
"$files[$ph_index]->[0]\n".
"width:\t\t$files[$ph_index]->[1]\n".
"height:\t\t$files[$ph_index]->[2]\n".
"orientation:\t".
($files[$ph_index]->[3] ?
$files[$ph_index]->[3] :
'-NOT FOUND-')."\n".
"creation:\t".
(join '.',map{defined $_ ? $_ : 'x'} @times[0..2]).' '.
(join ':',map{defined $_ ? $_ : 'x'} @times[3..5])."\n".
"data loaded:\t".
(defined $files[$ph_index]->[6] ? 'OK' : 'ERROR');
# udate the name used to (eventually) save current pic
$newname = &create_name($files[$ph_index]->[4]);
$phwin->focus();
}
################################################################################
sub next_pic {
return unless @files;
my $increment = shift;
$ph_index = $ph_index + $increment;
$tk_timer->cancel if $ph_index > $#files;
$ph_index = $#files if $ph_index > $#files;
$ph_index = 0 if $ph_index < 0;
#return unless @files;
print +($debug ? "\n" : '').
"Considering files[$ph_index] $files[$ph_index]->[0]\n";
&setup_draw_area unless Exists($phwin);
# enable button back because it starts disabled
if ($ph_index > 0 && $butrwd->cget('-state') eq 'disabled'){
$butrwd->configure( -text =>' < ',-state => 'normal' );
}
# disable it if first photo
if ($ph_index == 0 ){
$butrwd->configure( -text =>'not available',-state => 'disabled' );
}
# disable fwd button if last photo
if ($ph_index == $#files ){
$butfwd->configure( -text =>'not available',-state => 'disabled' );
}
# enable it again if not last photo
if ($ph_index < $#files && $butfwd->cget('-state') eq 'disabled'){
$butfwd->configure( -text =>' > ',-state => 'normal' );
}
# preload
if ($preload > 0){
# check if img is yet loaded(change of ratio cleared it?)
if (defined $files[$ph_index]->[6]) {
print "\tphoto data yet defined for files[$ph_index]\n" if $debug;
&draw_photo($ph_index);
}
else {
print "\tphoto data NOT defined for files[$ph_index]\n" if $debug;
$files[$ph_index]->[6] = &get_ph_data($ph_index);
print "\tfilled photo data for current: files[$ph_index]\n" if $debug;
&draw_photo($ph_index);
}
# dump exif tags on request
if ($debug and $dump_exif){
print "\n\t\tEXIF tags for file: ".$files[$ph_index][0]."\n\n";
my $exifTool = new Image::ExifTool;
$exifTool-> Options(Binary => 1, Composite => 1,
DateFormat => $date_format, #'%Y_%m_%d_%H_%M_%S',
Unknown => 2, Verbose => 0);
my $exifinfo = $exifTool->ImageInfo($files[$ph_index][0]);
# suppres thumbs data
$exifinfo->{ThumbnailImage} = '..thumbnail data skipped for brevity..' if $exifinfo->{ThumbnailImage};
foreach my $tag (sort keys %$exifinfo){
print "\t\t$tag\t=>\t$$exifinfo{$tag}\n"
}
print "\n";
}
# elaborate preload: filling and clearing actions
@prepost = grep {$_ != $ph_index &&
$_ >= 0 &&
$_ <= $#files}
($ph_index - $preload)..($ph_index + $preload);
print "\tcurrent $ph_index preload [@prepost]\n" if $debug;
foreach my $ind (@prepost){
if (defined $files[$ind]->[6]){
print "\tskipping PRELOADED files[$ind] (yet defined)\n" if $debug;
next;
}
else {$files[$ind]->[6] = &get_ph_data($ind);
print "\tfilled files[$ind] photo data\n" if $debug; }
}
# delete unneeded elements leaved behind or forward
if ($increment == 1){
if ( $prepost[0]-1 >= 0 && $prepost[0]-1 < $ph_index){
$files[$prepost[0]-1]->[6] = undef;
print "\tcleared files[".($prepost[0]-1)."] photo data\n" if $debug;
}
}
elsif ($increment == -1){
if ($prepost[-1]+1 <= $#files){
$files[$prepost[-1]+1]->[6] = undef;
print "\tcleared files[".
($prepost[-1]+1)."] photo data\n" if $debug;
}
}
else{print "\tzero or other non significant increment for next_pic\n" if $debug}
# update status
if ((scalar grep{defined $files[$_]->[6]}@prepost,$ph_index )
==
(@prepost +1)){
status('DarkGreen',"\tOK loaded ".
(@prepost +1).
" photo data for files[".
(join',',$ph_index,@prepost)."]");
}
else{status('red3',"\tERROR not all loaded correctly!");}
}
# no preload activated
else{
@prepost = ($ph_index);
$files[$ph_index]->[6] = &get_ph_data($ph_index);
&draw_photo($ph_index);
if (defined $files[$ph_index - 1]->[6]){
$files[$ph_index - 1]->[6] = undef;
print "\tCurrent $ph_index cleared files[".
($ph_index-1)."]\n" if $debug;
}
}
# ultradebug file list
if ($debug == 2){
foreach my $f (0..$#files){
print "FOR FILES[$f]",
(defined $files[$f]->[6]?'DATA DEFINED':'undef'),
"\n";
}
}
}
################################################################################
sub get_ph_data {
my $index = shift;
return unless -e $files[$index]->[0];
# load original pic file in GD using general purpose method
my $gd_image = GD::Image->new($files[$index]->[0]);
# if not defined try newFromJpeg
unless ($gd_image){
status('red3', "\tGD image not defined for [$files[$index]->[0]]".
" i'll try assuming it is JPEG");
$gd_image = GD::Image->newFromJpeg($files[$index]->[0]);
}
# if it is still undefined...
unless ($gd_image){
status('red3', "\tGD image UNAVAILABLE for [$files[$index]->[0]] $!\n");
#added in 9.12e
return undef;
}
# handle rotation
if (defined $files[$index]->[3] && $files[$index]->[3] =~/(\d+)/){
my $rot = $1;
print "\tRotation detected in main photo: $rot\n" if $debug;
$gd_image = &handle_rotation(\$gd_image,$rot);
}
# check if dimensions are not present (probably never happens)
unless ($files[$index]->[1]){
$files[$index]->[1] = $gd_image->width;
print "\twidth not in EXIF tags: i'll use [$files[$index]->[1]]\n";
}
unless ($files[$index]->[2]){
$files[$index]->[2] = $gd_image->height;
print "\theight not in EXIF tags: i'll use [$files[$index]->[2]]\n";
}
return $gd_image;
}
################################################################################
sub clear_thumbs{
foreach my $temp_thumb(@temp_thumbs){
$temp_thumb->delete if $temp_thumb->blank;
$temp_thumb = undef;
}
foreach my $slave_frame (@temp_frames){
next unless Exists($slave_frame);
$slave_frame->destroy;
}
@temp_frames = ();
@temp_thumbs = ();
}
################################################################################
sub clear_current {
return unless @files;# prevent autovivification in the next line
map { $files[$_]->[6] = undef} @prepost,$ph_index;
print "\tcleared photo data of preloaded files [@prepost]\n" if $debug;
}
################################################################################
sub next_thumbs {
print "\tnext_thumbs: index was $ph_index\n" if $debug;
$ph_index += ($grid_col + 1) * ($grid_row + 1) ;
print "\tnext_thumbs: index is now $ph_index\n" if $debug;
if ($ph_index > $#files){$ph_index = $#files}
&setup_draw_area() unless Exists $phwin;
&draw_thumbs();
}
################################################################################
sub prev_thumbs {
print "\tprev_thumbs: index was $ph_index\n" if $debug;
$ph_index -= ($grid_col + 1) * ($grid_row + 1) ;
print "\tprev_thumbs: index is now $ph_index\n" if $debug;
if ($ph_index < 0){$ph_index = 0}
&setup_draw_area() unless Exists $phwin;
&draw_thumbs();
}
################################################################################
sub which_thumbs {
my $last = ($grid_col + 1) * ($grid_row + 1) - 1 + $ph_index;
if ($last > $#files){$last = $#files}
print "\twhich_thumbs: [",(join ' ',($ph_index .. $last)),"]\n" if $debug;
$label_exif_txt = "thumbnail grid of photos: ".
(join '-',($ph_index,$last))."(0 .. $#files)";
return ($ph_index .. $last);
}
################################################################################
sub choice_thumb {
# see master zentara: http://www.perlmonks.org/?node_id=969034
# http://www.perlmonks.org/?node_id=931375
my ($index) = @_;
print "\tchoice_thumb received $index\n" if $debug;
&clear_thumbs();#added in v9.12b
$display_mode = 'photo';
&setup_draw_area;
&gotonum($index +1);
}
################################################################################
sub get_exif_data {
my $file = shift;
my $exifTool = new Image::ExifTool;
$exifTool-> Options(Binary => 1, Composite => 1,
DateFormat => $date_format, #'%Y_%m_%d_%H_%M_%S',
Unknown => 2, Verbose => 0);
my $exifinfo = $exifTool->ImageInfo($file,'ImageWidth',
'ImageHeight',
'Orientation',
'DateTimeOriginal',
'ThumbnailImage');
###
foreach my $it (qw(ImageWidth ImageHeight Orientation DateTimeOriginal)){
unless ($$exifinfo{$it}){
print "WARNING: '$it' exif not defined for file [$file]\n" if $debug;
}
}
my $temp_gd;
unless ($$exifinfo{'ImageHeight'}){
print "WRNING: trying to extract height using GD\n" if $debug;
$temp_gd = GD::Image->new($file);
$$exifinfo{'ImageHeight'} = $temp_gd->height;
}
unless ($$exifinfo{'ImageWidth'}){
print "WRNING: trying to extract width using GD\n" if $debug;
$temp_gd ||= GD::Image->new($file);
$$exifinfo{'ImageWidth'} = $temp_gd->width;
}
###
my $gd;
if (defined $$exifinfo{'ThumbnailImage'} and ${$$exifinfo{'ThumbnailImage'}}){ # double dereference only for thumb!!!
eval{$gd = GD::Image->newFromJpegData(${$$exifinfo{'ThumbnailImage'}}||'')};
if ($@){
print "ERROR creating a thumbnail for file [$file].".
"I will use an empty one.\n";
$gd = GD::Image->new(160,160);
}
}
else {
print "WARNING exif thumb not found in file [$file]: creating it with GD\n" if $debug;
$temp_gd ||= GD::Image->new($file);
$gd=GD::Image->new(160,160);
$gd->copyResampled($temp_gd,0,0,0,0,
160,
160,
$$exifinfo{'ImageWidth'},
$$exifinfo{'ImageHeight'}
)
}
undef $temp_gd;
##
# handle rotation
if(defined $$exifinfo{'Orientation'} && $$exifinfo{'Orientation'}=~/(\d+)/){
my $rot = $1;
print "Rotation detected in thumbnail: $rot\t[$file]\n" if $debug;
$gd = &handle_rotation(\$gd,$rot);
if ($rot == 90 or $rot == 270){
# rearrange returned exif infos to adjust the photo window too
my $orig_w = $$exifinfo{'ImageWidth'};
my $orig_y = $$exifinfo{'ImageHeight'};
$$exifinfo{'ImageHeight'} = $orig_w;
$$exifinfo{'ImageWidth'} = $orig_y;
}
}
# return a five elements list
return ($$exifinfo{'ImageWidth'},
$$exifinfo{'ImageHeight'},
$$exifinfo{'Orientation'},
$$exifinfo{'DateTimeOriginal'},
($nothumbs ? '' : $gd)
);
}
################################################################################
sub handle_rotation {
my $imgref = shift;
my $rot = shift;
my $gd = $$imgref;
if ($rot == 90){
$gd=$gd->copyRotate90();
}
elsif($rot == 180){
$gd=$gd->copyRotate180();
}
elsif($rot == 270){
$gd=$gd->copyRotate270();
}
else{print "Warning! unexpected rotation [$rot] received!\n"}
return $gd;
}
################################################################################
sub build_list{
my $glob=shift;
my $add = shift;
print "build_list received [$glob]\n" if $debug;
my @list = glob($glob);
if (@list == 1 and -d $glob) {
print "DIR found: [$glob] will be converted to [$glob".
'/*.jpg]'."\n" if $debug;
@list = glob($glob.'/*.jpg');
}
elsif (scalar @list and ! -e $list[0] ){
print "[$glob] NOT found: './*.jpg' will be used\n" if $debug;
@list = glob('./*.jpg');
}
elsif(scalar @list == 0){print "Empty list searching [$glob]!\n";return();}
else {1}
return () unless @list;
print "Please wait while processing ".(scalar @list)." files....\n";
# rel2abs
@list = map {File::Spec->file_name_is_absolute($_) ?
$_ : File::Spec->rel2abs($_)} @list;
# when adding to the list check for duplicates
if ($add){
my %uniq;
@uniq{@list}=map {1} @list;
foreach my $yet (@files){
if (defined $yet->[0] && exists $uniq{$yet->[0]} ){
print "\tskipping $yet->[0] because yet in the list\n";
delete $uniq{$yet->[0]};
}
}
@list = keys %uniq;
}
my @files_to_add;