bitmap2component: add management of image resolution: Is read from file, when exists, and can be modified in bitmap2component.
It also fix issue #747631
This commit is contained in:
@@ -27,10 +27,6 @@
|
||||
#include <confirm.h>
|
||||
#include <gestfich.h>
|
||||
|
||||
#include <wx/wx.h>
|
||||
#include <wx/config.h>
|
||||
#include <wx/filename.h>
|
||||
|
||||
#include <bitmap2cmp_gui_base.h>
|
||||
|
||||
#include <potracelib.h>
|
||||
@@ -50,7 +46,10 @@
|
||||
#define KEYWORD_BINARY_THRESHOLD wxT( "Threshold" )
|
||||
#define KEYWORD_BW_NEGATIVE wxT( "Negative_choice" )
|
||||
|
||||
extern int bitmap2component( potrace_bitmap_t* aPotrace_bitmap, FILE* aOutfile, int aFormat );
|
||||
#define DEFAULT_DPI 300 // Default resolution in Bit per inches
|
||||
|
||||
extern int bitmap2component( potrace_bitmap_t* aPotrace_bitmap, FILE* aOutfile,
|
||||
int aFormat, int aDpi_X, int aDpi_Y );
|
||||
|
||||
/* Class BM2CMP_FRAME_BASE
|
||||
This is the main frame for this application
|
||||
@@ -64,11 +63,13 @@ private:
|
||||
wxBitmap m_Greyscale_Bitmap;
|
||||
wxImage m_NB_Image;
|
||||
wxBitmap m_BN_Bitmap;
|
||||
wxSize m_imageDPI; // The initial image resolution. When unknown,
|
||||
// set to DEFAULT_DPI x DEFAULT_DPI per Inch
|
||||
wxString m_BitmapFileName;
|
||||
wxString m_ConvertedFileName;
|
||||
wxSize m_FrameSize;
|
||||
wxPoint m_FramePos;
|
||||
wxConfig * m_Config;
|
||||
wxSize m_frameSize;
|
||||
wxPoint m_framePos;
|
||||
wxConfig* m_config;
|
||||
|
||||
public:
|
||||
BM2CMP_FRAME();
|
||||
@@ -109,27 +110,43 @@ private:
|
||||
void Binarize( double aThreshold ); // aThreshold = 0.0 (black level) to 1.0 (white level)
|
||||
void OnOptionsSelection( wxCommandEvent& event );
|
||||
void OnThresholdChange( wxScrollEvent& event );
|
||||
void OnResolutionChange( wxCommandEvent& event );
|
||||
|
||||
// called when texts controls which handle the image resolution
|
||||
// lose the focus, to ensure the rigyht vaules are displayed
|
||||
// because the m_imageDPI are clipped to acceptable values, and
|
||||
// the text displayed could be differ duringa text edition
|
||||
// We are using ChangeValue here to avoid generating a wxEVT_TEXT event.
|
||||
void UpdateDPITextValueX( wxMouseEvent& event )
|
||||
{
|
||||
m_DPIValueX->ChangeValue( wxString::Format( wxT( "%d" ), m_imageDPI.x ) );
|
||||
}
|
||||
void UpdateDPITextValueY( wxMouseEvent& event )
|
||||
{
|
||||
m_DPIValueY->ChangeValue( wxString::Format( wxT( "%d" ), m_imageDPI.y ) );
|
||||
}
|
||||
|
||||
void NegateGreyscaleImage( );
|
||||
void ExportFile( FILE* aOutfile, int aFormat );
|
||||
void updateImageInfo();
|
||||
};
|
||||
|
||||
|
||||
BM2CMP_FRAME::BM2CMP_FRAME() : BM2CMP_FRAME_BASE( NULL )
|
||||
{
|
||||
int tmp;
|
||||
m_Config = new wxConfig();
|
||||
m_Config->Read( KEYWORD_FRAME_POSX, & m_FramePos.x, -1 );
|
||||
m_Config->Read( KEYWORD_FRAME_POSY, & m_FramePos.y, -1 );
|
||||
m_Config->Read( KEYWORD_FRAME_SIZEX, & m_FrameSize.x, -1 );
|
||||
m_Config->Read( KEYWORD_FRAME_SIZEY, & m_FrameSize.y, -1 );
|
||||
m_Config->Read( KEYWORD_LAST_INPUT_FILE, &m_BitmapFileName );
|
||||
m_Config->Read( KEYWORD_LAST_OUTPUT_FILE, &m_ConvertedFileName );
|
||||
if( m_Config->Read( KEYWORD_BINARY_THRESHOLD, &tmp ) )
|
||||
m_config = new wxConfig();
|
||||
m_config->Read( KEYWORD_FRAME_POSX, & m_framePos.x, -1 );
|
||||
m_config->Read( KEYWORD_FRAME_POSY, & m_framePos.y, -1 );
|
||||
m_config->Read( KEYWORD_FRAME_SIZEX, & m_frameSize.x, -1 );
|
||||
m_config->Read( KEYWORD_FRAME_SIZEY, & m_frameSize.y, -1 );
|
||||
m_config->Read( KEYWORD_LAST_INPUT_FILE, &m_BitmapFileName );
|
||||
m_config->Read( KEYWORD_LAST_OUTPUT_FILE, &m_ConvertedFileName );
|
||||
if( m_config->Read( KEYWORD_BINARY_THRESHOLD, &tmp ) )
|
||||
m_sliderThreshold->SetValue( tmp );
|
||||
if( m_Config->Read( KEYWORD_BW_NEGATIVE, &tmp ) )
|
||||
if( m_config->Read( KEYWORD_BW_NEGATIVE, &tmp ) )
|
||||
m_rbOptions->SetSelection( tmp ? 1 : 0 );
|
||||
|
||||
m_Config->Read( KEYWORD_LAST_FORMAT, &tmp );
|
||||
m_config->Read( KEYWORD_LAST_FORMAT, &tmp );
|
||||
m_radioBoxFormat->SetSelection( tmp );
|
||||
|
||||
|
||||
@@ -140,34 +157,36 @@ BM2CMP_FRAME::BM2CMP_FRAME() : BM2CMP_FRAME_BASE( NULL )
|
||||
|
||||
GetSizer()->SetSizeHints( this );
|
||||
|
||||
SetSize( m_FramePos.x, m_FramePos.y, m_FrameSize.x, m_FrameSize.y );
|
||||
SetSize( m_framePos.x, m_framePos.y, m_frameSize.x, m_frameSize.y );
|
||||
|
||||
m_buttonExport->Enable( false );
|
||||
|
||||
if ( m_FramePos == wxDefaultPosition )
|
||||
m_imageDPI.x = m_imageDPI.y = DEFAULT_DPI; // Default resolution in Bit per inches
|
||||
|
||||
if ( m_framePos == wxDefaultPosition )
|
||||
Centre();
|
||||
}
|
||||
|
||||
|
||||
BM2CMP_FRAME::~BM2CMP_FRAME()
|
||||
{
|
||||
if( ( m_Config == NULL ) || IsIconized() )
|
||||
if( ( m_config == NULL ) || IsIconized() )
|
||||
return;
|
||||
|
||||
m_FrameSize = GetSize();
|
||||
m_FramePos = GetPosition();
|
||||
m_frameSize = GetSize();
|
||||
m_framePos = GetPosition();
|
||||
|
||||
m_Config->Write( KEYWORD_FRAME_POSX, (long) m_FramePos.x );
|
||||
m_Config->Write( KEYWORD_FRAME_POSY, (long) m_FramePos.y );
|
||||
m_Config->Write( KEYWORD_FRAME_SIZEX, (long) m_FrameSize.x );
|
||||
m_Config->Write( KEYWORD_FRAME_SIZEY, (long) m_FrameSize.y );
|
||||
m_Config->Write( KEYWORD_LAST_INPUT_FILE, m_BitmapFileName );
|
||||
m_Config->Write( KEYWORD_LAST_OUTPUT_FILE, m_ConvertedFileName );
|
||||
m_Config->Write( KEYWORD_BINARY_THRESHOLD, m_sliderThreshold->GetValue() );
|
||||
m_Config->Write( KEYWORD_BW_NEGATIVE, m_rbOptions->GetSelection() );
|
||||
m_Config->Write( KEYWORD_LAST_FORMAT, m_radioBoxFormat->GetSelection() );
|
||||
m_config->Write( KEYWORD_FRAME_POSX, (long) m_framePos.x );
|
||||
m_config->Write( KEYWORD_FRAME_POSY, (long) m_framePos.y );
|
||||
m_config->Write( KEYWORD_FRAME_SIZEX, (long) m_frameSize.x );
|
||||
m_config->Write( KEYWORD_FRAME_SIZEY, (long) m_frameSize.y );
|
||||
m_config->Write( KEYWORD_LAST_INPUT_FILE, m_BitmapFileName );
|
||||
m_config->Write( KEYWORD_LAST_OUTPUT_FILE, m_ConvertedFileName );
|
||||
m_config->Write( KEYWORD_BINARY_THRESHOLD, m_sliderThreshold->GetValue() );
|
||||
m_config->Write( KEYWORD_BW_NEGATIVE, m_rbOptions->GetSelection() );
|
||||
m_config->Write( KEYWORD_LAST_FORMAT, m_radioBoxFormat->GetSelection() );
|
||||
|
||||
delete m_Config;
|
||||
delete m_config;
|
||||
|
||||
/* This needed for OSX: avoids further OnDraw processing after this
|
||||
* destructor and before the native window is destroyed
|
||||
@@ -246,15 +265,31 @@ bool BM2CMP_FRAME::LoadFile( wxString& aFullFileName )
|
||||
|
||||
int h = m_Pict_Bitmap.GetHeight();
|
||||
int w = m_Pict_Bitmap.GetWidth();
|
||||
int nb = m_Pict_Bitmap.GetDepth();
|
||||
// Determine image resolution in DPI (does not existing in all formats).
|
||||
// the resolution can be given in bit per inches or bit per cm in file
|
||||
m_imageDPI.x = m_Pict_Image.GetOptionInt( wxIMAGE_OPTION_RESOLUTIONX );
|
||||
m_imageDPI.y = m_Pict_Image.GetOptionInt( wxIMAGE_OPTION_RESOLUTIONY );
|
||||
|
||||
wxString msg;
|
||||
msg.Printf( wxT( "%d" ), w );
|
||||
m_SizeXValue->SetLabel(msg);
|
||||
msg.Printf( wxT( "%d" ), h );
|
||||
m_SizeYValue->SetLabel(msg);
|
||||
msg.Printf( wxT( "%d" ), nb );
|
||||
m_BPPValue->SetLabel(msg);
|
||||
if( m_imageDPI.x > 1 && m_imageDPI.y > 1 )
|
||||
{
|
||||
if( m_Pict_Image.GetOptionInt( wxIMAGE_OPTION_RESOLUTIONUNIT ) == wxIMAGE_RESOLUTION_CM )
|
||||
{
|
||||
// When the initial resolution is given in bits per cm,
|
||||
// experience shows adding 1.27 to the resolution converted in dpi
|
||||
// before convert to int value reduce the conversion error
|
||||
// but it is not perfect
|
||||
m_imageDPI.x = m_imageDPI.x * 2.54 + 1.27;
|
||||
m_imageDPI.y = m_imageDPI.y * 2.54 + 1.27;
|
||||
}
|
||||
}
|
||||
else // fallback to the default value
|
||||
m_imageDPI.x = m_imageDPI.y = DEFAULT_DPI;
|
||||
|
||||
// Display image info:
|
||||
// We are using ChangeValue here to avoid generating a wxEVT_TEXT event.
|
||||
m_DPIValueX->ChangeValue( wxString::Format( wxT( "%d" ), m_imageDPI.x ) );
|
||||
m_DPIValueY->ChangeValue( wxString::Format( wxT( "%d" ), m_imageDPI.y ) );
|
||||
updateImageInfo();
|
||||
|
||||
m_InitialPicturePanel->SetVirtualSize( w, h );
|
||||
m_GreyscalePicturePanel->SetVirtualSize( w, h );
|
||||
@@ -272,6 +307,43 @@ bool BM2CMP_FRAME::LoadFile( wxString& aFullFileName )
|
||||
return true;
|
||||
}
|
||||
|
||||
void BM2CMP_FRAME::updateImageInfo()
|
||||
{
|
||||
// Note: the image resolution text controls are not modified
|
||||
// here, to avoid a race between text change when entered by user and
|
||||
// a text change if it is modifed here.
|
||||
int h = m_Pict_Bitmap.GetHeight();
|
||||
int w = m_Pict_Bitmap.GetWidth();
|
||||
int nb = m_Pict_Bitmap.GetDepth();
|
||||
|
||||
m_SizeXValue->SetLabel( wxString::Format( wxT( "%d" ), w ) );
|
||||
m_SizeYValue->SetLabel( wxString::Format( wxT( "%d" ), h ) );
|
||||
m_BPPValue->SetLabel( wxString::Format( wxT( "%d" ), nb ) );
|
||||
|
||||
m_SizeXValue_mm->SetLabel( wxString::Format( wxT( "%.1f" ),
|
||||
(double) w / m_imageDPI.x * 25.4 ) );
|
||||
m_SizeYValue_mm->SetLabel( wxString::Format( wxT( "%.1f" ),
|
||||
(double) h / m_imageDPI.y * 25.4 ) );
|
||||
}
|
||||
|
||||
void BM2CMP_FRAME::OnResolutionChange( wxCommandEvent& event )
|
||||
{
|
||||
long tmp;
|
||||
|
||||
if( m_DPIValueX->GetValue().ToLong( &tmp ) )
|
||||
m_imageDPI.x = tmp;
|
||||
|
||||
if( m_DPIValueY->GetValue().ToLong( &tmp ) )
|
||||
m_imageDPI.y = tmp;
|
||||
|
||||
if( m_imageDPI.x < 32 )
|
||||
m_imageDPI.x = 32;
|
||||
|
||||
if( m_imageDPI.y < 32 )
|
||||
m_imageDPI.y = 32;
|
||||
|
||||
updateImageInfo();
|
||||
}
|
||||
|
||||
void BM2CMP_FRAME::Binarize( double aThreshold )
|
||||
{
|
||||
@@ -514,14 +586,14 @@ void BM2CMP_FRAME::ExportFile( FILE* aOutfile, int aFormat )
|
||||
/* fill the bitmap with data */
|
||||
for( int y = 0; y < h; y++ )
|
||||
{
|
||||
for( int x = 0; x<w; x++ )
|
||||
for( int x = 0; x < w; x++ )
|
||||
{
|
||||
unsigned char pix = m_NB_Image.GetGreen( x, y );
|
||||
BM_PUT( potrace_bitmap, x, y, pix ? 1 : 0 );
|
||||
}
|
||||
}
|
||||
|
||||
bitmap2component( potrace_bitmap, aOutfile, aFormat );
|
||||
bitmap2component( potrace_bitmap, aOutfile, aFormat, m_imageDPI.x, m_imageDPI.y );
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user