blib/lib/App/Exifpic.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 88 CC 2 SCAR 9.4
CC
2
Cov
0%
process_image
6.0
run
2.0
get_cores
2.0
CRAP 2.6
1 package App::Exifpic;
2 use 5.010;
3 use strict;
4 use warnings;
5 use threads;
6 use Thread::Queue;
7 use autodie;
8 use Image::ExifTool;
9 use Imager;
10 use File::Slurp qw(read_file);
11  
12 use constant EXIT_SUCCESS => 0;
13  
14 # ABSTRACT: Extract embedded jpegs from RAW files
15  
16 our $VERSION = '0.02'; # VERSION: Generated by DZP::OurPkg:Version
17  
18  
19 # Run our application...
20  
21  
22 sub run {
23 my ($self) = shift;
24 # Imager needs to be preloaded if we're using threads.
25 Imager->preload;
26  
27 # Right now we treat everything as a file to process...
28 my $work_queue = Thread::Queue->new;
29 $work_queue->enqueue(@_);
30 $work_queue->end;
31  
32 # Spawn our threads, each of which will process files until we're done.
33  
34 my @threads;
35 my $cores = $self->get_cores();
36  
37 # TODO: This could look less ugly
38 for (1..$cores) {
39 push(@threads,
40 threads->create( sub {
41 while (my $src = $work_queue->dequeue) {
42 $self->process_image($src);
43 }
44 })
45 );
46 }
47  
48 # Join threads.
49 foreach my $thread (@threads) { $thread->join; }
50  
51 return EXIT_SUCCESS;
52 }
53  
54  
55 sub process_image {
56 my ($self, $raw) = @_;
57  
58 my ($new) = $raw =~ m{(.*).CR2$}i;
59  
60 next if not $new; # Skip non-CR2 files
61 $new .= ".jpg";
62  
63 say "$raw -> $new...";
64  
65 my $exiftool = Image::ExifTool->new;
66  
67 my $exif = $exiftool->ImageInfo($raw, [qw(PreviewImage)], { Binary => 1 });
68  
69 my $img = Imager->new();
70 $img->read(data => ${$exif->{PreviewImage}})
71 or die $img->errstr;
72  
73 $img
74 ->scale(type=>'min', xpixels=>2048, ypixels=>2048)
75 ->write(file=>$new, type=>'jpeg', jpegquality=>100)
76 ;
77  
78 # Add EXIF info back in to the new file.
79  
80 $exiftool->SetNewValuesFromFile($raw);
81 $exiftool->WriteInfo($new);
82  
83 return;
84 }
85  
86  
87 sub get_cores {
88  
89 # This only works on systems with a /proc/cpuinfo
90  
91 my $cpuinfo = read_file("/proc/cpuinfo");
92  
93 my ($cores) = $cpuinfo =~ m{.*processor\s*:\s*(?<cores>\d+)}msi;
94 $cores++;
95  
96 return $cores;
97 }
98  
99  
100 1;
101  
102 __END__