arklumpus / MuPDFCore

Multiplatform .NET bindings for MuPDF
GNU Affero General Public License v3.0
115 stars 20 forks source link

How to improve or decrease png quality? #11

Closed vlad12244 closed 3 years ago

vlad12244 commented 3 years ago

Hello

I am trying this method, but how can I Increase or decrease the image quality?

MuPDFContext context = new MuPDFContext();
            MuPDFDocument document = new MuPDFDocument(context, @"blankAI\test.pdf");

            document.SaveImage(1, 1, PixelFormats.RGB, @"blankAI\0test.png", RasterOutputFileTypes.PNG);

I also tried some older demo when I changed width and height:(but it gets memory leaking errors )

for (int i = 0; i < document.Pages.Count; i++)
                                    {
                                        //Initialise the renderer for the current page, using two threads (total number of threads: number of pages x 2
                                        renderers[i] = document.GetMultiThreadedRenderer(i, 2);
                                        //Determine the boundaries of the page when it is rendered with a 1.5x zoom factor
                                        RoundedRectangle roundedBounds = document.Pages[i].Bounds.Round(1);
                                        renderedPageSizes[i] = new RoundedSize(roundedBounds.Width / 2, roundedBounds.Height / 2);
                                        //Determine the boundaries of each tile by splitting the total size of the page by the number of  threads.
                                        tileBounds[i] = renderedPageSizes[i].Split(renderers[i].ThreadCount);
                                        destinations[i] = new IntPtr[renderers[i].ThreadCount];
                                        for (int j = 0; j < renderers[i].ThreadCount; j++)
                                        {
                                            //Allocate the required memory for the j-th tile of the i-th page.
                                            //Since we will be rendering with a 24-bit-per-pixel format, the required memory in bytes is height   x width x 3.
                                            destinations[i][j] = System.Runtime.InteropServices.Marshal.AllocHGlobal(tileBounds[i][j].Height * tileBounds[i][j].Width * 3);
                                        }
                                    }
arklumpus commented 3 years ago

Hi!

If you want to change the resolution of the PNG image produced by the SaveImage method, you need to change the value of the second parameter:

// Default resolution
double zoom = 1;
document.SaveImage(1, zoom, PixelFormats.RGB, @"blankAI\0test.png", RasterOutputFileTypes.PNG);

// Higher resolution - better quality
zoom = 2;
document.SaveImage(1, zoom, PixelFormats.RGB, @"blankAI\0test.png", RasterOutputFileTypes.PNG);

// Lower resolution - worse quality
zoom = 0.5;
document.SaveImage(1, zoom, PixelFormats.RGB, @"blankAI\0test.png", RasterOutputFileTypes.PNG);

By the way, note that the first parameter (the page number) is zero-based, so in this way you are rendering the second page of the document.

The reason why you get the exception in the other example is that you are allocating a buffer with a different size than the actual rendered image. In particular, you are doing this:

RoundedRectangle roundedBounds = document.Pages[i].Bounds.Round(1);
renderedPageSizes[i] = new RoundedSize(roundedBounds.Width / 2, roundedBounds.Height / 2);

However, the fact that you are dividing by 2 the size of the roundedBounds rectangle probably introduces a rounding error between the size calculated by your C# code and the one calculated internally by the MuPDF library. This is because MuPDF uses some weird rounding routines, which may add or subtract a pixel somewhat unexpectedly.

The "proper" way to do this is, again, to change the value of the zoom parameter of the Round method, e.g.:

RoundedRectangle roundedBounds = document.Pages[i].Bounds.Round(0.5);
renderedPageSizes[i] = new RoundedSize(roundedBounds.Width, roundedBounds.Height);

After you call this method, you don't really want to change the size of the rounded rectangle you get, otherwise you risk losing the alignment with the MuPDF library.

I hope this makes sense, but let me know if anything isn't clear!

vlad12244 commented 3 years ago

Hello. It seems to work now. Thank you for your quick reply.

arklumpus commented 3 years ago

Great, happy to help!