Some command line options are not GNU/POSIX compliant (4)
[geeqie.git] / scripts / decode_sim
1 #! /usr/bin/perl
2 #
3
4 use strict;
5 use warnings;
6
7 use GD;
8
9 my $file = shift or die;
10
11 open my $in_fh, '<', $file or die;
12 binmode $in_fh;
13 my $type = <$in_fh>;
14 chomp $type;
15
16 die unless $type eq 'SIMcache';
17
18 while (<$in_fh>)
19 {
20    my $raw = $_;
21    chomp;
22    if (/^#(.*)/)
23    {
24       printf "Comment: %s\n", $1;
25    }
26    elsif (/^Dimensions=\[(\d+) x (\d+)\]$/)
27    {
28       printf "Original image dimensions: %dx%d\n", $1, $2;
29    }
30    elsif (/^Date=(\[-1\])/)
31    {
32       printf "Exif Date Original: %s\n", $1;
33    }
34    elsif (/^Date=(.*)/)
35    {
36       my $unix_time = substr($1, 1, 10);
37
38       my ($S, $M, $H, $d, $m, $Y) = localtime($unix_time);
39       $m += 1;
40       $Y += 1900;
41       my $date_time = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $Y, $m, $d, $H, $M, $S);
42
43       printf "Exif Date Original: %s %s\n", $1, $date_time;
44    }
45    elsif (/^Checksum=(.*)/)
46    {
47       printf "Checksum (never seen): %s\n", $1;
48    }
49    elsif (/^MD5sum=\[(.*)\]$/)
50    {
51       printf "MD5 sum: %s\n", $1;
52    }
53    elsif ($raw =~ /^SimilarityGrid\[(\d+) x (\d+)\]=(.*)$/s)
54    {
55       printf "Similarity image %dx%d\n", $1, $2;
56       if ($1 != 32 or $2 != 32)
57       {
58          print "Warning, similarity data are not size 32x32!\n";
59       }
60
61       my $simn = $1 * $2 * 3;
62       my ($width, $height) = ($1, $2);
63       my $simdata = $3;
64
65       $simdata = substr($simdata, 0, -1) if length($simdata) == $simn + 1; # In case all fits to one line
66
67       if (length($simdata) < $simn)
68       {
69          read $in_fh, $simdata, $simn - length($simdata), length($simdata) or die;
70          my $dummy;
71          read $in_fh, $dummy, 1 or die;
72       }
73
74       printf "Warning, similarity data is not %d bytes", $simn unless length($simdata) == $simn;
75
76       if (length($simdata) == $simn)
77       {
78          my $gd = GD::Image->new($width, $height, 1);
79
80          for (my $x = 0; $x < $width; $x++)
81          {
82             for (my $y = 0; $y < $height; $y++)
83             {
84                my $colors = substr($simdata, ($x + $y * $width) * 3, 3);
85                my @rgb = unpack("CCC", $colors);
86                my $index = $gd->colorAllocate(@rgb);
87                $gd->setPixel($x, $y, $index);
88             }
89          } ## end for (my $x = 0; $x < $width; $...
90
91          my $png = $gd->png;
92          open my $display_fh, '|-', qw(display -resize), sprintf("%dx%d", $width*10, $height*10), '-' or die;
93          binmode $display_fh;
94          print {$display_fh} $png;
95          close $display_fh;
96       } ## end if (length($simdata) == $simn)
97    } ## end elsif (/^SimilarityGrid\[(\d+)...
98    else
99    {
100       my $field = $_;
101       $field = $1 if /^(.*)=/;
102
103       printf "Unknown Field '$field'\n";
104    } ## end elsif (/^SimilarityGrid\[(\d+)...
105 } ## end while (<$in_fh>)
106
107 close $in_fh;