KiCad PCB EDA Suite
Loading...
Searching...
No Matches
bitmap_base.h
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 jp.charras at wanadoo.fr
5 * Copyright (C) 2013-2024 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#pragma once
26
27#include <wx/bitmap.h>
28#include <wx/image.h>
29
30#include <core/mirror.h>
31#include <kiid.h>
32#include <math/box2.h>
33#include <gal/color4d.h>
34
35class LINE_READER;
36class PLOTTER;
37
38
49{
50public:
51 BITMAP_BASE( const VECTOR2I& pos = VECTOR2I( 0, 0 ) );
52
53 BITMAP_BASE( const BITMAP_BASE& aSchBitmap );
54
56 {
57 delete m_bitmap;
58 delete m_image;
59 delete m_originalImage;
60 }
61
62 /*
63 * Accessors:
64 */
65 double GetPixelSizeIu() const { return m_pixelSizeIu; }
66 void SetPixelSizeIu( double aPixSize ) { m_pixelSizeIu = aPixSize; }
67
68 wxImage* GetImageData() { return m_image; }
69 const wxImage* GetImageData() const { return m_image; }
70
71 const wxImage* GetOriginalImageData() const { return m_originalImage; }
72
73 double GetScale() const { return m_scale; }
74 void SetScale( double aScale ) { m_scale = aScale; }
75
76 KIID GetImageID() const { return m_imageId; }
77
81 void ImportData( BITMAP_BASE& aItem );
82
94 double GetScalingFactor() const
95 {
96 return m_pixelSizeIu * m_scale;
97 }
98
102 VECTOR2I GetSize() const;
103
108 {
109 if( m_image )
110 return VECTOR2I( m_image->GetWidth(), m_image->GetHeight() );
111 else
112 return VECTOR2I( 0, 0 );
113 }
114
118 int GetPPI() const
119 {
120 return m_ppi;
121 }
122
130 const BOX2I GetBoundingBox() const;
131
132 void DrawBitmap( wxDC* aDC, const VECTOR2I& aPos,
133 const KIGFX::COLOR4D& aBackgroundColor = KIGFX::COLOR4D::UNSPECIFIED ) const;
134
145 bool ReadImageFile( const wxString& aFullFilename );
146
158 bool ReadImageFile( wxInputStream& aInStream );
159
171 bool ReadImageFile( wxMemoryBuffer& aBuf );
172
176 bool SetImage( const wxImage& aImage );
177
186 bool SaveImageData( wxOutputStream& aOutStream ) const;
187
198 bool LoadLegacyData( LINE_READER& aLine, wxString& aErrorMsg );
199
205 void Mirror( FLIP_DIRECTION aFlipDirection );
206
212 void Rotate( bool aRotateCCW );
213
214 void ConvertToGreyscale();
215
216 bool IsMirroredX() const { return m_isMirroredX; }
217 bool IsMirroredY() const { return m_isMirroredY; }
218 EDA_ANGLE Rotation() const { return m_rotation; }
219
230 void PlotImage( PLOTTER* aPlotter, const VECTOR2I& aPos,
231 const KIGFX::COLOR4D& aDefaultColor, int aDefaultPensize ) const;
232
236 wxBitmapType GetImageType() const { return m_imageType; }
237
241 void SetImageType( wxBitmapType aType ) { m_imageType = aType; }
242
246 const wxMemoryBuffer& GetImageDataBuffer() const { return m_imageData; }
247
252
253private:
254 /*
255 * Rebuild the internal bitmap used to draw/plot image.
256 *
257 * This must be called after a #m_image change.
258 *
259 * @param aResetID is used to reset the cache ID used for OpenGL rendering.
260 */
261 void rebuildBitmap( bool aResetID = true );
262
263 void updatePPI();
264
265 double m_scale; // The scaling factor of the bitmap
266 // With m_pixelSizeIu, controls the actual draw size
267 wxMemoryBuffer m_imageData; // The original image data, in its original format
268 wxBitmapType m_imageType; // the image type (png, jpeg, etc.)
269
270 wxImage* m_image; // the raw, uncompressed image data
271 wxImage* m_originalImage; // Raw image data, not transformed by rotate/mirror
272 wxBitmap* m_bitmap; // the bitmap used to draw/plot image
273 double m_pixelSizeIu; // The scaling factor of the bitmap
274 // to convert the bitmap size (in pixels)
275 // to internal KiCad units
276 // Usually does not change
277 int m_ppi; // the bitmap definition. the default is 300PPI
279 bool m_isMirroredX; // Used for OpenGL rendering only
280 bool m_isMirroredY; // Used for OpenGL rendering only
281 EDA_ANGLE m_rotation; // Used for OpenGL rendering only
282};
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:49
wxMemoryBuffer m_imageData
Definition: bitmap_base.h:267
void Rotate(bool aRotateCCW)
Rotate image CW or CCW.
bool LoadLegacyData(LINE_READER &aLine, wxString &aErrorMsg)
Load an image data saved by #SaveData.
double GetScalingFactor() const
This scaling factor depends on m_pixelSizeIu and m_scale.
Definition: bitmap_base.h:94
void SetImageType(wxBitmapType aType)
Set the bitmap type (png, jpeg, etc.)
Definition: bitmap_base.h:241
const wxImage * GetOriginalImageData() const
Definition: bitmap_base.h:71
void PlotImage(PLOTTER *aPlotter, const VECTOR2I &aPos, const KIGFX::COLOR4D &aDefaultColor, int aDefaultPensize) const
Plot bitmap on plotter.
VECTOR2I GetSizePixels() const
Definition: bitmap_base.h:107
EDA_ANGLE Rotation() const
Definition: bitmap_base.h:218
void Mirror(FLIP_DIRECTION aFlipDirection)
Mirror image vertically (i.e.
void updatePPI()
Definition: bitmap_base.cpp:95
const BOX2I GetBoundingBox() const
Return the orthogonal, bounding box of this object for display purposes.
double GetPixelSizeIu() const
Definition: bitmap_base.h:65
double GetScale() const
Definition: bitmap_base.h:73
wxBitmapType GetImageType() const
Return the bitmap type (png, jpeg, etc.)
Definition: bitmap_base.h:236
bool m_isMirroredY
Definition: bitmap_base.h:280
bool m_isMirroredX
Definition: bitmap_base.h:279
void ImportData(BITMAP_BASE &aItem)
Copy aItem image to this object and update m_bitmap.
const wxMemoryBuffer & GetImageDataBuffer() const
Definition: bitmap_base.h:246
KIID m_imageId
Definition: bitmap_base.h:278
bool SaveImageData(wxOutputStream &aOutStream) const
Write the bitmap data to aOutStream.
bool IsMirroredX() const
Definition: bitmap_base.h:216
wxImage * m_originalImage
Definition: bitmap_base.h:271
void rebuildBitmap(bool aResetID=true)
Definition: bitmap_base.cpp:82
wxBitmapType m_imageType
Definition: bitmap_base.h:268
bool IsMirroredY() const
Definition: bitmap_base.h:217
void SetPixelSizeIu(double aPixSize)
Definition: bitmap_base.h:66
wxBitmap * m_bitmap
Definition: bitmap_base.h:272
void ConvertToGreyscale()
bool SetImage(const wxImage &aImage)
Set the image from an existing wxImage.
void UpdateImageDataBuffer()
Resets the image data buffer using the current image data.
const wxImage * GetImageData() const
Definition: bitmap_base.h:69
bool ReadImageFile(const wxString &aFullFilename)
Reads and stores in memory an image file.
KIID GetImageID() const
Definition: bitmap_base.h:76
wxImage * m_image
Definition: bitmap_base.h:270
double m_scale
Definition: bitmap_base.h:265
double m_pixelSizeIu
Definition: bitmap_base.h:273
int GetPPI() const
Definition: bitmap_base.h:118
void DrawBitmap(wxDC *aDC, const VECTOR2I &aPos, const KIGFX::COLOR4D &aBackgroundColor=KIGFX::COLOR4D::UNSPECIFIED) const
wxImage * GetImageData()
Definition: bitmap_base.h:68
VECTOR2I GetSize() const
EDA_ANGLE m_rotation
Definition: bitmap_base.h:281
void SetScale(double aScale)
Definition: bitmap_base.h:74
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition: color4d.h:398
Definition: kiid.h:49
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:93
Base plotter engine class.
Definition: plotter.h:105
FLIP_DIRECTION
Definition: mirror.h:27
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691