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
181 bool SaveImageData( wxOutputStream& aOutStream ) const;
182
193 bool LoadLegacyData( LINE_READER& aLine, wxString& aErrorMsg );
194
200 void Mirror( FLIP_DIRECTION aFlipDirection );
201
207 void Rotate( bool aRotateCCW );
208
209 void ConvertToGreyscale();
210
211 bool IsMirroredX() const { return m_isMirroredX; }
212 bool IsMirroredY() const { return m_isMirroredY; }
213 EDA_ANGLE Rotation() const { return m_rotation; }
214
225 void PlotImage( PLOTTER* aPlotter, const VECTOR2I& aPos,
226 const KIGFX::COLOR4D& aDefaultColor, int aDefaultPensize ) const;
227
231 wxBitmapType GetImageType() const { return m_imageType; }
232
236 void SetImageType( wxBitmapType aType ) { m_imageType = aType; }
237
241 const wxMemoryBuffer& GetImageDataBuffer() const { return m_imageData; }
242
247
248private:
249 /*
250 * Rebuild the internal bitmap used to draw/plot image.
251 *
252 * This must be called after a #m_image change.
253 *
254 * @param aResetID is used to reset the cache ID used for OpenGL rendering.
255 */
256 void rebuildBitmap( bool aResetID = true );
257
258 void updatePPI();
259
260 double m_scale; // The scaling factor of the bitmap
261 // With m_pixelSizeIu, controls the actual draw size
262 wxMemoryBuffer m_imageData; // The original image data, in its original format
263 wxBitmapType m_imageType; // the image type (png, jpeg, etc.)
264
265 wxImage* m_image; // the raw, uncompressed image data
266 wxImage* m_originalImage; // Raw image data, not transformed by rotate/mirror
267 wxBitmap* m_bitmap; // the bitmap used to draw/plot image
268 double m_pixelSizeIu; // The scaling factor of the bitmap
269 // to convert the bitmap size (in pixels)
270 // to internal KiCad units
271 // Usually does not change
272 int m_ppi; // the bitmap definition. the default is 300PPI
274 bool m_isMirroredX; // Used for OpenGL rendering only
275 bool m_isMirroredY; // Used for OpenGL rendering only
276 EDA_ANGLE m_rotation; // Used for OpenGL rendering only
277};
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:49
wxMemoryBuffer m_imageData
Definition: bitmap_base.h:262
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:236
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:213
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:231
bool m_isMirroredY
Definition: bitmap_base.h:275
bool m_isMirroredX
Definition: bitmap_base.h:274
void ImportData(BITMAP_BASE &aItem)
Copy aItem image to this object and update m_bitmap.
const wxMemoryBuffer & GetImageDataBuffer() const
Definition: bitmap_base.h:241
KIID m_imageId
Definition: bitmap_base.h:273
bool SaveImageData(wxOutputStream &aOutStream) const
Write the bitmap data to aOutStream.
bool IsMirroredX() const
Definition: bitmap_base.h:211
wxImage * m_originalImage
Definition: bitmap_base.h:266
void rebuildBitmap(bool aResetID=true)
Definition: bitmap_base.cpp:82
wxBitmapType m_imageType
Definition: bitmap_base.h:263
bool IsMirroredY() const
Definition: bitmap_base.h:212
void SetPixelSizeIu(double aPixSize)
Definition: bitmap_base.h:66
wxBitmap * m_bitmap
Definition: bitmap_base.h:267
void ConvertToGreyscale()
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:265
double m_scale
Definition: bitmap_base.h:260
double m_pixelSizeIu
Definition: bitmap_base.h:268
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:276
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