Skip to content

Commit

Permalink
Merge pull request #642 from shutter-project/webp-support
Browse files Browse the repository at this point in the history
Added webp as additional image format
  • Loading branch information
DarthGandalf authored Mar 16, 2024
2 parents 4f9072c + ec9fde6 commit 0966fc2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
21 changes: 17 additions & 4 deletions bin/shutter
Original file line number Diff line number Diff line change
Expand Up @@ -1125,13 +1125,14 @@ sub STARTUP {

#add compatile, writeable file types
my $combobox_type = Gtk3::ComboBoxText->new;
my ($int_png, $int_jpeg, $int_bmp) = (-1, -1, -1);
my ($int_png, $int_jpeg, $int_bmp, $int_webp) = (-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 "bmp"
|| $format->get_name eq "webp")
{

#we want jpg not jpeg
Expand All @@ -1149,6 +1150,8 @@ sub STARTUP {
$int_png = $format_counter;
} elsif ($format->get_name eq "bmp") {
$int_bmp = $format_counter;
} elsif ($format->get_name eq "webp") {
$int_webp = $format_counter;
}

$format_counter++;
Expand Down Expand Up @@ -1190,7 +1193,7 @@ sub STARTUP {
} else {

#we will try to set a default value in this order
foreach my $cformat (@{[$int_png, $int_jpeg, $int_bmp]}) {
foreach my $cformat (@{[$int_png, $int_jpeg, $int_bmp, $int_webp]}) {
if ($cformat > -1) {
$combobox_type->set_active($cformat);
last;
Expand Down Expand Up @@ -2871,7 +2874,9 @@ sub STARTUP {
} elsif ($combobox_type->get_active_text =~ /jpg/) {
$settings->set_jpg_quality($scale->get_value);
} elsif ($combobox_type->get_active_text =~ /png/) {
$settings->set_png_quality($scale->get_value);
$settings->set_jpg_quality($scale->get_value);
} elsif ($combobox_type->get_active_text =~ /webp/) {
$settings->set_webp_quality($scale->get_value);
} else {
$settings->clear_quality_settings();
}
Expand Down Expand Up @@ -2911,6 +2916,12 @@ sub STARTUP {
$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") . ":");
} else {
$scale->set_sensitive(FALSE);
$scale_label->set_sensitive(FALSE);
Expand Down Expand Up @@ -9749,6 +9760,8 @@ sub STARTUP {
$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);
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions share/shutter/resources/modules/Shutter/App/GlobalSettings.pm
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ sub new {

$self->{_png_quality} = undef;
$self->{_jpg_quality} = undef;
$self->{_webp_quality} = undef;

bless $self, $class;
return $self;
Expand All @@ -63,6 +64,15 @@ sub get_jpg_quality {
}
}

sub get_webp_quality {
my $self = shift;
if (defined $self->{_webp_quality}) {
return $self->{_webp_quality};
} else {
return 98;
}
}

sub set_png_quality {
my $self = shift;
if (@_) {
Expand All @@ -79,10 +89,19 @@ sub set_jpg_quality {
return $self->{_jpg_quality};
}

sub set_webp_quality {
my $self = shift;
if (@_) {
$self->{_webp_quality} = shift;
}
return $self->{_webp_quality};
}

sub clear_quality_settings {
my $self = shift;
$self->{_jpg_quality} = undef;
$self->{_png_quality} = undef;
$self->{_webp_quality} = undef;
}

1;
16 changes: 16 additions & 0 deletions share/shutter/resources/modules/Shutter/Pixbuf/Save.pm
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@ sub save_pixbuf_to_file {
eval { $pixbuf->save($filename, $filetype, "tEXt::Software" => "Shutter", compression => $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;
}

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 'pdf') {

print "Saving file $filename, $filetype\n" if $self->{_common}->get_debug;
Expand Down

0 comments on commit 0966fc2

Please sign in to comment.