KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <wx/dcclient.h>
22#include <wx/msgdlg.h>
23#include <bitmap_base.h>
24#include <pcb_base_edit_frame.h>
25#include <tool/actions.h>
26#include <confirm.h>
27#include <units_provider.h>
30
31#include <algorithm>
32
33
34PANEL_IMAGE_EDITOR::PANEL_IMAGE_EDITOR( UNITS_PROVIDER* aUnitsProvider, wxWindow* aParent, const BITMAP_BASE& aItem ) :
35 PANEL_IMAGE_EDITOR_BASE( aParent ),
36 m_scale( aUnitsProvider, aParent, m_staticTextScale, m_textCtrlScale, nullptr ),
37 m_workingImage( std::make_unique<BITMAP_BASE>( aItem ) )
38{
39 m_scale.SetUnits( EDA_UNITS::UNSCALED );
40}
41
42
44{
45 m_scale.SetDoubleValue( m_workingImage->GetScale() );
46
47 m_stPPI_Value->SetLabel( wxString::Format( wxT( "%d" ), m_workingImage->GetPPI() ) );
48
49 return true;
50}
51
52
53void PANEL_IMAGE_EDITOR::OnGreyScaleConvert( wxCommandEvent& event )
54{
55 m_workingImage->ConvertToGreyscale();
56 m_panelDraw->Refresh();
57}
58
59
60void PANEL_IMAGE_EDITOR::OnInvert( wxCommandEvent& event )
61{
62 m_workingImage->InvertColors();
63 m_panelDraw->Refresh();
64}
65
66
67void PANEL_IMAGE_EDITOR::OnRemoveBackground( wxCommandEvent& event )
68{
69 // Nearly all images are white background (probably crops from documents)
70 // So that seems a good default on startup.
71 static KIGFX::COLOR4D s_defaultColor = KIGFX::COLOR4D::WHITE;
72
73 DIALOG_COLOR_PICKER dialog( this, s_defaultColor, false );
74
75 if( dialog.ShowModal() == wxID_OK )
76 {
77 m_workingImage->ConvertColourToAlpha( dialog.GetColor().ToColour() );
78 m_panelDraw->Refresh();
79
80 s_defaultColor = dialog.GetColor();
81 }
82}
83
84
85/*
86 * Test params values correctness
87 * Currently scale value must give an actual image > MIN_SIZE pixels (mandatory to be able to
88 * see the image) and < MAX_SIZE pixels (if bigger, a confirmation will be asked)
89 * Note: The image definition is 300ppi in drawing routines.
90 */
92{
93 wxWindow* host = wxGetTopLevelParent( this );
94
95#define MIN_SIZE 15 // Min size in pixels after scaling (50 mils)
96#define MAX_SIZE 6000 // Max size in pixels after scaling (20 inches)
97 double tmp = m_scale.GetDoubleValue();
98
99 // Test number correctness
100 if( tmp < 0.0 )
101 {
102 DisplayErrorMessage( host, _( "Scale must be a positive number." ) );
103 return false;
104 }
105
106 // Test value correctness
107 VECTOR2I psize = m_workingImage->GetSizePixels();
108 int size_min = (int) std::min( ( psize.x * tmp ), ( psize.y * tmp ) );
109
110 if( size_min < MIN_SIZE ) // if the size is too small, the image will be hard to locate
111 {
112 DisplayErrorMessage( host, wxString::Format( _( "This scale results in an image which is too small "
113 "(%.2f mm or %.1f mil)." ),
114 25.4 / 300 * size_min,
115 1000.0 / 300.0 * size_min ) );
116 return false;
117 }
118
119 int size_max = (int) std::max( ( psize.x * tmp ), ( psize.y * tmp ) );
120
121 if( size_max > MAX_SIZE )
122 {
123 // the actual size is 25.4/300 * size_max in mm
124 if( !IsOK( host, wxString::Format( _( "This scale results in an image which is very large "
125 "(%.1f mm or %.2f in). Are you sure?" ),
126 25.4 / 300 * size_max,
127 size_max / 300.0 ) ) )
128 {
129 return false;
130 }
131 }
132
133 return true;
134}
135
136
141
142
144{
145 return m_scale.GetDoubleValue();
146}
147
148
149void PANEL_IMAGE_EDITOR::SetScale( double aScale )
150{
151 m_scale.ChangeDoubleValue( aScale );
152 UpdateImageScale( aScale );
153}
154
155
157{
158 m_workingImage->SetScale( aScale );
159 m_panelDraw->Refresh();
160}
161
162
164{
165 return m_workingImage->GetSize();
166}
167
168
169void PANEL_IMAGE_EDITOR::OnRedrawPanel( wxPaintEvent& event )
170{
171 wxPaintDC dc( m_panelDraw );
172 wxSize display_size = m_panelDraw->GetClientSize();
173
174 double img_scale = 1.0 / m_workingImage->GetScalingFactor();
175 VECTOR2I img_size_pixels = m_workingImage->GetSizePixels();
176
177 // Adjust the display scale to use the full available display area
178 double scale_X = (double)display_size.x/img_size_pixels.x;
179 double scale_Y = (double)display_size.y/img_size_pixels.y;
180
181 double display_scale = img_scale * std::min( scale_X, scale_Y );
182
183 dc.SetUserScale( display_scale, display_scale );
184 m_workingImage->DrawBitmap( &dc, VECTOR2I( m_workingImage->GetSize()/2 ) );
185}
186
187
189{
190 wxString msg = m_textCtrlScale->GetValue();
191 double scale = 1.0;
192 msg.ToDouble( &scale );
193 m_workingImage->SetScale( scale );
194 aItem.ImportData( *m_workingImage );
195}
This class handle bitmap images in KiCad.
Definition bitmap_base.h:45
void ImportData(BITMAP_BASE &aItem)
Copy aItem image to this object and update m_bitmap.
KIGFX::COLOR4D GetColor()
int ShowModal() override
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
static const COLOR4D WHITE
Definition color4d.h:402
wxColour ToColour() const
Definition color4d.cpp:221
PANEL_IMAGE_EDITOR_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
bool TransferDataFromWindow() override
void SetScale(double aScale)
void UpdateImageScale(double aScale)
PANEL_IMAGE_EDITOR(UNITS_PROVIDER *aUnitsProvider, wxWindow *aParent, const BITMAP_BASE &aItem)
std::unique_ptr< BITMAP_BASE > m_workingImage
void OnInvert(wxCommandEvent &event) override
bool TransferDataToWindow() override
void OnRemoveBackground(wxCommandEvent &event) override
void TransferToImage(BITMAP_BASE &aItem)
Copy edited image to aItem.
VECTOR2I GetImageSize() const
void OnRedrawPanel(wxPaintEvent &event) override
void OnGreyScaleConvert(wxCommandEvent &event) override
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition confirm.cpp:274
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
This file is part of the common library.
#define _(s)
STL namespace.
#define MAX_SIZE
#define MIN_SIZE
const int scale
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683