Skip to content

Commit

Permalink
Back ported setvariable fix from 3.03 to dev (Issues #120 and #68)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesw committed Sep 20, 2014
1 parent d260eae commit e3cfe2f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,4 @@ pip-log.txt

/*.Log
*.sdsettings
*.orig
24 changes: 24 additions & 0 deletions src/Tesseract.Tests/EngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ public void CanSetDoubleVariable(string variableName, double variableValue)
[TestCase("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")]
[TestCase("tessedit_char_whitelist", "")]
[TestCase("tessedit_char_whitelist", "Test")]
[TestCase("tessedit_char_whitelist", "chinese 漢字")] // Issue 68
public void CanSetStringVariable(string variableName, string variableValue)
{
using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default)) {
Expand All @@ -328,6 +329,29 @@ public void CanSetStringVariable(string variableName, string variableValue)
}
}


/// <summary>
/// As per Bug #52 setting 'classify_bln_numeric_mode' variable to '1' causes the engine to fail on processing.
/// </summary>
[Test,
Ignore("Broken in Tesseract 3.02")]
public void CanSetClassifyBlnNumericModeVariable()
{
using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default)) {
engine.SetVariable("classify_bln_numeric_mode", 1);

using(var img = Pix.LoadFromFile("./Data/processing/numbers.png")) {
using(var page = engine.Process(img)) {
var text = page.GetText();

const string expectedText = "1234567890\n\n";

Assert.That(text, Is.EqualTo(expectedText));
}
}
}
}

#endregion

#region File Helpers
Expand Down
33 changes: 30 additions & 3 deletions src/Tesseract/Interop/BaseApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ int BaseApiInit(HandleRef handle,


[RuntimeDllImport(Constants.TesseractDllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "TessBaseAPISetVariable")]
int BaseApiSetVariable(HandleRef handle, string name, string value);
int BaseApiSetVariable(HandleRef handle, string name, IntPtr valPtr);


[RuntimeDllImport(Constants.TesseractDllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "TessBaseAPISetDebugVariable")]
int BaseApiSetDebugVariable(HandleRef handle, string name, string value);
int BaseApiSetDebugVariable(HandleRef handle, string name, IntPtr valPtr);

[RuntimeDllImport(Constants.TesseractDllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "TessBaseAPIGetIntVariable")]
int BaseApiGetIntVariable(HandleRef handle, string name, out int value);
Expand Down Expand Up @@ -205,12 +205,39 @@ public static void Initialize()
native = InteropRuntimeImplementer.CreateInstance<ITessApiSignatures>();
}
}

public static int BaseApiSetVariable(HandleRef handle, string name, string value)
{
IntPtr valuePtr = IntPtr.Zero;
try {
valuePtr = MarshalHelper.StringToPtr(value, Encoding.UTF8);
return Native.BaseApiSetVariable(handle, name, valuePtr);
} finally {
if(valuePtr != IntPtr.Zero) {
Marshal.FreeHGlobal(valuePtr);
}
}
}

public static int BaseApiSetDebugVariable(HandleRef handle, string name, string value)
{
IntPtr valuePtr = IntPtr.Zero;
try {
valuePtr = MarshalHelper.StringToPtr(value, Encoding.UTF8);
return Native.BaseApiSetDebugVariable(handle, name, valuePtr);
} finally {
if(valuePtr != IntPtr.Zero) {
Marshal.FreeHGlobal(valuePtr);
}
}
}


public static string BaseApiGetStringVariable(HandleRef handle, string name)
{
var resultHandle = Native.BaseApiGetStringVariableInternal(handle, name);
return MarshalHelper.PtrToString(resultHandle, Encoding.UTF8);

return Marshal.PtrToStringAnsi(resultHandle);
}

public static string BaseAPIGetUTF8Text(HandleRef handle)
Expand Down
14 changes: 14 additions & 0 deletions src/Tesseract/Interop/MarshalHelper.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace Tesseract.Interop
{
unsafe static class MarshalHelper
{
public static IntPtr StringToPtr(string value, Encoding encoding)
{
var encoder = encoding.GetEncoder();
var length = encoding.GetByteCount(value);
// The encoded value is null terminated that's the reason for the '+1'.
var encodedValue = new byte[length + 1];
encoding.GetBytes(value, 0, value.Length, encodedValue, 0);
var handle = Marshal.AllocHGlobal(new IntPtr(encodedValue.Length));
Marshal.Copy(encodedValue, 0, handle, encodedValue.Length);
return handle;
}


public static string PtrToString(IntPtr handle, Encoding encoding)
{
var length = StrLength(handle);
Expand Down
10 changes: 5 additions & 5 deletions src/Tesseract/TesseractEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public string Version
/// <returns>Returns <c>True</c> if successful; otherwise <c>False</c>.</returns>
public bool SetVariable(string name, string value)
{
return Interop.TessApi.Native.BaseApiSetVariable(handle, name, value) != 0;
return Interop.TessApi.BaseApiSetVariable(handle, name, value) != 0;
}

/// <summary>
Expand All @@ -94,7 +94,7 @@ public bool SetVariable(string name, string value)
public bool SetVariable(string name, bool value)
{
var strEncodedValue = value ? "TRUE" : "FALSE";
return Interop.TessApi.Native.BaseApiSetVariable(handle, name, strEncodedValue) != 0;
return Interop.TessApi.BaseApiSetVariable(handle, name, strEncodedValue) != 0;
}

/// <summary>
Expand All @@ -106,7 +106,7 @@ public bool SetVariable(string name, bool value)
public bool SetVariable(string name, int value)
{
var strEncodedValue = value.ToString("D", CultureInfo.InvariantCulture.NumberFormat);
return Interop.TessApi.Native.BaseApiSetVariable(handle, name, strEncodedValue) != 0;
return Interop.TessApi.BaseApiSetVariable(handle, name, strEncodedValue) != 0;
}

/// <summary>
Expand All @@ -118,12 +118,12 @@ public bool SetVariable(string name, int value)
public bool SetVariable(string name, double value)
{
var strEncodedValue = value.ToString("R", CultureInfo.InvariantCulture.NumberFormat);
return Interop.TessApi.Native.BaseApiSetVariable(handle, name, strEncodedValue) != 0;
return Interop.TessApi.BaseApiSetVariable(handle, name, strEncodedValue) != 0;
}

public bool SetDebugVariable(string name, string value)
{
return Interop.TessApi.Native.BaseApiSetDebugVariable(handle, name, value) != 0;
return Interop.TessApi.BaseApiSetDebugVariable(handle, name, value) != 0;
}

/// <summary>
Expand Down

0 comments on commit e3cfe2f

Please sign in to comment.