diff --git a/bin/shutter b/bin/shutter index f53e6072..c3f93085 100755 --- a/bin/shutter +++ b/bin/shutter @@ -1125,39 +1125,27 @@ 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 $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") + 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; + 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' @@ -1168,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'}); @@ -1193,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]}) { - if ($cformat > -1) { - $combobox_type->set_active($cformat); + foreach my $i (0 .. $#supported_formats) { + $combobox_type->set_active($i); last; - } } } @@ -2870,13 +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_image_quality("avif", $scale->get_value); } else { $settings->clear_quality_settings(); } @@ -2898,30 +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_range(0, 100); + $scale->set_value(68); } else { $scale->set_sensitive(FALSE); $scale_label->set_sensitive(FALSE); @@ -4756,22 +4724,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'}); @@ -9766,13 +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") { + 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); } } diff --git a/share/shutter/resources/modules/Shutter/App/GlobalSettings.pm b/share/shutter/resources/modules/Shutter/App/GlobalSettings.pm index d1f83e4c..8300443b 100644 --- a/share/shutter/resources/modules/Shutter/App/GlobalSettings.pm +++ b/share/shutter/resources/modules/Shutter/App/GlobalSettings.pm @@ -37,71 +37,53 @@ sub new { my $self = {}; - $self->{_png_quality} = undef; - $self->{_jpg_quality} = undef; - $self->{_webp_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 { +sub get_image_quality { my $self = shift; - if (defined $self->{_webp_quality}) { - return $self->{_webp_quality}; + my $format = shift; + if (defined $self->{_image_quality}{$format}) { + return $self->{_image_quality}{$format}; } else { - return 98; - } -} - -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->{_default_image_quality}{$format}; } - return $self->{_jpg_quality}; } -sub set_webp_quality { +sub set_image_quality { my $self = shift; + my $format = shift; if (@_) { - $self->{_webp_quality} = shift; + $self->{_image_quality}{$format} = shift; } - return $self->{_webp_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 65438109..ce52c39a 100644 --- a/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm +++ b/share/shutter/resources/modules/Shutter/Pixbuf/Save.pm @@ -49,17 +49,59 @@ 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_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; my $filename = shift; my $filetype = shift; my $quality = shift; + + $self->{_quality} = $quality; #gettext variable my $d = $self->{_common}->get_gettext; @@ -81,22 +123,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_jpg_quality) { - $quality = $settings->get_jpg_quality; - } 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,86 +149,39 @@ 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_png_quality) { - $quality = $settings->get_png_quality; - } 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_webp_quality) { - $quality = $settings->get_webp_quality; - } 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); }; - } elsif ($filetype eq 'pdf') { + eval { $pixbuf->save($filename, $filetype, "tEXt::Software" => "Shutter", quality => $self->{_quality}); }; - print "Saving file $filename, $filetype\n" if $self->{_common}->get_debug; + + } elsif ($filetype eq 'avif') { - #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->set_quality_setting($filetype); - undef $surface; - undef $cr; + print "Saving file $filename, $filetype, " . $self->{_quality} . "\n" if $self->{_common}->get_debug; - } elsif ($filetype eq 'ps') { + eval { $pixbuf->save($filename, $filetype, quality => $self->{_quality}); }; - print "Saving file $filename, $filetype\n" if $self->{_common}->get_debug; + } elsif ($filetype eq 'pdf' || $filetype eq 'ps' || $filetype eq 'svg') { - #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; - - undef $surface; - undef $cr; - - } elsif ($filetype eq 'svg') { + $self->save_pdf_ps_svg($filename, $pixbuf); 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; - - undef $surface; - undef $cr; - } 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();