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.

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