KiCad PCB EDA Suite
Loading...
Searching...
No Matches
reference_image.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "reference_image.h"
21
22#include <cmath>
23#include <cstdlib>
24#include <string>
25
26#include <wx/debug.h>
27#include <wx/mstream.h>
28
29#include <bitmap_base.h>
31#include <plotters/plotter.h>
32
33
34static bool compareImages( const BITMAP_BASE& aLeft, const BITMAP_BASE& aRight )
35{
36 const wxImage* leftImage = aLeft.GetImageData();
37 const wxImage* rightImage = aRight.GetImageData();
38
39 if( !leftImage || !rightImage )
40 return leftImage == rightImage;
41
42 wxMemoryOutputStream leftStream;
43 wxMemoryOutputStream rightStream;
44
45 if( !aLeft.SaveImageData( leftStream ) || !aRight.SaveImageData( rightStream ) )
46 return false;
47
48 size_t leftSize = leftStream.GetSize();
49 size_t rightSize = rightStream.GetSize();
50
51 if( leftSize != rightSize )
52 return false;
53
54 if( leftSize == 0 )
55 return true;
56
57 std::string leftData( leftSize, '\0' );
58 std::string rightData( rightSize, '\0' );
59
60 leftStream.CopyTo( leftData.data(), leftSize );
61 rightStream.CopyTo( rightData.data(), rightSize );
62
63 return leftData == rightData;
64}
65
66
68 m_iuScale( aIuScale ),
69 m_pos( 0, 0 ),
72 m_opacity( 1.0 ),
73 m_bitmapBase( std::make_unique<BITMAP_BASE>() )
74{
76}
77
78
80 m_iuScale( aOther.m_iuScale ),
81 m_pos( aOther.m_pos ),
84 m_opacity( aOther.m_opacity ),
85 m_bitmapBase( std::make_unique<BITMAP_BASE>( *aOther.m_bitmapBase ) )
86{
88}
89
90
94
95
97{
98 const double pixelSizeIu = (double) m_iuScale.MilsToIU( 1000 ) / m_bitmapBase->GetPPI();
99 m_bitmapBase->SetPixelSizeIu( pixelSizeIu );
100}
101
102
104{
105 wxASSERT( m_iuScale.IU_PER_MILS == aOther.m_iuScale.IU_PER_MILS );
106
107 if( &aOther != this )
108 {
109 if( aOther.m_bitmapBase )
110 {
111 m_bitmapBase = std::make_unique<BITMAP_BASE>( *aOther.m_bitmapBase );
112 }
113 m_pos = aOther.m_pos;
116 m_opacity = aOther.m_opacity;
118 }
119
120 return *this;
121}
122
123
125{
126 if( m_pos != aOther.m_pos )
127 return false;
128
130 return false;
131
132 if( m_orientation != aOther.m_orientation )
133 return false;
134
135 if( m_opacity != aOther.m_opacity )
136 return false;
137
138 if( m_bitmapBase->GetSize() != aOther.m_bitmapBase->GetSize() )
139 return false;
140
141 if( m_bitmapBase->GetPPI() != aOther.m_bitmapBase->GetPPI() )
142 return false;
143
144 if( m_bitmapBase->GetScale() != aOther.m_bitmapBase->GetScale() )
145 return false;
146
147 if( !compareImages( *m_bitmapBase, *aOther.m_bitmapBase ) )
148 return false;
149
150 return true;
151}
152
153
154double REFERENCE_IMAGE::Similarity( const REFERENCE_IMAGE& aOther ) const
155{
156 double similarity = 1.0;
157
158 if( m_pos != aOther.m_pos )
159 similarity *= 0.9;
160
161 if( m_orientation != aOther.m_orientation )
162 similarity *= 0.9;
163
164 if( m_opacity != aOther.m_opacity )
165 similarity *= 0.9;
166
167 if( m_bitmapBase->GetSize() != aOther.m_bitmapBase->GetSize() )
168 similarity *= 0.9;
169
170 if( m_bitmapBase->GetPPI() != aOther.m_bitmapBase->GetPPI() )
171 similarity *= 0.9;
172
173 if( m_bitmapBase->GetScale() != aOther.m_bitmapBase->GetScale() )
174 similarity *= 0.9;
175
176 if( !compareImages( *m_bitmapBase, *aOther.m_bitmapBase ) )
177 similarity *= 0.9;
178
179 return similarity;
180}
181
182
187
188
190{
191 return boundingSize( m_orientation.GetAngle() );
192}
193
194
196{
197 // Un-rotated size of the image.
198 const VECTOR2I size = m_bitmapBase->GetSize();
199
200 if( aOrientation.IsHorizontal() )
201 return size;
202
203 if( aOrientation.IsVertical() )
204 return VECTOR2I{ size.y, size.x };
205
206 // The raster is stored unrotated, so its axis-aligned extent grows with the orientation.
207 const double angleCos = std::abs( aOrientation.Cos() );
208 const double angleSin = std::abs( aOrientation.Sin() );
209
210 const double w = size.x * angleCos + size.y * angleSin;
211 const double h = size.x * angleSin + size.y * angleCos;
212
213 return VECTOR2I{ KiROUND( w ), KiROUND( h ) };
214}
215
216
218{
219 return m_pos;
220}
221
222
224{
225 const BOX2D newBox = BOX2D::ByCenter( aPos, boundingSize() );
226
227 if( !IsBOX2Safe( newBox ) )
228 return;
229
230 m_pos = aPos;
231}
232
233
238
239
241{
242 m_transformOriginOffset = aCenter;
243}
244
245
247{
248 return m_bitmapBase->GetSize();
249}
250
251
253{
254 if( aWidth <= 0 )
255 return;
256
257 const double ratio = aWidth / (double) m_bitmapBase->GetSize().x;
258 scaleBy( ratio );
259}
260
261
262void REFERENCE_IMAGE::SetHeight( int aHeight )
263{
264 if( aHeight <= 0 )
265 return;
266
267 const double ratio = aHeight / (double) m_bitmapBase->GetSize().y;
268 scaleBy( ratio );
269}
270
271
273{
274 return m_bitmapBase->GetScale();
275}
276
277
279{
280 if( aScale <= 0 )
281 return;
282
283 const double ratio = aScale / m_bitmapBase->GetScale();
284 scaleBy( ratio );
285}
286
287
288void REFERENCE_IMAGE::scaleBy( double aRatio )
289{
290 if( aRatio <= 0 )
291 return;
292
293 const VECTOR2D currentOrigin = m_pos + m_transformOriginOffset;
294 const VECTOR2D newOffset = m_transformOriginOffset * aRatio;
295 const VECTOR2D newCenter = currentOrigin - newOffset;
296 const VECTOR2D newSize = boundingSize() * aRatio;
297
298 // The span of the image is limited to the size of the coordinate system
299 if( !IsVec2SafeXY( newSize ) )
300 return;
301
302 const BOX2D newBox = BOX2D::ByCenter( newCenter, newSize );
303
304 // Any overflow, just reject the call
305 if( !IsBOX2Safe( newBox ) )
306 return;
307
308 m_bitmapBase->SetScale( m_bitmapBase->GetScale() * aRatio );
309 SetTransformOriginOffset( KiROUND( newOffset ) );
310
311 // Don't need to recheck the box, we just did that
312 m_pos = KiROUND( newCenter );
313}
314
315
316bool REFERENCE_IMAGE::Flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection )
317{
318 VECTOR2I newPos = m_pos;
319 MIRROR( newPos, aCentre, aFlipDirection );
320
321 const BOX2D newBox = BOX2D::ByCenter( newPos, boundingSize() );
322
323 if( !IsBOX2Safe( newBox ) )
324 return false;
325
326 m_pos = newPos;
327 m_bitmapBase->Mirror( aFlipDirection );
328
329 // A reflection reverses the sense of the stored rotation.
331
332 MIRROR( m_transformOriginOffset, VECTOR2I( 0, 0 ), aFlipDirection );
333 return true;
334}
335
336
337bool REFERENCE_IMAGE::Mirror( const SEG& aAxis )
338{
339 const EDA_ANGLE axisAngle = -EDA_ANGLE( aAxis.B - aAxis.A );
340 const EDA_ANGLE newOrientation = m_orientation.GetAngle().Reflect( axisAngle );
341 const VECTOR2I newPos = aAxis.ReflectPoint( m_pos );
342
343 // Check the bounding box with the new orientation to ensure it fits within the coordinate system.
344 const BOX2D newBox = BOX2D::ByCenter( newPos, boundingSize( newOrientation ) );
345
346 if( !IsBOX2Safe( newBox ) )
347 return false;
348
349 m_pos = newPos;
351 m_orientation = newOrientation;
352
353 // The transform origin is an offset from the image center, so it reflects about the axis
354 // through the image origin and not about the mirror axis itself.
355 const SEG offsetAxis( VECTOR2I( 0, 0 ), aAxis.B - aAxis.A );
357
358 return true;
359}
360
361
362void REFERENCE_IMAGE::Rotate( const VECTOR2I& aCenter, const EDA_ANGLE& aAngle )
363{
364 RotatePoint( m_pos, aCenter, aAngle );
366
367 m_orientation += aAngle;
368}
369
370
372{
373 return m_orientation.GetAngle();
374}
375
376
378{
379 m_orientation = aAngle;
380}
381
382
383void REFERENCE_IMAGE::SetOpacity( double aOpacity )
384{
385 m_opacity = std::clamp( aOpacity, 0.0, 1.0 );
386}
387
388
389bool REFERENCE_IMAGE::ReadImageFile( const wxString& aFullFilename )
390{
391 if( m_bitmapBase->ReadImageFile( aFullFilename ) )
392 {
394 return true;
395 }
396
397 return false;
398}
399
400
401bool REFERENCE_IMAGE::ReadImageFile( wxMemoryBuffer& aBuffer )
402{
403 if( m_bitmapBase->ReadImageFile( aBuffer ) )
404 {
406 return true;
407 }
408
409 return false;
410}
411
412
413bool REFERENCE_IMAGE::SetImage( const wxImage& aImage )
414{
415 if( m_bitmapBase->SetImage( aImage ) )
416 {
418 return true;
419 }
420
421 return false;
422}
423
424
425void REFERENCE_IMAGE::PackToBytes( std::string& aOutputBytes ) const
426{
427 wxMemoryOutputStream imageStream;
428
429 if( m_bitmapBase->GetImageData() && m_bitmapBase->SaveImageData( imageStream ) )
430 {
431 size_t size = imageStream.GetSize();
432
433 if( size > 0 )
434 {
435 aOutputBytes.resize( size );
436 imageStream.CopyTo( aOutputBytes.data(), size );
437 }
438 }
439}
440
441
442bool REFERENCE_IMAGE::UnpackFromBytes( const std::string& aInputBytes )
443{
444 if( aInputBytes.empty() )
445 return false;
446
447 wxMemoryBuffer imageBuffer;
448 imageBuffer.AppendData( aInputBytes.data(), aInputBytes.size() );
449
450 return ReadImageFile( imageBuffer );
451}
452
453
455{
456 // This cannot be null after construction
457 return *m_bitmapBase;
458}
459
460
465
466
467void REFERENCE_IMAGE::PlotImage( PLOTTER* aPlotter, const VECTOR2I& aPos, const KIGFX::COLOR4D& aDefaultColor,
468 int aDefaultPensize ) const
469{
470 const wxImage* image = m_bitmapBase->GetImageData();
471
472 if( !image || !image->IsOk() )
473 return;
474
475 // These 2 lines are useful only for plotters that cannot plot a bitmap and plot a
476 // rectangle instead of the actual image.
477 aPlotter->SetColor( aDefaultColor );
478 aPlotter->SetCurrentLineWidth( aDefaultPensize );
479
480 const double scaleFactor = m_bitmapBase->GetScalingFactor();
481 const EDA_ANGLE angle = m_orientation.GetAngle();
482
483 if( angle.IsZero() )
484 {
485 aPlotter->PlotImage( *image, aPos, scaleFactor, m_opacity );
486 return;
487 }
488
489 // Avoid modifying the original image via the shared buffer.
490 wxImage turned = image->Copy();
491
492 if( angle == ANGLE_90 )
493 {
494 turned = turned.Rotate90( false );
495 }
496 else if( angle == ANGLE_180 )
497 {
498 turned = turned.Rotate180();
499 }
500 else if( angle == ANGLE_270 )
501 {
502 turned = turned.Rotate90( true );
503 }
504 else if( !angle.IsZero() )
505 {
506 // Avoid opaque corners after non-cardinal rotation
507 if( !turned.HasAlpha() )
508 turned.InitAlpha();
509
510 const wxPoint centerPoint( turned.GetWidth() / 2, turned.GetHeight() / 2 );
511 turned = turned.Rotate( angle.AsRadians(), centerPoint, true );
512 }
513
514 wxCHECK_MSG( turned.IsOk(), /* void */, wxT( "Failed to rotate image" ) );
515
516 aPlotter->PlotImage( turned, aPos, scaleFactor, m_opacity );
517}
518
519
521{
522 std::swap( m_pos, aOther.m_pos );
524 std::swap( m_orientation, aOther.m_orientation );
525 std::swap( m_opacity, aOther.m_opacity );
526 std::swap( m_bitmapBase, aOther.m_bitmapBase );
527}
constexpr bool IsBOX2Safe(const BOX2< Vec > &aInput)
Check if a BOX2 is safe for use with BOX2D (probably BOX2D or BOX2L)
Definition box2.h:944
BOX2< VECTOR2I > BOX2I
Definition box2.h:917
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
BOX2< VECTOR2D > BOX2D
Definition box2.h:918
This class handle bitmap images in KiCad.
Definition bitmap_base.h:47
double GetScale() const
Definition bitmap_base.h:71
bool SaveImageData(wxOutputStream &aOutStream) const
Write the bitmap data to aOutStream.
int GetPPI() const
wxImage * GetImageData()
Definition bitmap_base.h:66
VECTOR2I GetSize() const
static constexpr BOX2< VECTOR2I > ByCenter(const VECTOR2I &aCenter, const SizeVec &aSize)
Definition box2.h:72
double Sin() const
Definition eda_angle.h:177
bool IsZero() const
Definition eda_angle.h:135
bool IsHorizontal() const
Definition eda_angle.h:141
double AsRadians() const
Definition eda_angle.h:119
bool IsVertical() const
Definition eda_angle.h:147
double Cos() const
Definition eda_angle.h:196
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
Base plotter engine class.
Definition plotter.h:138
virtual void SetCurrentLineWidth(int width, void *aData=nullptr)=0
Set the line width for the next drawing.
virtual void PlotImage(const wxImage &aImage, const VECTOR2I &aPos, double aScaleFactor, double aAlpha)
Plot a bitmap.
Definition plotter.cpp:258
virtual void SetColor(const COLOR4D &color)=0
BITMAP_BASE & MutableImage() const
Only use this if you really need to modify the underlying image.
const EDA_IU_SCALE & m_iuScale
EDA_ANGLE GetOrientation() const
void SwapData(REFERENCE_IMAGE &aItem)
bool Flip(const VECTOR2I &aCentre, FLIP_DIRECTION aFlipDirection)
VECTOR2I m_pos
XY coordinates of center of the bitmap.
void Rotate(const VECTOR2I &aCenter, const EDA_ANGLE &aAngle)
Rotate the image about aCenter.
void SetTransformOriginOffset(const VECTOR2I &aCenter)
bool ReadImageFile(const wxString &aFullFilename)
Read and store an image file.
VECTOR2I GetTransformOriginOffset() const
Get the center of scaling, etc, relative to the image center (GetPosition()).
VECTOR2I GetPosition() const
void PlotImage(PLOTTER *aPlotter, const VECTOR2I &aPos, const KIGFX::COLOR4D &aDefaultColor, int aDefaultPensize) const
Plot the image on a plotter, turned to its stored orientation.
EDA_ORIENTATION m_orientation
Rotation of the image content relative to the raster, in [0, 360).
bool SetImage(const wxImage &aImage)
Set the image from an existing wxImage.
std::unique_ptr< BITMAP_BASE > m_bitmapBase
REFERENCE_IMAGE & operator=(const REFERENCE_IMAGE &aOther)
void SetHeight(int aHeight)
VECTOR2I GetSize() const
VECTOR2I m_transformOriginOffset
Center of scaling, etc, relative to the image center.
bool UnpackFromBytes(const std::string &aInputBytes)
void SetOrientation(const EDA_ANGLE &aAngle)
Set the rotation of the image content relative to the stored raster.
void SetOpacity(double aOpacity)
void SetPosition(const VECTOR2I &aPos)
void SetWidth(int aWidth)
const BITMAP_BASE & GetImage() const
Get the underlying image.
double Similarity(const REFERENCE_IMAGE &aOther) const
double GetImageScale() const
void SetImageScale(double aScale)
Set the image "zoom" value.
bool Mirror(const SEG &aAxis)
Mirror the image about an arbitrary axis, keeping its layer.
REFERENCE_IMAGE(const EDA_IU_SCALE &aIuScale)
VECTOR2I boundingSize() const
BOX2I GetBoundingBox() const
void scaleBy(double ratio)
void PackToBytes(std::string &aOutputBytes) const
double m_opacity
The opacity of the image, in the range [0, 1].
bool operator==(const REFERENCE_IMAGE &aOther) const
Definition seg.h:38
const VECTOR2I ReflectPoint(const VECTOR2I &aP) const
Reflect a point using this segment as axis.
Definition seg.cpp:630
VECTOR2I A
Definition seg.h:45
VECTOR2I B
Definition seg.h:46
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:472
static constexpr EDA_ANGLE ANGLE_90
Definition eda_angle.h:474
static constexpr EDA_ANGLE ANGLE_270
Definition eda_angle.h:477
static constexpr EDA_ANGLE ANGLE_180
Definition eda_angle.h:476
a few functions useful in geometry calculations.
bool IsVec2SafeXY(const VECTOR2< T > &aVec)
Check if both coordinates of a vector are within the limits of the integer type.
constexpr void MIRROR(T &aPoint, const T &aMirrorRef)
Updates aPoint with the mirror of aPoint relative to the aMirrorRef.
Definition mirror.h:44
FLIP_DIRECTION
Definition mirror.h:26
@ TOP_BOTTOM
Flip top to bottom (around the X axis)
Definition mirror.h:28
STL namespace.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:461
static bool compareImages(const BITMAP_BASE &aLeft, const BITMAP_BASE &aRight)
const double IU_PER_MILS
Definition base_units.h:75
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:227
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707