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.

277 lines
9.3 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-2024 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 );
  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. * Write the bitmap data to \a aOutStream.
  151. *
  152. * This writes binary data, not hexadecimal strings
  153. *
  154. * @param aOutStream The output stream to write to.
  155. * @return true if success writing else false.
  156. */
  157. bool SaveImageData( wxOutputStream& aOutStream ) const;
  158. /**
  159. * Load an image data saved by #SaveData.
  160. *
  161. * The file format must be png format in hexadecimal.
  162. *
  163. * @param aLine the LINE_READER used to read the data file.
  164. * @param aErrorMsg Description of the error if an error occurs while loading the
  165. * png bitmap data.
  166. * @return true if the bitmap loaded successfully.
  167. */
  168. bool LoadLegacyData( LINE_READER& aLine, wxString& aErrorMsg );
  169. /**
  170. * Mirror image vertically (i.e. relative to its horizontal X axis ) or horizontally (i.e
  171. * relative to its vertical Y axis).
  172. * @param aFlipDirection the direction to flip the image.
  173. */
  174. void Mirror( FLIP_DIRECTION aFlipDirection );
  175. /**
  176. * Rotate image CW or CCW.
  177. *
  178. * @param aRotateCCW true to rotate CCW or false to rotate CW.
  179. */
  180. void Rotate( bool aRotateCCW );
  181. void ConvertToGreyscale();
  182. bool IsMirroredX() const { return m_isMirroredX; }
  183. bool IsMirroredY() const { return m_isMirroredY; }
  184. EDA_ANGLE Rotation() const { return m_rotation; }
  185. /**
  186. * Plot bitmap on plotter.
  187. *
  188. * If the plotter does not support bitmaps, plot a
  189. *
  190. * @param aPlotter the plotter to use.
  191. * @param aPos the position of the center of the bitmap.
  192. * @param aDefaultColor the color used to plot the rectangle when bitmap is not supported.
  193. * @param aDefaultPensize the pen size used to plot the rectangle when bitmap is not supported.
  194. */
  195. void PlotImage( PLOTTER* aPlotter, const VECTOR2I& aPos,
  196. const KIGFX::COLOR4D& aDefaultColor, int aDefaultPensize ) const;
  197. /**
  198. * Return the bitmap type (png, jpeg, etc.)
  199. */
  200. wxBitmapType GetImageType() const { return m_imageType; }
  201. /**
  202. * Set the bitmap type (png, jpeg, etc.)
  203. */
  204. void SetImageType( wxBitmapType aType ) { m_imageType = aType; }
  205. /**
  206. * @return the image data buffer.
  207. */
  208. const wxMemoryBuffer& GetImageDataBuffer() const { return m_imageData; }
  209. /**
  210. * Resets the image data buffer using the current image data.
  211. */
  212. void UpdateImageDataBuffer();
  213. private:
  214. /*
  215. * Rebuild the internal bitmap used to draw/plot image.
  216. *
  217. * This must be called after a #m_image change.
  218. *
  219. * @param aResetID is used to reset the cache ID used for OpenGL rendering.
  220. */
  221. void rebuildBitmap( bool aResetID = true );
  222. void updatePPI();
  223. double m_scale; // The scaling factor of the bitmap
  224. // With m_pixelSizeIu, controls the actual draw size
  225. wxMemoryBuffer m_imageData; // The original image data, in its original format
  226. wxBitmapType m_imageType; // the image type (png, jpeg, etc.)
  227. wxImage* m_image; // the raw, uncompressed image data
  228. wxImage* m_originalImage; // Raw image data, not transformed by rotate/mirror
  229. wxBitmap* m_bitmap; // the bitmap used to draw/plot image
  230. double m_pixelSizeIu; // The scaling factor of the bitmap
  231. // to convert the bitmap size (in pixels)
  232. // to internal KiCad units
  233. // Usually does not change
  234. int m_ppi; // the bitmap definition. the default is 300PPI
  235. KIID m_imageId;
  236. bool m_isMirroredX; // Used for OpenGL rendering only
  237. bool m_isMirroredY; // Used for OpenGL rendering only
  238. EDA_ANGLE m_rotation; // Used for OpenGL rendering only
  239. };