VectSharp  2.2.1
A light library for C# vector graphics
RasterImages.cs
1 /*
2  VectSharp - A light library for C# vector graphics.
3  Copyright (C) 2020 Giorgio Bianchini
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Affero General Public License as
7  published by the Free Software Foundation, version 3.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Affero General Public License for more details.
13 
14  You should have received a copy of the GNU Affero General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>
16 */
17 
18 using MuPDFCore;
19 using System;
20 using System.IO;
21 using System.Runtime.InteropServices;
22 
23 namespace VectSharp.MuPDFUtils
24 {
25  /// <summary>
26  /// A <see cref="RasterImage"/> created from a file.
27  /// </summary>
29  {
30  /// <summary>
31  /// Creates a new <see cref="RasterImage"/> from the specified file.
32  /// </summary>
33  /// <param name="fileName">The path to the file containing the image.</param>
34  /// <param name="pageNumber">The number of the page in the file from which the image should be created, starting at 0. Only useful for multi-page formats, such as PDF.</param>
35  /// <param name="scale">The scale factor at which to render the image.</param>
36  /// <param name="alpha">A boolean value indicating whether transparency (alpha) data from the image should be preserved or not.</param>
37  /// <param name="interpolate">A boolean value indicating whether the image should be interpolated when it is resized or not.</param>
38  public RasterImageFile(string fileName, int pageNumber = 0, double scale = 1, bool alpha = true, bool interpolate = true)
39  {
40  using (MuPDFContext context = new MuPDFContext())
41  {
42  using (MuPDFDocument document = new MuPDFDocument(context, fileName))
43  {
44  RoundedRectangle roundedBounds = document.Pages[pageNumber].Bounds.Round(scale);
45 
46  this.Width = roundedBounds.Width;
47  this.Height = roundedBounds.Height;
48 
49  this.HasAlpha = alpha;
50  this.Interpolate = interpolate;
51 
52  this.Id = Guid.NewGuid().ToString();
53 
54  int imageSize = document.GetRenderedSize(pageNumber, scale, alpha ? MuPDFCore.PixelFormats.RGBA : MuPDFCore.PixelFormats.RGB);
55 
56  this.ImageDataAddress = Marshal.AllocHGlobal(imageSize);
58  GC.AddMemoryPressure(imageSize);
59 
60  document.Render(pageNumber, scale, alpha ? MuPDFCore.PixelFormats.RGBA : MuPDFCore.PixelFormats.RGB, this.ImageDataAddress);
61  }
62  }
63  }
64  }
65 
66  /// <summary>
67  /// A <see cref="RasterImage"/> created from a stream.
68  /// </summary>
70  {
71  /// <summary>
72  /// Creates a new <see cref="RasterImage"/> from the specified stream.
73  /// </summary>
74  /// <param name="imageStream">The stream containing the image data.</param>
75  /// <param name="fileType">The type of the image contained in the stream.</param>
76  /// <param name="pageNumber">The number of the page in the file from which the image should be created, starting at 0. Only useful for multi-page formats, such as PDF.</param>
77  /// <param name="scale">The scale factor at which to render the image.</param>
78  /// <param name="alpha">A boolean value indicating whether transparency (alpha) data from the image should be preserved or not.</param>
79  /// <param name="interpolate">A boolean value indicating whether the image should be interpolated when it is resized or not.</param>
80  public RasterImageStream(Stream imageStream, InputFileTypes fileType, int pageNumber = 0, double scale = 1, bool alpha = true, bool interpolate = true)
81  {
82  using (MuPDFContext context = new MuPDFContext())
83  {
84  IntPtr originalImageAddress;
85  long originalImageLength;
86 
87  IDisposable toBeDisposed = null;
88  GCHandle handleToFree;
89 
90  if (imageStream is MemoryStream ms)
91  {
92  int origin = (int)ms.Seek(0, SeekOrigin.Begin);
93  originalImageLength = ms.Length;
94 
95  handleToFree = GCHandle.Alloc(ms.GetBuffer(), GCHandleType.Pinned);
96  originalImageAddress = handleToFree.AddrOfPinnedObject();
97  }
98  else
99  {
100  MemoryStream mem = new MemoryStream((int)imageStream.Length);
101  imageStream.CopyTo(mem);
102 
103  toBeDisposed = mem;
104 
105  int origin = (int)mem.Seek(0, SeekOrigin.Begin);
106  originalImageLength = mem.Length;
107 
108  handleToFree = GCHandle.Alloc(mem.GetBuffer(), GCHandleType.Pinned);
109  originalImageAddress = handleToFree.AddrOfPinnedObject();
110  }
111 
112  using (MuPDFDocument document = new MuPDFDocument(context, originalImageAddress, originalImageLength, fileType))
113  {
114  RoundedRectangle roundedBounds = document.Pages[pageNumber].Bounds.Round(scale);
115 
116  this.Width = roundedBounds.Width;
117  this.Height = roundedBounds.Height;
118 
119  this.HasAlpha = alpha;
120  this.Interpolate = interpolate;
121 
122  this.Id = Guid.NewGuid().ToString();
123 
124  int imageSize = document.GetRenderedSize(pageNumber, scale, alpha ? MuPDFCore.PixelFormats.RGBA : MuPDFCore.PixelFormats.RGB);
125 
126  this.ImageDataAddress = Marshal.AllocHGlobal(imageSize);
128  GC.AddMemoryPressure(imageSize);
129 
130  document.Render(pageNumber, scale, alpha ? MuPDFCore.PixelFormats.RGBA : MuPDFCore.PixelFormats.RGB, this.ImageDataAddress);
131  }
132 
133  handleToFree.Free();
134  toBeDisposed?.Dispose();
135  }
136  }
137 
138  /// <summary>
139  /// Creates a new <see cref="RasterImage"/> from the specified stream.
140  /// </summary>
141  /// <param name="imageAddress">A pointer to the address where the image data is contained.</param>
142  /// <param name="imageLength">The length in bytes of the image data.</param>
143  /// <param name="fileType">The type of the image contained in the stream.</param>
144  /// <param name="pageNumber">The number of the page in the file from which the image should be created, starting at 0. Only useful for multi-page formats, such as PDF.</param>
145  /// <param name="scale">The scale factor at which to render the image.</param>
146  /// <param name="alpha">A boolean value indicating whether transparency (alpha) data from the image should be preserved or not.</param>
147  /// <param name="interpolate">A boolean value indicating whether the image should be interpolated when it is resized or not.</param>
148  public RasterImageStream(IntPtr imageAddress, long imageLength, InputFileTypes fileType, int pageNumber = 0, double scale = 1, bool alpha = true, bool interpolate = true)
149  {
150  using (MuPDFContext context = new MuPDFContext())
151  {
152  using (MuPDFDocument document = new MuPDFDocument(context, imageAddress, imageLength, fileType))
153  {
154  RoundedRectangle roundedBounds = document.Pages[pageNumber].Bounds.Round(scale);
155 
156  this.Width = roundedBounds.Width;
157  this.Height = roundedBounds.Height;
158 
159  this.HasAlpha = alpha;
160  this.Interpolate = interpolate;
161 
162  this.Id = Guid.NewGuid().ToString();
163 
164  int imageSize = document.GetRenderedSize(pageNumber, scale, alpha ? MuPDFCore.PixelFormats.RGBA : MuPDFCore.PixelFormats.RGB);
165 
166  this.ImageDataAddress = Marshal.AllocHGlobal(imageSize);
168  GC.AddMemoryPressure(imageSize);
169 
170  document.Render(pageNumber, scale, alpha ? MuPDFCore.PixelFormats.RGBA : MuPDFCore.PixelFormats.RGB, this.ImageDataAddress);
171  }
172  }
173  }
174  }
175 }
VectSharp.MuPDFUtils.RasterImageStream.RasterImageStream
RasterImageStream(IntPtr imageAddress, long imageLength, InputFileTypes fileType, int pageNumber=0, double scale=1, bool alpha=true, bool interpolate=true)
Creates a new RasterImage from the specified stream.
Definition: RasterImages.cs:148
VectSharp.MuPDFUtils
Definition: ImageURIParser.cs:25
VectSharp.RasterImage
Represents a raster image, created from raw pixel data. Consider using the derived classes included i...
Definition: RasterImage.cs:99
VectSharp.MuPDFUtils.RasterImageFile.RasterImageFile
RasterImageFile(string fileName, int pageNumber=0, double scale=1, bool alpha=true, bool interpolate=true)
Creates a new RasterImage from the specified file.
Definition: RasterImages.cs:38
VectSharp.DisposableIntPtr
An IDisposable wrapper around an IntPtr that frees the allocated memory when it is disposed.
Definition: RasterImage.cs:54
VectSharp.RasterImage.Width
int Width
The width in pixels of the image.
Definition: RasterImage.cs:123
VectSharp.MuPDFUtils.RasterImageStream.RasterImageStream
RasterImageStream(Stream imageStream, InputFileTypes fileType, int pageNumber=0, double scale=1, bool alpha=true, bool interpolate=true)
Creates a new RasterImage from the specified stream.
Definition: RasterImages.cs:80
VectSharp.MuPDFUtils.RasterImageStream
A RasterImage created from a stream.
Definition: RasterImages.cs:70
VectSharp.RasterImage.Interpolate
bool Interpolate
Determines whether the image should be interpolated when it is resized.
Definition: RasterImage.cs:133
VectSharp.RasterImage.HasAlpha
bool HasAlpha
Determines whether the image has an alpha channel.
Definition: RasterImage.cs:118
VectSharp.RasterImage.DataHolder
IDisposable DataHolder
An IDisposable that will be disposed when the image is disposed.
Definition: RasterImage.cs:108
VectSharp.RasterImage.Height
int Height
The height in pixels of the image.
Definition: RasterImage.cs:128
VectSharp.RasterImage.ImageDataAddress
IntPtr ImageDataAddress
The memory address of the image pixel data.
Definition: RasterImage.cs:103
VectSharp.MuPDFUtils.RasterImageFile
A RasterImage created from a file.
Definition: RasterImages.cs:29
VectSharp.RasterImage.Id
string Id
A univocal identifier for this image.
Definition: RasterImage.cs:113