Skip to content

Commit

Permalink
Add support for progress feedback (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
kleisauke committed Jan 28, 2024
1 parent d2f66cd commit c004099
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

Uses libvips v8.15.1, compiled with Emscripten v3.1.52.

### Added

- Add `image.onProgress` callback for progress feedback.
[#63](https://github.com/kleisauke/wasm-vips/issues/63)

## [v0.0.7] - 2023-11-12

Uses libvips v8.15.0, compiled with Emscripten v3.1.48.
Expand Down
15 changes: 15 additions & 0 deletions build/preamble_vips.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,21 @@ declare module Vips {
*/
readonly filename: string;

/**
* Attach progress feedback.
*
* This method can update user-interfaces with progress feedback,
* for example:
* ```js
* const image = vips.Image.newFromFile('huge.jpg');
* image.onProgress = (percent) =>
* console.log(`${percent}% complete`);
* image.writeToFile('x.png');
* ```
* @param percent Percent complete.
*/
onProgress: (percent: number) => void;

//#region Constructor functions

/**
Expand Down
15 changes: 15 additions & 0 deletions lib/vips.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/bindings/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ void Image::call(const char *operation_name, Option *args,
Image::call(operation_name, nullptr, args, kwargs, this);
}

void Image::eval_handler(VipsImage *image, VipsProgress *progress, void *user) {
Image *self = reinterpret_cast<Image *>(user);
if (self->progress_callback == nullptr)
return;

// Ensure that we call the JS function on the main thread, see:
// https://github.com/emscripten-core/emscripten/issues/11317
emscripten_sync_run_in_main_runtime_thread(
EM_FUNC_SIG_VI, self->progress_callback, progress->percent);
}

void Image::set_progress_callback(emscripten::val js_func) {
emscripten::val ptr = emscripten::val::module_property("addFunction")(
js_func, emscripten::val("vi"));
progress_callback = reinterpret_cast<void (*)(int)>(ptr.as<int>());

vips_image_set_progress(get_image(), 1);
g_signal_connect(get_image(), "eval", G_CALLBACK(eval_handler), this);
}

Image Image::new_memory() {
return Image(vips_image_new_memory());
}
Expand Down
14 changes: 14 additions & 0 deletions src/bindings/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <vector>

#include <emscripten/val.h>
#include <emscripten/threading.h>

namespace vips {

Expand Down Expand Up @@ -81,6 +82,15 @@ class Image : public Object {
return vips_image_get_filename(get_image());
}

static void eval_handler(VipsImage *image, VipsProgress *progress,
void *user);

void set_progress_callback(emscripten::val js_func);

emscripten::val stub_getter() const {
return emscripten::val::null();
}

const void *data() const {
return vips_image_get_data(get_image());
}
Expand Down Expand Up @@ -612,6 +622,10 @@ class Image : public Object {

return out;
}

private:
// sig = vi
void (*progress_callback)(int percent) = nullptr;
};

} // namespace vips
3 changes: 3 additions & 0 deletions src/vips-emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,9 @@ EMSCRIPTEN_BINDINGS(my_module) {
.property("xres", &Image::xres)
.property("yres", &Image::yres)
.property("filename", &Image::filename)
// Handwritten setters
.property("onProgress", &Image::stub_getter,
&Image::set_progress_callback)
// Auto-generated (class-)functions
.class_function("analyzeload", &Image::analyzeload)
.class_function("analyzeload", optional_override([](const std::string &filename) {
Expand Down

0 comments on commit c004099

Please sign in to comment.