KiCad PCB EDA Suite
Loading...
Searching...
No Matches
bitmap_base.h
Go to the documentation of this file.
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, see <https://www.gnu.org/licenses/>.
19 */
20
21#pragma once
22
23#include <optional>
24
25#include <wx/bitmap.h>
26#include <wx/image.h>
27
28#include <core/mirror.h>
29#include <kiid.h>
30#include <math/box2.h>
31#include <gal/color4d.h>
32
33class LINE_READER;
34class PLOTTER;
35
36
47{
48public:
49 BITMAP_BASE( const VECTOR2I& pos = VECTOR2I( 0, 0 ) );
50
51 BITMAP_BASE( const BITMAP_BASE& aSchBitmap );
52
54 {
55 delete m_bitmap;
56 delete m_image;
57 delete m_originalImage;
58 }
59
60 /*
61 * Accessors:
62 */
63 double GetPixelSizeIu() const { return m_pixelSizeIu; }
64 void SetPixelSizeIu( double aPixSize ) { m_pixelSizeIu = aPixSize; }
65
66 wxImage* GetImageData() { return m_image; }
67 const wxImage* GetImageData() const { return m_image; }
68
69 const wxImage* GetOriginalImageData() const { return m_originalImage; }
70
71 double GetScale() const { return m_scale; }
72 void SetScale( double aScale ) { m_scale = aScale; }
73
74 KIID GetImageID() const { return m_imageId; }
75
79 void ImportData( BITMAP_BASE& aItem );
80
92 double GetScalingFactor() const
93 {
94 return m_pixelSizeIu * m_scale;
95 }
96
100 VECTOR2I GetSize() const;
101
106 {
107 if( m_image )
108 return VECTOR2I( m_image->GetWidth(), m_image->GetHeight() );
109 else
110 return VECTOR2I( 0, 0 );
111 }
112
116 int GetPPI() const
117 {
118 return m_ppi;
119 }
120
129 int GetLegacyPPI() const;
130
138 const BOX2I GetBoundingBox() const;
139
140 void DrawBitmap( wxDC* aDC, const VECTOR2I& aPos,
141 const KIGFX::COLOR4D& aBackgroundColor = KIGFX::COLOR4D::UNSPECIFIED ) const;
142
153 bool ReadImageFile( const wxString& aFullFilename );
154
166 bool ReadImageFile( wxInputStream& aInStream );
167
179 bool ReadImageFile( wxMemoryBuffer& aBuf );
180
184 bool SetImage( const wxImage& aImage );
185
194 void BeginPreviewImage( const wxImage& aImage );
195
199 void EndPreviewImage();
200
209 bool SaveImageData( wxOutputStream& aOutStream ) const;
210
221 bool LoadLegacyData( LINE_READER& aLine, wxString& aErrorMsg );
222
228 void Mirror( FLIP_DIRECTION aFlipDirection );
229
235 void Rotate( bool aRotateCCW );
236
237 void ConvertToGreyscale();
238
244 void InvertColors();
245
254 void ConvertColourToAlpha( const wxColour& aColour );
255
256 bool IsMirroredX() const { return m_isMirroredX; }
257 bool IsMirroredY() const { return m_isMirroredY; }
258 EDA_ANGLE Rotation() const { return m_rotation; }
259
270 void PlotImage( PLOTTER* aPlotter, const VECTOR2I& aPos,
271 const KIGFX::COLOR4D& aDefaultColor, int aDefaultPensize ) const;
272
276 wxBitmapType GetImageType() const { return m_imageType; }
277
281 void SetImageType( wxBitmapType aType ) { m_imageType = aType; }
282
283private:
288
296 void rebuildBitmap( bool aResetID = true );
297
302 void ensureBitmapUpToDate() const;
303
304 void updatePPI();
305
309 static void mirrorImageInPlace( wxImage& aImage, FLIP_DIRECTION aFlipDirection );
310
316 static void invertImageInPlace( wxImage& aImage );
317
318 double m_scale;
320 mutable wxMemoryBuffer m_imageData;
321 wxBitmapType m_imageType;
322
333
334 std::optional<PREVIEW_SAVE> m_previewSave;
335
336 wxImage* m_image;
338 mutable wxBitmap* m_bitmap;
339 mutable bool m_bitmapDirty;
343 int m_ppi;
345 bool m_isMirroredX; // Used for OpenGL rendering only
346 bool m_isMirroredY; // Used for OpenGL rendering only
347 EDA_ANGLE m_rotation; // Used for OpenGL rendering only
348};
BOX2< VECTOR2I > BOX2I
Definition box2.h:914
This class handle bitmap images in KiCad.
Definition bitmap_base.h:47
void ensureBitmapUpToDate() const
Ensure the cached wxBitmap is up-to-date with the current wxImage.
wxMemoryBuffer m_imageData
Cached encoded image data (PNG/JPEG).
void Rotate(bool aRotateCCW)
Rotate image CW or CCW.
bool LoadLegacyData(LINE_READER &aLine, wxString &aErrorMsg)
Load an image data saved by SaveImageData.
void updateImageDataBuffer()
Resets the image data buffer using the current image data.
double GetScalingFactor() const
This scaling factor depends on m_pixelSizeIu and m_scale.
Definition bitmap_base.h:92
void SetImageType(wxBitmapType aType)
Set the bitmap type (png, jpeg, etc.)
void BeginPreviewImage(const wxImage &aImage)
Show finished pixels in place of the image, without disturbing the scale or the resolution.
const wxImage * GetOriginalImageData() const
Definition bitmap_base.h:69
void PlotImage(PLOTTER *aPlotter, const VECTOR2I &aPos, const KIGFX::COLOR4D &aDefaultColor, int aDefaultPensize) const
Plot bitmap on plotter.
VECTOR2I GetSizePixels() const
EDA_ANGLE Rotation() const
void Mirror(FLIP_DIRECTION aFlipDirection)
Mirror image vertically (i.e.
const BOX2I GetBoundingBox() const
Return the orthogonal, bounding box of this object for display purposes.
double GetPixelSizeIu() const
Definition bitmap_base.h:63
double GetScale() const
Definition bitmap_base.h:71
wxBitmapType GetImageType() const
Return the bitmap type (png, jpeg, etc.)
bool m_isMirroredY
void InvertColors()
Invert the colours of the image (e.g.
bool m_isMirroredX
void EndPreviewImage()
Put back the image a preview stood in for, and do nothing if none is showing.
void ImportData(BITMAP_BASE &aItem)
Copy aItem image to this object and update m_bitmap.
bool m_bitmapDirty
True when m_bitmap needs rebuilding from m_image.
bool SaveImageData(wxOutputStream &aOutStream) const
Write the bitmap data to aOutStream.
bool IsMirroredX() const
wxImage * m_originalImage
Raw image data, not transformed by rotate/mirror.
void rebuildBitmap(bool aResetID=true)
Rebuild the internal bitmap used to draw/plot image.
static void mirrorImageInPlace(wxImage &aImage, FLIP_DIRECTION aFlipDirection)
Mirror the wxImage pixel data in-place without allocating a new image.
wxBitmapType m_imageType
The image type (png, jpeg, etc.).
void ConvertColourToAlpha(const wxColour &aColour)
Convert aColour to transparent in the image, proportionally to the 'distance' of each pixel from aCol...
bool IsMirroredY() const
void SetPixelSizeIu(double aPixSize)
Definition bitmap_base.h:64
wxBitmap * m_bitmap
The bitmap used to draw/plot image.
void ConvertToGreyscale()
bool SetImage(const wxImage &aImage)
Set the image from an existing wxImage.
const wxImage * GetImageData() const
Definition bitmap_base.h:67
bool ReadImageFile(const wxString &aFullFilename)
Reads and stores in memory an image file.
KIID GetImageID() const
Definition bitmap_base.h:74
wxImage * m_image
The raw, uncompressed image data.
double m_scale
The scaling factor of the bitmap with m_pixelSizeIu, controls the actual draw size.
double m_pixelSizeIu
The scaling factor of the bitmap to convert the bitmap size (in pixels) to internal KiCad units.
int m_ppi
The bitmap definition. The default is 300PPI.
int GetPPI() const
void DrawBitmap(wxDC *aDC, const VECTOR2I &aPos, const KIGFX::COLOR4D &aBackgroundColor=KIGFX::COLOR4D::UNSPECIFIED) const
std::optional< PREVIEW_SAVE > m_previewSave
Set only while a preview is showing.
int GetLegacyPPI() const
Recompute the PPI the way it was computed before the pixels/cm truncation fix.
wxImage * GetImageData()
Definition bitmap_base.h:66
BITMAP_BASE(const VECTOR2I &pos=VECTOR2I(0, 0))
VECTOR2I GetSize() const
EDA_ANGLE m_rotation
static void invertImageInPlace(wxImage &aImage)
Invert the colour of every pixel of the wxImage in-place without allocating a new image.
void SetScale(double aScale)
Definition bitmap_base.h:72
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:400
Definition kiid.h:46
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition richio.h:65
Base plotter engine class.
Definition plotter.h:138
FLIP_DIRECTION
Definition mirror.h:23
Everything a preview image stands in for, kept so it can be put back exactly.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708