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