Increasing dither speed.

This commit is contained in:
Sergiotarxz 2024-10-21 17:26:47 +02:00
parent 8216e62f66
commit 09a0ccedd8

View File

@ -15,30 +15,32 @@ use Cairo;
use Exd::Utils::String; use Exd::Utils::String;
has device => ( has device => (
is => 'ro', is => 'ro',
required => 1, required => 1,
); );
has font => ( has font => (
is => 'rw', is => 'rw',
default => sub { default => sub {
return 'Noto Sans CJK JP'; return 'Noto Sans CJK JP';
}, },
); );
has current_image => ( is => 'rw', );
sub qr( $self, $url ) { sub qr( $self, $url ) {
my $tmpdir = Path::Tiny->tempdir; my $tmpdir = Path::Tiny->tempdir;
my $image_file = $tmpdir->child('image.png'); my $image_file = $tmpdir->child('image.png');
my $qrcode = Imager::QRCode->new( size => 8 ); my $qrcode = Imager::QRCode->new( size => 8 );
my $img = $qrcode->plot( $url, {} ); my $img = $qrcode->plot( $url, {} );
$img->write( file => '' . $image_file ) or die $img->errstr; $img->write( file => '' . $image_file ) or die $img->errstr;
$self->image( $image_file ); $self->image($image_file);
$self->print_text( $url, 20 ); $self->print_text( $url, 20 );
} }
sub image( $self, $image ) { sub image( $self, $image ) {
$image = ''.$image if ref $image && $image->isa('Path::Tiny'); $image = '' . $image if ref $image && $image->isa('Path::Tiny');
if (!ref $image) { if ( !ref $image ) {
$image = Exd::Utils::get_gd_image($image); $image = Exd::Utils::get_gd_image($image);
} }
my $width = $image->width; my $width = $image->width;
@ -68,46 +70,69 @@ sub image( $self, $image ) {
my ($alpha) = $image_final->colorAllocateAlpha( 0, 0, 0, 127 ); my ($alpha) = $image_final->colorAllocateAlpha( 0, 0, 0, 127 );
$image_final->colorDeallocate($alpha); $image_final->colorDeallocate($alpha);
$image_final->colorAllocate( 255, 255, 255 ); $image_final->colorAllocate( 255, 255, 255 );
my $tempdir = Path::Tiny->tempdir;
my $image_file_in = $tempdir->child('in.png'); if ( !defined $self->current_image ) {
$image_final->_file(''.$image_file_in); $self->current_image($image_final);
my $image_file_out = $tempdir->child('out.png'); }
die 'Cannot convert' if system (qw/convert/, $image_file_in, qw/-resize 384x -dither FloydSteinberg -remap pattern:gray50 -monochrome/, $image_file_out); else {
$image_final = Exd::Utils::get_gd_image($image_file_out.''); my $current_image = $self->current_image;
my $device = $self->device; my $image = $image_final;
$device->image($image_final); my $new_current = GD::Image->new( $current_image->width,
$current_image->height + $image->height );
$new_current->copy( $current_image, 0, 0, 0, 0,
$current_image->width, $current_image->height );
$new_current->copy( $image, 0, $current_image->height, 0, 0,
$image->width, $image->height );
$self->current_image($new_current);
}
} }
} }
sub print_text( $self, $text, $font_size ) { sub print_text( $self, $text, $font_size ) {
my ($none, $file_code, $line) = caller(0); my ( $none, $file_code, $line ) = caller(0);
die "The bare minimum for font size is 4 at $file_code line $line" if $font_size < 4; die "The bare minimum for font size is 4 at $file_code line $line"
my $font = Pango::FontDescription->from_string( if $font_size < 4;
my $font = Pango::FontDescription->from_string(
$self->font . ' ' . ( $font_size - ( 2 * 50 / 30 ) ) ); $self->font . ' ' . ( $font_size - ( 2 * 50 / 30 ) ) );
my $device = $self->device; my $device = $self->device;
my $img = Exd::Utils::String->print_text($text, $font, $font_size); my $img = Exd::Utils::String->print_text( $text, $font, $font_size );
$self->image($img); $self->image($img);
} }
sub _font_to_weight ($self, $font) { sub _font_to_weight ( $self, $font ) {
my $weight = $font->get_weight; my $weight = $font->get_weight;
if ($weight eq 'normal') { if ( $weight eq 'normal' ) {
return 4; return 4;
} }
if ($weight eq 'bold') { if ( $weight eq 'bold' ) {
return 7; return 7;
} }
} }
sub print($self) { sub print($self) {
$self->device->print; my $device = $self->device;
my $image_final = $self->current_image;
my $tempdir = Path::Tiny->tempdir;
my $image_file_in = $tempdir->child('in.png');
$image_final->_file( '' . $image_file_in );
my $image_file_out = $tempdir->child('out.png');
die 'Cannot convert'
if system(
qw/convert/,
$image_file_in,
qw/-resize 384x -dither FloydSteinberg -remap pattern:gray50 -monochrome/,
$image_file_out
);
$image_final = Exd::Utils::get_gd_image( $image_file_out . '' );
$device->image($image_final);
$device->print;
} }
sub print_n_lf($self, $n) { sub print_n_lf( $self, $n ) {
$self->print_text("\n" x $n, 30); $self->print_text( "\n" x $n, 30 );
} }
sub serialize($self) { sub serialize($self) {
my $hash = {%$self}; my $hash = {%$self};
$hash->{device} = $hash->{device}->serialize; $hash->{device} = $hash->{device}->serialize;