x64 Any CPU support

This commit is contained in:
Paddy Xu
2017-06-03 02:50:39 +03:00
parent 438781b7f7
commit 3101059b4d
32 changed files with 621 additions and 232 deletions

View File

@@ -15,7 +15,10 @@ namespace QuickLook.Plugin.PDFViewer
var pix = IntPtr.Zero;
var dev = IntPtr.Zero;
NativeMethods.BoundPage(document, page, ref pageBound);
if (App.Is64Bit)
NativeMethods.BoundPage_64(document, page, ref pageBound);
else
NativeMethods.BoundPage_32(document, page, ref pageBound);
var currentDpi = DpiHelper.GetCurrentDpi();
var zoomX = zoomFactor * (currentDpi.HorizontalDpi / DpiHelper.DEFAULT_DPI);
@@ -30,17 +33,33 @@ namespace QuickLook.Plugin.PDFViewer
ctm.D = (float) zoomY;
// creates a pixmap the same size as the width and height of the page
pix = NativeMethods.NewPixmap(context, NativeMethods.LookupDeviceColorSpace(context, "DeviceRGB"), width,
height);
if (App.Is64Bit)
pix = NativeMethods.NewPixmap_64(context,
NativeMethods.LookupDeviceColorSpace_64(context, "DeviceRGB"), width, height);
else
pix = NativeMethods.NewPixmap_32(context,
NativeMethods.LookupDeviceColorSpace_32(context, "DeviceRGB"), width, height);
// sets white color as the background color of the pixmap
NativeMethods.ClearPixmap(context, pix, 0xFF);
if (App.Is64Bit)
NativeMethods.ClearPixmap_64(context, pix, 0xFF);
else
NativeMethods.ClearPixmap_32(context, pix, 0xFF);
// creates a drawing device
dev = NativeMethods.NewDrawDevice(context, pix);
if (App.Is64Bit)
dev = NativeMethods.NewDrawDevice_64(context, pix);
else
dev = NativeMethods.NewDrawDevice_32(context, pix);
// draws the page on the device created from the pixmap
NativeMethods.RunPage(document, page, dev, ref ctm, IntPtr.Zero);
if (App.Is64Bit)
NativeMethods.RunPage_64(document, page, dev, ref ctm, IntPtr.Zero);
else
NativeMethods.RunPage_32(document, page, dev, ref ctm, IntPtr.Zero);
NativeMethods.FreeDevice(dev); // frees the resources consumed by the device
if (App.Is64Bit)
NativeMethods.FreeDevice_64(dev); // frees the resources consumed by the device
else
NativeMethods.FreeDevice_32(dev); // frees the resources consumed by the device
dev = IntPtr.Zero;
// creates a colorful bitmap of the same size of the pixmap
@@ -50,7 +69,13 @@ namespace QuickLook.Plugin.PDFViewer
unsafe
{
// converts the pixmap data to Bitmap data
var ptrSrc = (byte*) NativeMethods.GetSamples(context, pix); // gets the rendered data from the pixmap
byte* ptrSrc;
if (App.Is64Bit)
ptrSrc =
(byte*) NativeMethods.GetSamples_64(context, pix); // gets the rendered data from the pixmap
else
ptrSrc = (byte*) NativeMethods
.GetSamples_32(context, pix); // gets the rendered data from the pixmap
var ptrDest = (byte*) imageData.Scan0;
for (var y = 0; y < height; y++)
{
@@ -72,7 +97,10 @@ namespace QuickLook.Plugin.PDFViewer
}
}
bmp.UnlockBits(imageData);
NativeMethods.DropPixmap(context, pix);
if (App.Is64Bit)
NativeMethods.DropPixmap_64(context, pix);
else
NativeMethods.DropPixmap_32(context, pix);
bmp.SetResolution(currentDpi.HorizontalDpi, currentDpi.VerticalDpi);
@@ -104,70 +132,137 @@ namespace QuickLook.Plugin.PDFViewer
internal class NativeMethods
{
private const uint FZ_STORE_DEFAULT = 256 << 20;
private const string DLL = "libmupdf.dll";
// please modify the version number to match the FZ_VERSION definition in "fitz\version.h" file
private const string MuPDFVersion = "1.6";
[DllImport(DLL, EntryPoint = "fz_new_context_imp", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr NewContext(IntPtr alloc, IntPtr locks, uint max_store, string version);
private const uint FzStoreDefault = 256 << 20;
private const string MuPdfVersion = "1.6";
public static IntPtr NewContext()
{
return NewContext(IntPtr.Zero, IntPtr.Zero, FZ_STORE_DEFAULT, MuPDFVersion);
return App.Is64Bit
? NewContext_64(IntPtr.Zero, IntPtr.Zero, FzStoreDefault, MuPdfVersion)
: NewContext_32(IntPtr.Zero, IntPtr.Zero, FzStoreDefault, MuPdfVersion);
}
[DllImport(DLL, EntryPoint = "fz_free_context", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr FreeContext(IntPtr ctx);
[DllImport("LibMuPdf.dll", EntryPoint = "fz_new_context_imp", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr NewContext_32(IntPtr alloc, IntPtr locks, uint maxStore, string version);
[DllImport(DLL, EntryPoint = "fz_open_file_w", CharSet = CharSet.Unicode,
[DllImport("LibMuPdf.dll", EntryPoint = "fz_free_context", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr FreeContext_32(IntPtr ctx);
[DllImport("LibMuPdf.dll", EntryPoint = "fz_open_file_w", CharSet = CharSet.Unicode,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr OpenFile(IntPtr ctx, string fileName);
public static extern IntPtr OpenFile_32(IntPtr ctx, string fileName);
[DllImport(DLL, EntryPoint = "pdf_open_document_with_stream", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr OpenDocumentStream(IntPtr ctx, IntPtr stm);
[DllImport("LibMuPdf.dll", EntryPoint = "pdf_open_document_with_stream",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr OpenDocumentStream_32(IntPtr ctx, IntPtr stm);
[DllImport(DLL, EntryPoint = "fz_close", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CloseStream(IntPtr stm);
[DllImport("LibMuPdf.dll", EntryPoint = "fz_close", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CloseStream_32(IntPtr stm);
[DllImport(DLL, EntryPoint = "pdf_close_document", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CloseDocument(IntPtr doc);
[DllImport("LibMuPdf.dll", EntryPoint = "pdf_close_document", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CloseDocument_32(IntPtr doc);
[DllImport(DLL, EntryPoint = "pdf_count_pages", CallingConvention = CallingConvention.Cdecl)]
public static extern int CountPages(IntPtr doc);
[DllImport("LibMuPdf.dll", EntryPoint = "pdf_count_pages", CallingConvention = CallingConvention.Cdecl)]
public static extern int CountPages_32(IntPtr doc);
[DllImport(DLL, EntryPoint = "pdf_bound_page", CallingConvention = CallingConvention.Cdecl)]
public static extern void BoundPage(IntPtr doc, IntPtr page, ref Rectangle bound);
[DllImport("LibMuPdf.dll", EntryPoint = "pdf_bound_page", CallingConvention = CallingConvention.Cdecl)]
public static extern void BoundPage_32(IntPtr doc, IntPtr page, ref Rectangle bound);
[DllImport(DLL, EntryPoint = "fz_clear_pixmap_with_value", CallingConvention = CallingConvention.Cdecl)]
public static extern void ClearPixmap(IntPtr ctx, IntPtr pix, int byteValue);
[DllImport("LibMuPdf.dll", EntryPoint = "fz_clear_pixmap_with_value",
CallingConvention = CallingConvention.Cdecl)]
public static extern void ClearPixmap_32(IntPtr ctx, IntPtr pix, int byteValue);
[DllImport(DLL, EntryPoint = "fz_lookup_device_colorspace", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr LookupDeviceColorSpace(IntPtr ctx, string colorspace);
[DllImport("LibMuPdf.dll", EntryPoint = "fz_lookup_device_colorspace",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr LookupDeviceColorSpace_32(IntPtr ctx, string colorspace);
[DllImport(DLL, EntryPoint = "fz_free_device", CallingConvention = CallingConvention.Cdecl)]
public static extern void FreeDevice(IntPtr dev);
[DllImport("LibMuPdf.dll", EntryPoint = "fz_free_device", CallingConvention = CallingConvention.Cdecl)]
public static extern void FreeDevice_32(IntPtr dev);
[DllImport(DLL, EntryPoint = "pdf_free_page", CallingConvention = CallingConvention.Cdecl)]
public static extern void FreePage(IntPtr doc, IntPtr page);
[DllImport("LibMuPdf.dll", EntryPoint = "pdf_free_page", CallingConvention = CallingConvention.Cdecl)]
public static extern void FreePage_32(IntPtr doc, IntPtr page);
[DllImport(DLL, EntryPoint = "pdf_load_page", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr LoadPage(IntPtr doc, int pageNumber);
[DllImport("LibMuPdf.dll", EntryPoint = "pdf_load_page", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr LoadPage_32(IntPtr doc, int pageNumber);
[DllImport(DLL, EntryPoint = "fz_new_draw_device", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr NewDrawDevice(IntPtr ctx, IntPtr pix);
[DllImport("LibMuPdf.dll", EntryPoint = "fz_new_draw_device", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr NewDrawDevice_32(IntPtr ctx, IntPtr pix);
[DllImport(DLL, EntryPoint = "fz_new_pixmap", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr NewPixmap(IntPtr ctx, IntPtr colorspace, int width, int height);
[DllImport("LibMuPdf.dll", EntryPoint = "fz_new_pixmap", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr NewPixmap_32(IntPtr ctx, IntPtr colorspace, int width, int height);
[DllImport(DLL, EntryPoint = "pdf_run_page", CallingConvention = CallingConvention.Cdecl)]
public static extern void RunPage(IntPtr doc, IntPtr page, IntPtr dev, ref Matrix transform, IntPtr cookie);
[DllImport("LibMuPdf.dll", EntryPoint = "pdf_run_page", CallingConvention = CallingConvention.Cdecl)]
public static extern void RunPage_32(IntPtr doc, IntPtr page, IntPtr dev, ref Matrix transform,
IntPtr cookie);
[DllImport(DLL, EntryPoint = "fz_drop_pixmap", CallingConvention = CallingConvention.Cdecl)]
public static extern void DropPixmap(IntPtr ctx, IntPtr pix);
[DllImport("LibMuPdf.dll", EntryPoint = "fz_drop_pixmap", CallingConvention = CallingConvention.Cdecl)]
public static extern void DropPixmap_32(IntPtr ctx, IntPtr pix);
[DllImport(DLL, EntryPoint = "fz_pixmap_samples", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetSamples(IntPtr ctx, IntPtr pix);
[DllImport("LibMuPdf.dll", EntryPoint = "fz_pixmap_samples", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetSamples_32(IntPtr ctx, IntPtr pix);
[DllImport("LibMuPdf.x64.dll", EntryPoint = "fz_new_context_imp",
CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr NewContext_64(IntPtr alloc, IntPtr locks, uint maxStore, string version);
[DllImport("LibMuPdf.x64.dll", EntryPoint = "fz_free_context", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr FreeContext_64(IntPtr ctx);
[DllImport("LibMuPdf.x64.dll", EntryPoint = "fz_open_file_w", CharSet = CharSet.Unicode,
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr OpenFile_64(IntPtr ctx, string fileName);
[DllImport("LibMuPdf.x64.dll", EntryPoint = "pdf_open_document_with_stream",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr OpenDocumentStream_64(IntPtr ctx, IntPtr stm);
[DllImport("LibMuPdf.x64.dll", EntryPoint = "fz_close", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CloseStream_64(IntPtr stm);
[DllImport("LibMuPdf.x64.dll", EntryPoint = "pdf_close_document",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CloseDocument_64(IntPtr doc);
[DllImport("LibMuPdf.x64.dll", EntryPoint = "pdf_count_pages", CallingConvention = CallingConvention.Cdecl)]
public static extern int CountPages_64(IntPtr doc);
[DllImport("LibMuPdf.x64.dll", EntryPoint = "pdf_bound_page", CallingConvention = CallingConvention.Cdecl)]
public static extern void BoundPage_64(IntPtr doc, IntPtr page, ref Rectangle bound);
[DllImport("LibMuPdf.x64.dll", EntryPoint = "fz_clear_pixmap_with_value",
CallingConvention = CallingConvention.Cdecl)]
public static extern void ClearPixmap_64(IntPtr ctx, IntPtr pix, int byteValue);
[DllImport("LibMuPdf.x64.dll", EntryPoint = "fz_lookup_device_colorspace",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr LookupDeviceColorSpace_64(IntPtr ctx, string colorspace);
[DllImport("LibMuPdf.x64.dll", EntryPoint = "fz_free_device", CallingConvention = CallingConvention.Cdecl)]
public static extern void FreeDevice_64(IntPtr dev);
[DllImport("LibMuPdf.x64.dll", EntryPoint = "pdf_free_page", CallingConvention = CallingConvention.Cdecl)]
public static extern void FreePage_64(IntPtr doc, IntPtr page);
[DllImport("LibMuPdf.x64.dll", EntryPoint = "pdf_load_page", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr LoadPage_64(IntPtr doc, int pageNumber);
[DllImport("LibMuPdf.x64.dll", EntryPoint = "fz_new_draw_device",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr NewDrawDevice_64(IntPtr ctx, IntPtr pix);
[DllImport("LibMuPdf.x64.dll", EntryPoint = "fz_new_pixmap", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr NewPixmap_64(IntPtr ctx, IntPtr colorspace, int width, int height);
[DllImport("LibMuPdf.x64.dll", EntryPoint = "pdf_run_page", CallingConvention = CallingConvention.Cdecl)]
public static extern void RunPage_64(IntPtr doc, IntPtr page, IntPtr dev, ref Matrix transform,
IntPtr cookie);
[DllImport("LibMuPdf.x64.dll", EntryPoint = "fz_drop_pixmap", CallingConvention = CallingConvention.Cdecl)]
public static extern void DropPixmap_64(IntPtr ctx, IntPtr pix);
[DllImport("LibMuPdf.x64.dll", EntryPoint = "fz_pixmap_samples",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetSamples_64(IntPtr ctx, IntPtr pix);
}
}
}