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