summaryrefslogtreecommitdiff
path: root/lib/Imager/QRCode/Fancy.pm
blob: eb7100a86b2871565951a2926faeb119ae522e57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package Imager::QRCode::Fancy; 
use 5.010;
use strict;
use warnings;
use Data::QRCode;
use Carp;
use List::AllUtils qw(min);
use Imager;
use Types::Standard qw(Dict InstanceOf ArrayRef CodeRef Optional HashRef is_ArrayRef is_CodeRef);
use Types::Common::Numeric qw(PositiveNum);
use Types::Path::Tiny qw(Path);
use Type::Params qw(compile);
use Type::Utils qw(enum union);
use namespace::clean;
 
=head1 SEE ALSO
 
https://github.com/sylnsfar/qrcode
 
http://www.thonky.com/qr-code-tutorial/
 
=cut
 
sub make {
    state $check = compile(Dict[
        qr_code => InstanceOf['Data::QRCode'],
        image => Optional[union[
            InstanceOf['Imager'],
            ArrayRef[InstanceOf['Imager']],
        ]],
        size => Optional[PositiveNum],
        colormap => Optional[HashRef],
        module => Optional[union[
            enum[qw(box circle)],
            CodeRef,
        ]],
        dot_scale => Optional[PositiveNum],
    ]);
    my ($arg) = $check->(@_);
 
    my $qr = $arg->{qr_code};
 
    my @images = is_ArrayRef($arg->{image}) ? @{$arg->{image}}
        defined($arg->{image}) ? $arg->{image}
        : ();
 
    if (not $arg->{sizeand not @images) {
        croak 'I need a target size, or some images to overlay';
    }
    my $size = $arg->{size} || min($images[0]->getwidth,$images[0]->getheight);
 
    state $module_map = {
        box => \&draw_box,
        circle => \&draw_circle,
    };
    my $module = is_CodeRef($arg->{module}) ? $arg->{module}
        $module_map->{$arg->{module}||'box'};
 
    my $colormap = {
        empty => 'white',
        data => 'black',
        format => 'black',
        version => 'black',
        timing => 'black',
        alignment => 'black',
        finder => 'black',
        misc => 'black',
        %{ $arg->{colormap} || {} },
    };
    my $dot_scale = $arg->{dot_scale} || 1/3;
 
    my $data_size = $qr->width;
    $size -= ($size % $data_size);
    my $scale = $size / $data_size;
 
    my $back_image = qr_image($qr,$size,$module,$colormap);
 
    return $back_image unless @images;
 
    my $front_image = qr_dots_image($qr,$size,$dot_scale,$module,$colormap);
 
    my @ret_images;
    for my $image (@images) {
        my $target = Imager->new(xsize=>$size,ysize=>$size);
        $target->paste(src => $back_image);
        $target->compose(src => $image);
        $target->compose(src => $front_image);
        push @ret_images$target;
    }
 
    if (@ret_images > 1) {
        return \@ret_images;
    }
    else {
        return $ret_images[0];
    }
}
 
sub qr_image {
    my ($qr,$size,$module,$colormap) = @_;
 
    my $data_size = $qr->width;
    my $scale = $size / $data_size;
 
    my $image = Imager->new(xsize=>$size,ysize=>$size);
    $image->box(filled=>1,color=>'white');
 
    for my $dx (0..$data_size-1) {
        for my $dy (0..$data_size-1) {
            my $dp = $qr->data_at($dx,$dy);
 
            $module->(
                image => $image,
                bounding_box($dx,$dy,$scale),
                color => Imager::Color->new(datapoint2color($dp,$colormap)),
            );
        }
    }
 
    return $image;
}
 
sub qr_dots_image {
    my ($qr,$size,$dot_scale,$module,$colormap) = @_;
 
    my $data_size = $qr->width;
    my $scale = $size / $data_size;
 
    my $image = Imager->new(xsize=>$size,ysize=>$size,model=>'rgba');
    $image->box(filled=>1,color=>[0,0,0,0]);
 
    for my $dx (0..$data_size-1) {
        for my $dy (0..$data_size-1) {
            my $dp = $qr->data_at($dx,$dy);
 
            my @box = $dp->{in_data}
                ? dot_in_bounding_box($dx,$dy,$scale,$dot_scale)
                : bounding_box($dx,$dy,$scale);
 
            $module->(
                image => $image,
                @box,
                color => Imager::Color->new(datapoint2color($dp,$colormap)),
            );
        }
    }
 
    return $image;
}
 
sub datapoint2color {
    my ($dp,$colormap) = @_;
 
    return $colormap->{emptyunless $dp->{color};
 
    for my $k (qw(format version timing alignment finder misc data)) {
        next unless $dp->{"in_$k"};
        if (my $v = $colormap->{$k}) {
            return $v;
        }
    }
    return $colormap->{misc}; # just in case 
}
 
sub bounding_box {
    my ($x,$y,$size) = @_;
    return (
        xmin => $x*$sizeymin => $y*$size,
        xmax => $x*$size+$size-1, ymax => $y*$size+$size-1,
    );
}
 
sub dot_in_bounding_box {
    my ($x,$y,$size,$dot_scale) = @_;
    my $dot_size = int($size*$dot_scale);
    my $dot_offset = int(($size-$dot_size)/2);
 
    return (
        xmin => $x*$size+$dot_offsetymin => $y*$size+$dot_offset,
        xmax => $x*$size+$size-$dot_offsetymax => $y*$size+$size-$dot_offset,
    );
}
 
sub draw_box {
    my (%args) = @_;
    $args{image}->box(
        %args,
        filled => 1,
    );
}
 
sub draw_circle {
    my (%args) = @_;
 
    my $cx = int($args{xmax}+$args{xmin})/2;
    my $cy = int($args{ymax}+$args{ymin})/2;
    my $radius = min(
        $args{xmax}-$cx,
        $args{ymax}-$cy,
        $cx-$args{xmin},
        $cy-$args{ymin},
    );
 
    $args{image}->circle(
        => $cx=> $cy,
        => $radius,
        color => $args{color},
        filled => 1,
    );
}
 
1;