KiCad PCB EDA Suite
BITMAP_BASE Class Reference

This class handle bitmap images in KiCad. More...

#include <bitmap_base.h>

Public Member Functions

 BITMAP_BASE (const VECTOR2I &pos=VECTOR2I(0, 0))
 
 BITMAP_BASE (const BITMAP_BASE &aSchBitmap)
 
 ~BITMAP_BASE ()
 
double GetPixelSizeIu () const
 
void SetPixelSizeIu (double aPixSize)
 
wxImage * GetImageData ()
 
const wxImage * GetImageData () const
 
void SetImage (wxImage *aImage)
 
double GetScale () const
 
void SetScale (double aScale)
 
void RebuildBitmap ()
 
void SetBitmap (wxBitmap *aBitMap)
 
void ImportData (BITMAP_BASE *aItem)
 Copy aItem image to this object and update m_bitmap. More...
 
double GetScalingFactor () const
 This scaling factor depends on m_pixelSizeIu and m_scale. More...
 
VECTOR2I GetSize () const
 
wxSize GetSizePixels () const
 
int GetPPI () const
 
const BOX2I GetBoundingBox () const
 Return the orthogonal, bounding box of this object for display purposes. More...
 
void DrawBitmap (wxDC *aDC, const VECTOR2I &aPos)
 
bool ReadImageFile (const wxString &aFullFilename)
 Reads and stores in memory an image file. More...
 
bool ReadImageFile (wxInputStream &aInStream)
 Reads and stores in memory an image file. More...
 
bool SaveData (FILE *aFile) const
 Write the bitmap data to aFile. More...
 
void SaveData (wxArrayString &aPngStrings) const
 Write the bitmap data to an array string. More...
 
bool LoadData (LINE_READER &aLine, wxString &aErrorMsg)
 Load an image data saved by SaveData. More...
 
void Mirror (bool aVertically)
 Mirror image vertically (i.e. More...
 
void Rotate (bool aRotateCCW)
 Rotate image CW or CCW. More...
 
void PlotImage (PLOTTER *aPlotter, const VECTOR2I &aPos, const KIGFX::COLOR4D &aDefaultColor, int aDefaultPensize) const
 Plot bitmap on plotter. More...
 

Private Attributes

double m_scale
 
wxImage * m_image
 
wxBitmap * m_bitmap
 
double m_pixelSizeIu
 
int m_ppi
 

Detailed Description

This class handle bitmap images in KiCad.

It is not intended to be used alone, but inside another class so all methods are protected or private. It is used in SCH_BITMAP class, DS_DRAW_ITEM_BITMAP, and possibly others in the future.

Warning
Not all plotters are able to plot a bitmap. Mainly GERBER plotters cannot.

Definition at line 50 of file bitmap_base.h.

Constructor & Destructor Documentation

◆ BITMAP_BASE() [1/2]

BITMAP_BASE::BITMAP_BASE ( const VECTOR2I pos = VECTOR2I( 0, 0 ))

Definition at line 35 of file bitmap_base.cpp.

36{
37 m_scale = 1.0; // 1.0 = original bitmap size
38 m_bitmap = nullptr;
39 m_image = nullptr;
40 m_ppi = 300; // the bitmap definition. the default is 300PPI
41 m_pixelSizeIu = 254000.0 / m_ppi; // a pixel size value OK for bitmaps using 300 PPI
42 // for Eeschema which uses currently 254000PPI
43}
wxBitmap * m_bitmap
Definition: bitmap_base.h:239
wxImage * m_image
Definition: bitmap_base.h:238
double m_scale
Definition: bitmap_base.h:236
double m_pixelSizeIu
Definition: bitmap_base.h:240

References m_bitmap, m_image, m_pixelSizeIu, m_ppi, and m_scale.

◆ BITMAP_BASE() [2/2]

BITMAP_BASE::BITMAP_BASE ( const BITMAP_BASE aSchBitmap)

Definition at line 46 of file bitmap_base.cpp.

47{
48 m_scale = aSchBitmap.m_scale;
49 m_ppi = aSchBitmap.m_ppi;
50 m_pixelSizeIu = aSchBitmap.m_pixelSizeIu;
51
52 m_image = nullptr;
53 m_bitmap = nullptr;
54
55 if( aSchBitmap.m_image )
56 {
57 m_image = new wxImage( *aSchBitmap.m_image );
58 m_bitmap = new wxBitmap( *m_image );
59 }
60}

References m_bitmap, m_image, m_pixelSizeIu, m_ppi, and m_scale.

◆ ~BITMAP_BASE()

BITMAP_BASE::~BITMAP_BASE ( )
inline

Definition at line 57 of file bitmap_base.h.

58 {
59 delete m_bitmap;
60 delete m_image;
61 }

References m_bitmap, and m_image.

Member Function Documentation

◆ DrawBitmap()

void BITMAP_BASE::DrawBitmap ( wxDC *  aDC,
const VECTOR2I aPos 
)

Definition at line 224 of file bitmap_base.cpp.

225{
226 if( m_bitmap == nullptr )
227 return;
228
229 VECTOR2I pos = aPos;
230 VECTOR2I size = GetSize();
231
232 // This fixes a bug in OSX that should be fixed in the 3.0.3 version or later.
233 if( ( size.x == 0 ) || ( size.y == 0 ) )
234 return;
235
236 // To draw the bitmap, pos is the upper left corner position
237 pos.x -= size.x / 2;
238 pos.y -= size.y / 2;
239
240 double scale;
241 int logicalOriginX, logicalOriginY;
242 aDC->GetUserScale( &scale, &scale );
243 aDC->GetLogicalOrigin( &logicalOriginX, &logicalOriginY );
244
245 // We already have issues to draw a bitmap on the wxDC, depending on wxWidgets version.
246 // Now we have an issue on wxWidgets 3.1.6 and later to fix the clipboard
247 // and the bitmap position when using TransformMatrix
248 // So for version >= 3.1.6 do not use it
249 // Be carefull before changing the code.
250 bool useTransform = aDC->CanUseTransformMatrix();
251
252 #if wxCHECK_VERSION( 3, 1, 6 )
253 useTransform = false;
254 #endif
255
256 wxAffineMatrix2D init_matrix = aDC->GetTransformMatrix();
257
258 wxPoint clipAreaPos;
259
260 if( useTransform )
261 {
262 wxAffineMatrix2D matrix = aDC->GetTransformMatrix();
263 matrix.Translate( pos.x, pos.y );
264 matrix.Scale( GetScalingFactor(), GetScalingFactor() );
265 aDC->SetTransformMatrix( matrix );
266 // Needed on wx <= 3.1.5, and this is strange...
267 // Nevertheless, this code has problem (the bitmap is not seen)
268 // with wx version > 3.1.5
269 clipAreaPos.x = pos.x;
270 clipAreaPos.y = pos.y;
271
272 pos.x = pos.y = 0;
273 }
274 else
275 {
276 aDC->SetUserScale( scale * GetScalingFactor(), scale * GetScalingFactor() );
277 aDC->SetLogicalOrigin( logicalOriginX / GetScalingFactor(),
278 logicalOriginY / GetScalingFactor() );
279
280 pos.x = KiROUND( pos.x / GetScalingFactor() );
281 pos.y = KiROUND( pos.y / GetScalingFactor() );
282 size.x = KiROUND( size.x / GetScalingFactor() );
283 size.y = KiROUND( size.y / GetScalingFactor() );
284 clipAreaPos.x = pos.x;
285 clipAreaPos.y = pos.y;
286 }
287
288 aDC->DestroyClippingRegion();
289 aDC->SetClippingRegion( clipAreaPos, wxSize( size.x, size.y ) );
290
292 {
293 wxBitmap result( m_bitmap->ConvertToImage().ConvertToGreyscale() );
294 aDC->DrawBitmap( result, pos.x, pos.y, true );
295 }
296 else
297 {
298 aDC->DrawBitmap( *m_bitmap, pos.x, pos.y, true );
299 }
300
301 if( useTransform )
302 aDC->SetTransformMatrix( init_matrix );
303 else
304 {
305 aDC->SetUserScale( scale, scale );
306 aDC->SetLogicalOrigin( logicalOriginX, logicalOriginY );
307 }
308
309 aDC->DestroyClippingRegion();
310}
double GetScalingFactor() const
This scaling factor depends on m_pixelSizeIu and m_scale.
Definition: bitmap_base.h:110
VECTOR2I GetSize() const
bool GetGRForceBlackPenState(void)
Definition: gr_basic.cpp:156
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

References GetGRForceBlackPenState(), GetScalingFactor(), GetSize(), KiROUND(), m_bitmap, scale, VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by PANEL_IMAGE_EDITOR::OnRedrawPanel(), SCH_BITMAP::Print(), and DS_DRAW_ITEM_BITMAP::PrintWsItem().

◆ GetBoundingBox()

const BOX2I BITMAP_BASE::GetBoundingBox ( ) const

Return the orthogonal, bounding box of this object for display purposes.

This box should be an enclosing perimeter for visible components of this object, and the units should be in the pcb or schematic coordinate system. It is OK to overestimate the size by a few counts.

Definition at line 213 of file bitmap_base.cpp.

214{
215 BOX2I bbox;
216 VECTOR2I size = GetSize();
217
218 bbox.Inflate( size.x / 2, size.y / 2 );
219
220 return bbox;
221}
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:506

References GetSize(), BOX2< Vec >::Inflate(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by BOOST_AUTO_TEST_CASE(), and SCH_BITMAP::GetBoundingBox().

◆ GetImageData() [1/2]

◆ GetImageData() [2/2]

const wxImage * BITMAP_BASE::GetImageData ( ) const
inline

Definition at line 70 of file bitmap_base.h.

70{ return m_image; }

References m_image.

◆ GetPixelSizeIu()

double BITMAP_BASE::GetPixelSizeIu ( ) const
inline

Definition at line 66 of file bitmap_base.h.

66{ return m_pixelSizeIu; }

References m_pixelSizeIu.

Referenced by BOOST_AUTO_TEST_CASE().

◆ GetPPI()

int BITMAP_BASE::GetPPI ( ) const
inline

◆ GetScale()

◆ GetScalingFactor()

double BITMAP_BASE::GetScalingFactor ( ) const
inline

This scaling factor depends on m_pixelSizeIu and m_scale.

m_pixelSizeIu gives the scaling factor between a pixel size and the internal units. m_scale is an user dependent value, and gives the "zoom" value.

  • m_scale = 1.0 = original size of bitmap.
  • m_scale < 1.0 = the bitmap is drawn smaller than its original size.
  • m_scale > 1.0 = the bitmap is drawn bigger than its original size.
Returns
The scaling factor from pixel size to actual draw size.

Definition at line 110 of file bitmap_base.h.

111 {
112 return m_pixelSizeIu * m_scale;
113 }

References m_pixelSizeIu, and m_scale.

Referenced by DrawBitmap(), GetSize(), PANEL_IMAGE_EDITOR::OnRedrawPanel(), and PlotImage().

◆ GetSize()

VECTOR2I BITMAP_BASE::GetSize ( ) const
Returns
the actual size (in user units, not in pixels) of the image

Definition at line 313 of file bitmap_base.cpp.

314{
315 VECTOR2I size;
316
317 if( m_bitmap )
318 {
319 size.x = m_bitmap->GetWidth();
320 size.y = m_bitmap->GetHeight();
321
322 size.x = KiROUND( size.x * GetScalingFactor() );
323 size.y = KiROUND( size.y * GetScalingFactor() );
324 }
325
326 return size;
327}

References GetScalingFactor(), KiROUND(), m_bitmap, VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by BOOST_AUTO_TEST_CASE(), DrawBitmap(), GetBoundingBox(), DS_DRAW_ITEM_BITMAP::GetBoundingBox(), PCB_BITMAP::GetBoundingBox(), SCH_BITMAP::GetSize(), and PCB_BITMAP::GetSize().

◆ GetSizePixels()

wxSize BITMAP_BASE::GetSizePixels ( ) const
inline
Returns
the size in pixels of the image

Definition at line 123 of file bitmap_base.h.

124 {
125 if( m_image )
126 return wxSize( m_image->GetWidth(), m_image->GetHeight() );
127 else
128 return wxSize( 0, 0 );
129 }

References m_image.

Referenced by BOOST_AUTO_TEST_CASE(), KIGFX::GL_BITMAP_CACHE::cacheBitmap(), PANEL_IMAGE_EDITOR::CheckValues(), KIGFX::CAIRO_GAL_BASE::DrawBitmap(), KIGFX::OPENGL_GAL::DrawBitmap(), and KIGFX::GL_BITMAP_CACHE::RequestBitmap().

◆ ImportData()

void BITMAP_BASE::ImportData ( BITMAP_BASE aItem)

Copy aItem image to this object and update m_bitmap.

Definition at line 63 of file bitmap_base.cpp.

64{
65 *m_image = *aItem->m_image;
66 *m_bitmap = *aItem->m_bitmap;
67 m_scale = aItem->m_scale;
68 m_ppi = aItem->m_ppi;
70}

References m_bitmap, m_image, m_pixelSizeIu, m_ppi, and m_scale.

Referenced by PANEL_IMAGE_EDITOR::TransferToImage().

◆ LoadData()

bool BITMAP_BASE::LoadData ( LINE_READER aLine,
wxString &  aErrorMsg 
)

Load an image data saved by SaveData.

The file format must be png format in hexadecimal.

Parameters
aLinethe LINE_READER used to read the data file.
aErrorMsgDescription of the error if an error occurs while loading the png bitmap data.
Returns
true if the bitmap loaded successfully.

Definition at line 167 of file bitmap_base.cpp.

168{
169 wxMemoryOutputStream stream;
170 char* line;
171
172 while( true )
173 {
174 if( !aLine.ReadLine() )
175 {
176 aErrorMsg = wxT("Unexpected end of data");
177 return false;
178 }
179
180 line = aLine.Line();
181
182 if( strncasecmp( line, "EndData", 4 ) == 0 )
183 {
184 // all the PNG date is read.
185 // We expect here m_image and m_bitmap are void
186 m_image = new wxImage();
187 wxMemoryInputStream istream( stream );
188 m_image->LoadFile( istream, wxBITMAP_TYPE_PNG );
189 m_bitmap = new wxBitmap( *m_image );
190 break;
191 }
192
193 // Read PNG data, stored in hexadecimal,
194 // each byte = 2 hexadecimal digits and a space between 2 bytes
195 // and put it in memory stream buffer
196 int len = strlen( line );
197
198 for( ; len > 0; len -= 3, line += 3 )
199 {
200 int value = 0;
201
202 if( sscanf( line, "%X", &value ) == 1 )
203 stream.PutC( (char) value );
204 else
205 break;
206 }
207 }
208
209 return true;
210}
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

References LINE_READER::Line(), m_bitmap, m_image, and LINE_READER::ReadLine().

Referenced by DRAWING_SHEET_PARSER::readPngdata().

◆ Mirror()

void BITMAP_BASE::Mirror ( bool  aVertically)

Mirror image vertically (i.e.

relative to its horizontal X axis ) or horizontally (i.e relative to its vertical Y axis).

Parameters
aVerticallyfalse to mirror horizontally or true to mirror vertically.

Definition at line 330 of file bitmap_base.cpp.

331{
332 if( m_image )
333 {
334 *m_image = m_image->Mirror( not aVertically );
336 }
337}
void RebuildBitmap()
Definition: bitmap_base.h:86

References m_image, and RebuildBitmap().

Referenced by PCB_BITMAP::Flip(), SCH_BITMAP::MirrorHorizontally(), and SCH_BITMAP::MirrorVertically().

◆ PlotImage()

void BITMAP_BASE::PlotImage ( PLOTTER aPlotter,
const VECTOR2I aPos,
const KIGFX::COLOR4D aDefaultColor,
int  aDefaultPensize 
) const

Plot bitmap on plotter.

If the plotter does not support bitmaps, plot a

Parameters
aPlotterthe plotter to use.
aPosthe position of the center of the bitmap.
aDefaultColorthe color used to plot the rectangle when bitmap is not supported.
aDefaultPensizethe pen size used to plot the rectangle when bitmap is not supported.

Definition at line 350 of file bitmap_base.cpp.

353{
354 if( m_image == nullptr )
355 return;
356
357 // These 2 lines are useful only for plotters that cannot plot a bitmap
358 // and plot a rectangle instead of.
359 aPlotter->SetColor( aDefaultColor );
360 aPlotter->SetCurrentLineWidth( aDefaultPensize );
361 aPlotter->PlotImage( *m_image, aPos, GetScalingFactor() );
362}
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

References GetScalingFactor(), m_image, PLOTTER::PlotImage(), PLOTTER::SetColor(), and PLOTTER::SetCurrentLineWidth().

Referenced by SCH_BITMAP::Plot(), and PlotDrawingSheet().

◆ ReadImageFile() [1/2]

bool BITMAP_BASE::ReadImageFile ( const wxString &  aFullFilename)

Reads and stores in memory an image file.

Initialize the bitmap format used to draw this item. Supported images formats are format supported by wxImage if all handlers are loaded. By default, .png, .jpeg are always loaded.

Parameters
aFullFilenameThe full filename of the image file to read.
Returns
true if success reading else false.

Definition at line 88 of file bitmap_base.cpp.

89{
90 wxImage* new_image = new wxImage();
91
92 if( !new_image->LoadFile( aFullFilename ) )
93 {
94 delete new_image;
95 return false;
96 }
97
98 delete m_image;
99 m_image = new_image;
100 m_bitmap = new wxBitmap( *m_image );
101
102 return true;
103}

References m_bitmap, and m_image.

Referenced by SCH_BITMAP::ReadImageFile(), PCB_BITMAP::ReadImageFile(), and TEST_BITMAP_BASE_FIXTURE::TEST_BITMAP_BASE_FIXTURE().

◆ ReadImageFile() [2/2]

bool BITMAP_BASE::ReadImageFile ( wxInputStream &  aInStream)

Reads and stores in memory an image file.

Initialize the bitmap format used to draw this item.

Supported images formats are format supported by wxImage if all handlers are loaded. By default, .png, .jpeg are always loaded.

Parameters
aInStreaman input stream containing the file data.
Returns
true if success reading else false.

Definition at line 73 of file bitmap_base.cpp.

74{
75 std::unique_ptr<wxImage> new_image = std::make_unique<wxImage>();
76
77 if( !new_image->LoadFile( aInStream ) )
78 return false;
79
80 delete m_image;
81 m_image = new_image.release();
82 m_bitmap = new wxBitmap( *m_image );
83
84 return true;
85}

References m_bitmap, and m_image.

◆ RebuildBitmap()

void BITMAP_BASE::RebuildBitmap ( )
inline

Definition at line 86 of file bitmap_base.h.

86{ *m_bitmap = wxBitmap( *m_image ); }

References m_bitmap, and m_image.

Referenced by Mirror(), PANEL_IMAGE_EDITOR::OnGreyScaleConvert(), and Rotate().

◆ Rotate()

void BITMAP_BASE::Rotate ( bool  aRotateCCW)

Rotate image CW or CCW.

Parameters
aRotateCCWtrue to rotate CCW or false to rotate CW.

Definition at line 340 of file bitmap_base.cpp.

341{
342 if( m_image )
343 {
344 *m_image = m_image->Rotate90( aRotateCCW );
346 }
347}

References m_image, and RebuildBitmap().

Referenced by SCH_BITMAP::Rotate(), and PCB_BITMAP::Rotate().

◆ SaveData() [1/2]

bool BITMAP_BASE::SaveData ( FILE *  aFile) const

Write the bitmap data to aFile.

The file format is png, in hexadecimal form. If the hexadecimal data is converted to binary it gives exactly a .png image data.

Parameters
aFileThe FILE to write to.
Returns
true if success writing else false.

Definition at line 106 of file bitmap_base.cpp.

107{
108 if( m_image )
109 {
110 wxMemoryOutputStream stream;
111 m_image->SaveFile( stream, wxBITMAP_TYPE_PNG );
112
113 // Write binary data in hexadecimal form (ASCII)
114 wxStreamBuffer* buffer = stream.GetOutputStreamBuffer();
115 char* begin = (char*) buffer->GetBufferStart();
116
117 for( int ii = 0; begin < buffer->GetBufferEnd(); begin++, ii++ )
118 {
119 if( ii >= 32 )
120 {
121 ii = 0;
122
123 if( fprintf( aFile, "\n" ) == EOF )
124 return false;
125 }
126
127 if( fprintf( aFile, "%2.2X ", *begin & 0xFF ) == EOF )
128 return false;
129 }
130 }
131
132 return true;
133}

References m_image.

Referenced by DS_DATA_MODEL_IO::format().

◆ SaveData() [2/2]

void BITMAP_BASE::SaveData ( wxArrayString &  aPngStrings) const

Write the bitmap data to an array string.

The format is png, in Hexadecimal form. If the hexadecimal data is converted to binary it gives exactly a .png image data.

Parameters
aPngStringsThe wxArrayString to write to.

Definition at line 136 of file bitmap_base.cpp.

137{
138 if( m_image )
139 {
140 wxMemoryOutputStream stream;
141 m_image->SaveFile( stream, wxBITMAP_TYPE_PNG );
142
143 // Write binary data in hexadecimal form (ASCII)
144 wxStreamBuffer* buffer = stream.GetOutputStreamBuffer();
145 char* begin = (char*) buffer->GetBufferStart();
146 wxString line;
147
148 for( int ii = 0; begin < buffer->GetBufferEnd(); begin++, ii++ )
149 {
150 if( ii >= 32 )
151 {
152 ii = 0;
153 aPngStrings.Add( line );
154 line.Empty();
155 }
156
157 line << wxString::Format( wxT( "%2.2X " ), *begin & 0xFF );
158 }
159
160 // Add last line:
161 if( !line.IsEmpty() )
162 aPngStrings.Add( line );
163 }
164}
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

References Format(), and m_image.

◆ SetBitmap()

void BITMAP_BASE::SetBitmap ( wxBitmap *  aBitMap)
inline

Definition at line 88 of file bitmap_base.h.

89 {
90 delete m_bitmap;
91 m_bitmap = aBitMap;
92 }

References m_bitmap.

◆ SetImage()

void BITMAP_BASE::SetImage ( wxImage *  aImage)
inline

Definition at line 72 of file bitmap_base.h.

73 {
74 delete m_image;
75 m_image = aImage;
76 }

References m_image.

◆ SetPixelSizeIu()

◆ SetScale()

Member Data Documentation

◆ m_bitmap

wxBitmap* BITMAP_BASE::m_bitmap
private

◆ m_image

◆ m_pixelSizeIu

double BITMAP_BASE::m_pixelSizeIu
private

◆ m_ppi

int BITMAP_BASE::m_ppi
private

Definition at line 244 of file bitmap_base.h.

Referenced by BITMAP_BASE(), GetPPI(), and ImportData().

◆ m_scale

double BITMAP_BASE::m_scale
private

Definition at line 236 of file bitmap_base.h.

Referenced by BITMAP_BASE(), GetScale(), GetScalingFactor(), ImportData(), and SetScale().


The documentation for this class was generated from the following files: