PDFBitmap.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. namespace Paroxe.PdfRenderer.Internal
  3. {
  4. #if !UNITY_WEBGL || UNITY_EDITOR
  5. public class PDFBitmap : IDisposable
  6. {
  7. public enum BitmapFormat
  8. {
  9. Unknown = 0,
  10. // Gray scale bitmap, one byte per pixel.
  11. Gray = 1,
  12. // 3 bytes per pixel, byte order: blue, green, red.
  13. BGR = 2,
  14. // 4 bytes per pixel, byte order: blue, green, red, unused.
  15. BGRx = 3,
  16. // 4 bytes per pixel, byte order: blue, green, red, alpha.
  17. BGRA = 4
  18. }
  19. private bool m_Disposed;
  20. private IntPtr m_NativePointer;
  21. private readonly int m_Width;
  22. private readonly int m_Height;
  23. private readonly bool m_UseAlphaChannel;
  24. public PDFBitmap(int width, int height, bool useAlphaChannel)
  25. {
  26. PDFLibrary.AddRef("PDFBitmap");
  27. m_Width = width;
  28. m_Height = height;
  29. m_UseAlphaChannel = useAlphaChannel;
  30. m_NativePointer = NativeMethods.FPDFBitmap_Create(m_Width, m_Height, useAlphaChannel);
  31. }
  32. ~PDFBitmap()
  33. {
  34. Dispose(false);
  35. }
  36. public void Dispose()
  37. {
  38. Dispose(true);
  39. GC.SuppressFinalize(this);
  40. }
  41. protected virtual void Dispose(bool disposing)
  42. {
  43. if (!m_Disposed)
  44. {
  45. lock (PDFLibrary.nativeLock)
  46. {
  47. if (m_NativePointer != IntPtr.Zero)
  48. NativeMethods.FPDFBitmap_Destroy(m_NativePointer);
  49. m_NativePointer = IntPtr.Zero;
  50. }
  51. PDFLibrary.RemoveRef("PDFBitmap");
  52. m_Disposed = true;
  53. }
  54. }
  55. public int Width
  56. {
  57. get { return m_Width; }
  58. }
  59. public int Height
  60. {
  61. get { return m_Height; }
  62. }
  63. public bool UseAlphaChannel
  64. {
  65. get { return m_UseAlphaChannel; }
  66. }
  67. public BitmapFormat Format
  68. {
  69. get { return NativeMethods.FPDFBitmap_GetFormat(m_NativePointer); }
  70. }
  71. public IntPtr NativePointer
  72. {
  73. get { return m_NativePointer; }
  74. }
  75. public bool HasSameSize(PDFBitmap other)
  76. {
  77. return (m_Width == other.m_Width && m_Height == other.m_Height);
  78. }
  79. public bool HasSameSize(int width, int height)
  80. {
  81. return (m_Width == width && m_Height == height);
  82. }
  83. public void FillRect(int left, int top, int width, int height, int color)
  84. {
  85. NativeMethods.FPDFBitmap_FillRect(m_NativePointer, left, top, width, height, color);
  86. }
  87. public IntPtr GetBuffer()
  88. {
  89. return NativeMethods.FPDFBitmap_GetBuffer(m_NativePointer);
  90. }
  91. public int GetStride()
  92. {
  93. return NativeMethods.FPDFBitmap_GetStride(m_NativePointer);
  94. }
  95. }
  96. #endif
  97. }