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