Comments (3)
Hi! I'm glad you're finding the library useful!
This sounds like a good idea, I can start working on it when I get back to my computers (likely next week). I will update this issue when it's done.
from mupdfcore.
Hi! Version 1.5.0 now has overloads of the render methods (both for the MuPDFDocument
and the MuPDFMultiThreadedPageRenderer
) that return Span
s.
However, you should note that:
-
If you're using ImageSharp, ImageSharp's
LoadPixelData
method will always create a copy of the pixel data, regardless of how you invoke it (e.g. with abyte[]
or with aSpan<byte>
). -
In addition to returning a
Span<byte>
, the new methods also return anIDisposable
. This keeps track of the unmanaged memory that is pointed to by theSpan
. You must ensure that the lifetime of thisIDisposable
matches the lifetime of theSpan
, preferably byDispose
ing it when you have finished working with the image. If theIDisposable
goes out of scope while you still have access to theSpan
, the GC may collect it and trigger its finaliser at any time - as a result, the memory to which theSpan
points will be released, while you still hold an apparently valid reference to it. -
Arrays of
Span
s are not allowed, therefore theMuPDFMultiThreadedPageRenderer.Render
method returns a delegate instead. To get theSpan<byte>
corresponding to thei
-th tile, you need to invoke the delegate, passingi
as a parameter:MuPDFMultiThreadedPageRenderer renderer = ... RoundedSize targetSize = ... Rectangle region = ... PixelFormats pixelFormat = ... MuPDFMultiThreadedPageRenderer.GetSpanItem tiles = renderer.Render(targetSize, region, out IDisposable[] disposables, pixelFormat); int tileCount = disposables.Length; for (int i = 0; i < tileCount; i++) { // Get the i-th tile. Span<byte> tilePixels = tiles(i); // Do something with tilePixels. // Release the memory where the i-th tile is stored. disposables[i].Dispose(); }
-
If you use the overload of the
MuPDFDocument.Render
method that returns abyte[]
, the pixel data will NOT be marshalled by MuPDFCore. Thus, there should not be much of a performance penalty if you use this overload instead of one with anIntPtr
parameter or one that returns aSpan<byte>
.
All in all, if you just need to manipulate the image using ImageSharp, it is probably better if you just use the MuPDFCore overload that returns a byte[]
; in this way, you don't have to worry about memory management (since this a "normal" byte array that will be collected by the GC as usual), and ImageSharp is going to copy the data to its own memory storage anyways.
The MuPDFMultiThreadedPageRenderer
does not have a Render
overload working with byte[]
arrays, but it's relatively straightforward to get an IntPtr from a byte[] staying in safe mode:
int bufferSize = ...
byte[] buffer = new byte[bufferSize];
GCHandle bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
IntPtr bufferPointer = bufferHandle.AddrOfPinnedObject();
// Do stuff with bufferPointer.
bufferHandle.Free();
As long as you don't wait too much before freeing the GCHandle
, the side effects on the GC shouldn't be too relevant.
In any case, if you need better performance, you may need to look into some other graphics library that lets you work directly with pixel data stored in unmanaged memory (perhaps something like SkiaSharp?)...
from mupdfcore.
This is great! Thanks for the detailed explanation. I will give it a spin right away!
from mupdfcore.
Related Issues (20)
- If MuPdfWrapper.dll can work in 32-bit windows HOT 8
- How to improve or decrease png quality? HOT 3
- .dll problem when released HOT 3
- Method not found: 'Void MuPDFCore.MuPDFContext..ctor(Int64)'. HOT 2
- Cannot open document HOT 5
- Other output image formats HOT 3
- do you support reading encrypted PDF files? HOT 3
- Install MuPDFCore in Blazor WebAssembly HOT 4
- PDFViewerDemo cannot chanage pages HOT 2
- show pdf files continuously HOT 1
- Don't work with musl-based linux distros HOT 2
- MuPDFCore.dll in MuPDFCore NuGet package 1.7 has no strong name HOT 3
- blank area above PDFRender
- using MuPDFRenderer control on Avalonia 11 HOT 3
- Set anti-aliasing level HOT 2
- Is there a way to support iOS and Mac Catalyst? HOT 1
- is there a plan to support the outline HOT 3
- What is the best way to get the text of a pdf? HOT 6
- Always get MuPDFCore.MuPDFException:“Cannot open document” exception in PDFViewer demo HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mupdfcore.