wxIScan
wxiscanglobals.cpp File Reference
#include "wx_pch.h"
#include <wx/clipbrd.h>
#include <wx/file.h>
#include "wxiscanglobals.h"

Go to the source code of this file.

Functions

wxImage Image2Mono (const wxImage &oSrcImage, int nMonoThreshold)
 Convert an image to monochrome (black and white).
static double x_exp_y (double x, double y)
 Calculate the value of x^y.
static unsigned char Limit255 (int x)
 Limit the given integer value to the intervall [0..255] (8 bit).
wxImage EnhenceColours (const wxImage &oSrcImage, int nBrightness, int nContrast, int nGamma, int nRed, int nGreen, int nBlue)
 Modify brightness, contrast, gamma value, red, green and blue channel of an image.
bool CopyText2Clipboard (const wxString &strText)
 Copy the given text to the sytem clipboard.
bool CopyImage2Clipboard (const wxImage &oImage)
 Copy the given image to the sytem clipboard.
wxString GetTextFromFile (const wxString &strFilename)
 Load the content of a text file into memory.

Function Documentation

bool CopyImage2Clipboard ( const wxImage &  oImage)

Copy the given image to the sytem clipboard.

Parameters:
oImagethe image to copy to the clipboard

Definition at line 199 of file wxiscanglobals.cpp.

Referenced by wxIScanFrame::OnEditCopy().

{
    // Create a image data object for the clipboard.
    wxBitmapDataObject *poBitmapData=new wxBitmapDataObject;

    if( !poBitmapData )
    {
        wxLogError( wxString( wxT( "wxIScanFrame::CopyImage2Clipboard -- " ) )
                      + _( "Failed to create clipboard image data object." ) );
        return false;
    }

    // Copy the image to the image data object for the clipboard.
    poBitmapData->SetBitmap( oImage );

    // Open the clipboard and copy the image to the clipboard.
    bool bRetC= wxTheClipboard->Open() && wxTheClipboard->SetData( poBitmapData );

    // Close the clipboard (and release it).
    wxTheClipboard->Close();

    // Log errors.
    if( !bRetC )
    {
        wxLogError( wxString( wxT( "wxIScanFrame::CopyImage2Clipboard -- " ) )
                      + _( "Failed to copy image to clipboard." ) );
    }

    // Return the error state.
    return bRetC;
}
bool CopyText2Clipboard ( const wxString &  strText)

Copy the given text to the sytem clipboard.

Parameters:
strTextthe text to copy to the clipboard

Definition at line 165 of file wxiscanglobals.cpp.

Referenced by wxIScanFrame::OnDocumentCopyText().

{
    // Create a text data object for the clipboard.
    wxTextDataObject *poTextData=new wxTextDataObject;

    if( !poTextData )
    {
        wxLogError( wxString( wxT( "wxIScanFrame::CopyText2Clipboard -- " ) )
                      + _( "Failed to create clipboard text data object." ) );
        return false;
    }

    // Copy the text to the text data object for the clipboard.
    poTextData->SetText( strText );

    // Open the clipboard and copy the text to the clipboard.
    bool bRetC= wxTheClipboard->Open() && wxTheClipboard->SetData( poTextData );

    // Close the clipboard (and release it).
    wxTheClipboard->Close();

    // Log errors.
    if( !bRetC )
    {
        wxLogError( wxString( wxT( "wxIScanFrame::CopyText2Clipboard -- " ) )
                      + _( "Failed to copy text to clipboard." ) );
    }

    // Return the error state.
    return bRetC;
}
wxImage EnhenceColours ( const wxImage &  oSrcImage,
int  nBrightness = 0,
int  nContrast = 0,
int  nGamma = 100,
int  nRed = 0,
int  nGreen = 0,
int  nBlue = 0 
)

Modify brightness, contrast, gamma value, red, green and blue channel of an image.

Parameters:
oSrcImagethe image to be transformed
nBrightnessadjustment value for brightness
nContrastadjustment value for constrast
nGammaadjustment value for gamma value * 100
nRedadjustment value for red value
nGreenadjustment value for green value
nBlueadjustment value for blue value

NOTE: The returned object is the transformed image. The original image is left intact.

Definition at line 99 of file wxiscanglobals.cpp.

References Limit255(), and x_exp_y().

{
    wxImage oImage= oSrcImage;

    // ...
    int nCount= oImage.GetWidth() * oImage.GetHeight() * 3;
    unsigned char *pImageDataSrc= oImage.GetData();
    unsigned char *pImageDataDest= (unsigned char *)malloc( nCount * sizeof( unsigned char ) );

    if( !pImageDataDest )
    {
        wxLogError( wxString( wxT( "EnhenceColours - " ) )
                      + _( "Cannot allocate memory for destination image." ) );
        return oImage;
    }

    // ...
    int nIncRed=   nRed   + nBrightness;
    int nIncGreen= nGreen + nBrightness;
    int nIncBlue=  nBlue  + nBrightness;
    double nContrastFactor= exp( (double)nContrast / 75.0 );
    double nInvGamma= 100.0 / (double)nGamma;

    // Recalculate the image as a whole. That is calculate brightness,
    // contrast, gamma and color correction all at once.
    //
    // NOTE: We use floating point (double) calculation for not to cut off
    //       values as early as 8 bit integer calculation would do (for
    //       numeric stability). The disadvantage is that the transformation
    //       of one image of about 8 Mega pixels (e. g. an A4 page) even on
    //       a 1.6 GHertz machine lasts for more than 5s.
    //
    unsigned char *pIndex= pImageDataDest;
    for( int i= 0; i < nCount; i += 3 )
    {
        // Red.
        (*pIndex)= Limit255( (int)( x_exp_y( ( (double)( (*pImageDataSrc) + nIncRed   ) * nContrastFactor ) / 255.0,
                                             nInvGamma ) * 255.0 ) );
        pImageDataSrc++;
        pIndex++;

        // Green.
        (*pIndex)= Limit255( (int)( x_exp_y( ( (double)( (*pImageDataSrc) + nIncGreen ) * nContrastFactor ) / 255.0,
                                               nInvGamma ) * 255.0 ) );
        pImageDataSrc++;
        pIndex++;

        // Blue.
        (*pIndex)= Limit255( (int)( x_exp_y( ( (double)( (*pImageDataSrc) + nIncBlue  ) * nContrastFactor ) / 255.0,
                                               nInvGamma ) * 255.0 ) );
        pImageDataSrc++;
        pIndex++;
    }

    oImage.SetData( pImageDataDest );
    return oImage;
}
wxString GetTextFromFile ( const wxString &  strFilename)

Load the content of a text file into memory.

Parameters:
strFilenamethe name of the text file

Definition at line 237 of file wxiscanglobals.cpp.

Referenced by wxIScanFrame::AddPdfPage(), wxIScanLuaScript::OCR(), wxIScanFrame::OnDocumentCopyText(), and wxIScanFrame::StartScript().

{
    // Open a textfile, ...
    wxFile oFile( strFilename );

    if( !oFile.IsOpened() )
    {
        // Return an empty string on failure.
        return wxEmptyString;
    }

    // ... create a temporary buffer of file size, ...
    wxFileOffset nLength= oFile.Length();
    char *pcBuffer= new char[nLength];

    if( !pcBuffer )
    {
        // Return an empty string on failure.
        return wxEmptyString;
    }

    // ... read the file's content into the buffer, ...
    oFile.Read( pcBuffer, nLength );

    // ... convert it to a wxString object (from UTF8), ...
    wxString strText= wxString::FromUTF8( pcBuffer, nLength );

    // ... delete the (temporary) buffer,  ...
    delete[] pcBuffer;

    // ... and return the text.
    return strText;
}
wxImage Image2Mono ( const wxImage &  oSrcImage,
int  nMonoThreshold = 128 
)

Convert an image to monochrome (black and white).

Parameters:
oSrcImagethe image to transformed
nMonoThresholdvalue for deciding if a point is black or white

NOTE: The returned object is the transformed image. The original image is left intact.

Definition at line 27 of file wxiscanglobals.cpp.

Referenced by wxIScanFrame::ApplyProfile(), and wxIScanFrame::OCR().

{
    wxImage oImage= oSrcImage.ConvertToGreyscale();

    int nCount= oImage.GetWidth() * oImage.GetHeight() * 3;
    unsigned char *pImageDataSrc= oImage.GetData();
    unsigned char *pImageDataDest= (unsigned char *)malloc( nCount * sizeof( unsigned char ) );

    if( !pImageDataDest )
    {
        wxLogError( wxString( wxT( "Image2Mono - " ) )
                      + _( "Cannot allocate memory for destination image." ) );
        return oImage;
    }

    // ...
    unsigned char *pIndex= pImageDataDest;

    for( int i= 0; i < nCount; i += 3 )
    {
        unsigned char nValue= ( (*pImageDataSrc) < nMonoThreshold ) ? 0 : 255;

        // Red.
        (*pIndex)= nValue;
        pImageDataSrc++;
        pIndex++;

        // Green.
        (*pIndex)= nValue;
        pImageDataSrc++;
        pIndex++;

        // Blue.
        (*pIndex)= nValue;
        pImageDataSrc++;
        pIndex++;
    }

    oImage.SetData( pImageDataDest );
    return oImage;
}
static unsigned char Limit255 ( int  x) [inline, static]

Limit the given integer value to the intervall [0..255] (8 bit).

Parameters:
xthe integer value to limit

NOTE: At the moment this function is internally called by EnhenceColours() only.

Definition at line 91 of file wxiscanglobals.cpp.

Referenced by EnhenceColours().

{
    return ( x > 255 ) ? 255 : ( ( x < 0 ) ? 0 : x );
}
static double x_exp_y ( double  x,
double  y 
) [inline, static]

Calculate the value of x^y.

Parameters:
xthe base (as a floating point value)
ythe exponent (as a floating point value)

NOTE: At the moment this function is internally called by EnhenceColours() only.

Definition at line 78 of file wxiscanglobals.cpp.

Referenced by EnhenceColours().

{
    return exp( y * log( x ) );
}