d51790613d64346bc58439aab97beae6c80a544e
[minime.git] / termimg
1 #!/usr/bin/perl
2 use 5.014;
3 my ($file, $size) = @ARGV;
4
5 my @info = eval {
6         require Image::ExifTool;
7         my $exif = Image::ExifTool->new->ImageInfo($file);
8         die "exiftool: $exif->{Error}\n" if $exif->{Error};
9         return (
10                 $exif->{MIMEType},
11                 $exif->{ImageSize} ? "$exif->{ImageSize} ($exif->{Megapixels}MP)" : (),
12                 join(' ',
13                         $exif->{YCbCrSubSampling} // (),
14                         $exif->{Interlace} // (),
15                         $exif->{Compression} // $exif->{FileType},
16                         $exif->{SVGVersion} // $exif->{PDFVersion} // (),
17                 ),
18                 $exif->{ColorType} ? "$exif->{BitDepth}bpp $exif->{ColorType}" : (),
19                 sprintf('exif x%d', scalar %{$exif}),
20                 $exif->{Thumb} ? "thumb $exif->{ThumbnailLength}B" : (),
21                 (map "@ $_",
22                         $exif->{DateCreated} //
23                         $exif->{CircaDateCreated} //
24                         $exif->{ModifyDate} //
25                         $exif->{ZipModifyDate} //
26                         (),
27                 ),
28                 $exif->{GPSPosition} ?  $exif->{GPSPosition} =~ s/ deg/./gr :
29                 $exif->{Location} ? $exif->{Location} : (),
30                 $exif->{GPSAltitude} ? $exif->{GPSAltitude} : (),
31                 (map "hw $_", join(' ',
32                         $exif->{Make} // (), $exif->{Model} // (),
33                         $exif->{FOV} ? "(FOV $exif->{FOV})" : (),
34                 ) || ()),
35                 (map "sw $_", $exif->{Software} // $exif->{Application} // ()),
36                 (map "> $_", $exif->{'Description-nl'} // ()),
37                 $exif->{Warning} ? "! $exif->{Warning}" : (),
38         );
39 };
40 warn $@ if $@;
41
42 my $filesize = (stat $file)[7];
43 eval {
44         require Digest::MD5;
45         open my $bin, '<', $file;
46         binmode $bin;
47         my $md5 = Digest::MD5->new->addfile($bin)->b64digest;
48         push @info, "# $md5 ($filesize)";
49 } or warn $@;
50
51 open my $pgm, '-|', convert => (
52         -compress => 'none',
53         '+distort' => SRT => '0,0 1,.56 0',
54         -thumbnail => $size || '40x12',
55         $file => 'pgm:-'
56 ) or die $!;
57
58 if (<$pgm> eq "P2\n") {
59         my ($width, $height) = split ' ', <$pgm>;
60         <$pgm>;  # ignore depth
61         my @ch = split //, " .:oO@";
62         while (<$pgm>) {
63                 print $ch[ $_ * @ch >> 8 ] for /\d+/g;
64                 print ' ', shift @info if @info;
65                 print $/;
66         }
67         substr $_, 0, 0, ' ' x ($width + 1) for @info;
68 }
69 say for @info;