2011-08-31 14:59:20 +00:00
|
|
|
/**
|
|
|
|
* @file dialog_image_editor.cpp
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
2018-07-27 11:41:54 +00:00
|
|
|
* Copyright (C) 2018 jean-pierre.charras
|
2018-01-29 08:39:13 +00:00
|
|
|
* Copyright (C) 2011-2018 Kicad Developers, see AUTHORS.txt for contributors.
|
2011-08-31 14:59:20 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <fctsys.h>
|
|
|
|
#include <gr_basic.h>
|
|
|
|
#include <common.h>
|
2018-08-04 17:10:20 +00:00
|
|
|
#include <confirm.h>
|
2018-01-29 08:39:13 +00:00
|
|
|
#include <bitmap_base.h>
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <dialog_image_editor.h>
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2016-07-11 19:48:46 +00:00
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
DIALOG_IMAGE_EDITOR::DIALOG_IMAGE_EDITOR( wxWindow* aParent, BITMAP_BASE* aItem )
|
|
|
|
: DIALOG_IMAGE_EDITOR_BASE( aParent )
|
|
|
|
{
|
|
|
|
m_workingImage = new BITMAP_BASE( * aItem );
|
2011-09-02 19:43:56 +00:00
|
|
|
m_lastImage = NULL;
|
|
|
|
m_buttonUndoLast->Enable( false );
|
2011-08-31 14:59:20 +00:00
|
|
|
wxString msg;
|
2016-07-11 19:48:46 +00:00
|
|
|
msg.Printf( wxT( "%f" ), m_workingImage->GetScale() );
|
2015-01-17 08:01:16 +00:00
|
|
|
m_textCtrlScale->SetValue( msg );
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2018-07-27 11:41:54 +00:00
|
|
|
FinishDialogSettings();
|
|
|
|
m_sdbSizerOK->SetDefault();
|
2011-08-31 14:59:20 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 19:48:46 +00:00
|
|
|
|
2011-09-02 19:43:56 +00:00
|
|
|
void DIALOG_IMAGE_EDITOR::OnUndoLastChange( wxCommandEvent& event )
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
2011-09-02 19:43:56 +00:00
|
|
|
BITMAP_BASE * tmp = m_workingImage;
|
|
|
|
m_workingImage = m_lastImage;
|
|
|
|
delete tmp;
|
|
|
|
m_buttonUndoLast->Enable( false );
|
|
|
|
m_lastImage = NULL;
|
|
|
|
m_panelDraw->Refresh();
|
|
|
|
}
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2016-07-11 19:48:46 +00:00
|
|
|
|
2011-09-02 19:43:56 +00:00
|
|
|
void DIALOG_IMAGE_EDITOR::OnMirrorX_click( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
delete m_lastImage;
|
|
|
|
m_lastImage = new BITMAP_BASE( * m_workingImage );
|
|
|
|
m_buttonUndoLast->Enable( true );
|
|
|
|
m_buttonUndoLast->Enable( true );
|
2011-08-31 14:59:20 +00:00
|
|
|
m_workingImage->Mirror( true );
|
|
|
|
m_panelDraw->Refresh();
|
|
|
|
}
|
|
|
|
|
2016-07-11 19:48:46 +00:00
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
void DIALOG_IMAGE_EDITOR::OnMirrorY_click( wxCommandEvent& event )
|
|
|
|
{
|
2011-09-02 19:43:56 +00:00
|
|
|
delete m_lastImage;
|
|
|
|
m_lastImage = new BITMAP_BASE( * m_workingImage );
|
|
|
|
m_buttonUndoLast->Enable( true );
|
2011-08-31 14:59:20 +00:00
|
|
|
m_workingImage->Mirror( false );
|
|
|
|
m_panelDraw->Refresh();
|
|
|
|
}
|
|
|
|
|
2016-07-11 19:48:46 +00:00
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
void DIALOG_IMAGE_EDITOR::OnRotateClick( wxCommandEvent& event )
|
|
|
|
{
|
2011-09-02 19:43:56 +00:00
|
|
|
delete m_lastImage;
|
|
|
|
m_lastImage = new BITMAP_BASE( * m_workingImage );
|
|
|
|
m_buttonUndoLast->Enable( true );
|
2011-08-31 14:59:20 +00:00
|
|
|
m_workingImage->Rotate( false );
|
|
|
|
m_panelDraw->Refresh();
|
|
|
|
}
|
|
|
|
|
2016-07-11 19:48:46 +00:00
|
|
|
|
2011-09-02 19:43:56 +00:00
|
|
|
void DIALOG_IMAGE_EDITOR::OnGreyScaleConvert( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
delete m_lastImage;
|
|
|
|
m_lastImage = new BITMAP_BASE( * m_workingImage );
|
|
|
|
m_buttonUndoLast->Enable( true );
|
|
|
|
wxImage& image = *m_workingImage->GetImageData();
|
|
|
|
image = image.ConvertToGreyscale();
|
|
|
|
m_workingImage->RebuildBitmap();
|
|
|
|
m_panelDraw->Refresh();
|
|
|
|
}
|
|
|
|
|
2016-07-11 19:48:46 +00:00
|
|
|
|
2011-09-02 19:43:56 +00:00
|
|
|
void DIALOG_IMAGE_EDITOR::OnHalfSize( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
delete m_lastImage;
|
|
|
|
m_lastImage = new BITMAP_BASE( * m_workingImage );
|
|
|
|
m_buttonUndoLast->Enable( true );
|
|
|
|
wxSize psize = m_workingImage->GetSizePixels();
|
|
|
|
wxImage& image = *m_workingImage->GetImageData();
|
|
|
|
|
2016-07-11 19:48:46 +00:00
|
|
|
image = image.Scale( psize.x/2, psize.y/2, wxIMAGE_QUALITY_HIGH );
|
2011-09-02 19:43:56 +00:00
|
|
|
m_workingImage->RebuildBitmap();
|
|
|
|
m_panelDraw->Refresh();
|
|
|
|
}
|
|
|
|
|
2016-07-11 19:48:46 +00:00
|
|
|
|
2011-09-02 19:43:56 +00:00
|
|
|
/* Test params values correctness
|
|
|
|
* Currently scale value must give an actual image
|
2018-08-04 17:10:20 +00:00
|
|
|
* > MIN_SIZE pixels (mandatory to be able to see the image)
|
|
|
|
* and < MAX_SIZE pixels (if bigger, a confirmation will be asked)
|
|
|
|
* Note: The image definition is 300ppi in drawing routines.
|
2011-09-02 19:43:56 +00:00
|
|
|
*/
|
|
|
|
bool DIALOG_IMAGE_EDITOR::CheckValues()
|
|
|
|
{
|
2018-08-04 17:10:20 +00:00
|
|
|
#define MIN_SIZE 32 // Min size in pixels after scaling
|
|
|
|
#define MAX_SIZE 6000 // Max size in pixels after scaling
|
2011-09-02 19:43:56 +00:00
|
|
|
double tmp;
|
|
|
|
wxString msg = m_textCtrlScale->GetValue();
|
2016-07-11 19:48:46 +00:00
|
|
|
|
2011-09-02 19:43:56 +00:00
|
|
|
// Test number correctness
|
2018-08-04 17:10:20 +00:00
|
|
|
if( !msg.ToDouble( &tmp ) || tmp < 0.0 )
|
2011-09-02 19:43:56 +00:00
|
|
|
{
|
|
|
|
wxMessageBox( _("Incorrect scale number" ) );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-04 17:10:20 +00:00
|
|
|
// Test value correctness
|
2011-09-02 19:43:56 +00:00
|
|
|
wxSize psize = m_workingImage->GetSizePixels();
|
2018-08-04 17:10:20 +00:00
|
|
|
int size_min = (int)std::min( (psize.x * tmp), (psize.y * tmp) );
|
2016-07-11 19:48:46 +00:00
|
|
|
|
2018-08-04 17:10:20 +00:00
|
|
|
if( size_min < MIN_SIZE ) // if the size is too small, the image will be hard to locate
|
2011-09-02 19:43:56 +00:00
|
|
|
{
|
2018-08-04 17:10:20 +00:00
|
|
|
wxMessageBox( wxString::Format(
|
|
|
|
_("This scale gives a too small image size (%.2f mm or %.1f mil)" ),
|
|
|
|
25.4 / 300 * size_min, 1000.0/300.0 * size_min ) );
|
2011-09-02 19:43:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-07-11 19:48:46 +00:00
|
|
|
|
2018-08-04 17:10:20 +00:00
|
|
|
int size_max = (int)std::max( (psize.x * tmp), (psize.y * tmp) );
|
|
|
|
|
|
|
|
if( size_max > MAX_SIZE )
|
2011-09-02 19:43:56 +00:00
|
|
|
{
|
2018-08-04 17:10:20 +00:00
|
|
|
// the actual size is 25.4/300 * size_max in mm
|
|
|
|
if( !IsOK( this, wxString::Format(
|
|
|
|
_("This scale gives a very large image size (%.1f mm or %.2f in). Are you sure?" ),
|
|
|
|
25.4 / 300 * size_max, size_max /300.0 ) ) )
|
|
|
|
return false;
|
2011-09-02 19:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-11 19:48:46 +00:00
|
|
|
|
2018-07-27 11:41:54 +00:00
|
|
|
bool DIALOG_IMAGE_EDITOR::TransferDataFromWindow()
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
2018-07-27 11:41:54 +00:00
|
|
|
return CheckValues();
|
2011-08-31 14:59:20 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 19:48:46 +00:00
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
void DIALOG_IMAGE_EDITOR::OnRedrawPanel( wxPaintEvent& event )
|
|
|
|
{
|
|
|
|
wxPaintDC dc( m_panelDraw );
|
|
|
|
wxSize size = m_panelDraw->GetClientSize();
|
|
|
|
dc.SetDeviceOrigin( size.x/2, size.y/2 );
|
|
|
|
|
|
|
|
double scale = 1.0 / m_workingImage->GetScalingFactor();
|
|
|
|
dc.SetUserScale( scale, scale );
|
2016-07-11 19:48:46 +00:00
|
|
|
m_workingImage->DrawBitmap( NULL, &dc, wxPoint( 0, 0 ) );
|
2011-08-31 14:59:20 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 19:48:46 +00:00
|
|
|
|
|
|
|
void DIALOG_IMAGE_EDITOR::TransfertToImage( BITMAP_BASE* aItem )
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
|
|
|
wxString msg = m_textCtrlScale->GetValue();
|
2016-07-11 19:48:46 +00:00
|
|
|
double scale = 1.0;
|
|
|
|
msg.ToDouble( &scale );
|
|
|
|
m_workingImage->SetScale( scale );
|
2011-08-31 14:59:20 +00:00
|
|
|
aItem->ImportData( m_workingImage );
|
|
|
|
}
|