KiCad PCB EDA Suite
dialog_image_editor.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KICAD, a free EDA CAD application.
3  *
4  * Copyright (C) 2018 jean-pierre.charras
5  * Copyright (C) 2011-2019 Kicad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <fctsys.h>
26 #include <gr_basic.h>
27 #include <common.h>
28 #include <confirm.h>
29 #include <bitmap_base.h>
30 
31 #include <dialog_image_editor.h>
32 
33 #include <algorithm>
34 
35 
37  : DIALOG_IMAGE_EDITOR_BASE( aParent )
38 {
39  m_workingImage = new BITMAP_BASE( *aItem );
40  wxString msg;
41  msg.Printf( wxT( "%f" ), m_workingImage->GetScale() );
42  m_textCtrlScale->SetValue( msg );
43 
45  m_sdbSizerOK->SetDefault();
46 }
47 
48 
49 void DIALOG_IMAGE_EDITOR::OnGreyScaleConvert( wxCommandEvent& event )
50 {
51  wxImage& image = *m_workingImage->GetImageData();
52  image = image.ConvertToGreyscale();
54  m_panelDraw->Refresh();
55 }
56 
57 
58 /*
59  * Test params values correctness
60  * Currently scale value must give an actual image > MIN_SIZE pixels (mandatory to be able to
61  * see the image) and < MAX_SIZE pixels (if bigger, a confirmation will be asked)
62  * Note: The image definition is 300ppi in drawing routines.
63  */
65 {
66  #define MIN_SIZE 15 // Min size in pixels after scaling (50 mils)
67  #define MAX_SIZE 6000 // Max size in pixels after scaling (20 inches)
68  double tmp;
69  wxString msg = m_textCtrlScale->GetValue();
70 
71  // Test number correctness
72  if( !msg.ToDouble( &tmp ) || tmp < 0.0 )
73  {
74  wxMessageBox( _( "Incorrect scale number" ) );
75  return false;
76  }
77 
78  // Test value correctness
79  wxSize psize = m_workingImage->GetSizePixels();
80  int size_min = (int)std::min( (psize.x * tmp), (psize.y * tmp) );
81 
82  if( size_min < MIN_SIZE ) // if the size is too small, the image will be hard to locate
83  {
84  wxMessageBox( wxString::Format( _( "This scale results in an image which is too small "
85  "(%.2f mm or %.1f mil)." ),
86  25.4 / 300 * size_min, 1000.0/300.0 * size_min ) );
87  return false;
88  }
89 
90  int size_max = (int)std::max( (psize.x * tmp), (psize.y * tmp) );
91 
92  if( size_max > MAX_SIZE )
93  {
94  // the actual size is 25.4/300 * size_max in mm
95  if( !IsOK( this, wxString::Format( _( "This scale results in an image which is very large "
96  "(%.1f mm or %.2f in). Are you sure?" ),
97  25.4 / 300 * size_max, size_max /300.0 ) ) )
98  return false;
99  }
100 
101  return true;
102 }
103 
104 
106 {
107  return CheckValues();
108 }
109 
110 
111 void DIALOG_IMAGE_EDITOR::OnRedrawPanel( wxPaintEvent& event )
112 {
113  wxPaintDC dc( m_panelDraw );
114  wxSize size = m_panelDraw->GetClientSize();
115  dc.SetDeviceOrigin( size.x/2, size.y/2 );
116 
117  double scale = 1.0 / m_workingImage->GetScalingFactor();
118  dc.SetUserScale( scale, scale );
119  m_workingImage->DrawBitmap( &dc, wxPoint( 0, 0 ) );
120 }
121 
122 
124 {
125  wxString msg = m_textCtrlScale->GetValue();
126  double scale = 1.0;
127  msg.ToDouble( &scale );
129  aItem->ImportData( m_workingImage );
130 }
double GetScalingFactor() const
Function GetScalingFactor.
Definition: bitmap_base.h:125
Class DIALOG_IMAGE_EDITOR_BASE.
This file is part of the common library.
BITMAP_BASE * m_workingImage
void SetScale(double aScale)
Definition: bitmap_base.h:93
void RebuildBitmap()
Definition: bitmap_base.h:100
double GetScale() const
Definition: bitmap_base.h:92
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
wxImage * GetImageData()
Definition: bitmap_base.h:83
DIALOG_IMAGE_EDITOR(wxWindow *aParent, BITMAP_BASE *aItem)
#define MIN_SIZE
#define MAX_SIZE
void OnRedrawPanel(wxPaintEvent &event) override
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:51
void OnGreyScaleConvert(wxCommandEvent &event) override
void ImportData(BITMAP_BASE *aItem)
Function ImportData Copy aItem image to me and update m_bitmap.
Definition: bitmap_base.cpp:77
const int scale
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
#define _(s)
Definition: 3d_actions.cpp:33
wxSize GetSizePixels() const
Function GetSizePixels.
Definition: bitmap_base.h:141
void TransferToImage(BITMAP_BASE *aItem)
Function TransferToImage copy edited image to aItem.
The common library.
void DrawBitmap(wxDC *aDC, const wxPoint &aPos)
bool TransferDataFromWindow() override
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:283