Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ruby and python tests #17

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.