File: dialog_image_editor.cpp

package info (click to toggle)
kicad 5.1.9%2Bdfsg1-1%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 240,044 kB
  • sloc: cpp: 464,812; ansic: 58,106; python: 4,972; sh: 488; awk: 294; makefile: 269; xml: 53; perl: 10
file content (193 lines) | stat: -rw-r--r-- 5,818 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
 * @file dialog_image_editor.cpp
 */

/*
 * This program source code file is part of KICAD, a free EDA CAD application.
 *
 * Copyright (C) 2018 jean-pierre.charras
 * Copyright (C) 2011-2018 Kicad Developers, see AUTHORS.txt for contributors.
 *
 * 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
 */

#include <fctsys.h>
#include <gr_basic.h>
#include <common.h>
#include <confirm.h>
#include <bitmap_base.h>

#include <dialog_image_editor.h>


DIALOG_IMAGE_EDITOR::DIALOG_IMAGE_EDITOR( wxWindow* aParent, BITMAP_BASE* aItem )
    : DIALOG_IMAGE_EDITOR_BASE( aParent )
{
    m_workingImage = new BITMAP_BASE( * aItem );
    m_lastImage = NULL;
    m_buttonUndoLast->Enable( false );
    wxString msg;
    msg.Printf( wxT( "%f" ), m_workingImage->GetScale() );
    m_textCtrlScale->SetValue( msg );

    FinishDialogSettings();
    m_sdbSizerOK->SetDefault();
}


void DIALOG_IMAGE_EDITOR::OnUndoLastChange( wxCommandEvent& event )
{
    BITMAP_BASE * tmp = m_workingImage;
    m_workingImage = m_lastImage;
    delete tmp;
    m_buttonUndoLast->Enable( false );
    m_lastImage = NULL;
    m_panelDraw->Refresh();
}


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 );
    m_workingImage->Mirror( true );
    m_panelDraw->Refresh();
}


void DIALOG_IMAGE_EDITOR::OnMirrorY_click( wxCommandEvent& event )
{
    delete m_lastImage;
    m_lastImage = new BITMAP_BASE( *  m_workingImage );
    m_buttonUndoLast->Enable( true );
    m_workingImage->Mirror( false );
    m_panelDraw->Refresh();
}


void DIALOG_IMAGE_EDITOR::OnRotateClick( wxCommandEvent& event )
{
    delete m_lastImage;
    m_lastImage = new BITMAP_BASE( *  m_workingImage );
    m_buttonUndoLast->Enable( true );
    m_workingImage->Rotate( false );
    m_panelDraw->Refresh();
}


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();
}


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();

    image = image.Scale( psize.x/2, psize.y/2, wxIMAGE_QUALITY_HIGH );
    m_workingImage->RebuildBitmap();
    m_panelDraw->Refresh();
}


/* Test params values correctness
 * Currently scale value must give an actual image
 * > 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.
 */
bool DIALOG_IMAGE_EDITOR::CheckValues()
{
    #define MIN_SIZE 32     // Min size in pixels after scaling
    #define MAX_SIZE 6000   // Max size in pixels after scaling
    double tmp;
    wxString msg = m_textCtrlScale->GetValue();

    // Test number correctness
    if( !msg.ToDouble( &tmp ) || tmp < 0.0 )
    {
        wxMessageBox( _("Incorrect scale number" ) );
        return false;
    }

    // Test value correctness
    wxSize psize = m_workingImage->GetSizePixels();
    int size_min = (int)std::min( (psize.x * tmp), (psize.y * tmp) );

    if( size_min < MIN_SIZE )   // if the size is too small, the image will be hard to locate
    {
        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 ) );
        return false;
    }

    int size_max = (int)std::max( (psize.x * tmp), (psize.y * tmp) );

    if( size_max > MAX_SIZE )
    {
        // 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;
    }

    return true;
}


bool DIALOG_IMAGE_EDITOR::TransferDataFromWindow()
{
    return CheckValues();
}


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 );
    m_workingImage->DrawBitmap( &dc, wxPoint( 0, 0 ) );
}


void DIALOG_IMAGE_EDITOR::TransfertToImage( BITMAP_BASE* aItem )
{
    wxString msg = m_textCtrlScale->GetValue();
    double scale = 1.0;
    msg.ToDouble( &scale );
    m_workingImage->SetScale( scale );
    aItem->ImportData( m_workingImage );
}