From 3db58a4fe8a121093bc5c5287eb808f71de87441 Mon Sep 17 00:00:00 2001 From: dakkar Date: Thu, 10 Nov 2016 14:26:47 +0000 Subject: move QRcode* to ::Result, wrap in Moo --- lib/Data/QRCode.pm | 106 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 62 insertions(+), 44 deletions(-) (limited to 'lib/Data/QRCode.pm') diff --git a/lib/Data/QRCode.pm b/lib/Data/QRCode.pm index b7f8c4e..74896c9 100644 --- a/lib/Data/QRCode.pm +++ b/lib/Data/QRCode.pm @@ -1,59 +1,77 @@ package Data::QRCode; -use strict; -use warnings; +use Moo; +use Data::QRCode::Input; +use Data::QRCode::Result; +use Data::QRCode::Types qw(QRCodeEC QRCodeMode); +use Types::Standard qw(Str Int); +use namespace::clean; # ABSTRACT: qrcodes in C # VERSION -use Data::QRCode::Inline with => 'Alien::QREncode'; -use Data::QRCode::Inline C => ( - 'DATA', - autowrap => 1, - typemaps => 'typemap', + +has error_correction_level => ( + is => 'ro', + isa => QRCodeEC, + coerce => 1, + default => Data::QRCode::Input::ECLEVEL_M, ); -sub new { - my ($class, $input) = @_; - my $self = QRcode_encodeInput($input); - bless $self, $class; - return $self; -} +has mode => ( + is => 'ro', + isa => QRCodeMode, + coerce => 1, + default => Data::QRCode::Input::MODE_8, +); -sub data_at { - my ($self,$x,$y) = @_; - my $width = $self->width; - if ($x < 0 or $x >= $width or $y < 0 or $y >= $width) { - return; - } - my $value = _data_at($self,$x,$y); - - return { - color => $value & 0x01, - in_data => $value & 0x02, - in_format => $value & 0x04, - in_version => $value & 0x08, - in_timing => $value & 0x10, - in_alignment => $value & 0x20, - in_finder => $value & 0x40, - in_misc => $value & 0x80, - }; -} +has version => ( + is => 'rwp', + isa => Int, + default => 0, +); -1; +has input_data => ( + is => 'ro', + required => 1, + isa => Str, +); -__DATA__ -__C__ +has _result => ( + is => 'lazy', + init_arg => undef, + handles => [ qw(width data_at) ], +); -QRcode *QRcode_encodeInput(QRinput *input); +sub _build__result { + my ($self) = @_; -void DESTROY(QRcode *qrcode) { QRcode_free(qrcode); } + my $input = Data::QRCode::Input->new(); + $input->error_correction_level($self->error_correction_level); + $input->version($self->version); + $input->append($self->mode,$self->input_data); -int version(QRcode* self) { - return self->version; -} + my $ret = Data::QRCode::Result->new($input); + $self->_set_version($ret->version); -int width(QRcode* self) { - return self->width; + return $ret; } -int _data_at(QRcode* self, int x, int y) { - return self->data[x+y*self->width]; +sub map { + my ($self,$code) = @_; + + my $r = $self->_result; + my @result; + for my $y (0..$r->width-1) { + push @result,[]; + for my $x (0..$r->width-1) { + # use the internal function to avoid re-checking the x/y + # bounds + my $raw_data = $r->_data_at($x,$y); + my $hash_data = Data::QRCode::Result::_data_hash($raw_data); + + push @{$result[-1]}, $code->($hash_data,$raw_data); + } + } + + return \@result; } + +1; -- cgit v1.2.3