Clean up pdfium resources

This commit is contained in:
Ben Olden-Cooligan 2019-04-13 12:33:37 -04:00
parent 1b20f220d3
commit a8a7c8e3d9
2 changed files with 45 additions and 26 deletions

View File

@ -34,10 +34,14 @@ namespace NAPS2.ImportExport.Pdf
public delegate IntPtr FPDF_LoadMemDocument_delegate(IntPtr buffer, int size, [MarshalAs(UnmanagedType.LPStr)] string password);
public delegate void FPDF_CloseDocument_delegate(IntPtr document);
public delegate int FPDF_GetPageCount_delegate(IntPtr document);
public delegate IntPtr FPDF_LoadPage_delegate(IntPtr document, int pageIndex);
public delegate void FPDF_ClosePage_delegate(IntPtr page);
public delegate double FPDF_GetPageWidth_delegate(IntPtr page);
public delegate double FPDF_GetPageHeight_delegate(IntPtr page);
@ -51,8 +55,10 @@ namespace NAPS2.ImportExport.Pdf
public FPDFBitmap_Destroy_delegate FPDFBitmap_Destroy => Load<FPDFBitmap_Destroy_delegate>();
public FPDF_LoadDocument_delegate FPDF_LoadDocument => Load<FPDF_LoadDocument_delegate>();
public FPDF_LoadMemDocument_delegate FPDF_LoadMemDocument => Load<FPDF_LoadMemDocument_delegate>();
public FPDF_CloseDocument_delegate FPDF_CloseDocument => Load<FPDF_CloseDocument_delegate>();
public FPDF_GetPageCount_delegate FPDF_GetPageCount => Load<FPDF_GetPageCount_delegate>();
public FPDF_LoadPage_delegate FPDF_LoadPage => Load<FPDF_LoadPage_delegate>();
public FPDF_ClosePage_delegate FPDF_ClosePage => Load<FPDF_ClosePage_delegate>();
public FPDF_GetPageWidth_delegate FPDF_GetPageWidth => Load<FPDF_GetPageWidth_delegate>();
public FPDF_GetPageHeight_delegate FPDF_GetPageHeight => Load<FPDF_GetPageHeight_delegate>();
public FPDF_RenderPageBitmap_delegate FPDF_RenderPageBitmap => Load<FPDF_RenderPageBitmap_delegate>();

View File

@ -24,36 +24,49 @@ namespace NAPS2.ImportExport.Pdf
public static IEnumerable<IImage> Render(string path, float dpi)
{
var doc = NativeLib.FPDF_LoadDocument(path, null);
var pageCount = NativeLib.FPDF_GetPageCount(doc);
for (int pageIndex = 0; pageIndex < pageCount; pageIndex++)
try
{
var page = NativeLib.FPDF_LoadPage(doc, pageIndex);
var widthInInches = NativeLib.FPDF_GetPageWidth(page) / 72;
var heightInInches = NativeLib.FPDF_GetPageHeight(page) / 72;
// Cap the resolution to 10k pixels in each dimension
dpi = Math.Min(dpi, (float)(10000 / heightInInches));
dpi = Math.Min(dpi, (float)(10000 / widthInInches));
int widthInPx = (int)Math.Round(widthInInches * dpi);
int heightInPx = (int)Math.Round(heightInInches * dpi);
var bitmap = StorageManager.ImageFactory.FromDimensions(widthInPx, heightInPx, StoragePixelFormat.RGB24);
bitmap.SetResolution(dpi, dpi);
var bitmapData = bitmap.Lock(LockMode.ReadWrite, out var scan0, out var stride);
try
var pageCount = NativeLib.FPDF_GetPageCount(doc);
for (int pageIndex = 0; pageIndex < pageCount; pageIndex++)
{
var pdfiumBitmap = NativeLib.FPDFBitmap_CreateEx(widthInPx, heightInPx, PdfiumNativeLibrary.FPDFBitmap_BGR, scan0, stride);
NativeLib.FPDFBitmap_FillRect(pdfiumBitmap, 0, 0, widthInPx, heightInPx, COLOR_WHITE);
NativeLib.FPDF_RenderPageBitmap(pdfiumBitmap, page, 0, 0, widthInPx, heightInPx, 0, RENDER_FLAGS);
yield return bitmap;
}
finally
{
bitmap.Unlock(bitmapData);
var page = NativeLib.FPDF_LoadPage(doc, pageIndex);
try
{
var widthInInches = NativeLib.FPDF_GetPageWidth(page) / 72;
var heightInInches = NativeLib.FPDF_GetPageHeight(page) / 72;
// Cap the resolution to 10k pixels in each dimension
dpi = Math.Min(dpi, (float) (10000 / heightInInches));
dpi = Math.Min(dpi, (float) (10000 / widthInInches));
int widthInPx = (int) Math.Round(widthInInches * dpi);
int heightInPx = (int) Math.Round(heightInInches * dpi);
var bitmap = StorageManager.ImageFactory.FromDimensions(widthInPx, heightInPx, StoragePixelFormat.RGB24);
bitmap.SetResolution(dpi, dpi);
var bitmapData = bitmap.Lock(LockMode.ReadWrite, out var scan0, out var stride);
try
{
var pdfiumBitmap = NativeLib.FPDFBitmap_CreateEx(widthInPx, heightInPx, PdfiumNativeLibrary.FPDFBitmap_BGR, scan0, stride);
NativeLib.FPDFBitmap_FillRect(pdfiumBitmap, 0, 0, widthInPx, heightInPx, COLOR_WHITE);
NativeLib.FPDF_RenderPageBitmap(pdfiumBitmap, page, 0, 0, widthInPx, heightInPx, 0, RENDER_FLAGS);
yield return bitmap;
}
finally
{
bitmap.Unlock(bitmapData);
}
}
finally
{
NativeLib.FPDF_ClosePage(page);
}
}
}
finally
{
NativeLib.FPDF_CloseDocument(doc);
}
}
}
}