Skip to content

Commit

Permalink
Added an adjustable quality setting (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmartt authored Aug 4, 2021
1 parent 8b5e38e commit 73ae439
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ all: wasm-thumbnail-py/src/wasm_thumbnail/data/wasm_thumbnail.wasm
wasm-thumbnail-py/src/wasm_thumbnail/data/wasm_thumbnail.wasm: wasm-thumbnail/src/lib.rs
cd wasm-thumbnail && cargo build --release
cp wasm-thumbnail/target/wasm32-unknown-unknown/release/wasm_thumbnail.wasm wasm-thumbnail-py/src/wasm_thumbnail/data/
cp wasm-thumbnail/target/wasm32-unknown-unknown/release/wasm_thumbnail.wasm wasm-thumbnail-rb/lib/wasm/thumbnail/rb/data/
Binary file modified wasm-thumbnail-py/src/wasm_thumbnail/data/wasm_thumbnail.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions wasm-thumbnail-py/src/wasm_thumbnail/thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def decode_padded_image(data):

return data[4:4 + payload_length]

def resize_and_pad_image(image_bytes, width, height, size):
def resize_and_pad_image(image_bytes, width, height, size, quality = 80):
"""Resize an image and pad to fit size, output is prefixed by image length without padding.
Throws an error if the resized image does not fit in size or is not a supported format"""
instance = Instance(module, import_object)
Expand All @@ -31,7 +31,7 @@ def resize_and_pad_image(image_bytes, width, height, size):
memory = instance.exports.memory.uint8_view(input_pointer)
memory[0:image_length] = image_bytes

output_pointer = instance.exports.resize_and_pad(input_pointer, image_length, width, height, size)
output_pointer = instance.exports.resize_and_pad(input_pointer, image_length, width, height, size, quality)
instance.exports.deallocate(input_pointer, image_length)

memory = instance.exports.memory.uint8_view(output_pointer)
Expand Down
13 changes: 9 additions & 4 deletions wasm-thumbnail-rb/lib/wasm/thumbnail/rb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def self.register_panic(_msg_ptr = nil, _msg_len = nil, _file_ptr = nil, _file_l
puts("WASM panicked")
end

def self.resize_and_pad_with_header(file_bytes:, width:, height:, size:)
def self.resize_and_pad_with_header(file_bytes:, width:, height:, size:, quality: 80)
# Let's compile the module to be able to execute it!
wasm_instance = Wasm::Thumbnail::Rb::GetWasmInstance.call

Expand All @@ -36,7 +36,8 @@ def self.resize_and_pad_with_header(file_bytes:, width:, height:, size:)
image_length,
width,
height,
size)
size,
quality)
rescue RuntimeError
raise "Error processing the image."
end
Expand All @@ -54,8 +55,12 @@ def self.resize_and_pad_with_header(file_bytes:, width:, height:, size:)
bytes
end

def self.resize_and_pad(file_bytes:, width:, height:, size:)
bytes = resize_and_pad_with_header(file_bytes: file_bytes, width: width, height: height, size: size + 4)
def self.resize_and_pad(file_bytes:, width:, height:, size:, quality: 80)
bytes = resize_and_pad_with_header(file_bytes: file_bytes,
width: width,
height: height,
size: size + 4,
quality: quality)

# The first 4 bytes are a header until the image. The actual image probably ends well before
# the whole buffer, but we keep the junk data on the end to make all the images the same size
Expand Down
Binary file not shown.
12 changes: 11 additions & 1 deletion wasm-thumbnail-rb/test/wasm/thumbnail/rb_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ def test_that_it_has_a_version_number
refute_nil ::Wasm::Thumbnail::Rb::VERSION
end

def test_it_does_something_useful
def test_it_resizes_with_custom_quality
file_bytes = File.binread("#{__dir__}/brave.png").unpack("C*")
image = Wasm::Thumbnail::Rb.resize_and_pad(file_bytes: file_bytes,
width: 100,
height: 200,
size: 250_000,
quality: 40)
puts "Image resized and padded to size #{image.length}"
end

def test_it_resizes_with_default_quality
file_bytes = File.binread("#{__dir__}/brave.png").unpack("C*")
image = Wasm::Thumbnail::Rb.resize_and_pad(file_bytes: file_bytes,
width: 100,
Expand Down
3 changes: 2 additions & 1 deletion wasm-thumbnail/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub extern "C" fn resize_and_pad(
nwidth: u32,
nheight: u32,
nsize: usize,
nquality: u8
) -> *const u8 {
register_panic_hook();

Expand All @@ -43,7 +44,7 @@ pub extern "C" fn resize_and_pad(
out.extend_from_slice(&[0, 0, 0, 0]);

result
.write_to(&mut out, ImageOutputFormat::Jpeg(80))
.write_to(&mut out, ImageOutputFormat::Jpeg(nquality))
.expect("can save as jpeg");

assert!(out.len() <= nsize);
Expand Down

0 comments on commit 73ae439

Please sign in to comment.