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.

281 lines
9.4 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 The 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. #pragma once
  25. #include <wx/bitmap.h>
  26. #include <wx/image.h>
  27. #include <core/mirror.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. double GetScale() const { return m_scale; }
  62. void SetScale( double aScale ) { m_scale = aScale; }
  63. KIID GetImageID() const { return m_imageId; }
  64. /**
  65. * Copy aItem image to this object and update #m_bitmap.
  66. */
  67. void ImportData( BITMAP_BASE& aItem );
  68. /**
  69. * This scaling factor depends on #m_pixelSizeIu and #m_scale.
  70. *
  71. * #m_pixelSizeIu gives the scaling factor between a pixel size and the internal units.
  72. * #m_scale is an user dependent value, and gives the "zoom" value.
  73. * - #m_scale = 1.0 = original size of bitmap.
  74. * - #m_scale < 1.0 = the bitmap is drawn smaller than its original size.
  75. * - #m_scale > 1.0 = the bitmap is drawn bigger than its original size.
  76. *
  77. * @return The scaling factor from pixel size to actual draw size.
  78. */
  79. double GetScalingFactor() const
  80. {
  81. return m_pixelSizeIu * m_scale;
  82. }
  83. /**
  84. * @return the actual size (in user units, not in pixels) of the image
  85. */
  86. VECTOR2I GetSize() const;
  87. /**
  88. * @return the size in pixels of the image
  89. */
  90. VECTOR2I GetSizePixels() const
  91. {
  92. if( m_image )
  93. return VECTOR2I( m_image->GetWidth(), m_image->GetHeight() );
  94. else
  95. return VECTOR2I( 0, 0 );
  96. }
  97. /**
  98. * @return the bitmap definition in ppi, the default is 300 ppi.
  99. */
  100. int GetPPI() const
  101. {
  102. return m_ppi;
  103. }
  104. /**
  105. * Return the orthogonal, bounding box of this object for display purposes.
  106. *
  107. * This box should be an enclosing perimeter for visible components of this object,
  108. * and the units should be in the pcb or schematic coordinate system. It is OK to
  109. * overestimate the size by a few counts.
  110. */
  111. const BOX2I GetBoundingBox() const;
  112. void DrawBitmap( wxDC* aDC, const VECTOR2I& aPos,
  113. const KIGFX::COLOR4D& aBackgroundColor = KIGFX::COLOR4D::UNSPECIFIED ) const;
  114. /**
  115. * Reads and stores in memory an image file.
  116. *
  117. * Initialize the bitmap format used to draw this item. Supported images formats are
  118. * format supported by wxImage if all handlers are loaded. By default, .png, .jpeg
  119. * are always loaded.
  120. *
  121. * @param aFullFilename The full filename of the image file to read.
  122. * @return true if success reading else false.
  123. */
  124. bool ReadImageFile( const wxString& aFullFilename );
  125. /**
  126. * Reads and stores in memory an image file.
  127. *
  128. * Initialize the bitmap format used to draw this item.
  129. *
  130. * Supported images formats are format supported by wxImage if all handlers are loaded.
  131. * By default, .png, .jpeg are always loaded.
  132. *
  133. * @param aInStream an input stream containing the file data.
  134. * @return true if success reading else false.
  135. */
  136. bool ReadImageFile( wxInputStream& aInStream );
  137. /**
  138. * Reads and stores in memory an image file.
  139. *
  140. * Initialize the bitmap format used to draw this item.
  141. *
  142. * Supported images formats are format supported by wxImage if all handlers are loaded.
  143. * By default, .png, .jpeg are always loaded.
  144. *
  145. * @param aBuf a memory buffer containing the file data.
  146. * @return true if success reading else false.
  147. */
  148. bool ReadImageFile( wxMemoryBuffer& aBuf );
  149. /**
  150. * Set the image from an existing wxImage.
  151. */
  152. bool SetImage( const wxImage& aImage );
  153. /**
  154. * Write the bitmap data to \a aOutStream.
  155. *
  156. * This writes binary data, not hexadecimal strings
  157. *
  158. * @param aOutStream The output stream to write to.
  159. * @return true if success writing else false.
  160. */
  161. bool SaveImageData( wxOutputStream& aOutStream ) const;
  162. /**
  163. * Load an image data saved by #SaveData.
  164. *
  165. * The file format must be png format in hexadecimal.
  166. *
  167. * @param aLine the LINE_READER used to read the data file.
  168. * @param aErrorMsg Description of the error if an error occurs while loading the
  169. * png bitmap data.
  170. * @return true if the bitmap loaded successfully.
  171. */
  172. bool LoadLegacyData( LINE_READER& aLine, wxString& aErrorMsg );
  173. /**
  174. * Mirror image vertically (i.e. relative to its horizontal X axis ) or horizontally (i.e
  175. * relative to its vertical Y axis).
  176. * @param aFlipDirection the direction to flip the image.
  177. */
  178. void Mirror( FLIP_DIRECTION aFlipDirection );
  179. /**
  180. * Rotate image CW or CCW.
  181. *
  182. * @param aRotateCCW true to rotate CCW or false to rotate CW.
  183. */
  184. void Rotate( bool aRotateCCW );
  185. void ConvertToGreyscale();
  186. bool IsMirroredX() const { return m_isMirroredX; }
  187. bool IsMirroredY() const { return m_isMirroredY; }
  188. EDA_ANGLE Rotation() const { return m_rotation; }
  189. /**
  190. * Plot bitmap on plotter.
  191. *
  192. * If the plotter does not support bitmaps, plot a
  193. *
  194. * @param aPlotter the plotter to use.
  195. * @param aPos the position of the center of the bitmap.
  196. * @param aDefaultColor the color used to plot the rectangle when bitmap is not supported.
  197. * @param aDefaultPensize the pen size used to plot the rectangle when bitmap is not supported.
  198. */
  199. void PlotImage( PLOTTER* aPlotter, const VECTOR2I& aPos,
  200. const KIGFX::COLOR4D& aDefaultColor, int aDefaultPensize ) const;
  201. /**
  202. * Return the bitmap type (png, jpeg, etc.)
  203. */
  204. wxBitmapType GetImageType() const { return m_imageType; }
  205. /**
  206. * Set the bitmap type (png, jpeg, etc.)
  207. */
  208. void SetImageType( wxBitmapType aType ) { m_imageType = aType; }
  209. /**
  210. * @return the image data buffer.
  211. */
  212. const wxMemoryBuffer& GetImageDataBuffer() const { return m_imageData; }
  213. /**
  214. * Resets the image data buffer using the current image data.
  215. */
  216. void UpdateImageDataBuffer();
  217. private:
  218. /**
  219. * Rebuild the internal bitmap used to draw/plot image.
  220. *
  221. * This must be called after a #m_image change.
  222. *
  223. * @param aResetID is used to reset the cache ID used for OpenGL rendering.
  224. */
  225. void rebuildBitmap( bool aResetID = true );
  226. void updatePPI();
  227. double m_scale; ///< The scaling factor of the bitmap
  228. ///< with #m_pixelSizeIu, controls the actual draw size.
  229. wxMemoryBuffer m_imageData; ///< The original image data in its original format.
  230. wxBitmapType m_imageType; ///< The image type (png, jpeg, etc.).
  231. wxImage* m_image; ///< The raw, uncompressed image data.
  232. wxImage* m_originalImage; ///< Raw image data, not transformed by rotate/mirror.
  233. wxBitmap* m_bitmap; ///< The bitmap used to draw/plot image.
  234. double m_pixelSizeIu; ///< The scaling factor of the bitmap to convert the bitmap
  235. ///< size (in pixels) to internal KiCad units. This usually
  236. ///< does not change.
  237. int m_ppi; ///< The bitmap definition. The default is 300PPI.
  238. KIID m_imageId;
  239. bool m_isMirroredX; // Used for OpenGL rendering only
  240. bool m_isMirroredY; // Used for OpenGL rendering only
  241. EDA_ANGLE m_rotation; // Used for OpenGL rendering only
  242. };