Using dither to avoid poor visibility in images.

This commit is contained in:
Sergiotarxz 2024-10-21 17:17:04 +02:00
parent 79eacc3af7
commit 8216e62f66
4 changed files with 20 additions and 12 deletions

View File

@ -18,7 +18,6 @@ has output_file => (
has current_image => ( is => 'rw', );
sub image( $self, $image ) {
$image->grayscale;
my $current_image = $self->current_image;
if ( !defined $current_image ) {

View File

@ -252,7 +252,7 @@ sub _populate_preview( $self, $box_editor_preview ) {
$self->_preview_widget($preview_picture);
my $preview_scroll_window = Gtk4::ScrolledWindow->new;
$preview_scroll_window->set_child($preview_picture);
$preview_scroll_window->set_property( 'width-request', 380 );
$preview_scroll_window->set_property( 'width-request', 384 );
$box_editor_preview->append($preview_scroll_window);
$self->_generate_preview_file();
}
@ -308,7 +308,7 @@ sub receive_packet( $self, $packet ) {
$data =~ s/\s+$//;
$data =~ s/^\s+//;
if ( $data =~ /^\s*$/ ) {
next;
return;
}
$data .= "\n";
$self->_add_to_log($data);

View File

@ -36,11 +36,14 @@ sub qr( $self, $url ) {
$self->print_text( $url, 20 );
}
sub image( $self, $file ) {
my $image = Exd::Utils::get_gd_image($file);
sub image( $self, $image ) {
$image = ''.$image if ref $image && $image->isa('Path::Tiny');
if (!ref $image) {
$image = Exd::Utils::get_gd_image($image);
}
my $width = $image->width;
my $height = $image->height;
my $dest_width = 380;
my $dest_width = 384;
my $dest_height = $dest_width * ( $height / $width );
my $image_dest = GD::Image->new( $dest_width, $dest_height );
$image_dest->copyResampled( $image, 0, 0, 0, 0, $dest_width, $dest_height,
@ -65,6 +68,12 @@ sub image( $self, $file ) {
my ($alpha) = $image_final->colorAllocateAlpha( 0, 0, 0, 127 );
$image_final->colorDeallocate($alpha);
$image_final->colorAllocate( 255, 255, 255 );
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.'');
my $device = $self->device;
$device->image($image_final);
}
@ -77,7 +86,7 @@ sub print_text( $self, $text, $font_size ) {
$self->font . ' ' . ( $font_size - ( 2 * 50 / 30 ) ) );
my $device = $self->device;
my $img = Exd::Utils::String->print_text($text, $font, $font_size);
$device->image($img);
$self->image($img);
}
sub _font_to_weight ($self, $font) {

View File

@ -10,13 +10,13 @@ use Encode qw/decode/;
sub print_text($class, $text, $font, $font_size) {
my $height;
my $surface = Cairo::ImageSurface->create( 'argb32', 380, 500 );
my $surface = Cairo::ImageSurface->create( 'argb32', 384, 500 );
my $cr = Cairo::Context->create($surface);
my $layout = Pango::Cairo::create_layout($cr);
my $context = $layout->get_context;
my $metrics = $context->get_metrics($font, undef);
$height = ($metrics->get_ascent + $metrics->get_descent + $layout->get_spacing) / 1024;
my @lines = $class->_split_text_lines( $text, 380, $font, $font_size );
my @lines = $class->_split_text_lines( $text, 384, $font, $font_size );
my $current_image;
my $tempdir = Path::Tiny->tempdir;
my $i = 0;
@ -24,11 +24,11 @@ sub print_text($class, $text, $font, $font_size) {
my $file = $tempdir->child("result-@{[$i++]}.png");
my $current_line = shift @lines;
my $surface = Cairo::ImageSurface->create( 'argb32', 380, $height );
my $surface = Cairo::ImageSurface->create( 'argb32', 384, $height );
my $cr = Cairo::Context->create($surface);
$cr->set_source_rgb( 1, 1, 1 );
$cr->rectangle( 0, 0, 380, $height );
$cr->rectangle( 0, 0, 384, $height );
$cr->fill;
$cr->set_source_rgb( 0, 0, 0 );
@ -112,7 +112,7 @@ sub _split_text_lines( $class, $text, $max_width, $font, $font_size ) {
$i++;
}
}
my $surface = Cairo::ImageSurface->create( 'argb32', 380, 100 );
my $surface = Cairo::ImageSurface->create( 'argb32', 384, 100 );
my $cr = Cairo::Context->create($surface);
my $layout = Pango::Cairo::create_layout($cr);
$layout->set_font_description($font);