Increasing dither speed.

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

View File

@ -26,6 +26,8 @@ has font => (
}, },
); );
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');
@ -68,20 +70,29 @@ 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"
if $font_size < 4;
my $font = Pango::FontDescription->from_string( 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;
@ -100,14 +111,28 @@ sub _font_to_weight ($self, $font) {
} }
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;