Modify decode_sim perl script for date-time
[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=(.*)/)
31    {
32       my $unix_time = substr($1, 1, 10);
33
34       my ($S, $M, $H, $d, $m, $Y) = localtime($unix_time);
35       $m += 1;
36       $Y += 1900;
37       my $date_time = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $Y, $m, $d, $H, $M, $S);
38
39       printf "Exif Date Original: %s %s\n", $1, $date_time;
40    }
41    elsif (/^Checksum=(.*)/)
42    {
43       printf "Checksum (never seen): %s\n", $1;
44    }
45    elsif (/^MD5sum=\[(.*)\]$/)
46    {
47       printf "MD5 sum: %s\n", $1;
48    }
49    elsif ($raw =~ /^SimilarityGrid\[(\d+) x (\d+)\]=(.*)$/s)
50    {
51       printf "Similarity image %dx%d\n", $1, $2;
52       if ($1 != 32 or $2 != 32)
53       {
54          print "Warning, similarity data are not size 32x32!\n";
55       }
56
57       my $simn = $1 * $2 * 3;
58       my ($width, $height) = ($1, $2);
59       my $simdata = $3;
60
61       $simdata = substr($simdata, 0, -1) if length($simdata) == $simn + 1; # In case all fits to one line
62
63       if (length($simdata) < $simn)
64       {
65          read $in_fh, $simdata, $simn - length($simdata), length($simdata) or die;
66          my $dummy;
67          read $in_fh, $dummy, 1 or die;
68       }
69
70       printf "Warning, similarity data is not %d bytes", $simn unless length($simdata) == $simn;
71
72       if (length($simdata) == $simn)
73       {
74          my $gd = GD::Image->new($width, $height, 1);
75
76          for (my $x = 0; $x < $width; $x++)
77          {
78             for (my $y = 0; $y < $height; $y++)
79             {
80                my $colors = substr($simdata, ($x + $y * $width) * 3, 3);
81                my @rgb = unpack("CCC", $colors);
82                my $index = $gd->colorAllocate(@rgb);
83                $gd->setPixel($x, $y, $index);
84             }
85          } ## end for (my $x = 0; $x < $width; $...
86
87          my $png = $gd->png;
88          open my $display_fh, '|-', qw(display -resize), sprintf("%dx%d", $width*10, $height*10), '-' or die;
89          binmode $display_fh;
90          print {$display_fh} $png;
91          close $display_fh;
92       } ## end if (length($simdata) == $simn)
93    } ## end elsif (/^SimilarityGrid\[(\d+)...
94    else
95    {
96       my $field = $_;
97       $field = $1 if /^(.*)=/;
98
99       printf "Unknown Field '$field'\n";
100    } ## end elsif (/^SimilarityGrid\[(\d+)...
101 } ## end while (<$in_fh>)
102
103 close $in_fh;