Pixmap.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "mupdf/fitz.h"
  2. #include "mupdf/pdf.h"
  3. #include "MuPDF.h"
  4. #ifndef __PIXMAP
  5. #define __PIXMAP
  6. using namespace System;
  7. #pragma once
  8. namespace MuPDF {
  9. public ref class Pixmap sealed : IDisposable {
  10. public:
  11. property IntPtr Samples {
  12. IntPtr get() { return _samples; }
  13. }
  14. property int Stride {
  15. int get() { return _stride; }
  16. }
  17. property int Width {
  18. int get() { return _width; }
  19. }
  20. property int Height {
  21. int get() { return _height; }
  22. }
  23. property int Components {
  24. int get() { return _components; }
  25. }
  26. property int Colorants {
  27. int get() { return fz_pixmap_colorants(Context::Ptr, _pixmap); }
  28. }
  29. property MuPDF::BBox BBox {
  30. MuPDF::BBox get() { return fz_pixmap_bbox(Context::Ptr, _pixmap); }
  31. }
  32. /// <summary>
  33. /// Return the number of alpha planes in a Pixmap. Does not throw exceptions.
  34. /// </summary>
  35. property int Alpha {
  36. int get() { return fz_pixmap_alpha(Context::Ptr, _pixmap); }
  37. }
  38. void SetBackgroundWhite() {
  39. Clear(0xFF);
  40. }
  41. void Clear(int value) {
  42. fz_clear_pixmap_with_value(Context::Ptr, _pixmap, value);
  43. }
  44. void Invert() {
  45. fz_invert_pixmap(Context::Ptr, _pixmap);
  46. }
  47. bool Tint(int black, int white);
  48. bool Tint(int color) {
  49. return Tint(0, color);
  50. }
  51. void Gamma(float gamma) {
  52. if (gamma == 1.0) {
  53. return;
  54. }
  55. fz_gamma_pixmap(Context::Ptr, _pixmap, gamma);
  56. }
  57. array<Byte>^ GetSampleBytes() {
  58. if (_samples == IntPtr::Zero) {
  59. return nullptr;
  60. }
  61. GcnewArray(Byte, d, _width * _height * _components);
  62. System::Runtime::InteropServices::Marshal::Copy(_samples, d, 0, d->Length);
  63. return d;
  64. }
  65. internal:
  66. Pixmap(fz_pixmap* pixmap) : _pixmap(pixmap) {
  67. auto ctx = Context::Ptr;
  68. _width = fz_pixmap_width(ctx, pixmap);
  69. _height = fz_pixmap_height(ctx, pixmap);
  70. _components = fz_pixmap_components(ctx, pixmap);
  71. _stride = fz_pixmap_stride(ctx, pixmap);
  72. _samples = (IntPtr)(void*)fz_pixmap_samples(ctx, pixmap);
  73. };
  74. ~Pixmap() {
  75. ReleaseHandle();
  76. }
  77. property fz_pixmap* Ptr {
  78. fz_pixmap* get() { return _pixmap; }
  79. }
  80. private:
  81. fz_pixmap* _pixmap;
  82. int _width, _height, _stride, _components;
  83. IntPtr _samples;
  84. void ReleaseHandle() {
  85. fz_drop_pixmap(Context::Ptr, _pixmap);
  86. _pixmap = NULL;
  87. }
  88. };
  89. };
  90. #endif