Skip to content

Commit

Permalink
Merge pull request #6 from estrabd/RT-113716-signature_as_char_string…
Browse files Browse the repository at this point in the history
…-possible-not-correct-at-all

Replaced unsigned int unpack template to used signed (per puzzle.h).
  • Loading branch information
B. Estrade committed Jun 7, 2016
2 parents 57607ef + 8852984 commit c935024
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 14 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Makefile.old
Makefile
MYMETA.json
MYMETA.yml
Libpuzzle.bs
Libpuzzle.c
Libpuzzle.o
blib/
pm_to_blib
3 changes: 3 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ t/pics/luxmarket_tshirt01_sheum.jpg
typemap
Libpuzzle.xs
CONTRIBUTING
bin/p5-puzzle-diff
bin/p5-puzzle-sig
bin/p5-puzzle-ngrams
7 changes: 0 additions & 7 deletions Makefile.PL
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
use 5.014004;
use ExtUtils::MakeMaker;

# Check that the local system has libpuzzle installed using
# Devel::CheckLib.

use lib 'inc';
use Devel::CheckLib;
check_lib_or_exit (lib => 'puzzle', header => 'puzzle.h');

# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
my $repo = 'https://github.com/estrabd/Image-Libpuzzle';
Expand Down
52 changes: 52 additions & 0 deletions bin/p5-puzzle-diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env perl

use strict;
use warnings;

use Image::Libpuzzle ();

my $p1 = Image::Libpuzzle->new;
my $p2 = Image::Libpuzzle->new;

my $sig1 = $p1->fill_cvec_from_file($ARGV[0]);
my $sig2 = $p2->fill_cvec_from_file($ARGV[1]);

print $p1->vector_normalized_distance($p2),qq{\n};

__END__
=pod
=head1 NAME
p5-puzzle-diff - output default distance between two images
=head1 SYNOPSIS
$ p5-puzzle-diff path/to/image1 path/to/image2
=head1 DESCRIPTION
This script is an implementation of the puzzle-diff(8) utility provide with libpuzzle(3) using
the bindings provided for by L<Image::Libpuzzle>.
The output is the normalized distance between two images.
=head1 BUGS AND LIMITATIONS
Currently this script accepts no options other than the names of the image files to compare.
Please report them via L<https://github.com/estrabd/p5-puzzle-xs/issues>.
=head1 AUTHOR
B. Estrade <estrabd@gmail.com>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2015 by B. Estrade
This library is free software; you can redistribute it and/or modify it under
the same terms as Perl itself, either Perl version 5.14.4 or, at your option,
any later version of Perl 5 you may have available.
=cut
62 changes: 62 additions & 0 deletions bin/p5-puzzle-ngrams
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env perl

use strict;
use warnings;

use Image::Libpuzzle ();

my $ngram_size = $Image::Libpuzzle::DEFAULT_NGRAM_SIZE;

if ($ARGV[1] and int $ARGV[1] > 0) {
$ngram_size = int $ARGV[1];
}
if ($ARGV[1] and int $ARGV[1] <= 0) {
warn qq{optional argument for ngram size must be >= 1; default is $Image::Libpuzzle::DEFAULT_NGRAM_SIZE};
exit 1;
}

my $p1 = Image::Libpuzzle->new;

my $sig1 = $p1->fill_cvec_from_file($ARGV[0]);

my $ngrams_ref = $p1->signature_as_char_ngrams($ngram_size);

foreach my $n (@$ngrams_ref) {
print qq{$n\n};
}

__END__
=pod
=head1 NAME
p5-puzzle-ngrams - produce list of ngrams from an image
=head1 SYNOPSIS
$ p5-puzzle-sig path/to/image [ngram_size]
=head1 DESCRIPTION
This script outputs a ngrams of the character representation of an image as provide for in libpuzzle(3) using
the bindings provided for by L<Image::Libpuzzle>.
=head1 BUGS AND LIMITATIONS
Currently this script accepts no options other than the name of the file to sign.
Please report them via L<https://github.com/estrabd/p5-puzzle-xs/issues>.
=head1 AUTHOR
B. Estrade <estrabd@gmail.com>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2015 by B. Estrade
This library is free software; you can redistribute it and/or modify it under
the same terms as Perl itself, either Perl version 5.14.4 or, at your option,
any later version of Perl 5 you may have available.
=cut
50 changes: 50 additions & 0 deletions bin/p5-puzzle-sig
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env perl

use strict;
use warnings;

use Image::Libpuzzle ();

my $p1 = Image::Libpuzzle->new;

my $sig1 = $p1->fill_cvec_from_file($ARGV[0]);

print $p1->signature_as_char_string, qq{\n};

__END__
=pod
=head1 NAME
p5-puzzle-sig - output character representation of image signature
=head1 SYNOPSIS
$ p5-puzzle-sig path/to/image
=head1 DESCRIPTION
This script outputs a character representation of an image as provide for in libpuzzle(3) using
the bindings provided for by L<Image::Libpuzzle>.
The output is the character representation of the signature, in the range of [0 .. 4].
=head1 BUGS AND LIMITATIONS
Currently this script accepts no options other than the name of the file to sign.
Please report them via L<https://github.com/estrabd/p5-puzzle-xs/issues>.
=head1 AUTHOR
B. Estrade <estrabd@gmail.com>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2015 by B. Estrade
This library is free software; you can redistribute it and/or modify it under
the same terms as Perl itself, either Perl version 5.14.4 or, at your option,
any later version of Perl 5 you may have available.
=cut
17 changes: 10 additions & 7 deletions lib/Image/Libpuzzle.pm
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ our $PUZZLE_CVEC_SIMILARITY_HIGH_THRESHOLD = Image::Libpuzzle->PUZZLE_CVEC_SIMI
our $PUZZLE_CVEC_SIMILARITY_LOW_THRESHOLD = Image::Libpuzzle->PUZZLE_CVEC_SIMILARITY_LOW_THRESHOLD;
our $PUZZLE_CVEC_SIMILARITY_LOWER_THRESHOLD = Image::Libpuzzle->PUZZLE_CVEC_SIMILARITY_LOWER_THRESHOLD;

# uses unpack as bin to char and $self accessor to get signature directly from the internal cvec
# uses unpack as bin to a signed char, then add 2 to remove negative number; provides range of 0..4;
# and $self accessor to get signature directly from the internal cvec
sub signature_as_char_string {
my $self = shift;
my @sig = unpack("C*", $self->get_signature());
# take as
my @sig = unpack("c*", $self->get_signature());
my $sig = q{};
foreach my $i (@sig) {
$sig .= sprintf("%02d", $i);
$sig .= sprintf("%d", $i+2);
}
return $sig;
}
Expand Down Expand Up @@ -384,9 +386,10 @@ considering two images as similar. Used by C<is_most_similar>.
=head2 C<signature_as_char_string()>
Returns a stringified version of the signature. The string is generated by
unpack'ing into an array of ASCII characters (C*). Before the array of character
codes is joined into a string, they are padded. For example, 1 turns into 001;
25 turns into 025; 211 remains the same.
unpack'ing into an array of ASCII of signed characters (c*). Before the array of character
codes is joined into a string, 2 is added. This means the range of each character in the signature
is [0..4]. The net result is that any image, no matter the size, has a stringified signature
that is of the same length.
Assumes C<fill_cvec_from_file> has been called on a valid image already.
Expand All @@ -408,7 +411,7 @@ Assumes C<fill_cvec_from_file> has been called on a valid image already.
Returns a stringified version of the signature. The string is generated by
unpack'ing into an array of hexidecimal digits (H*). Before the array of character
codes is joined into a string. They are not padded like the C* based strings that
codes is joined into a string. They are modified like the c* based strings that
are generated with C<signature_as_char_ngrams>. As a result, the signatures are
not as long and therefore create fewer ngrams with C<signature_as_hex_ngrams>.
Expand Down
Binary file added t/pics/phil1.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added t/pics/phil2.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added t/pics/phil3.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c935024

Please sign in to comment.