Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix leaks of Image objects in TesseractOcrUtil #3

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<OutputType>library</OutputType>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net45</TargetFramework>
<TargetFramework>net46</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
Expand Down Expand Up @@ -42,4 +42,4 @@
<HintPath>..\..\itext\itext.pdfocr.tesseract4\lib\Tesseract.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,62 @@ public virtual void TestHocrStringOutput() {
}
}

[NUnit.Framework.Test]
public virtual void TestTiffIsDisposed() {
String testName = "testTiffIsDisposed";
String path = TEST_IMAGES_DIRECTORY + "numbers_01.tif";
String tmpPath = System.IO.Path.GetTempFileName();
String pdfPath = GetTargetDirectory() + testName + ".pdf";
FileInfo file = new FileInfo(tmpPath);
File.Copy(path, tmpPath, true);
OcrPdfCreator ocrPdfCreator = new OcrPdfCreator(tesseractReader);
NUnit.Framework.Assert.IsTrue(System.GC.TryStartNoGCRegion(8*1024*1024));
PdfDocument doc = ocrPdfCreator.CreatePdf(JavaCollectionsUtil.SingletonList<FileInfo>(file), GetPdfWriter(
pdfPath));
File.Delete(tmpPath);
System.GC.EndNoGCRegion();
NUnit.Framework.Assert.IsNotNull(doc);
doc.Close();
}

[NUnit.Framework.Test]
public virtual void TestTiffIsDisposedWithoutPreprocessing() {
String testName = "testTiffIsDisposedWithoutPreprocessing";
String path = TEST_IMAGES_DIRECTORY + "numbers_01.tif";
String tmpPath = System.IO.Path.GetTempFileName();
String pdfPath = GetTargetDirectory() + testName + ".pdf";
FileInfo file = new FileInfo(tmpPath);
File.Copy(path, tmpPath, true);
tesseractReader.SetTesseract4OcrEngineProperties(tesseractReader.GetTesseract4OcrEngineProperties().SetPreprocessingImages
(false));
OcrPdfCreator ocrPdfCreator = new OcrPdfCreator(tesseractReader);
NUnit.Framework.Assert.IsTrue(System.GC.TryStartNoGCRegion(8 * 1024 * 1024));
PdfDocument doc = ocrPdfCreator.CreatePdf(JavaCollectionsUtil.SingletonList<FileInfo>(file), GetPdfWriter(
pdfPath));
File.Delete(tmpPath);
System.GC.EndNoGCRegion();
NUnit.Framework.Assert.IsNotNull(doc);
doc.Close();
}

[NUnit.Framework.Test]
public virtual void TestJpegIsDisposed() {
String testName = "testJpegIsDisposed";
String path = TEST_IMAGES_DIRECTORY + "numbers_01.jpg";
String tmpPath = System.IO.Path.GetTempFileName();
String pdfPath = GetTargetDirectory() + testName + ".pdf";
FileInfo file = new FileInfo(tmpPath);
File.Copy(path, tmpPath, true);
OcrPdfCreator ocrPdfCreator = new OcrPdfCreator(tesseractReader);
NUnit.Framework.Assert.IsTrue(System.GC.TryStartNoGCRegion(8 * 1024 * 1024));
PdfDocument doc = ocrPdfCreator.CreatePdf(JavaCollectionsUtil.SingletonList<FileInfo>(file), GetPdfWriter(
pdfPath));
File.Delete(tmpPath);
System.GC.EndNoGCRegion();
NUnit.Framework.Assert.IsNotNull(doc);
doc.Close();
}

/// <summary>Parse text from image and compare with expected.</summary>
private void TestImageOcrText(AbstractTesseract4OcrEngine tesseractReader, String path, String expectedOutput
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ internal void InitializeImagesListFromTiff(FileInfo inputFile)
temp.SetResolution(2 * xResolution, 2 * yResolution);
bitmapList.Add(temp);
}
originalImage.Dispose();
SetListOfPages(bitmapList);
} catch (Exception e)
{
Expand Down Expand Up @@ -514,6 +515,7 @@ internal static Bitmap GetImagePage(FileInfo input, int page)
}
image.SelectActiveFrame(FrameDimension.Page, page);
img = new Bitmap(image);
image.Dispose();
} catch (Exception e)
{
LogManager.GetLogger(typeof(TesseractOcrUtil))
Expand Down Expand Up @@ -889,8 +891,8 @@ internal static int DetectRotation(FileInfo inputFile)
{
try
{
System.Drawing.Image image = System.Drawing.Image.FromFile(inputFile.FullName);
return ReadRotationFromMetadata(image);
using (System.Drawing.Image image = System.Drawing.Image.FromFile(inputFile.FullName))
return ReadRotationFromMetadata(image);
}
catch (Exception e)
{
Expand Down