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