2011-08-31 14:59:20 +00:00
|
|
|
/*
|
2011-09-30 18:15:37 +00:00
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
2011-08-31 14:59:20 +00:00
|
|
|
*
|
2018-01-29 08:39:13 +00:00
|
|
|
* Copyright (C) 2018 jean-pierre.charras jp.charras at wanadoo.fr
|
2023-07-02 22:59:38 +00:00
|
|
|
* Copyright (C) 2013-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2011-08-31 14:59:20 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2018-01-29 08:39:13 +00:00
|
|
|
#ifndef BITMAP_BASE_H
|
|
|
|
#define BITMAP_BASE_H
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2019-04-24 15:35:01 +00:00
|
|
|
#include <wx/bitmap.h>
|
|
|
|
#include <wx/image.h>
|
2023-01-03 03:14:22 +00:00
|
|
|
#include <kiid.h>
|
2022-08-31 16:17:14 +00:00
|
|
|
#include <math/box2.h>
|
2023-07-02 22:59:38 +00:00
|
|
|
#include <gal/color4d.h>
|
2019-05-23 18:31:15 +00:00
|
|
|
|
2019-04-24 15:35:01 +00:00
|
|
|
class LINE_READER;
|
2013-10-04 08:42:09 +00:00
|
|
|
class PLOTTER;
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2019-04-24 15:35:01 +00:00
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
/**
|
2011-09-30 18:15:37 +00:00
|
|
|
* This class handle bitmap images in KiCad.
|
2011-08-31 14:59:20 +00:00
|
|
|
*
|
2020-12-18 14:04:03 +00:00
|
|
|
* It is not intended to be used alone, but inside another class so all methods are protected
|
2021-02-22 23:47:17 +00:00
|
|
|
* or private. It is used in #SCH_BITMAP class, #DS_DRAW_ITEM_BITMAP, and possibly others in
|
2020-12-18 14:04:03 +00:00
|
|
|
* the future.
|
|
|
|
*
|
|
|
|
* @warning Not all plotters are able to plot a bitmap. Mainly GERBER plotters cannot.
|
2011-08-31 14:59:20 +00:00
|
|
|
*/
|
|
|
|
class BITMAP_BASE
|
|
|
|
{
|
2016-07-06 09:22:56 +00:00
|
|
|
public:
|
2022-01-01 06:04:08 +00:00
|
|
|
BITMAP_BASE( const VECTOR2I& pos = VECTOR2I( 0, 0 ) );
|
2011-08-31 14:59:20 +00:00
|
|
|
|
|
|
|
BITMAP_BASE( const BITMAP_BASE& aSchBitmap );
|
|
|
|
|
|
|
|
~BITMAP_BASE()
|
|
|
|
{
|
|
|
|
delete m_bitmap;
|
|
|
|
delete m_image;
|
2023-01-03 21:54:20 +00:00
|
|
|
delete m_originalImage;
|
2011-08-31 14:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Accessors:
|
|
|
|
*/
|
2020-11-28 09:53:26 +00:00
|
|
|
double GetPixelSizeIu() const { return m_pixelSizeIu; }
|
|
|
|
void SetPixelSizeIu( double aPixSize ) { m_pixelSizeIu = aPixSize; }
|
2019-04-24 09:50:18 +00:00
|
|
|
|
2011-09-02 19:43:56 +00:00
|
|
|
wxImage* GetImageData() { return m_image; }
|
2019-04-24 09:50:18 +00:00
|
|
|
const wxImage* GetImageData() const { return m_image; }
|
|
|
|
|
2023-01-03 21:54:20 +00:00
|
|
|
const wxImage* GetOriginalImageData() const { return m_originalImage; }
|
|
|
|
|
2016-07-11 19:48:46 +00:00
|
|
|
double GetScale() const { return m_scale; }
|
|
|
|
void SetScale( double aScale ) { m_scale = aScale; }
|
|
|
|
|
2023-01-03 03:14:22 +00:00
|
|
|
KIID GetImageID() const { return m_imageId; }
|
2016-07-06 09:22:56 +00:00
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Copy aItem image to this object and update #m_bitmap.
|
2011-08-31 14:59:20 +00:00
|
|
|
*/
|
|
|
|
void ImportData( BITMAP_BASE* aItem );
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* This scaling factor depends on #m_pixelSizeIu and #m_scale.
|
|
|
|
*
|
|
|
|
* #m_pixelSizeIu gives the scaling factor between a pixel size and the internal units.
|
|
|
|
* #m_scale is an user dependent value, and gives the "zoom" value.
|
|
|
|
* - #m_scale = 1.0 = original size of bitmap.
|
|
|
|
* - #m_scale < 1.0 = the bitmap is drawn smaller than its original size.
|
|
|
|
* - #m_scale > 1.0 = the bitmap is drawn bigger than its original size.
|
|
|
|
*
|
|
|
|
* @return The scaling factor from pixel size to actual draw size.
|
2011-08-31 14:59:20 +00:00
|
|
|
*/
|
|
|
|
double GetScalingFactor() const
|
|
|
|
{
|
2020-11-28 09:53:26 +00:00
|
|
|
return m_pixelSizeIu * m_scale;
|
2011-08-31 14:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-10-04 08:42:09 +00:00
|
|
|
* @return the actual size (in user units, not in pixels) of the image
|
2011-08-31 14:59:20 +00:00
|
|
|
*/
|
2022-08-31 09:15:42 +00:00
|
|
|
VECTOR2I GetSize() const;
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2011-09-02 19:43:56 +00:00
|
|
|
/**
|
2013-10-04 08:42:09 +00:00
|
|
|
* @return the size in pixels of the image
|
2011-09-02 19:43:56 +00:00
|
|
|
*/
|
2023-02-19 03:40:07 +00:00
|
|
|
VECTOR2I GetSizePixels() const
|
2011-09-02 19:43:56 +00:00
|
|
|
{
|
|
|
|
if( m_image )
|
2023-02-19 03:40:07 +00:00
|
|
|
return VECTOR2I( m_image->GetWidth(), m_image->GetHeight() );
|
2011-09-02 19:43:56 +00:00
|
|
|
else
|
2023-02-19 03:40:07 +00:00
|
|
|
return VECTOR2I( 0, 0 );
|
2011-09-02 19:43:56 +00:00
|
|
|
}
|
|
|
|
|
2013-10-18 17:38:03 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* @return the bitmap definition in ppi, the default is 300 ppi.
|
2013-10-18 17:38:03 +00:00
|
|
|
*/
|
|
|
|
int GetPPI() const
|
|
|
|
{
|
2013-10-19 10:29:54 +00:00
|
|
|
return m_ppi;
|
2013-10-18 17:38:03 +00:00
|
|
|
}
|
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Return the orthogonal, bounding box of this object for display purposes.
|
|
|
|
*
|
|
|
|
* This box should be an enclosing perimeter for visible components of this object,
|
|
|
|
* and the units should be in the pcb or schematic coordinate system. It is OK to
|
|
|
|
* overestimate the size by a few counts.
|
2011-08-31 14:59:20 +00:00
|
|
|
*/
|
2022-08-31 09:15:42 +00:00
|
|
|
const BOX2I GetBoundingBox() const;
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2023-07-02 22:59:38 +00:00
|
|
|
void DrawBitmap( wxDC* aDC, const VECTOR2I& aPos,
|
|
|
|
const KIGFX::COLOR4D& aBackgroundColor = KIGFX::COLOR4D::UNSPECIFIED );
|
2011-08-31 14:59:20 +00:00
|
|
|
|
|
|
|
/**
|
2013-10-04 08:42:09 +00:00
|
|
|
* Reads and stores in memory an image file.
|
2019-04-24 15:35:01 +00:00
|
|
|
*
|
2020-12-18 14:04:03 +00:00
|
|
|
* Initialize the bitmap format used to draw this item. Supported images formats are
|
|
|
|
* format supported by wxImage if all handlers are loaded. By default, .png, .jpeg
|
|
|
|
* are always loaded.
|
2019-04-24 15:35:01 +00:00
|
|
|
*
|
2011-08-31 14:59:20 +00:00
|
|
|
* @param aFullFilename The full filename of the image file to read.
|
2020-12-18 14:04:03 +00:00
|
|
|
* @return true if success reading else false.
|
2011-08-31 14:59:20 +00:00
|
|
|
*/
|
2016-07-11 19:48:46 +00:00
|
|
|
bool ReadImageFile( const wxString& aFullFilename );
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2019-04-24 15:35:01 +00:00
|
|
|
/**
|
|
|
|
* Reads and stores in memory an image file.
|
|
|
|
*
|
2020-12-18 14:04:03 +00:00
|
|
|
* Initialize the bitmap format used to draw this item.
|
2019-04-24 15:35:01 +00:00
|
|
|
*
|
2020-12-18 14:04:03 +00:00
|
|
|
* Supported images formats are format supported by wxImage if all handlers are loaded.
|
|
|
|
* By default, .png, .jpeg are always loaded.
|
|
|
|
*
|
|
|
|
* @param aInStream an input stream containing the file data.
|
|
|
|
* @return true if success reading else false.
|
2019-04-24 15:35:01 +00:00
|
|
|
*/
|
|
|
|
bool ReadImageFile( wxInputStream& aInStream );
|
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
/**
|
2023-06-08 15:54:36 +00:00
|
|
|
* Reads and stores in memory an image file.
|
2020-12-18 14:04:03 +00:00
|
|
|
*
|
2023-06-08 15:54:36 +00:00
|
|
|
* Initialize the bitmap format used to draw this item.
|
2020-12-18 14:04:03 +00:00
|
|
|
*
|
2023-06-08 15:54:36 +00:00
|
|
|
* Supported images formats are format supported by wxImage if all handlers are loaded.
|
|
|
|
* By default, .png, .jpeg are always loaded.
|
|
|
|
*
|
|
|
|
* @param aBuf a memory buffer containing the file data.
|
|
|
|
* @return true if success reading else false.
|
2011-08-31 14:59:20 +00:00
|
|
|
*/
|
2023-06-08 15:54:36 +00:00
|
|
|
bool ReadImageFile( wxMemoryBuffer& aBuf );
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2013-10-18 17:38:03 +00:00
|
|
|
/**
|
2023-06-08 15:54:36 +00:00
|
|
|
* Write the bitmap data to \a aOutStream.
|
|
|
|
*
|
|
|
|
* This writes binary data, not hexadecimal strings
|
|
|
|
*
|
|
|
|
* @param aOutStream The output stream to write to.
|
|
|
|
* @return true if success writing else false.
|
|
|
|
*/
|
|
|
|
bool SaveImageData( wxOutputStream& aOutStream ) const;
|
2013-10-18 17:38:03 +00:00
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Load an image data saved by #SaveData.
|
|
|
|
*
|
|
|
|
* The file format must be png format in hexadecimal.
|
|
|
|
*
|
|
|
|
* @param aLine the LINE_READER used to read the data file.
|
|
|
|
* @param aErrorMsg Description of the error if an error occurs while loading the
|
|
|
|
* png bitmap data.
|
2011-08-31 14:59:20 +00:00
|
|
|
* @return true if the bitmap loaded successfully.
|
|
|
|
*/
|
2023-06-08 15:54:36 +00:00
|
|
|
bool LoadLegacyData( LINE_READER& aLine, wxString& aErrorMsg );
|
2011-08-31 14:59:20 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Mirror image vertically (i.e. relative to its horizontal X axis ) or horizontally (i.e
|
|
|
|
* relative to its vertical Y axis).
|
|
|
|
* @param aVertically false to mirror horizontally or true to mirror vertically.
|
2011-08-31 14:59:20 +00:00
|
|
|
*/
|
2016-07-11 19:48:46 +00:00
|
|
|
void Mirror( bool aVertically );
|
2011-08-31 14:59:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Rotate image CW or CCW.
|
2020-12-18 14:04:03 +00:00
|
|
|
*
|
|
|
|
* @param aRotateCCW true to rotate CCW or false to rotate CW.
|
2011-08-31 14:59:20 +00:00
|
|
|
*/
|
2016-07-11 19:48:46 +00:00
|
|
|
void Rotate( bool aRotateCCW );
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2023-01-03 03:14:22 +00:00
|
|
|
void ConvertToGreyscale();
|
|
|
|
|
2023-04-23 14:00:05 +00:00
|
|
|
bool IsMirroredX() const { return m_isMirroredX; }
|
|
|
|
bool IsMirroredY() const { return m_isMirroredY; }
|
2023-01-03 21:54:20 +00:00
|
|
|
EDA_ANGLE Rotation() const { return m_rotation; }
|
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
/**
|
|
|
|
* Plot bitmap on plotter.
|
2020-12-18 14:04:03 +00:00
|
|
|
*
|
2011-08-31 14:59:20 +00:00
|
|
|
* If the plotter does not support bitmaps, plot a
|
2020-12-18 14:04:03 +00:00
|
|
|
*
|
|
|
|
* @param aPlotter the plotter to use.
|
|
|
|
* @param aPos the position of the center of the bitmap.
|
|
|
|
* @param aDefaultColor the color used to plot the rectangle when bitmap is not supported.
|
|
|
|
* @param aDefaultPensize the pen size used to plot the rectangle when bitmap is not supported.
|
2011-08-31 14:59:20 +00:00
|
|
|
*/
|
2022-01-01 06:04:08 +00:00
|
|
|
void PlotImage( PLOTTER* aPlotter, const VECTOR2I& aPos,
|
2021-07-26 17:28:37 +00:00
|
|
|
const KIGFX::COLOR4D& aDefaultColor, int aDefaultPensize ) const;
|
2020-12-18 14:04:03 +00:00
|
|
|
|
2023-06-07 02:59:17 +00:00
|
|
|
/**
|
|
|
|
* Return the bitmap type (png, jpeg, etc.)
|
|
|
|
*/
|
|
|
|
wxBitmapType GetImageType() const { return m_imageType; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the bitmap type (png, jpeg, etc.)
|
|
|
|
*/
|
|
|
|
void SetImageType( wxBitmapType aType ) { m_imageType = aType; }
|
|
|
|
|
2023-06-08 15:54:36 +00:00
|
|
|
/**
|
|
|
|
* @return the image data buffer.
|
|
|
|
*/
|
|
|
|
const wxMemoryBuffer& GetImageDataBuffer() const { return m_imageData; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the image data buffer using the current image data.
|
|
|
|
*/
|
|
|
|
void UpdateImageDataBuffer();
|
|
|
|
|
2020-12-18 14:04:03 +00:00
|
|
|
private:
|
2023-01-03 03:14:22 +00:00
|
|
|
/*
|
|
|
|
* Rebuild the internal bitmap used to draw/plot image.
|
|
|
|
*
|
|
|
|
* This must be called after a #m_image change.
|
2023-01-03 21:54:20 +00:00
|
|
|
*
|
|
|
|
* @param aResetID is used to reset the cache ID used for OpenGL rendering.
|
2023-01-03 03:14:22 +00:00
|
|
|
*/
|
2023-01-03 21:54:20 +00:00
|
|
|
void rebuildBitmap( bool aResetID = true );
|
2023-01-03 03:14:22 +00:00
|
|
|
|
2023-02-15 00:41:54 +00:00
|
|
|
void updatePPI();
|
|
|
|
|
2020-12-18 14:04:03 +00:00
|
|
|
double m_scale; // The scaling factor of the bitmap
|
|
|
|
// With m_pixelSizeIu, controls the actual draw size
|
2023-06-08 15:54:36 +00:00
|
|
|
wxMemoryBuffer m_imageData; // The original image data, in its original format
|
|
|
|
wxBitmapType m_imageType; // the image type (png, jpeg, etc.)
|
|
|
|
|
|
|
|
wxImage* m_image; // the raw, uncompressed image data
|
2023-01-03 21:54:20 +00:00
|
|
|
wxImage* m_originalImage; // Raw image data, not transformed by rotate/mirror
|
2020-12-18 14:04:03 +00:00
|
|
|
wxBitmap* m_bitmap; // the bitmap used to draw/plot image
|
|
|
|
double m_pixelSizeIu; // The scaling factor of the bitmap
|
|
|
|
// to convert the bitmap size (in pixels)
|
|
|
|
// to internal KiCad units
|
|
|
|
// Usually does not change
|
|
|
|
int m_ppi; // the bitmap definition. the default is 300PPI
|
2023-01-03 03:14:22 +00:00
|
|
|
KIID m_imageId;
|
2023-04-23 14:00:05 +00:00
|
|
|
bool m_isMirroredX; // Used for OpenGL rendering only
|
|
|
|
bool m_isMirroredY; // Used for OpenGL rendering only
|
2023-01-03 21:54:20 +00:00
|
|
|
EDA_ANGLE m_rotation; // Used for OpenGL rendering only
|
2011-08-31 14:59:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-01-29 08:39:13 +00:00
|
|
|
#endif // BITMAP_BASE_H
|