-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
Question: get percentage of progress in dzsave #31
Comments
Hello @martinweihrauch, You can just set the Unfortunately there is no option yet to get progress feedback in C#. I'll have to expand NetVips a bit for that. TODO List:
|
Thx for the answer! I haven't worked with environment variables yet in C#. I am currently creating a WPF desktop application. |
- Use the "in" and "out" parameter modifier where possible. - Clean up internal P/Invoke layer. - Remove "vips_argument_map" callback for libvips >= 8.7. - Remove superfluous "ToIntPtr" function. - Update the NetVips benchmarks.
I've just added support for attaching progress feedback. For example: // Load from a huge JPEG file
var image = Image.NewFromFile("huge.jpg", access: Enums.Access.Sequential);
var progress = new Progress<int>(value =>
{
Console.Write($"\r{value}% complete");
});
// Attach progress feedback
image.SetProgress(progress);
// Save image pyramid
image.Dzsave("image-pyramid"); If you want to test this, you can use the nightly version of NetVips. Add the
And update NetVips to 1.1.0. Thank for reporting this! |
Thanks a lot. Assistent für verwaltetes Debuggen "CallbackOnCollectedDelegate" Ausnahme ausgelöst: "System.NullReferenceException" in NetVips.dll Das Programm "[15844] PathoZoomConverter.exe" wurde mit Code -1 (0xffffffff) beendet. |
My code:
|
And there is a "System.NullReferenceException" in NetVips.dll after some percent of running dzsave... |
@martinweihrauch I had forgotten to protect the delegate against premature garbage collection. It's fixed with commit a5ce84a. This code example now runs without any problems: var im = Image.NewFromFile(Filename, access: Enums.Access.Sequential);
var progress = new Progress<int>(value =>
{
Console.Write($"\r{value}% complete");
});
im.SetProgress(progress);
// Force garbage collection cycle to prove
// that the delegate doesn't get disposed
GC.Collect();
GC.WaitForPendingFinalizers();
// Save image pyramid
im.Dzsave("images/image-pyramid"); Thanks for reporting this dumbness! |
:))) |
If you want, I can adjust the method to this: Example/// <summary>
/// Attach progress feedback, if required.
/// </summary>
/// <remarks>
/// You can use this function to update user-interfaces with
/// progress feedback, for example:
/// <code language="lang-csharp">
/// var image = Image.NewFromFile("huge.jpg", access: Enums.Access.Sequential);
///
/// var progress = new Progress<int>(value =>
/// {
/// Console.Write($"\r{value}% complete");
/// });
/// image.SetProgress(progress);
///
/// image.Dzsave("image-pyramid");
/// </code>
/// </remarks>
/// <param name="progress">A provider for progress updates.</param>
/// <param name="token">Cancellation token to stop progress updates.</param>
public void SetProgress(IProgress<int> progress, CancellationToken token = default)
{
VipsImage.SetProgress(this, progress == null ? 0 : 1);
var lastPercent = 0;
GCallback evalCallback = (imagePtr, progressPtr, userDataPtr) =>
{
// Stop reporting the progress if a cancellation
// has been requested for this token
if (token.IsCancellationRequested)
{
return;
}
var progressStruct = progressPtr.Dereference<VipsProgress.Struct>();
if (progressStruct.Percent != lastPercent)
{
progress?.Report(progressStruct.Percent);
lastPercent = progressStruct.Percent;
}
};
// prevent it from being re-located or disposed of by the garbage collector
_progress = GCHandle.Alloc(evalCallback);
this.Connect(Internal.Enums.VipsEvaluation.Eval, evalCallback);
} For example, this allows you to stop the progress reporting after 5 sec: var cts = new CancellationTokenSource();
cts.CancelAfter(5000);
var progress = new Progress<int>(value =>
{
Console.Write($"\r{value}% complete");
});
im.SetProgress(progress, cts.Token); Unfortunately, this will only stop the progress updates and not the image processing itself. libvips does support cancellation see: But it's internal API. @jcupitt Is it possible to move the |
Sure, let's make it public. I'll just check exactly how nip2 does cancel. |
I just checked how nip2 does this exactly. It seems that if the callback in I can create a PR to move the |
Ah yes, I think I remember now. It's OK, I'll do it, it's a very simple change. |
the vips_image_set_kill() system was internal. Move it to the public API. See eg.: kleisauke/net-vips#31
+ update image.SetProgress() to handle the possible cancellation of the operation. Also, use vips_image_hasalpha() for libvips >= 8.5.
I've just released NetVips 1.1.0-rc1 and NetVips.Native 8.8.0-rc1 (which contains the pre-compiled libvips 8.8.0-rc1 binaries for Linux, macOS and Windows) on NuGet. This release adds support for progress feedback ( Support for associated images is also included. You can attach all associated images as metadata by setting the second parameter of var image = Image.Openslideload("image.svs", attachAssociated: true) (see relevant difference here) Or by doing: var image = Image.NewFromFile("image.svs", kwargs: new VOption
{
{"attach_associated", true}
});
image.Dzsave("image-pyramid", container: "szi"); Note that you need the the libvips If you would like to see what's new in libvips 8.8, please visit the the release notes of libvips: |
NetVips v1.1.0 is now available. |
Hi!
Thx so much for netVips!!
I would like to use dzsave. Is it possible to pass a C# Action to get the percentage of the process?
I would like to show the progress on the console or in a Windows desktop program.
Thanks
Martin
The text was updated successfully, but these errors were encountered: