summaryrefslogtreecommitdiff
path: root/fml/lib/FML/String/Banner/Image.pm
blob: 887cc82db4e5924fd834848fb3d1e91e7562f4f9 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#-*- perl -*-
#
#  Copyright (C) 2008 Ken'ichi Fukamachi
#   All rights reserved. This program is free software; you can
#   redistribute it and/or modify it under the same terms as Perl itself.
#
# $FML: @template.pm,v 1.12 2008/08/24 08:28:36 fukachan Exp $
#

package FML::String::Banner::Image;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD $banner_table);
use Carp;

use FML::Char::Ascii::Data;
$banner_table = $FML::Char::Ascii::Data::banner_table;


=head1 NAME

FML::String::Banner::Image - generate a banner by using GD library.

=head1 SYNOPSIS

    use FML::String::Banner::Image;
    my $banner = new FML::String::Banner::Image;
    my $png    = $banner->as_png($_string);

    use FileHandle;
    my $wh = new FileHandle "> /var/tmp/test.png";
    if (defined $wh) {
        $wh->binmode();
        $wh->print($png);
        $wh->close();
    }
    else {
        croak("cannot open /var/tmp/test.png");
    }

=head1 DESCRIPTION

See L<FML::String::Banner> CLASS. This class provides the image part
of the class. 

Currently the image format is PNG only (but extensible if needed).

=head1 METHODS

=head2 new()

constructor.

=cut


# Descriptions: constructor.
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: OBJ
sub new
{
    my ($self) = @_;
    my ($type) = ref($self) || $self;
    my $me     = {};
    return bless $me, $type;
}


=head2 as_png($string)

return a generated banner as PNG image.

=cut


# Descriptions: return a generated banner as PNG image.
#    Arguments: OBJ($self) STR($string)
# Side Effects: none
# Return Value: IMAGE
sub as_png
{
    my ($self, $string) = @_;

    $self->_bitmap_string($string);
    $self->_bitmap_merge();
}


# Descriptions: return color table as HASH_REF.
#    Arguments: OBJ($self) OBJ($image)
# Side Effects: none
# Return Value: HASH_REF
sub _bitmap_color_table_example
{
    my ($self, $image) = @_;

    # allocate some colors
    my $white = $image->colorAllocate(255, 255, 255);
    my $black = $image->colorAllocate(  0,   0,   0);
    my $red   = $image->colorAllocate(255,   0,   0);
    my $blue  = $image->colorAllocate(  0,   0, 255);

    my $color_table = {
	white => $white,
	black => $black,
	red   => $red,
	blue  => $blue,
    };

    return $color_table;
}


# Descriptions: build a new image template.
#    Arguments: OBJ($self) NUM($width) NUM($height)
# Side Effects: none
# Return Value: IMAGE
sub _bitmap_new_image
{
    my ($self, $width, $height) = @_;

    # 8*8 x 8*8 dots.
    $width  ||= 64;
    $height ||= 64;

    use GD;
    my $image = new GD::Image($width, $height);
    my $color_table = $self->_bitmap_color_table_example($image);
    $image->transparent($color_table->{ black });
    $image->interlaced('true');
    return $image;
}


# Descriptions: merge character images to one string image.
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: IMAGE
sub _bitmap_merge
{
    my ($self) = @_;

    my $image_buffer = $self->{ _image_buffer } || [];
    my $image = $self->_bitmap_new_image(64*8, 64*2);

    my $i = 0;
    for (my $i = 0; $i < 8; $i++) {
	my $im = $image_buffer->[ $i ];
	$image->copyResampled($im,
			      64*$i, int(rand(32)),
			      32, 32,
			      96, 96, 96, 96);
    }

    return $image->png;
}


# Descriptions: create a string image.
#    Arguments: OBJ($self) STR($string)
# Side Effects: $self modified.
# Return Value: none
sub _bitmap_string
{
    my ($self, $string) = @_;
    my (@image_buffer)  = ();

    my $len = length($string);
    for (my $i = 0; $i < $len; $i++) {
	my $c     = substr($string, $i, 1);
	$image_buffer[ $i ] = $self->_bitmap_char($c, $i);
    }

    $self->{ _image_buffer } = \@image_buffer;
}


# Descriptions: create one character and rotate it a little.
#    Arguments: OBJ($self) STR($char) NUM($pos)
# Side Effects: none
# Return Value: IMAGE
sub _bitmap_char
{
    my ($self, $char, $pos) = @_;
    my ($x, $y) = (0, 0);

    my $image = $self->_bitmap_new_image(128, 128);

    # XXX 8 is the magic number
    # XXX since each letter of the ascii banner_table is 8x8 dots.
    my $banner = $banner_table->{ $char };
    for my $line (@$banner) {
	++$x;
	for (my $i = 0; $i < 8; $i++) {
	    my $c = substr($line, $i, 1);
	    $y = $i + 1;
	    if ($c =~ /^\S+$/) {
		$self->_create_sub_image($image, $x, $y);
	    }
	}
    }

    # rotate the image.
    my $rt_image = $self->_bitmap_new_image(128, 128);
    my $angle    = $self->_random_drift(8);
    $rt_image->copyRotated($image,
			   64, 64,
			   0, 0,
			   128, 128, $angle);
    return $rt_image;
}


# Descriptions: create a part of a character image.
#    Arguments: OBJ($self) OBJ($image) NUM($x) NUM($y)
# Side Effects: none
# Return Value: none
sub _create_sub_image
{
    my ($self, $image, $x, $y) = @_;
    my $color_table = $self->_bitmap_color_table_example($image);

    # drift the end position a little.
    my ($r0, $r1, $r2, $r3) = ($self->_random_drift(),
			       $self->_random_drift(),
			       $self->_random_drift(),
			       $self->_random_drift());

    # region
    my ($c0, $c1, $c2, $c3) = ($y*8 + $r0 + 32,
			       $x*8 + $r1 + 32,
			       $y*8 + 8 + $r2 + 32,
			       $x*8 + 8 + $r3 + 32);


    my $type = int(rand(4)) % 4;
    $image->setThickness($type);

    $type = int(rand(4)) % 4;
    if ($type == 0) {
	$image->filledRectangle($c0, $c1, $c2, $c3, $color_table->{ red });
    }
    elsif ($type == 1) {
	$image->filledRectangle($c0, $c1, $c2, $c3, $color_table->{ blue });
    }
    elsif ($type == 2) {
	$image->filledArc($c0 +4, $c1 +4, 8, 8, 0, 360, $color_table->{black});
    }
    elsif ($type == 3) {
	$image->filledArc($c0 +4, $c1 +4, 9, 9, 0, 360, $color_table->{ red });
    }
    else {
	$image->filledRectangle($c0, $c1, $c2, $c3, $color_table->{ black });
    }
}


# Descriptions: return a randomized drift parameter.
#    Arguments: OBJ($self) NUM($max)
# Side Effects: none
# Return Value: NUM
sub _random_drift
{
    my ($self, $max) = @_;

    my $shift = int(rand($max || 2));

    my $pm = int(rand(2)) % 1;
    if ($pm == 1) {
	return( -1 * $shift );
    }
    else {
	return( $shift );
    }
}


=head1 CODING STYLE

See C<http://www.fml.org/software/FNF/> on fml coding style guide.

=head1 AUTHOR

Ken'ichi Fukamachi

=head1 COPYRIGHT

Copyright (C) 2008 Ken'ichi Fukamachi

All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.

=head1 HISTORY

FML::String::Banner::Image appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.

=cut


1;