Skip to content

Commit

Permalink
Update ruby and python tests (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmartt authored Jan 26, 2024
1 parent 40e627e commit 3254bc1
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 7 deletions.
Binary file modified wasm-thumbnail-py/src/wasm_thumbnail/data/wasm_thumbnail.wasm
Binary file not shown.
32 changes: 26 additions & 6 deletions wasm-thumbnail-py/src/wasm_thumbnail/thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,39 @@ def decode_padded_image(data):
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)

wasm = resources.open_binary('wasm_thumbnail.data', "wasm_thumbnail.wasm")
store = Store(engine.JIT(Compiler))
module = Module(store, wasm.read())
import_object = ImportObject()
import_object.register(
"env",
{
"register_panic": Function(store, register_panic)
}
)
instance = Instance(module, import_object)
image_length = len(image_bytes)
input_pointer = instance.exports.allocate(image_length)
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, quality)
instance.exports.deallocate(input_pointer, image_length)
try:
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)
out_bytes = bytes(memory[:size])

instance.exports.deallocate(output_pointer, size)

memory = instance.exports.memory.uint8_view(output_pointer)
out_bytes = bytes(memory[:size])
instance.exports.deallocate(output_pointer, size)
except RuntimeError:
print(
f"resize_and_pad() hit a RuntimeError "
f"(length={image_length}, width={width}, height={height}, size={size}):"
)

unpadded_length = get_unpadded_length(out_bytes)
if unpadded_length == 0:
Expand Down
Binary file added wasm-thumbnail-py/tests/largeimage.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added wasm-thumbnail-py/tests/shipwreck.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
2 changes: 1 addition & 1 deletion wasm-thumbnail-rb/test/wasm/thumbnail/rb_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_it_calls_panic_if_image_too_big
height: 200,
size: 5)
end
assert_equal(exception.message, "Error processing the image.")
assert_equal(exception.message.split(/:/, 2).first, "Error processing the image")
end

def test_should_not_crash_after_many_resizes
Expand Down
Binary file not shown.

0 comments on commit 3254bc1

Please sign in to comment.