KiCad PCB EDA Suite
BITMAP_BASE Class Reference

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

#include <class_bitmap_base.h>

Public Member Functions

 BITMAP_BASE (const wxPoint &pos=wxPoint(0, 0))
 
 BITMAP_BASE (const BITMAP_BASE &aSchBitmap)
 
 ~BITMAP_BASE ()
 
double GetPixelScaleFactor () const
 
void SetPixelScaleFactor (double aSF)
 
wxImage * GetImageData ()
 
void SetImage (wxImage *aImage)
 
double GetScale () const
 
void SetScale (double aScale)
 
void RebuildBitmap ()
 
void SetBitmap (wxBitmap *aBitMap)
 
void ImportData (BITMAP_BASE *aItem)
 Function ImportData Copy aItem image to me and update m_bitmap. More...
 
double GetScalingFactor () const
 Function GetScalingFactor. More...
 
wxSize GetSize () const
 Function GetSize. More...
 
wxSize GetSizePixels () const
 Function GetSizePixels. More...
 
int GetPPI () const
 
const EDA_RECT GetBoundingBox () const
 Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes. More...
 
void DrawBitmap (EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aPos)
 
bool ReadImageFile (const wxString &aFullFilename)
 Function ReadImageFile Reads and stores in memory an image file. More...
 
bool SaveData (FILE *aFile) const
 writes the bitmap data to aFile The format is png, in Hexadecimal form: If the hexadecimal data is converted to binary it gives exactly a .png image data More...
 
void SaveData (wxArrayString &aPngStrings) const
 writes 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 More...
 
bool LoadData (LINE_READER &aLine, wxString &aErrorMsg)
 Load an image data saved by SaveData (png, in Hexadecimal form) More...
 
void Mirror (bool aVertically)
 Function Mirror Mirror image vertically (i.e. More...
 
void Rotate (bool aRotateCCW)
 Function Rotate Rotate image CW or CCW. More...
 
void PlotImage (PLOTTER *aPlotter, const wxPoint &aPos, COLOR4D aDefaultColor, int aDefaultPensize)
 Function PlotImage Plot bitmap on plotter. More...
 

Private Attributes

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

Detailed Description

This class handle bitmap images in KiCad.

It is not intended to be used alone, but inside an other class, so all methods are protected ( or private ) It is used in SCH_BITMAP class and WS_DRAW_ITEM_BITMAP (and other in future)

Remember not all plotters are able to plot a bitmap Mainly GERBER plotters cannot.

Definition at line 44 of file class_bitmap_base.h.

Constructor & Destructor Documentation

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

Definition at line 47 of file class_bitmap_base.cpp.

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

48 {
49  m_scale = 1.0; // 1.0 = original bitmap size
50  m_bitmap = NULL;
51  m_image = NULL;
52  m_ppi = 300; // the bitmap definition. the default is 300PPI
53  m_pixelScaleFactor = 1000.0 / m_ppi; // a value OK for bitmaps using 300 PPI
54  // for Eeschema which uses currently 1000PPI
55 }
wxBitmap * m_bitmap
double m_pixelScaleFactor
wxImage * m_image
BITMAP_BASE::BITMAP_BASE ( const BITMAP_BASE aSchBitmap)

Definition at line 58 of file class_bitmap_base.cpp.

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

59 {
60  m_scale = aSchBitmap.m_scale;
61  m_ppi = aSchBitmap.m_ppi;
63  m_image = new wxImage( *aSchBitmap.m_image );
64  m_bitmap = new wxBitmap( *m_image );
65 }
wxBitmap * m_bitmap
double m_pixelScaleFactor
wxImage * m_image
BITMAP_BASE::~BITMAP_BASE ( )
inline

Definition at line 63 of file class_bitmap_base.h.

References m_bitmap, and m_image.

64  {
65  delete m_bitmap;
66  delete m_image;
67  }
wxBitmap * m_bitmap
wxImage * m_image

Member Function Documentation

void BITMAP_BASE::DrawBitmap ( EDA_DRAW_PANEL aPanel,
wxDC *  aDC,
const wxPoint aPos 
)

Definition at line 219 of file class_bitmap_base.cpp.

References GetScalingFactor(), GetSize(), KiROUND(), m_bitmap, scale, wxPoint::x, and wxPoint::y.

Referenced by SCH_BITMAP::Draw(), WS_DRAW_ITEM_BITMAP::DrawWsItem(), and DIALOG_IMAGE_EDITOR::OnRedrawPanel().

220 {
221  if( m_bitmap == NULL )
222  return;
223 
224  wxPoint pos = aPos;
225  wxSize size = GetSize();
226 
227  // This fixes a bug in OSX that should be fixed in the 3.0.3 version or later.
228  if( ( size.x == 0 ) || ( size.y == 0 ) )
229  return;
230 
231  // To draw the bitmap, pos is the upper left corner position
232  pos.x -= size.x / 2;
233  pos.y -= size.y / 2;
234 
235  double scale;
236  int logicalOriginX, logicalOriginY;
237  aDC->GetUserScale( &scale, &scale );
238  aDC->GetLogicalOrigin( &logicalOriginX, &logicalOriginY );
239  aDC->SetUserScale( scale * GetScalingFactor(), scale * GetScalingFactor() );
240  aDC->SetLogicalOrigin( logicalOriginX / GetScalingFactor(),
241  logicalOriginY / GetScalingFactor() );
242  aDC->DrawBitmap( *m_bitmap,
243  KiROUND( pos.x / GetScalingFactor() ),
244  KiROUND( pos.y / GetScalingFactor() ),
245  true );
246  aDC->SetUserScale( scale, scale );
247  aDC->SetLogicalOrigin( logicalOriginX, logicalOriginY );
248 }
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:106
wxSize GetSize() const
Function GetSize.
wxBitmap * m_bitmap
const int scale
double GetScalingFactor() const
Function GetScalingFactor.
const EDA_RECT BITMAP_BASE::GetBoundingBox ( ) const

Function GetBoundingBox returns 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 207 of file class_bitmap_base.cpp.

References GetSize(), and EDA_RECT::Inflate().

Referenced by SCH_BITMAP::GetBoundingBox(), and WS_DRAW_ITEM_BITMAP::HitTest().

208 {
209  EDA_RECT rect;
210 
211  wxSize size = GetSize();
212 
213  rect.Inflate( size.x / 2, size.y / 2 );
214 
215  return rect;
216 }
wxSize GetSize() const
Function GetSize.
Class EDA_RECT handles the component boundary box.
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
wxImage* BITMAP_BASE::GetImageData ( )
inline
double BITMAP_BASE::GetPixelScaleFactor ( ) const
inline

Definition at line 73 of file class_bitmap_base.h.

References m_pixelScaleFactor.

Referenced by SCH_BITMAP::GetPixelScaleFactor().

73 { return m_pixelScaleFactor; }
double m_pixelScaleFactor
int BITMAP_BASE::GetPPI ( ) const
inline
Returns
the bitmap definition in ppi the default is 300 ppi

Definition at line 143 of file class_bitmap_base.h.

References m_ppi.

Referenced by WORKSHEET_DATAITEM_BITMAP::GetPPI(), WORKSHEET_DATAITEM_BITMAP::SetPixelScaleFactor(), and WORKSHEET_DATAITEM_BITMAP::SetPPI().

144  {
145  return m_ppi;
146  }
double BITMAP_BASE::GetScale ( ) const
inline
double BITMAP_BASE::GetScalingFactor ( ) const
inline

Function GetScalingFactor.

Returns
the scaling factor from pixel size to actual draw size this scaling factor depend on m_pixelScaleFactor and m_scale m_pixelScaleFactor gives the scaling factor between a pixel size and the internal schematic units m_scale is an user dependant 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.

Definition at line 115 of file class_bitmap_base.h.

References m_scale.

Referenced by DrawBitmap(), SCH_BITMAP::GetScalingFactor(), GetSize(), DIALOG_IMAGE_EDITOR::OnRedrawPanel(), and PlotImage().

116  {
117  return m_pixelScaleFactor * m_scale;
118  }
double m_pixelScaleFactor
wxSize BITMAP_BASE::GetSize ( ) const

Function GetSize.

Returns
the actual size (in user units, not in pixels) of the image

Definition at line 251 of file class_bitmap_base.cpp.

References GetScalingFactor(), KiROUND(), and m_bitmap.

Referenced by SCH_BITMAP::Draw(), DrawBitmap(), GetBoundingBox(), and SCH_BITMAP::GetSize().

252 {
253  wxSize size;
254 
255  if( m_bitmap )
256  {
257  size.x = m_bitmap->GetWidth();
258  size.y = m_bitmap->GetHeight();
259 
260  size.x = KiROUND( size.x * GetScalingFactor() );
261  size.y = KiROUND( size.y * GetScalingFactor() );
262  }
263 
264  return size;
265 }
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:106
wxBitmap * m_bitmap
double GetScalingFactor() const
Function GetScalingFactor.
wxSize BITMAP_BASE::GetSizePixels ( ) const
inline

Function GetSizePixels.

Returns
the size in pixels of the image

Definition at line 131 of file class_bitmap_base.h.

Referenced by DIALOG_IMAGE_EDITOR::CheckValues(), and DIALOG_IMAGE_EDITOR::OnHalfSize().

132  {
133  if( m_image )
134  return wxSize( m_image->GetWidth(), m_image->GetHeight() );
135  else
136  return wxSize( 0, 0 );
137  }
wxImage * m_image
void BITMAP_BASE::ImportData ( BITMAP_BASE aItem)

Function ImportData Copy aItem image to me and update m_bitmap.

Definition at line 72 of file class_bitmap_base.cpp.

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

Referenced by DIALOG_IMAGE_EDITOR::TransfertToImage().

73 {
74  *m_image = *aItem->m_image;
75  *m_bitmap = *aItem->m_bitmap;
76  m_scale = aItem->m_scale;
77  m_ppi = aItem->m_ppi;
79 }
wxBitmap * m_bitmap
double m_pixelScaleFactor
wxImage * m_image
bool BITMAP_BASE::LoadData ( LINE_READER aLine,
wxString &  aErrorMsg 
)

Load an image data saved by SaveData (png, in Hexadecimal form)

Parameters
aLine- the LINE_READER used to read the data file.
aErrorMsg- Description of the error if an error occurs while loading the png bimap data.
Returns
true if the bitmap loaded successfully.

Definition at line 161 of file class_bitmap_base.cpp.

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

Referenced by PAGE_LAYOUT_READER_PARSER::readPngdata().

162 {
163  wxMemoryOutputStream stream;
164  char* line;
165 
166  while( true )
167  {
168  if( !aLine.ReadLine() )
169  {
170  aErrorMsg = wxT("Unexpected end of data");
171  return false;
172  }
173 
174  line = aLine.Line();
175 
176  if( strncasecmp( line, "EndData", 4 ) == 0 )
177  {
178  // all the PNG date is read.
179  // We expect here m_image and m_bitmap are void
180  m_image = new wxImage();
181  wxMemoryInputStream istream( stream );
182  m_image->LoadFile( istream, wxBITMAP_TYPE_PNG );
183  m_bitmap = new wxBitmap( *m_image );
184  break;
185  }
186 
187  // Read PNG data, stored in hexadecimal,
188  // each byte = 2 hexadecimal digits and a space between 2 bytes
189  // and put it in memory stream buffer
190  int len = strlen( line );
191 
192  for( ; len > 0; len -= 3, line += 3 )
193  {
194  int value = 0;
195 
196  if( sscanf( line, "%X", &value ) == 1 )
197  stream.PutC( (char) value );
198  else
199  break;
200  }
201  }
202 
203  return true;
204 }
char * Line() const
Function Line returns a pointer to the last line that was read in.
Definition: richio.h:139
wxBitmap * m_bitmap
virtual char * ReadLine()=0
Function ReadLine reads a line of text into the buffer and increments the line number counter...
wxImage * m_image
void BITMAP_BASE::Mirror ( bool  aVertically)

Function Mirror Mirror image vertically (i.e.

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

Parameters
aVertically= false to mirror horizontally or true to mirror vertically

Definition at line 268 of file class_bitmap_base.cpp.

References m_image, and RebuildBitmap().

Referenced by SCH_BITMAP::MirrorX(), SCH_BITMAP::MirrorY(), DIALOG_IMAGE_EDITOR::OnMirrorX_click(), and DIALOG_IMAGE_EDITOR::OnMirrorY_click().

269 {
270  if( m_image )
271  {
272  *m_image = m_image->Mirror( not aVertically );
273  RebuildBitmap();
274  }
275 }
void RebuildBitmap()
wxImage * m_image
void BITMAP_BASE::PlotImage ( PLOTTER aPlotter,
const wxPoint aPos,
COLOR4D  aDefaultColor,
int  aDefaultPensize 
)

Function PlotImage Plot bitmap on plotter.

If the plotter does not support bitmaps, plot a

Parameters
aPlotter= the plotter to use
aPos= the position od the center of the bitmap
aDefaultColor= the color used to plot the rectangle when bitmap is not supported
aDefaultPensize= the pen size used to plot the rectangle when bitmap is not supported

Definition at line 288 of file class_bitmap_base.cpp.

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

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

292 {
293  if( m_image == NULL )
294  return;
295 
296  // These 2 lines are useful only for plotters that cannot plot a bitmap
297  // and plot a rectangle instead of.
298  aPlotter->SetColor( aDefaultColor );
299  aPlotter->SetCurrentLineWidth( aDefaultPensize );
300  aPlotter->PlotImage( *m_image, aPos, GetScalingFactor() );
301 }
virtual void SetColor(COLOR4D color)=0
virtual void PlotImage(const wxImage &aImage, const wxPoint &aPos, double aScaleFactor)
Function PlotImage Only Postscript plotters can plot bitmaps for plotters that cannot plot a bitmap...
double GetScalingFactor() const
Function GetScalingFactor.
wxImage * m_image
virtual void SetCurrentLineWidth(int width, void *aData=NULL)=0
Set the line width for the next drawing.
bool BITMAP_BASE::ReadImageFile ( const wxString &  aFullFilename)

Function ReadImageFile Reads and stores in memory an image file.

Init 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 alway loaded

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

Definition at line 82 of file class_bitmap_base.cpp.

References m_bitmap, and m_image.

Referenced by PL_EDITOR_FRAME::AddPageLayoutItem(), and SCH_BITMAP::ReadImageFile().

83 {
84  wxImage* new_image = new wxImage();
85 
86  if( !new_image->LoadFile( aFullFilename ) )
87  {
88  delete new_image;
89  return false;
90  }
91 
92  delete m_image;
93  m_image = new_image;
94  m_bitmap = new wxBitmap( *m_image );
95 
96  return true;
97 }
wxBitmap * m_bitmap
wxImage * m_image
void BITMAP_BASE::RebuildBitmap ( )
inline

Definition at line 90 of file class_bitmap_base.h.

Referenced by Mirror(), DIALOG_IMAGE_EDITOR::OnGreyScaleConvert(), DIALOG_IMAGE_EDITOR::OnHalfSize(), and Rotate().

90 { *m_bitmap = wxBitmap( *m_image ); }
wxBitmap * m_bitmap
wxImage * m_image
void BITMAP_BASE::Rotate ( bool  aRotateCCW)

Function Rotate Rotate image CW or CCW.

Parameters
aRotateCCW= true to rotate CCW

Definition at line 278 of file class_bitmap_base.cpp.

References m_image, and RebuildBitmap().

Referenced by DIALOG_IMAGE_EDITOR::OnRotateClick(), and SCH_BITMAP::Rotate().

279 {
280  if( m_image )
281  {
282  *m_image = m_image->Rotate90( aRotateCCW );
283  RebuildBitmap();
284  }
285 }
void RebuildBitmap()
wxImage * m_image
bool BITMAP_BASE::SaveData ( FILE *  aFile) const

writes the bitmap data to aFile The 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
bool - true if success writing else false.

Definition at line 100 of file class_bitmap_base.cpp.

References m_image.

Referenced by WORKSHEET_LAYOUT_IO::format().

101 {
102  if( m_image )
103  {
104  wxMemoryOutputStream stream;
105  m_image->SaveFile( stream, wxBITMAP_TYPE_PNG );
106 
107  // Write binary data in hexadecimal form (ASCII)
108  wxStreamBuffer* buffer = stream.GetOutputStreamBuffer();
109  char* begin = (char*) buffer->GetBufferStart();
110 
111  for( int ii = 0; begin < buffer->GetBufferEnd(); begin++, ii++ )
112  {
113  if( ii >= 32 )
114  {
115  ii = 0;
116 
117  if( fprintf( aFile, "\n" ) == EOF )
118  return false;
119  }
120 
121  if( fprintf( aFile, "%2.2X ", *begin & 0xFF ) == EOF )
122  return false;
123  }
124  }
125 
126  return true;
127 }
wxImage * m_image
void BITMAP_BASE::SaveData ( wxArrayString &  aPngStrings) const

writes 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 130 of file class_bitmap_base.cpp.

References Format(), and m_image.

131 {
132  if( m_image )
133  {
134  wxMemoryOutputStream stream;
135  m_image->SaveFile( stream, wxBITMAP_TYPE_PNG );
136 
137  // Write binary data in hexadecimal form (ASCII)
138  wxStreamBuffer* buffer = stream.GetOutputStreamBuffer();
139  char* begin = (char*) buffer->GetBufferStart();
140  wxString line;
141 
142  for( int ii = 0; begin < buffer->GetBufferEnd(); begin++, ii++ )
143  {
144  if( ii >= 32 )
145  {
146  ii = 0;
147  aPngStrings.Add( line );
148  line.Empty();
149  }
150 
151  line << wxString::Format( wxT( "%2.2X " ), *begin & 0xFF );
152  }
153 
154  // Add last line:
155  if( !line.IsEmpty() )
156  aPngStrings.Add( line );
157  }
158 }
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
wxImage * m_image
void BITMAP_BASE::SetBitmap ( wxBitmap *  aBitMap)
inline

Definition at line 92 of file class_bitmap_base.h.

References m_bitmap.

93  {
94  delete m_bitmap;
95  m_bitmap = aBitMap;
96  }
wxBitmap * m_bitmap
void BITMAP_BASE::SetImage ( wxImage *  aImage)
inline

Definition at line 76 of file class_bitmap_base.h.

References m_image.

77  {
78  delete m_image;
79  m_image = aImage;
80  }
wxImage * m_image
void BITMAP_BASE::SetPixelScaleFactor ( double  aSF)
inline
void BITMAP_BASE::SetScale ( double  aScale)
inline

Member Data Documentation

wxBitmap* BITMAP_BASE::m_bitmap
private
double BITMAP_BASE::m_pixelScaleFactor
private

Definition at line 51 of file class_bitmap_base.h.

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

int BITMAP_BASE::m_ppi
private

Definition at line 55 of file class_bitmap_base.h.

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

double BITMAP_BASE::m_scale
private

Definition at line 47 of file class_bitmap_base.h.

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


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