You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
7.9 KiB

  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-2020 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. #ifndef BITMAP_BASE_H
  25. #define BITMAP_BASE_H
  26. #include <wx/bitmap.h>
  27. #include <wx/image.h>
  28. #include <math/box2.h>
  29. namespace KIGFX
  30. {
  31. class COLOR4D;
  32. }
  33. class LINE_READER;
  34. class PLOTTER;
  35. /**
  36. * This class handle bitmap images in KiCad.
  37. *
  38. * It is not intended to be used alone, but inside another class so all methods are protected
  39. * or private. It is used in #SCH_BITMAP class, #DS_DRAW_ITEM_BITMAP, and possibly others in
  40. * the future.
  41. *
  42. * @warning Not all plotters are able to plot a bitmap. Mainly GERBER plotters cannot.
  43. */
  44. class BITMAP_BASE
  45. {
  46. public:
  47. BITMAP_BASE( const VECTOR2I& pos = VECTOR2I( 0, 0 ) );
  48. BITMAP_BASE( const BITMAP_BASE& aSchBitmap );
  49. ~BITMAP_BASE()
  50. {
  51. delete m_bitmap;
  52. delete m_image;
  53. }
  54. /*
  55. * Accessors:
  56. */
  57. double GetPixelSizeIu() const { return m_pixelSizeIu; }
  58. void SetPixelSizeIu( double aPixSize ) { m_pixelSizeIu = aPixSize; }
  59. wxImage* GetImageData() { return m_image; }
  60. const wxImage* GetImageData() const { return m_image; }
  61. void SetImage( wxImage* aImage )
  62. {
  63. delete m_image;
  64. m_image = aImage;
  65. }
  66. double GetScale() const { return m_scale; }
  67. void SetScale( double aScale ) { m_scale = aScale; }
  68. /*
  69. * Rebuild the internal bitmap used to draw/plot image.
  70. *
  71. * This must be called after a #m_image change.
  72. */
  73. void RebuildBitmap() { *m_bitmap = wxBitmap( *m_image ); }
  74. void SetBitmap( wxBitmap* aBitMap )
  75. {
  76. delete m_bitmap;
  77. m_bitmap = aBitMap;
  78. }
  79. /**
  80. * Copy aItem image to this object and update #m_bitmap.
  81. */
  82. void ImportData( BITMAP_BASE* aItem );
  83. /**
  84. * This scaling factor depends on #m_pixelSizeIu and #m_scale.
  85. *
  86. * #m_pixelSizeIu gives the scaling factor between a pixel size and the internal units.
  87. * #m_scale is an user dependent value, and gives the "zoom" value.
  88. * - #m_scale = 1.0 = original size of bitmap.
  89. * - #m_scale < 1.0 = the bitmap is drawn smaller than its original size.
  90. * - #m_scale > 1.0 = the bitmap is drawn bigger than its original size.
  91. *
  92. * @return The scaling factor from pixel size to actual draw size.
  93. */
  94. double GetScalingFactor() const
  95. {
  96. return m_pixelSizeIu * m_scale;
  97. }
  98. /**
  99. * @return the actual size (in user units, not in pixels) of the image
  100. */
  101. VECTOR2I GetSize() const;
  102. /**
  103. * @return the size in pixels of the image
  104. */
  105. wxSize GetSizePixels() const
  106. {
  107. if( m_image )
  108. return wxSize( m_image->GetWidth(), m_image->GetHeight() );
  109. else
  110. return wxSize( 0, 0 );
  111. }
  112. /**
  113. * @return the bitmap definition in ppi, the default is 300 ppi.
  114. */
  115. int GetPPI() const
  116. {
  117. return m_ppi;
  118. }
  119. /**
  120. * Return the orthogonal, bounding box of this object for display purposes.
  121. *
  122. * This box should be an enclosing perimeter for visible components of this object,
  123. * and the units should be in the pcb or schematic coordinate system. It is OK to
  124. * overestimate the size by a few counts.
  125. */
  126. const BOX2I GetBoundingBox() const;
  127. void DrawBitmap( wxDC* aDC, const VECTOR2I& aPos );
  128. /**
  129. * Reads and stores in memory an image file.
  130. *
  131. * Initialize the bitmap format used to draw this item. Supported images formats are
  132. * format supported by wxImage if all handlers are loaded. By default, .png, .jpeg
  133. * are always loaded.
  134. *
  135. * @param aFullFilename The full filename of the image file to read.
  136. * @return true if success reading else false.
  137. */
  138. bool ReadImageFile( const wxString& aFullFilename );
  139. /**
  140. * Reads and stores in memory an image file.
  141. *
  142. * Initialize the bitmap format used to draw this item.
  143. *
  144. * Supported images formats are format supported by wxImage if all handlers are loaded.
  145. * By default, .png, .jpeg are always loaded.
  146. *
  147. * @param aInStream an input stream containing the file data.
  148. * @return true if success reading else false.
  149. */
  150. bool ReadImageFile( wxInputStream& aInStream );
  151. /**
  152. * Write the bitmap data to \a aFile.
  153. *
  154. * The file format is png, in hexadecimal form. If the hexadecimal data is converted to
  155. * binary it gives exactly a .png image data.
  156. *
  157. * @param aFile The FILE to write to.
  158. * @return true if success writing else false.
  159. */
  160. bool SaveData( FILE* aFile ) const;
  161. /**
  162. * Write the bitmap data to an array string.
  163. *
  164. * The format is png, in Hexadecimal form. If the hexadecimal data is converted to binary
  165. * it gives exactly a .png image data.
  166. *
  167. * @param aPngStrings The wxArrayString to write to.
  168. */
  169. void SaveData( wxArrayString& aPngStrings ) const;
  170. /**
  171. * Load an image data saved by #SaveData.
  172. *
  173. * The file format must be png format in hexadecimal.
  174. *
  175. * @param aLine the LINE_READER used to read the data file.
  176. * @param aErrorMsg Description of the error if an error occurs while loading the
  177. * png bitmap data.
  178. * @return true if the bitmap loaded successfully.
  179. */
  180. bool LoadData( LINE_READER& aLine, wxString& aErrorMsg );
  181. /**
  182. * Mirror image vertically (i.e. relative to its horizontal X axis ) or horizontally (i.e
  183. * relative to its vertical Y axis).
  184. * @param aVertically false to mirror horizontally or true to mirror vertically.
  185. */
  186. void Mirror( bool aVertically );
  187. /**
  188. * Rotate image CW or CCW.
  189. *
  190. * @param aRotateCCW true to rotate CCW or false to rotate CW.
  191. */
  192. void Rotate( bool aRotateCCW );
  193. /**
  194. * Plot bitmap on plotter.
  195. *
  196. * If the plotter does not support bitmaps, plot a
  197. *
  198. * @param aPlotter the plotter to use.
  199. * @param aPos the position of the center of the bitmap.
  200. * @param aDefaultColor the color used to plot the rectangle when bitmap is not supported.
  201. * @param aDefaultPensize the pen size used to plot the rectangle when bitmap is not supported.
  202. */
  203. void PlotImage( PLOTTER* aPlotter, const VECTOR2I& aPos,
  204. const KIGFX::COLOR4D& aDefaultColor, int aDefaultPensize ) const;
  205. private:
  206. double m_scale; // The scaling factor of the bitmap
  207. // With m_pixelSizeIu, controls the actual draw size
  208. wxImage* m_image; // the raw image data (png format)
  209. wxBitmap* m_bitmap; // the bitmap used to draw/plot image
  210. double m_pixelSizeIu; // The scaling factor of the bitmap
  211. // to convert the bitmap size (in pixels)
  212. // to internal KiCad units
  213. // Usually does not change
  214. int m_ppi; // the bitmap definition. the default is 300PPI
  215. };
  216. #endif // BITMAP_BASE_H