Skip to content

Commit

Permalink
Add near_lossless option
Browse files Browse the repository at this point in the history
  • Loading branch information
le0pard committed Jan 31, 2019
1 parent 3e67105 commit a39602f
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v0.2.6

* Tested on webp 1.0.2
* Added near_lossless option

## v0.2.5

* Fix build for Travis CI docker containers
Expand Down
6 changes: 5 additions & 1 deletion ext/webp_ffi/webp_ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ int webp_encode(const char *in_file, const char *out_file, const FfiWebpEncodeCo
if (encode_config->lossless == 0 || encode_config->lossless == 1){
config.lossless = encode_config->lossless;
}
if (encode_config->near_lossless >= 0 && encode_config->near_lossless <= 100){
config.near_lossless = encode_config->near_lossless;
config.lossless = 1; // use near-lossless only with lossless
}
if (encode_config->quality >= 0 && encode_config->quality <= 100){
config.quality = encode_config->quality;
}
Expand Down Expand Up @@ -288,4 +292,4 @@ int test_c(int n) {

#if defined(__cplusplus) || defined(c_plusplus)
} // extern "C"
#endif
#endif
7 changes: 4 additions & 3 deletions ext/webp_ffi/webp_ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern "C" {

typedef struct {
int lossless; // Lossless encoding (0=lossy(default), 1=lossless).
int near_lossless; // use near-lossless image preprocessing (0..100=off)
float quality; // between 0 (smallest file) and 100 (biggest)
int method; // quality/speed trade-off (0=fast, 6=slower-better)

Expand Down Expand Up @@ -42,7 +43,7 @@ extern "C" {
int crop_x, crop_y, crop_w, crop_h;
int resize_w, resize_h;
} FfiWebpEncodeConfig;

typedef struct {
OutputFileFormat output_format;
int bypass_filtering; // if true, skip the in-loop filtering
Expand All @@ -51,7 +52,7 @@ extern "C" {
int resize_w, resize_h;
int use_threads; // if true, use multi-threaded decoding
} FfiWebpDecodeConfig;


void decoder_version(char *version);
void encoder_version(char *version);
Expand All @@ -64,4 +65,4 @@ extern "C" {
} // extern "C"
#endif

#endif /* _WEBP_FFI_H_ */
#endif /* _WEBP_FFI_H_ */
1 change: 1 addition & 0 deletions lib/webp/c.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module C
# struct
class FfiWebpEncodeConfig < FFI::Struct
layout :lossless, :int,
:near_lossless, :int,
:quality, :float,
:method, :int,
:target_size, :int,
Expand Down
2 changes: 1 addition & 1 deletion lib/webp/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(options)
def encode_pointer
options_pointer = FFI::MemoryPointer.new :char, C::FfiWebpEncodeConfig.size, false
options_struct = C::FfiWebpEncodeConfig.new options_pointer
[:lossless, :method, :target_size, :target_PSNR, :segments,
[:lossless, :near_lossless, :method, :target_size, :target_PSNR, :segments,
:sns_strength, :filter_strength, :filter_sharpness,
:filter_type, :autofilter, :alpha_compression, :alpha_filtering,
:alpha_quality, :pass, :show_compressed, :preprocessing, :partitions,
Expand Down
2 changes: 1 addition & 1 deletion lib/webp/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module WebP
VERSION = "1.0.2"
VERSION = "0.2.6"
end
22 changes: 17 additions & 5 deletions spec/webp_ffi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
Dir.mkdir(@out_dir) unless File.exists?(@out_dir)
end
after :all do
@out_dir = File.expand_path(File.join(File.dirname(__FILE__), "../tmp/"))
Dir["#{@out_dir}/*{.png,.webp}"].each do |file|
File.delete(file) rescue nil
end
# @out_dir = File.expand_path(File.join(File.dirname(__FILE__), "../tmp/"))
# Dir["#{@out_dir}/*{.png,.webp}"].each do |file|
# File.delete(file) rescue nil
# end
end

it "calculate plus 100 by test_c (verify C)" do
Expand Down Expand Up @@ -81,13 +81,25 @@
out_filename = File.expand_path(File.join(@out_dir, "#{image}.png.webp"))
expect(WebP.encode(in_filename, out_filename)).to be_truthy
end

it "#{image}.png image with near_lossless" do
in_filename = File.expand_path(File.join(File.dirname(__FILE__), "factories/#{image}.png"))
out_filename = File.expand_path(File.join(@out_dir, "#{image}_near_lossless.png.webp"))
expect(WebP.encode(in_filename, out_filename, near_lossless: 60)).to be_truthy
end
end
factories[:jpg].each do |image|
it "#{image}.jpg image" do
in_filename = File.expand_path(File.join(File.dirname(__FILE__), "factories/#{image}.jpg"))
out_filename = File.expand_path(File.join(@out_dir, "#{image}.jpg.webp"))
expect(WebP.encode(in_filename, out_filename)).to be_truthy
end

it "#{image}.jpg image with near_lossless" do
in_filename = File.expand_path(File.join(File.dirname(__FILE__), "factories/#{image}.jpg"))
out_filename = File.expand_path(File.join(@out_dir, "#{image}_near_lossless.jpg.webp"))
expect(WebP.encode(in_filename, out_filename, near_lossless: 60)).to be_truthy
end
end
factories[:tiff].each do |image|
it "#{image}.tif image" do
Expand Down Expand Up @@ -176,4 +188,4 @@
end
end

end
end

0 comments on commit a39602f

Please sign in to comment.