From e13ee7d08326f398129b12a3df53287b49805943 Mon Sep 17 00:00:00 2001 From: Photon89 Date: Fri, 16 Aug 2024 21:47:44 +0200 Subject: [PATCH 01/11] Added AVIF support --- bin/shutter | 19 +++++++++++++++--- .../modules/Shutter/App/GlobalSettings.pm | 20 ++++++++++++++++++- .../resources/modules/Shutter/Pixbuf/Save.pm | 19 ++++++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/bin/shutter b/bin/shutter index f53e6072..d1983148 100755 --- a/bin/shutter +++ b/bin/shutter @@ -1125,14 +1125,15 @@ sub STARTUP { #add compatile, writeable file types my $combobox_type = Gtk3::ComboBoxText->new; - my ($int_png, $int_jpeg, $int_bmp, $int_webp) = (-1, -1, -1, -1); + my ($int_png, $int_jpeg, $int_bmp, $int_webp, $int_avif) = (-1, -1, -1, -1, -1); my $format_counter = 0; foreach my $format (Gtk3::Gdk::Pixbuf::get_formats()) { if ( $format->get_name eq "jpeg" || $format->get_name eq "png" || $format->get_name eq "bmp" - || $format->get_name eq "webp") + || $format->get_name eq "webp" + || $format->get_name eq "avif") { #we want jpg not jpeg @@ -1152,6 +1153,8 @@ sub STARTUP { $int_bmp = $format_counter; } elsif ($format->get_name eq "webp") { $int_webp = $format_counter; + } elsif ($format->get_name eq "avif") { + $int_webp = $format_counter; } $format_counter++; @@ -1193,7 +1196,7 @@ sub STARTUP { } else { #we will try to set a default value in this order - foreach my $cformat (@{[$int_png, $int_jpeg, $int_bmp, $int_webp]}) { + foreach my $cformat (@{[$int_png, $int_jpeg, $int_bmp, $int_webp, $int_avif]}) { if ($cformat > -1) { $combobox_type->set_active($cformat); last; @@ -2877,6 +2880,8 @@ sub STARTUP { $settings->set_png_quality($scale->get_value); } elsif ($combobox_type->get_active_text =~ /webp/) { $settings->set_webp_quality($scale->get_value); + } elsif ($combobox_type->get_active_text =~ /avif/) { + $settings->set_avif_quality($scale->get_value); } else { $settings->clear_quality_settings(); } @@ -2922,6 +2927,12 @@ sub STARTUP { $scale->set_range(0, 100); $scale->set_value(98); $scale_label->set_text($d->get("Quality") . ":"); + } elsif ($combobox_type->get_active_text =~ /avif/) { + $scale->set_sensitive(TRUE); + $scale_label->set_sensitive(TRUE); + $scale->set_range(0, 100); + $scale->set_value(68); + $scale_label->set_text($d->get("Quality") . ":"); } else { $scale->set_sensitive(FALSE); $scale_label->set_sensitive(FALSE); @@ -9774,6 +9785,8 @@ sub STARTUP { $combobox_save_as_type->set_active($counter); } elsif ($mime eq "image/webp" && $ext eq ".webp") { $combobox_save_as_type->set_active($counter); + } elsif ($mime eq "image/avif" && $ext eq ".avif") { + $combobox_save_as_type->set_active($counter); } } } diff --git a/share/shutter/resources/modules/Shutter/App/GlobalSettings.pm b/share/shutter/resources/modules/Shutter/App/GlobalSettings.pm index d1f83e4c..17796642 100644 --- a/share/shutter/resources/modules/Shutter/App/GlobalSettings.pm +++ b/share/shutter/resources/modules/Shutter/App/GlobalSettings.pm @@ -40,7 +40,8 @@ sub new { $self->{_png_quality} = undef; $self->{_jpg_quality} = undef; $self->{_webp_quality} = undef; - + $self->{_avif_quality} = undef; + bless $self, $class; return $self; } @@ -73,6 +74,15 @@ sub get_webp_quality { } } +sub get_avif_quality { + my $self = shift; + if (defined $self->{_avif_quality}) { + return $self->{_avif_quality}; + } else { + return 68; + } +} + sub set_png_quality { my $self = shift; if (@_) { @@ -97,6 +107,14 @@ sub set_webp_quality { return $self->{_webp_quality}; } +sub set_avif_quality { + my $self = shift; + if (@_) { + $self->{_avif_quality} = shift; + } + return $self->{_avif_quality}; +} + sub clear_quality_settings { my $self = shift; $self->{_jpg_quality} = undef; diff --git a/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm b/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm index 65438109..67037c33 100644 --- a/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm +++ b/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm @@ -148,6 +148,25 @@ sub save_pixbuf_to_file { print "Saving file $filename, $filetype, $quality\n" if $self->{_common}->get_debug; eval { $pixbuf->save($filename, $filetype, "tEXt::Software" => "Shutter", quality => $quality); }; + + + } elsif ($filetype eq 'avif') { + + #get quality value from settings if not set + if (my $settings = $self->{_common}->get_globalsettings_object) { + if (defined $settings->get_avif_quality) { + $quality = $settings->get_avif_quality; + } else { + $quality = 68; + } + } else { + $quality = 68; + } + + print "Saving file $filename, $filetype, $quality\n" if $self->{_common}->get_debug; + + eval { $pixbuf->save($filename, $filetype, quality => $quality); }; + } elsif ($filetype eq 'pdf') { print "Saving file $filename, $filetype\n" if $self->{_common}->get_debug; From 17e869fe190b8de78f5087d556dad1237b35091c Mon Sep 17 00:00:00 2001 From: Photon89 Date: Mon, 19 Aug 2024 17:02:42 +0200 Subject: [PATCH 02/11] Refactor file format recognition code --- bin/shutter | 83 ++++++++++++----------------------------------------- 1 file changed, 18 insertions(+), 65 deletions(-) diff --git a/bin/shutter b/bin/shutter index d1983148..637a789e 100755 --- a/bin/shutter +++ b/bin/shutter @@ -1125,42 +1125,27 @@ sub STARTUP { #add compatile, writeable file types my $combobox_type = Gtk3::ComboBoxText->new; - my ($int_png, $int_jpeg, $int_bmp, $int_webp, $int_avif) = (-1, -1, -1, -1, -1); - my $format_counter = 0; + my @supported_formats; foreach my $format (Gtk3::Gdk::Pixbuf::get_formats()) { - if ( $format->get_name eq "jpeg" - || $format->get_name eq "png" - || $format->get_name eq "bmp" - || $format->get_name eq "webp" - || $format->get_name eq "avif") + my $format_name = $format->get_name; + if ( $format_name eq "jpeg" + || $format_name eq "png" + || $format_name eq "bmp" + || $format_name eq "webp" + || $format_name eq "avif") { #we want jpg not jpeg - if ($format->get_name eq "jpeg" || $format->get_name eq "jpg") { - $combobox_type->append_text("jpg" . " - " . $format->get_description); - } else { - $combobox_type->append_text($format->get_name . " - " . $format->get_description); - } - - #a little ugly here, maybe the values are in alternative order on several machine... - #just remember the number when appending, so we can set png as default for example - if ($format->get_name eq "jpeg" || $format->get_name eq "jpg") { - $int_jpeg = $format_counter; - } elsif ($format->get_name eq "png") { - $int_png = $format_counter; - } elsif ($format->get_name eq "bmp") { - $int_bmp = $format_counter; - } elsif ($format->get_name eq "webp") { - $int_webp = $format_counter; - } elsif ($format->get_name eq "avif") { - $int_webp = $format_counter; + if ($format_name eq "jpeg" || $format_name eq "jpg") { + $format_name = "jpg"; } - - $format_counter++; - + + $combobox_type->append_text($format_name . " - " . $format->get_description); + push(@supported_formats, $format_name); } } + $combobox_type->signal_connect( 'changed' => \&evt_value_changed, 'type_changed' @@ -1171,23 +1156,8 @@ sub STARTUP { ); if (defined $settings_xml->{'general'}->{'filetype'}) { - - #migration from gscrot to shutter - #maybe we can drop this in future releases - # 0 := jpeg (jpg) - # 1 := png - unless (defined $settings_xml->{'general'}->{'app_version'}) { - if ($settings_xml->{'general'}->{'filetype'} == 0) { - $combobox_type->set_active($int_jpeg); - } elsif ($settings_xml->{'general'}->{'filetype'} == 1) { - $combobox_type->set_active($int_png); - } - - #shutter - } else { - $combobox_type->set_active($settings_xml->{'general'}->{'filetype'}); - } - + $combobox_type->set_active($settings_xml->{'general'}->{'filetype'}); + #set saved quality/compression value if there is one if (defined $settings_xml->{'general'}->{'quality'}) { $scale->set_value($settings_xml->{'general'}->{'quality'}); @@ -1196,11 +1166,9 @@ sub STARTUP { } else { #we will try to set a default value in this order - foreach my $cformat (@{[$int_png, $int_jpeg, $int_bmp, $int_webp, $int_avif]}) { - if ($cformat > -1) { - $combobox_type->set_active($cformat); + foreach my $i (0 .. $#supported_formats) { + $combobox_type->set_active($i); last; - } } } @@ -4767,22 +4735,7 @@ sub STARTUP { $settings_xml = XMLin(IO::File->new($settingsfile)); if ($data eq 'profile_load') { - - #migration from gscrot to shutter - #maybe we can drop this in future releases - # 0 := jpeg - # 1 := png - unless (defined $settings_xml->{'general'}->{'app_version'}) { - if ($settings_xml->{'general'}->{'filetype'} == 0) { - $combobox_type->set_active($int_jpeg); - } elsif ($settings_xml->{'general'}->{'filetype'} == 1) { - $combobox_type->set_active($int_png); - } - - #shutter - } else { - $combobox_type->set_active($settings_xml->{'general'}->{'filetype'}); - } + $combobox_type->set_active($settings_xml->{'general'}->{'filetype'}); #main $scale->set_value($settings_xml->{'general'}->{'quality'}); From 9cc0c83198e5827e090427e576a3f4fe102e570a Mon Sep 17 00:00:00 2001 From: Photon89 Date: Mon, 19 Aug 2024 21:12:38 +0200 Subject: [PATCH 03/11] Refactor GlobalSettings.pm --- bin/shutter | 10 +- .../modules/Shutter/App/GlobalSettings.pm | 94 ++++++------------- .../resources/modules/Shutter/Pixbuf/Save.pm | 16 ++-- 3 files changed, 42 insertions(+), 78 deletions(-) diff --git a/bin/shutter b/bin/shutter index 637a789e..aea055e1 100755 --- a/bin/shutter +++ b/bin/shutter @@ -2841,15 +2841,15 @@ sub STARTUP { $sc->set_globalsettings_object($settings); } if ($combobox_type->get_active_text =~ /jpeg/) { - $settings->set_jpg_quality($scale->get_value); + $settings->set_image_quality("jpg", $scale->get_value); } elsif ($combobox_type->get_active_text =~ /jpg/) { - $settings->set_jpg_quality($scale->get_value); + $settings->set_image_quality("jpg", $scale->get_value); } elsif ($combobox_type->get_active_text =~ /png/) { - $settings->set_png_quality($scale->get_value); + $settings->set_image_quality("png", $scale->get_value); } elsif ($combobox_type->get_active_text =~ /webp/) { - $settings->set_webp_quality($scale->get_value); + $settings->set_image_quality("webp", $scale->get_value); } elsif ($combobox_type->get_active_text =~ /avif/) { - $settings->set_avif_quality($scale->get_value); + $settings->set_image_quality("avif", $scale->get_value); } else { $settings->clear_quality_settings(); } diff --git a/share/shutter/resources/modules/Shutter/App/GlobalSettings.pm b/share/shutter/resources/modules/Shutter/App/GlobalSettings.pm index 17796642..7b9e586b 100644 --- a/share/shutter/resources/modules/Shutter/App/GlobalSettings.pm +++ b/share/shutter/resources/modules/Shutter/App/GlobalSettings.pm @@ -37,89 +37,53 @@ sub new { my $self = {}; - $self->{_png_quality} = undef; - $self->{_jpg_quality} = undef; - $self->{_webp_quality} = undef; - $self->{_avif_quality} = undef; - + $self->{_image_quality} = ( + "png" => undef, + "jpg" => undef, + "webp" => undef, + "avif" => undef + ); + + $self->{_default_image_quality} = ( + "png" => 9, + "jpg" => 90, + "webp" => 98, + "avif" => 68 + ); + bless $self, $class; return $self; } #getter / setter -sub get_png_quality { - my $self = shift; - if (defined $self->{_png_quality}) { - return $self->{_png_quality}; - } else { - return 9; - } -} - -sub get_jpg_quality { - my $self = shift; - if (defined $self->{_jpg_quality}) { - return $self->{_jpg_quality}; - } else { - return 90; - } -} - -sub get_webp_quality { - my $self = shift; - if (defined $self->{_webp_quality}) { - return $self->{_webp_quality}; - } else { - return 98; - } -} -sub get_avif_quality { +sub get_image_quality { my $self = shift; - if (defined $self->{_avif_quality}) { - return $self->{_avif_quality}; + my $format = shift; + if (defined $self->{_image_quality}{$format}) { + return $self->{_image_quality}{$format}; } else { - return 68; - } -} - -sub set_png_quality { - my $self = shift; - if (@_) { - $self->{_png_quality} = shift; - } - return $self->{_png_quality}; -} - -sub set_jpg_quality { - my $self = shift; - if (@_) { - $self->{_jpg_quality} = shift; - } - return $self->{_jpg_quality}; -} - -sub set_webp_quality { - my $self = shift; - if (@_) { - $self->{_webp_quality} = shift; + return $self->{_default_image_quality}{$format}; } - return $self->{_webp_quality}; } -sub set_avif_quality { +sub set_image_quality { my $self = shift; + my $format = shift; if (@_) { - $self->{_avif_quality} = shift; + $self->{_image_quality}{$format} = shift; } - return $self->{_avif_quality}; + return $self->{_image_quality}{$format}; } sub clear_quality_settings { my $self = shift; - $self->{_jpg_quality} = undef; - $self->{_png_quality} = undef; - $self->{_webp_quality} = undef; + $self->{_image_quality} = ( + "png" => undef, + "jpg" => undef, + "webp" => undef, + "avif" => undef + ); } 1; diff --git a/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm b/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm index 67037c33..ff6cb8ee 100644 --- a/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm +++ b/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm @@ -84,8 +84,8 @@ sub save_pixbuf_to_file { #get quality value from settings if not set if (my $settings = $self->{_common}->get_globalsettings_object) { - if (defined $settings->get_jpg_quality) { - $quality = $settings->get_jpg_quality; + if (defined $settings->set_image_quality("jpg")) { + $quality = $settings->set_image_quality("jpg"); } else { $quality = 90; } @@ -118,8 +118,8 @@ sub save_pixbuf_to_file { #get quality value from settings if not set if (my $settings = $self->{_common}->get_globalsettings_object) { - if (defined $settings->get_png_quality) { - $quality = $settings->get_png_quality; + if (defined $settings->set_image_quality("png")) { + $quality = $settings->set_image_quality("png"); } else { $quality = 9; } @@ -136,8 +136,8 @@ sub save_pixbuf_to_file { #get quality value from settings if not set if (my $settings = $self->{_common}->get_globalsettings_object) { - if (defined $settings->get_webp_quality) { - $quality = $settings->get_webp_quality; + if (defined $settings->set_image_quality("webp")) { + $quality = $settings->set_image_quality("webp"); } else { $quality = 98; } @@ -154,8 +154,8 @@ sub save_pixbuf_to_file { #get quality value from settings if not set if (my $settings = $self->{_common}->get_globalsettings_object) { - if (defined $settings->get_avif_quality) { - $quality = $settings->get_avif_quality; + if (defined $settings->set_image_quality("avif")) { + $quality = $settings->set_image_quality("avif"); } else { $quality = 68; } From c640381d86a1079d0f188e67b21516c8d1173691 Mon Sep 17 00:00:00 2001 From: Photon89 Date: Mon, 19 Aug 2024 21:56:59 +0200 Subject: [PATCH 04/11] Simplify setting image quality value a bit --- bin/shutter | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/bin/shutter b/bin/shutter index aea055e1..b890c2f1 100755 --- a/bin/shutter +++ b/bin/shutter @@ -2871,36 +2871,25 @@ sub STARTUP { #filetype changed if ($data eq "type_changed") { + $scale->set_sensitive(TRUE); + $scale_label->set_sensitive(TRUE); + $scale_label->set_text($d->get("Quality") . ":"); if ($combobox_type->get_active_text =~ /jpeg/) { - $scale->set_sensitive(TRUE); - $scale_label->set_sensitive(TRUE); $scale->set_range(1, 100); $scale->set_value(90); - $scale_label->set_text($d->get("Quality") . ":"); } elsif ($combobox_type->get_active_text =~ /jpg/) { - $scale->set_sensitive(TRUE); - $scale_label->set_sensitive(TRUE); $scale->set_range(1, 100); $scale->set_value(90); - $scale_label->set_text($d->get("Quality") . ":"); } elsif ($combobox_type->get_active_text =~ /png/) { - $scale->set_sensitive(TRUE); - $scale_label->set_sensitive(TRUE); $scale->set_range(0, 9); $scale->set_value(9); $scale_label->set_text($d->get("Compression") . ":"); } elsif ($combobox_type->get_active_text =~ /webp/) { - $scale->set_sensitive(TRUE); - $scale_label->set_sensitive(TRUE); $scale->set_range(0, 100); $scale->set_value(98); - $scale_label->set_text($d->get("Quality") . ":"); } elsif ($combobox_type->get_active_text =~ /avif/) { - $scale->set_sensitive(TRUE); - $scale_label->set_sensitive(TRUE); $scale->set_range(0, 100); $scale->set_value(68); - $scale_label->set_text($d->get("Quality") . ":"); } else { $scale->set_sensitive(FALSE); $scale_label->set_sensitive(FALSE); From bcadbd1ac4776db5141ff47b6fac8d0a6294bd40 Mon Sep 17 00:00:00 2001 From: Photon89 Date: Mon, 19 Aug 2024 22:00:44 +0200 Subject: [PATCH 05/11] Fix typo in Save.pm --- .../resources/modules/Shutter/Pixbuf/Save.pm | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm b/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm index ff6cb8ee..698128f2 100644 --- a/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm +++ b/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm @@ -84,8 +84,8 @@ sub save_pixbuf_to_file { #get quality value from settings if not set if (my $settings = $self->{_common}->get_globalsettings_object) { - if (defined $settings->set_image_quality("jpg")) { - $quality = $settings->set_image_quality("jpg"); + if (defined $settings->get_image_quality("jpg")) { + $quality = $settings->get_image_quality("jpg"); } else { $quality = 90; } @@ -118,8 +118,8 @@ sub save_pixbuf_to_file { #get quality value from settings if not set if (my $settings = $self->{_common}->get_globalsettings_object) { - if (defined $settings->set_image_quality("png")) { - $quality = $settings->set_image_quality("png"); + if (defined $settings->get_image_quality("png")) { + $quality = $settings->get_image_quality("png"); } else { $quality = 9; } @@ -136,8 +136,8 @@ sub save_pixbuf_to_file { #get quality value from settings if not set if (my $settings = $self->{_common}->get_globalsettings_object) { - if (defined $settings->set_image_quality("webp")) { - $quality = $settings->set_image_quality("webp"); + if (defined $settings->get_image_quality("webp")) { + $quality = $settings->get_image_quality("webp"); } else { $quality = 98; } @@ -154,8 +154,8 @@ sub save_pixbuf_to_file { #get quality value from settings if not set if (my $settings = $self->{_common}->get_globalsettings_object) { - if (defined $settings->set_image_quality("avif")) { - $quality = $settings->set_image_quality("avif"); + if (defined $settings->get_image_quality("avif")) { + $quality = $settings->get_image_quality("avif"); } else { $quality = 68; } From 633b2c9f116b96904ff740600ec7e71111848b02 Mon Sep 17 00:00:00 2001 From: Photon89 Date: Tue, 20 Aug 2024 00:12:47 +0200 Subject: [PATCH 06/11] Refactor parts of Save.pm --- .../resources/modules/Shutter/Pixbuf/Save.pm | 89 ++++++++----------- 1 file changed, 39 insertions(+), 50 deletions(-) diff --git a/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm b/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm index 698128f2..f8b50210 100644 --- a/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm +++ b/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm @@ -49,17 +49,42 @@ sub new { my $current_window = $self->{_window} || $self->{_common}->get_mainwindow; $self->{_dialogs} = Shutter::App::SimpleDialogs->new($current_window); $self->{_lp} = Shutter::Pixbuf::Load->new($self->{_common}, $current_window); + $self->{_quality} = undef; bless $self, $class; return $self; } +sub set_quality_setting { + my $self = shift; + my $filetype = shift; + my $default_image_quality = ( + "png" => 9, + "jpg" => 90, + "webp" => 98, + "avif" => 68 + ); + + #get quality value from settings if not set + if (my $settings = $self->{_common}->get_globalsettings_object) { + if (defined $settings->get_image_quality($filetype)) { + $self->{_quality} = $settings->get_image_quality($filetype); + } else { + $self->{_quality} = default_image_quality{$filetype}; + } + } else { + $self->{_quality} = default_image_quality{$filetype}; + } +} + sub save_pixbuf_to_file { my $self = shift; my $pixbuf = shift; my $filename = shift; my $filetype = shift; my $quality = shift; + + $self->{_quality} = $quality; #gettext variable my $d = $self->{_common}->get_gettext; @@ -81,22 +106,13 @@ sub save_pixbuf_to_file { #currently this is bmp, jpeg (jpg), png and ico (ico is not useful here) my $imagemagick_result = undef; if ($filetype eq 'jpeg' || $filetype eq 'jpg') { + + $self->set_quality_setting($filetype); - #get quality value from settings if not set - if (my $settings = $self->{_common}->get_globalsettings_object) { - if (defined $settings->get_image_quality("jpg")) { - $quality = $settings->get_image_quality("jpg"); - } else { - $quality = 90; - } - } else { - $quality = 90; - } - - print "Saving file $filename, $filetype, $quality\n" if $self->{_common}->get_debug; + print "Saving file $filename, $filetype, " . $self->{_quality} . "\n" if $self->{_common}->get_debug; eval { - $pixbuf->save($filename, 'jpeg', quality => $quality); + $pixbuf->save($filename, 'jpeg', quality => $self->{_quality}); #FIXME: NOT COVERED BY BINDINGS YET (we use Image::ExifTool instead) #~ $pixbuf->set_option( 'orientation' => $option ); @@ -116,56 +132,29 @@ sub save_pixbuf_to_file { }; } elsif ($filetype eq 'png') { - #get quality value from settings if not set - if (my $settings = $self->{_common}->get_globalsettings_object) { - if (defined $settings->get_image_quality("png")) { - $quality = $settings->get_image_quality("png"); - } else { - $quality = 9; - } - } else { - $quality = 9; - } + $self->set_quality_setting($filetype); - print "Saving file $filename, $filetype, $quality\n" if $self->{_common}->get_debug; + print "Saving file $filename, $filetype, " . $self->{_quality} . "\n" if $self->{_common}->get_debug; - eval { $pixbuf->save($filename, $filetype, "tEXt::Software" => "Shutter", compression => $quality); }; + eval { $pixbuf->save($filename, $filetype, "tEXt::Software" => "Shutter", compression => $self->{_quality}); }; } elsif ($filetype eq 'bmp') { eval { $pixbuf->save($filename, $filetype); }; } elsif ($filetype eq 'webp') { - #get quality value from settings if not set - if (my $settings = $self->{_common}->get_globalsettings_object) { - if (defined $settings->get_image_quality("webp")) { - $quality = $settings->get_image_quality("webp"); - } else { - $quality = 98; - } - } else { - $quality = 98; - } + $self->set_quality_setting($filetype); - print "Saving file $filename, $filetype, $quality\n" if $self->{_common}->get_debug; + print "Saving file $filename, $filetype, " . $self->{_quality} . "\n" if $self->{_common}->get_debug; - eval { $pixbuf->save($filename, $filetype, "tEXt::Software" => "Shutter", quality => $quality); }; + eval { $pixbuf->save($filename, $filetype, "tEXt::Software" => "Shutter", quality => $self->{_quality}); }; } elsif ($filetype eq 'avif') { - #get quality value from settings if not set - if (my $settings = $self->{_common}->get_globalsettings_object) { - if (defined $settings->get_image_quality("avif")) { - $quality = $settings->get_image_quality("avif"); - } else { - $quality = 68; - } - } else { - $quality = 68; - } + $self->set_quality_setting($filetype); - print "Saving file $filename, $filetype, $quality\n" if $self->{_common}->get_debug; + print "Saving file $filename, $filetype, " . $self->{_quality} . "\n" if $self->{_common}->get_debug; - eval { $pixbuf->save($filename, $filetype, quality => $quality); }; + eval { $pixbuf->save($filename, $filetype, quality => $self->{_quality}); }; } elsif ($filetype eq 'pdf') { @@ -214,7 +203,7 @@ sub save_pixbuf_to_file { } else { - print "Saving file $filename, $filetype, $quality (using fallback-mode)\n" if $self->{_common}->get_debug; + print "Saving file $filename, $filetype, $self->{_quality} (using fallback-mode)\n" if $self->{_common}->get_debug; #save pixbuf to tempfile my ($tmpfh, $tmpfilename) = tempfile(); From 17199b5dea176e6f3a305ba45b7140978a5f4659 Mon Sep 17 00:00:00 2001 From: Photon89 Date: Tue, 20 Aug 2024 07:49:55 +0200 Subject: [PATCH 07/11] Refactor further parts of Save.pm --- .../resources/modules/Shutter/Pixbuf/Save.pm | 56 ++++++++----------- 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm b/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm index f8b50210..2f49ca49 100644 --- a/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm +++ b/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm @@ -77,6 +77,23 @@ sub set_quality_setting { } } +sub save_pdf_ps_svg { + my $self = shift; + my $filename = shift; + my $pixbuf = shift; + + #0.8? => 72 / 90 dpi + my $surface = Cairo::SvgSurface->create($filename, $pixbuf->get_width * 0.8, $pixbuf->get_height * 0.8); + my $cr = Cairo::Context->create($surface); + $cr->scale(0.8, 0.8); + Gtk3::Gdk::cairo_set_source_pixbuf($cr, $pixbuf, 0, 0); + $cr->paint; + $cr->show_page; + + undef $surface; + undef $cr; +} + sub save_pixbuf_to_file { my $self = shift; my $pixbuf = shift; @@ -158,48 +175,21 @@ sub save_pixbuf_to_file { } elsif ($filetype eq 'pdf') { - print "Saving file $filename, $filetype\n" if $self->{_common}->get_debug; - - #0.8? => 72 / 90 dpi - my $surface = Cairo::PdfSurface->create($filename, $pixbuf->get_width * 0.8, $pixbuf->get_height * 0.8); - my $cr = Cairo::Context->create($surface); - $cr->scale(0.8, 0.8); - Gtk3::Gdk::cairo_set_source_pixbuf($cr, $pixbuf, 0, 0); - $cr->paint; - $cr->show_page; + $self->save_pdf_ps_svg($filename, $pixbuf); - undef $surface; - undef $cr; + print "Saving file $filename, $filetype\n" if $self->{_common}->get_debug; } elsif ($filetype eq 'ps') { - print "Saving file $filename, $filetype\n" if $self->{_common}->get_debug; - - #0.8? => 72 / 90 dpi - my $surface = Cairo::PsSurface->create($filename, $pixbuf->get_width * 0.8, $pixbuf->get_height * 0.8); - my $cr = Cairo::Context->create($surface); - $cr->scale(0.8, 0.8); - Gtk3::Gdk::cairo_set_source_pixbuf($cr, $pixbuf, 0, 0); - $cr->paint; - $cr->show_page; + $self->save_pdf_ps_svg($filename, $pixbuf); - undef $surface; - undef $cr; + print "Saving file $filename, $filetype\n" if $self->{_common}->get_debug; } elsif ($filetype eq 'svg') { - print "Saving file $filename, $filetype\n" if $self->{_common}->get_debug; - - #0.8? => 72 / 90 dpi - my $surface = Cairo::SvgSurface->create($filename, $pixbuf->get_width * 0.8, $pixbuf->get_height * 0.8); - my $cr = Cairo::Context->create($surface); - $cr->scale(0.8, 0.8); - Gtk3::Gdk::cairo_set_source_pixbuf($cr, $pixbuf, 0, 0); - $cr->paint; - $cr->show_page; + $self->save_pdf_ps_svg($filename, $pixbuf); - undef $surface; - undef $cr; + print "Saving file $filename, $filetype\n" if $self->{_common}->get_debug; } else { From 722728a8bb0c01f39d25d3f13d2a54d3bd0e93ac Mon Sep 17 00:00:00 2001 From: Photon89 Date: Tue, 20 Aug 2024 08:10:15 +0200 Subject: [PATCH 08/11] Further simplify Save.pm --- .../resources/modules/Shutter/Pixbuf/Save.pm | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm b/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm index 2f49ca49..f6b49290 100644 --- a/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm +++ b/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm @@ -173,19 +173,7 @@ sub save_pixbuf_to_file { eval { $pixbuf->save($filename, $filetype, quality => $self->{_quality}); }; - } elsif ($filetype eq 'pdf') { - - $self->save_pdf_ps_svg($filename, $pixbuf); - - print "Saving file $filename, $filetype\n" if $self->{_common}->get_debug; - - } elsif ($filetype eq 'ps') { - - $self->save_pdf_ps_svg($filename, $pixbuf); - - print "Saving file $filename, $filetype\n" if $self->{_common}->get_debug; - - } elsif ($filetype eq 'svg') { + } elsif ($filetype eq 'pdf' || $filetype eq 'ps' || $filetype eq 'svg') { $self->save_pdf_ps_svg($filename, $pixbuf); From 75f07a6893d23fa1463fcd0c8b6c97c6d0a09008 Mon Sep 17 00:00:00 2001 From: Photon89 Date: Tue, 20 Aug 2024 08:16:03 +0200 Subject: [PATCH 09/11] Fix array definition to get rid of warnings --- .../resources/modules/Shutter/App/GlobalSettings.pm | 12 ++++++------ .../shutter/resources/modules/Shutter/Pixbuf/Save.pm | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/share/shutter/resources/modules/Shutter/App/GlobalSettings.pm b/share/shutter/resources/modules/Shutter/App/GlobalSettings.pm index 7b9e586b..8300443b 100644 --- a/share/shutter/resources/modules/Shutter/App/GlobalSettings.pm +++ b/share/shutter/resources/modules/Shutter/App/GlobalSettings.pm @@ -37,19 +37,19 @@ sub new { my $self = {}; - $self->{_image_quality} = ( + $self->{_image_quality} = { "png" => undef, "jpg" => undef, "webp" => undef, "avif" => undef - ); + }; - $self->{_default_image_quality} = ( + $self->{_default_image_quality} = { "png" => 9, "jpg" => 90, "webp" => 98, "avif" => 68 - ); + }; bless $self, $class; return $self; @@ -78,12 +78,12 @@ sub set_image_quality { sub clear_quality_settings { my $self = shift; - $self->{_image_quality} = ( + $self->{_image_quality} = { "png" => undef, "jpg" => undef, "webp" => undef, "avif" => undef - ); + }; } 1; diff --git a/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm b/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm index f6b49290..73689d55 100644 --- a/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm +++ b/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm @@ -58,12 +58,12 @@ sub new { sub set_quality_setting { my $self = shift; my $filetype = shift; - my $default_image_quality = ( + my $default_image_quality = { "png" => 9, "jpg" => 90, "webp" => 98, "avif" => 68 - ); + }; #get quality value from settings if not set if (my $settings = $self->{_common}->get_globalsettings_object) { From 6305e6995d84dba05df48ca4e2d1a4ad0b44ae4d Mon Sep 17 00:00:00 2001 From: Photon89 Date: Tue, 20 Aug 2024 09:39:12 +0200 Subject: [PATCH 10/11] Fixed crash due to wrong access to a hash --- share/shutter/resources/modules/Shutter/Pixbuf/Save.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm b/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm index 73689d55..ce52c39a 100644 --- a/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm +++ b/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm @@ -70,10 +70,10 @@ sub set_quality_setting { if (defined $settings->get_image_quality($filetype)) { $self->{_quality} = $settings->get_image_quality($filetype); } else { - $self->{_quality} = default_image_quality{$filetype}; + $self->{_quality} = $default_image_quality->{$filetype}; } } else { - $self->{_quality} = default_image_quality{$filetype}; + $self->{_quality} = $default_image_quality->{$filetype}; } } From b044321bc286fd2ccb934797e871c61284e12b7f Mon Sep 17 00:00:00 2001 From: Photon89 Date: Tue, 20 Aug 2024 09:39:58 +0200 Subject: [PATCH 11/11] Simplify file format code a bit --- bin/shutter | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/bin/shutter b/bin/shutter index b890c2f1..c3f93085 100755 --- a/bin/shutter +++ b/bin/shutter @@ -9719,15 +9719,11 @@ sub STARTUP { #Fix Bug #966159 if (defined $rfilename) { my ($short, $folder, $ext) = fileparse($rfilename, qr/\.[^.]*/); - if ($mime eq "image/jpeg" && $ext eq ".jpg") { - $combobox_save_as_type->set_active($counter); - } elsif ($mime eq "image/png" && $ext eq ".png") { - $combobox_save_as_type->set_active($counter); - } elsif ($mime eq "image/bmp" && $ext eq ".bmp") { - $combobox_save_as_type->set_active($counter); - } elsif ($mime eq "image/webp" && $ext eq ".webp") { - $combobox_save_as_type->set_active($counter); - } elsif ($mime eq "image/avif" && $ext eq ".avif") { + if ($mime eq "image/jpeg" && $ext eq ".jpg" + || $mime eq "image/png" && $ext eq ".png" + || $mime eq "image/bmp" && $ext eq ".bmp" + || $mime eq "image/webp" && $ext eq ".webp" + || $mime eq "image/avif" && $ext eq ".avif") { $combobox_save_as_type->set_active($counter); } }