KiCad PCB EDA Suite
bitmap_base.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) 2017 jean-pierre.charras
5 * Copyright (C) 2011-2023 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 <bitmap_base.h>
26#include <gr_basic.h>
27#include <math/util.h> // for KiROUND
28#include <memory> // for make_unique, unique_ptr
29#include <plotters/plotter.h>
30#include <richio.h>
31#include <wx/bitmap.h> // for wxBitmap
32#include <wx/mstream.h>
33
34
36{
37 m_scale = 1.0; // 1.0 = original bitmap size
38 m_bitmap = nullptr;
39 m_image = nullptr;
40 m_originalImage = nullptr;
41 m_ppi = 300; // the bitmap definition. the default is 300PPI
42 m_pixelSizeIu = 254000.0 / m_ppi; // a pixel size value OK for bitmaps using 300 PPI
43 // for Eeschema which uses currently 254000PPI
44 m_isMirrored = false;
46}
47
48
50{
51 m_scale = aSchBitmap.m_scale;
52 m_ppi = aSchBitmap.m_ppi;
53 m_pixelSizeIu = aSchBitmap.m_pixelSizeIu;
54 m_isMirrored = aSchBitmap.m_isMirrored;
55 m_rotation = aSchBitmap.m_rotation;
56
57 m_image = nullptr;
58 m_bitmap = nullptr;
59 m_originalImage = nullptr;
60
61 if( aSchBitmap.m_image )
62 {
63 m_image = new wxImage( *aSchBitmap.m_image );
64 m_bitmap = new wxBitmap( *m_image );
65 m_originalImage = new wxImage( *aSchBitmap.m_originalImage );
66 m_imageId = aSchBitmap.m_imageId;
67 }
68}
69
70
71void BITMAP_BASE::SetImage( wxImage* aImage )
72{
73 delete m_image;
74 m_image = aImage;
75 delete m_originalImage;
76 m_originalImage = new wxImage( *aImage );
78}
79
80
81void BITMAP_BASE::rebuildBitmap( bool aResetID )
82{
83 if( m_bitmap )
84 delete m_bitmap;
85
86 m_bitmap = new wxBitmap( *m_image );
87
88 if( aResetID )
89 m_imageId = KIID();
90}
91
92
94{
95 *m_image = *aItem->m_image;
96 *m_bitmap = *aItem->m_bitmap;
98 m_imageId = aItem->m_imageId;
99 m_scale = aItem->m_scale;
100 m_ppi = aItem->m_ppi;
102 m_isMirrored = aItem->m_isMirrored;
103 m_rotation = aItem->m_rotation;
104}
105
106
107bool BITMAP_BASE::ReadImageFile( wxInputStream& aInStream )
108{
109 std::unique_ptr<wxImage> new_image = std::make_unique<wxImage>();
110
111 if( !new_image->LoadFile( aInStream ) )
112 return false;
113
114 delete m_image;
115 m_image = new_image.release();
116 delete m_originalImage;
117 m_originalImage = new wxImage( *m_image );
119
120 return true;
121}
122
123
124bool BITMAP_BASE::ReadImageFile( const wxString& aFullFilename )
125{
126 wxImage* new_image = new wxImage();
127
128 if( !new_image->LoadFile( aFullFilename ) )
129 {
130 delete new_image;
131 return false;
132 }
133
134 delete m_image;
135 m_image = new_image;
136 delete m_originalImage;
137 m_originalImage = new wxImage( *m_image );
139
140 // Todo: eventually we need to support dpi / scaling in both dimensions
141 int dpiX = m_originalImage->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONX );
142
143 if( dpiX > 1 )
144 {
145 if( m_originalImage->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONUNIT ) == wxIMAGE_RESOLUTION_CM )
146 m_ppi = KiROUND( dpiX * 2.54 );
147 else
148 m_ppi = dpiX;
149 }
150
151 return true;
152}
153
154
155bool BITMAP_BASE::SaveData( FILE* aFile ) const
156{
157 if( m_image )
158 {
159 wxMemoryOutputStream stream;
160 m_image->SaveFile( stream, wxBITMAP_TYPE_PNG );
161
162 // Write binary data in hexadecimal form (ASCII)
163 wxStreamBuffer* buffer = stream.GetOutputStreamBuffer();
164 char* begin = (char*) buffer->GetBufferStart();
165
166 for( int ii = 0; begin < buffer->GetBufferEnd(); begin++, ii++ )
167 {
168 if( ii >= 32 )
169 {
170 ii = 0;
171
172 if( fprintf( aFile, "\n" ) == EOF )
173 return false;
174 }
175
176 if( fprintf( aFile, "%2.2X ", *begin & 0xFF ) == EOF )
177 return false;
178 }
179 }
180
181 return true;
182}
183
184
185void BITMAP_BASE::SaveData( wxArrayString& aPngStrings ) const
186{
187 if( m_image )
188 {
189 wxMemoryOutputStream stream;
190 m_image->SaveFile( stream, wxBITMAP_TYPE_PNG );
191
192 // Write binary data in hexadecimal form (ASCII)
193 wxStreamBuffer* buffer = stream.GetOutputStreamBuffer();
194 char* begin = (char*) buffer->GetBufferStart();
195 wxString line;
196
197 for( int ii = 0; begin < buffer->GetBufferEnd(); begin++, ii++ )
198 {
199 if( ii >= 32 )
200 {
201 ii = 0;
202 aPngStrings.Add( line );
203 line.Empty();
204 }
205
206 line << wxString::Format( wxT( "%2.2X " ), *begin & 0xFF );
207 }
208
209 // Add last line:
210 if( !line.IsEmpty() )
211 aPngStrings.Add( line );
212 }
213}
214
215
216bool BITMAP_BASE::LoadData( LINE_READER& aLine, wxString& aErrorMsg )
217{
218 wxMemoryOutputStream stream;
219 char* line;
220
221 while( true )
222 {
223 if( !aLine.ReadLine() )
224 {
225 aErrorMsg = wxT("Unexpected end of data");
226 return false;
227 }
228
229 line = aLine.Line();
230
231 if( strncasecmp( line, "EndData", 4 ) == 0 )
232 {
233 // all the PNG date is read.
234 // We expect here m_image and m_bitmap are void
235 m_image = new wxImage();
236 wxMemoryInputStream istream( stream );
237 m_image->LoadFile( istream, wxBITMAP_TYPE_PNG );
238 m_bitmap = new wxBitmap( *m_image );
239 m_originalImage = new wxImage( *m_image );
240 break;
241 }
242
243 // Read PNG data, stored in hexadecimal,
244 // each byte = 2 hexadecimal digits and a space between 2 bytes
245 // and put it in memory stream buffer
246 int len = strlen( line );
247
248 for( ; len > 0; len -= 3, line += 3 )
249 {
250 int value = 0;
251
252 if( sscanf( line, "%X", &value ) == 1 )
253 stream.PutC( (char) value );
254 else
255 break;
256 }
257 }
258
259 return true;
260}
261
262
264{
265 BOX2I bbox;
266 VECTOR2I size = GetSize();
267
268 bbox.Inflate( size.x / 2, size.y / 2 );
269
270 return bbox;
271}
272
273
274void BITMAP_BASE::DrawBitmap( wxDC* aDC, const VECTOR2I& aPos )
275{
276 if( m_bitmap == nullptr )
277 return;
278
279 VECTOR2I pos = aPos;
280 VECTOR2I size = GetSize();
281
282 // This fixes a bug in OSX that should be fixed in the 3.0.3 version or later.
283 if( ( size.x == 0 ) || ( size.y == 0 ) )
284 return;
285
286 // To draw the bitmap, pos is the upper left corner position
287 pos.x -= size.x / 2;
288 pos.y -= size.y / 2;
289
290 double scale;
291 int logicalOriginX, logicalOriginY;
292 aDC->GetUserScale( &scale, &scale );
293 aDC->GetLogicalOrigin( &logicalOriginX, &logicalOriginY );
294
295 // We already have issues to draw a bitmap on the wxDC, depending on wxWidgets version.
296 // Now we have an issue on wxWidgets 3.1.6 to fix the clip area
297 // and the bitmap position when using TransformMatrix
298 // So for version == 3.1.6 do not use it
299 // Be carefull before changing the code.
300 bool useTransform = aDC->CanUseTransformMatrix();
301
302 #if wxCHECK_VERSION( 3, 1, 6 ) && !wxCHECK_VERSION( 3, 1, 7 )
303 useTransform = false;
304 #endif
305
306 wxAffineMatrix2D init_matrix = aDC->GetTransformMatrix();
307
308 // Note: clipping bitmap area was made to fix a minor issue in old versions of
309 // Kicad/wxWidgets (5.1 / wx 3.0)
310 // However SetClippingRegion creates a lot of issues (different ways to fix the
311 // position and size of the area, depending on wxWidget version)because it changes with
312 // each versions of wxWigets, so it is now disabled
313 // However the code is still here, just in case
314 // #define USE_CLIP_AREA
315
316 wxPoint clipAreaPos;
317
318 if( useTransform )
319 {
320 wxAffineMatrix2D matrix = aDC->GetTransformMatrix();
321 matrix.Translate( pos.x, pos.y );
322 matrix.Scale( GetScalingFactor(), GetScalingFactor() );
323 aDC->SetTransformMatrix( matrix );
324 // Needed on wx <= 3.1.5, and this is strange...
325 // Nevertheless, this code has problem (the bitmap is not seen)
326 // with wx version > 3.1.5
327 clipAreaPos.x = pos.x;
328 clipAreaPos.y = pos.y;
329
330 pos.x = pos.y = 0;
331 }
332 else
333 {
334 aDC->SetUserScale( scale * GetScalingFactor(), scale * GetScalingFactor() );
335 aDC->SetLogicalOrigin( logicalOriginX / GetScalingFactor(),
336 logicalOriginY / GetScalingFactor() );
337
338 pos.x = KiROUND( pos.x / GetScalingFactor() );
339 pos.y = KiROUND( pos.y / GetScalingFactor() );
340 size.x = KiROUND( size.x / GetScalingFactor() );
341 size.y = KiROUND( size.y / GetScalingFactor() );
342 clipAreaPos.x = pos.x;
343 clipAreaPos.y = pos.y;
344 }
345
346 #ifdef USE_CLIP_AREA
347 aDC->DestroyClippingRegion();
348 aDC->SetClippingRegion( clipAreaPos, wxSize( size.x, size.y ) );
349 #endif
350
352 {
353 wxBitmap result( m_bitmap->ConvertToImage().ConvertToGreyscale() );
354 aDC->DrawBitmap( result, pos.x, pos.y, true );
355 }
356 else
357 {
358 aDC->DrawBitmap( *m_bitmap, pos.x, pos.y, true );
359 }
360
361 if( useTransform )
362 aDC->SetTransformMatrix( init_matrix );
363 else
364 {
365 aDC->SetUserScale( scale, scale );
366 aDC->SetLogicalOrigin( logicalOriginX, logicalOriginY );
367 }
368
369 #ifdef USE_CLIP_AREA
370 aDC->DestroyClippingRegion();
371 #endif
372}
373
374
376{
377 VECTOR2I size;
378
379 if( m_bitmap )
380 {
381 size.x = m_bitmap->GetWidth();
382 size.y = m_bitmap->GetHeight();
383
384 size.x = KiROUND( size.x * GetScalingFactor() );
385 size.y = KiROUND( size.y * GetScalingFactor() );
386 }
387
388 return size;
389}
390
391
392void BITMAP_BASE::Mirror( bool aVertically )
393{
394 if( m_image )
395 {
396 *m_image = m_image->Mirror( not aVertically );
398 rebuildBitmap( false );
399 }
400}
401
402
403void BITMAP_BASE::Rotate( bool aRotateCCW )
404{
405 if( m_image )
406 {
407 *m_image = m_image->Rotate90( aRotateCCW );
408 m_rotation += ( aRotateCCW ? -ANGLE_90 : ANGLE_90 );
409 rebuildBitmap( false );
410 }
411}
412
413
415{
416 if( m_image )
417 {
418 *m_image = m_image->ConvertToGreyscale();
419 *m_originalImage = m_originalImage->ConvertToGreyscale();
421 }
422}
423
424
425void BITMAP_BASE::PlotImage( PLOTTER* aPlotter, const VECTOR2I& aPos,
426 const COLOR4D& aDefaultColor,
427 int aDefaultPensize ) const
428{
429 if( m_image == nullptr )
430 return;
431
432 // These 2 lines are useful only for plotters that cannot plot a bitmap
433 // and plot a rectangle instead of.
434 aPlotter->SetColor( aDefaultColor );
435 aPlotter->SetCurrentLineWidth( aDefaultPensize );
436 aPlotter->PlotImage( *m_image, aPos, GetScalingFactor() );
437}
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:52
void Rotate(bool aRotateCCW)
Rotate image CW or CCW.
double GetScalingFactor() const
This scaling factor depends on m_pixelSizeIu and m_scale.
Definition: bitmap_base.h:99
bool SaveData(FILE *aFile) const
Write the bitmap data to aFile.
void PlotImage(PLOTTER *aPlotter, const VECTOR2I &aPos, const KIGFX::COLOR4D &aDefaultColor, int aDefaultPensize) const
Plot bitmap on plotter.
const BOX2I GetBoundingBox() const
Return the orthogonal, bounding box of this object for display purposes.
void Mirror(bool aVertically)
Mirror image vertically (i.e.
KIID m_imageId
Definition: bitmap_base.h:249
wxImage * m_originalImage
Definition: bitmap_base.h:242
void rebuildBitmap(bool aResetID=true)
Definition: bitmap_base.cpp:81
wxBitmap * m_bitmap
Definition: bitmap_base.h:243
void ImportData(BITMAP_BASE *aItem)
Copy aItem image to this object and update m_bitmap.
Definition: bitmap_base.cpp:93
void ConvertToGreyscale()
void DrawBitmap(wxDC *aDC, const VECTOR2I &aPos)
void SetImage(wxImage *aImage)
Definition: bitmap_base.cpp:71
bool ReadImageFile(const wxString &aFullFilename)
Reads and stores in memory an image file.
wxImage * m_image
Definition: bitmap_base.h:241
double m_scale
Definition: bitmap_base.h:239
double m_pixelSizeIu
Definition: bitmap_base.h:244
bool m_isMirrored
Definition: bitmap_base.h:250
bool LoadData(LINE_READER &aLine, wxString &aErrorMsg)
Load an image data saved by SaveData.
BITMAP_BASE(const VECTOR2I &pos=VECTOR2I(0, 0))
Definition: bitmap_base.cpp:35
VECTOR2I GetSize() const
EDA_ANGLE m_rotation
Definition: bitmap_base.h:251
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:506
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
Definition: kiid.h:48
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:81
virtual char * ReadLine()=0
Read a line of text into the buffer and increments the line number counter.
char * Line() const
Return a pointer to the last line that was read in.
Definition: richio.h:117
Base plotter engine class.
Definition: plotter.h:110
virtual void PlotImage(const wxImage &aImage, const VECTOR2I &aPos, double aScaleFactor)
Only PostScript plotters can plot bitmaps.
Definition: plotter.cpp:254
virtual void SetCurrentLineWidth(int width, void *aData=nullptr)=0
Set the line width for the next drawing.
virtual void SetColor(const COLOR4D &color)=0
static constexpr EDA_ANGLE & ANGLE_90
Definition: eda_angle.h:425
static constexpr EDA_ANGLE & ANGLE_0
Definition: eda_angle.h:423
bool GetGRForceBlackPenState(void)
Definition: gr_basic.cpp:156
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
const int scale
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:85