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 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 (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 another 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 46 of file bitmap_base.h.

Constructor & Destructor Documentation

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

Definition at line 46 of file bitmap_base.cpp.

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

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

Definition at line 57 of file bitmap_base.cpp.

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

58 {
59  m_scale = aSchBitmap.m_scale;
60  m_ppi = aSchBitmap.m_ppi;
62  m_image = new wxImage( *aSchBitmap.m_image );
63  m_bitmap = new wxBitmap( *m_image );
64 }
double m_scale
Definition: bitmap_base.h:49
wxBitmap * m_bitmap
Definition: bitmap_base.h:52
double m_pixelScaleFactor
Definition: bitmap_base.h:53
wxImage * m_image
Definition: bitmap_base.h:51
BITMAP_BASE::~BITMAP_BASE ( )
inline

Definition at line 65 of file bitmap_base.h.

References m_bitmap, and m_image.

66  {
67  delete m_bitmap;
68  delete m_image;
69  }
wxBitmap * m_bitmap
Definition: bitmap_base.h:52
wxImage * m_image
Definition: bitmap_base.h:51

Member Function Documentation

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

Definition at line 218 of file bitmap_base.cpp.

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

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

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

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

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

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

Definition at line 77 of file bitmap_base.h.

References m_image.

Referenced by DIALOG_IMAGE_EDITOR::OnGreyScaleConvert(), DIALOG_IMAGE_EDITOR::OnHalfSize(), and SCH_LEGACY_PLUGIN::saveBitmap().

77 { return m_image; }
wxImage * m_image
Definition: bitmap_base.h:51
double BITMAP_BASE::GetPixelScaleFactor ( ) const
inline

Definition at line 75 of file bitmap_base.h.

References m_pixelScaleFactor.

Referenced by SCH_BITMAP::GetPixelScaleFactor().

75 { return m_pixelScaleFactor; }
double m_pixelScaleFactor
Definition: bitmap_base.h:53
int BITMAP_BASE::GetPPI ( ) const
inline
Returns
the bitmap definition in ppi the default is 300 ppi

Definition at line 145 of file bitmap_base.h.

References DrawBitmap(), GetBoundingBox(), LoadData(), m_ppi, Mirror(), PlotImage(), ReadImageFile(), Rotate(), and SaveData().

Referenced by KIGFX::CAIRO_GAL_BASE::DrawBitmap(), and KIGFX::OPENGL_GAL::DrawBitmap().

146  {
147  return m_ppi;
148  }
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 117 of file bitmap_base.h.

References GetSize(), and m_scale.

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

118  {
119  return m_pixelScaleFactor * m_scale;
120  }
double m_scale
Definition: bitmap_base.h:49
double m_pixelScaleFactor
Definition: bitmap_base.h:53
wxSize BITMAP_BASE::GetSize ( ) const

Function GetSize.

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

Definition at line 250 of file bitmap_base.cpp.

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

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

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

Function GetSizePixels.

Returns
the size in pixels of the image

Definition at line 133 of file bitmap_base.h.

Referenced by KIGFX::GL_BITMAP_CACHE::cacheBitmap(), DIALOG_IMAGE_EDITOR::CheckValues(), KIGFX::CAIRO_GAL_BASE::DrawBitmap(), KIGFX::OPENGL_GAL::DrawBitmap(), and DIALOG_IMAGE_EDITOR::OnHalfSize().

134  {
135  if( m_image )
136  return wxSize( m_image->GetWidth(), m_image->GetHeight() );
137  else
138  return wxSize( 0, 0 );
139  }
wxImage * m_image
Definition: bitmap_base.h:51
void BITMAP_BASE::ImportData ( BITMAP_BASE aItem)

Function ImportData Copy aItem image to me and update m_bitmap.

Definition at line 71 of file bitmap_base.cpp.

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

Referenced by SetBitmap(), and DIALOG_IMAGE_EDITOR::TransfertToImage().

72 {
73  *m_image = *aItem->m_image;
74  *m_bitmap = *aItem->m_bitmap;
75  m_scale = aItem->m_scale;
76  m_ppi = aItem->m_ppi;
78 }
double m_scale
Definition: bitmap_base.h:49
wxBitmap * m_bitmap
Definition: bitmap_base.h:52
double m_pixelScaleFactor
Definition: bitmap_base.h:53
wxImage * m_image
Definition: bitmap_base.h:51
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 160 of file bitmap_base.cpp.

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

Referenced by GetPPI(), and PAGE_LAYOUT_READER_PARSER::readPngdata().

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

References m_image, and RebuildBitmap().

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

268 {
269  if( m_image )
270  {
271  *m_image = m_image->Mirror( not aVertically );
272  RebuildBitmap();
273  }
274 }
void RebuildBitmap()
Definition: bitmap_base.h:92
wxImage * m_image
Definition: bitmap_base.h:51
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 287 of file bitmap_base.cpp.

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

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

291 {
292  if( m_image == NULL )
293  return;
294 
295  // These 2 lines are useful only for plotters that cannot plot a bitmap
296  // and plot a rectangle instead of.
297  aPlotter->SetColor( aDefaultColor );
298  aPlotter->SetCurrentLineWidth( aDefaultPensize );
299  aPlotter->PlotImage( *m_image, aPos, GetScalingFactor() );
300 }
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...
Definition: plotter.cpp:181
double GetScalingFactor() const
Function GetScalingFactor.
Definition: bitmap_base.h:117
wxImage * m_image
Definition: bitmap_base.h:51
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 81 of file bitmap_base.cpp.

References m_bitmap, and m_image.

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

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

Definition at line 92 of file bitmap_base.h.

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

92 { *m_bitmap = wxBitmap( *m_image ); }
wxBitmap * m_bitmap
Definition: bitmap_base.h:52
wxImage * m_image
Definition: bitmap_base.h:51
void BITMAP_BASE::Rotate ( bool  aRotateCCW)

Function Rotate Rotate image CW or CCW.

Parameters
aRotateCCW= true to rotate CCW

Definition at line 277 of file bitmap_base.cpp.

References m_image, and RebuildBitmap().

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

278 {
279  if( m_image )
280  {
281  *m_image = m_image->Rotate90( aRotateCCW );
282  RebuildBitmap();
283  }
284 }
void RebuildBitmap()
Definition: bitmap_base.h:92
wxImage * m_image
Definition: bitmap_base.h:51
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 99 of file bitmap_base.cpp.

References m_image.

Referenced by WORKSHEET_LAYOUT_IO::format(), and GetPPI().

100 {
101  if( m_image )
102  {
103  wxMemoryOutputStream stream;
104  m_image->SaveFile( stream, wxBITMAP_TYPE_PNG );
105 
106  // Write binary data in hexadecimal form (ASCII)
107  wxStreamBuffer* buffer = stream.GetOutputStreamBuffer();
108  char* begin = (char*) buffer->GetBufferStart();
109 
110  for( int ii = 0; begin < buffer->GetBufferEnd(); begin++, ii++ )
111  {
112  if( ii >= 32 )
113  {
114  ii = 0;
115 
116  if( fprintf( aFile, "\n" ) == EOF )
117  return false;
118  }
119 
120  if( fprintf( aFile, "%2.2X ", *begin & 0xFF ) == EOF )
121  return false;
122  }
123  }
124 
125  return true;
126 }
wxImage * m_image
Definition: bitmap_base.h:51
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 129 of file bitmap_base.cpp.

References Format(), and m_image.

130 {
131  if( m_image )
132  {
133  wxMemoryOutputStream stream;
134  m_image->SaveFile( stream, wxBITMAP_TYPE_PNG );
135 
136  // Write binary data in hexadecimal form (ASCII)
137  wxStreamBuffer* buffer = stream.GetOutputStreamBuffer();
138  char* begin = (char*) buffer->GetBufferStart();
139  wxString line;
140 
141  for( int ii = 0; begin < buffer->GetBufferEnd(); begin++, ii++ )
142  {
143  if( ii >= 32 )
144  {
145  ii = 0;
146  aPngStrings.Add( line );
147  line.Empty();
148  }
149 
150  line << wxString::Format( wxT( "%2.2X " ), *begin & 0xFF );
151  }
152 
153  // Add last line:
154  if( !line.IsEmpty() )
155  aPngStrings.Add( line );
156  }
157 }
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
Definition: bitmap_base.h:51
void BITMAP_BASE::SetBitmap ( wxBitmap *  aBitMap)
inline

Definition at line 94 of file bitmap_base.h.

References ImportData(), and m_bitmap.

95  {
96  delete m_bitmap;
97  m_bitmap = aBitMap;
98  }
wxBitmap * m_bitmap
Definition: bitmap_base.h:52
void BITMAP_BASE::SetImage ( wxImage *  aImage)
inline

Definition at line 78 of file bitmap_base.h.

References m_image.

79  {
80  delete m_image;
81  m_image = aImage;
82  }
wxImage * m_image
Definition: bitmap_base.h:51
void BITMAP_BASE::SetPixelScaleFactor ( double  aSF)
inline

Definition at line 76 of file bitmap_base.h.

Referenced by SCH_BITMAP::SetPixelScaleFactor().

76 { m_pixelScaleFactor = aSF; }
double m_pixelScaleFactor
Definition: bitmap_base.h:53
void BITMAP_BASE::SetScale ( double  aScale)
inline

Definition at line 85 of file bitmap_base.h.

Referenced by PAGE_LAYOUT_READER_PARSER::parseBitmap(), and DIALOG_IMAGE_EDITOR::TransfertToImage().

85 { m_scale = aScale; }
double m_scale
Definition: bitmap_base.h:49

Member Data Documentation

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

Definition at line 53 of file bitmap_base.h.

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

int BITMAP_BASE::m_ppi
private

Definition at line 57 of file bitmap_base.h.

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

double BITMAP_BASE::m_scale
private

Definition at line 49 of file bitmap_base.h.

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


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