Skip to content

Commit

Permalink
Dispose SKBitmap after Encode calls
Browse files Browse the repository at this point in the history
  • Loading branch information
sungaila committed Dec 29, 2023
1 parent 6beeac1 commit e1cc513
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/PDFtoImage/Conversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,8 @@ internal static void SaveImpl(Stream imageStream, SKEncodedImageFormat format, s
if (imageStream == null)
throw new ArgumentNullException(nameof(imageStream));

ToImage(pdfAsBase64String, password, page, dpi, width, height, withAnnotations, withFormFill, withAspectRatio, rotation, antiAliasing, backgroundColor).Encode(imageStream, format, 100);
using var bitmap = ToImage(pdfAsBase64String, password, page, dpi, width, height, withAnnotations, withFormFill, withAspectRatio, rotation, antiAliasing, backgroundColor);
bitmap.Encode(imageStream, format, 100);
}

#if NET6_0_OR_GREATER
Expand Down Expand Up @@ -568,7 +569,8 @@ internal static void SaveImpl(Stream imageStream, SKEncodedImageFormat format, b
if (imageStream == null)
throw new ArgumentNullException(nameof(imageStream));

ToImage(pdfAsByteArray, password, page, dpi, width, height, withAnnotations, withFormFill, withAspectRatio, rotation, antiAliasing, backgroundColor).Encode(imageStream, format, 100);
using var bitmap = ToImage(pdfAsByteArray, password, page, dpi, width, height, withAnnotations, withFormFill, withAspectRatio, rotation, antiAliasing, backgroundColor);
bitmap.Encode(imageStream, format, 100);
}

#if NET6_0_OR_GREATER
Expand All @@ -591,7 +593,8 @@ internal static void SaveImpl(string filename, SKEncodedImageFormat format, Stre
#endif
internal static void SaveImpl(Stream stream, SKEncodedImageFormat format, Stream pdfStream, string? password = null, int page = 0, int dpi = 300, int? width = null, int? height = null, bool withAnnotations = false, bool withFormFill = false, bool withAspectRatio = false, PdfRotation rotation = PdfRotation.Rotate0, PdfAntiAliasing antiAliasing = PdfAntiAliasing.All, SKColor? backgroundColor = null)
{
ToImage(pdfStream, password, page, dpi, width, height, withAnnotations, withFormFill, withAspectRatio, rotation, antiAliasing, backgroundColor).Encode(stream, format, 100);
using var bitmap = ToImage(pdfStream, password, page, dpi, width, height, withAnnotations, withFormFill, withAspectRatio, rotation, antiAliasing, backgroundColor);
bitmap.Encode(stream, format, 100);
}
#endregion

Expand Down
30 changes: 28 additions & 2 deletions src/PDFtoImage/PdfiumViewer/NativeMethods.Pdfium.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ public static int FPDF_GetPageSizeByIndex(IntPtr document, int page_index, out d
}
}

public static IntPtr FPDFBitmap_CreateEx(int width, int height, int format, IntPtr first_scan, int stride)
public static IntPtr FPDFBitmap_CreateEx(int width, int height, FPDFBitmap format, IntPtr first_scan, int stride)
{
lock (LockString)
{
return Imports.FPDFBitmap_CreateEx(width, height, format, first_scan, stride);
return Imports.FPDFBitmap_CreateEx(width, height, (int)format, first_scan, stride);
}
}

Expand Down Expand Up @@ -653,6 +653,32 @@ public enum FPDFPAGE_AACTION
CLOSE = 1
}

/// <summary>
/// A number indicating for bitmap format.
/// </summary>
public enum FPDFBitmap : int
{
/// <summary>
/// Gray scale bitmap, one byte per pixel.
/// </summary>
Gray = 1,

/// <summary>
/// 3 bytes per pixel, byte order: blue, green, red.
/// </summary>
BGR = 2,

/// <summary>
/// 4 bytes per pixel, byte order: blue, green, red, unused.
/// </summary>
BGRx = 3,

/// <summary>
/// 4 bytes per pixel, byte order: blue, green, red, alpha.
/// </summary>
BGRA = 4
}

[Flags]
public enum FPDF
{
Expand Down
2 changes: 1 addition & 1 deletion src/PDFtoImage/PdfiumViewer/PdfDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public SKBitmap Render(int page, int width, int height, float dpiX, float dpiY,
}

var bitmap = new SKBitmap(width, height, SKColorType.Bgra8888, SKAlphaType.Premul);
var handle = NativeMethods.FPDFBitmap_CreateEx(width, height, 4, bitmap.GetPixels(), width * 4);
var handle = NativeMethods.FPDFBitmap_CreateEx(width, height, NativeMethods.FPDFBitmap.BGRA, bitmap.GetPixels(), width * 4);

try
{
Expand Down
6 changes: 3 additions & 3 deletions src/Tests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ public static string GetPlatformAsString()

public static FileStream GetInputStream(string filePath)
{
return new FileStream(filePath, FileMode.Open, FileAccess.Read);
return new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan);
}

public static FileStream GetExpectedStream(string filePath)
{
if (!File.Exists(filePath))
Assert.Inconclusive("The expected asset '{0}' could not be found.", filePath);

return new FileStream(filePath, FileMode.Open, FileAccess.Read);
return new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan);
}

private static readonly object _lockObject = new();
Expand All @@ -87,7 +87,7 @@ public static Stream CreateOutputStream(string expectedPath)
outputPath,
FileMode.CreateNew,
FileAccess.ReadWrite,
FileShare.Read,
FileShare.None,
4096,
FileOptions.SequentialScan);
}
Expand Down
3 changes: 2 additions & 1 deletion src/WebConverter/Pages/Index.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ private async Task Reset()
private async Task Submit()
{
Logger.LogInformation("Converting {Model}.", Model);
SKBitmap? bitmap = null;

try
{
Expand All @@ -134,7 +135,6 @@ private async Task Submit()
}

Model.Input.Position = 0;
SKBitmap? bitmap = null;
bool encodeSuccess = false;
PdfAntiAliasing antiAliasing = default;
var backgroundColor = SKColors.White;
Expand Down Expand Up @@ -187,6 +187,7 @@ await Task.Factory.StartNew(() =>
}
finally
{
bitmap?.Dispose();
IsLoading = false;
}
}
Expand Down

0 comments on commit e1cc513

Please sign in to comment.