1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2018 jean-pierre.charras jp.charras at wanadoo.fr
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
*
* 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
*/
#pragma once
#include <wx/bitmap.h>
#include <wx/image.h>
#include <core/mirror.h>
#include <kiid.h>
#include <math/box2.h>
#include <gal/color4d.h>
class LINE_READER;
class PLOTTER;
/**
* This class handle bitmap images in KiCad.
*
* It is not intended to be used alone, but inside another class so all methods are protected
* or private. It is used in #SCH_BITMAP class, #DS_DRAW_ITEM_BITMAP, and possibly others in
* the future.
*
* @warning Not all plotters are able to plot a bitmap. Mainly GERBER plotters cannot.
*/
class BITMAP_BASE
{
public:
BITMAP_BASE( const VECTOR2I& pos = VECTOR2I( 0, 0 ) );
BITMAP_BASE( const BITMAP_BASE& aSchBitmap );
~BITMAP_BASE()
{
delete m_bitmap;
delete m_image;
delete m_originalImage;
}
/*
* Accessors:
*/
double GetPixelSizeIu() const { return m_pixelSizeIu; }
void SetPixelSizeIu( double aPixSize ) { m_pixelSizeIu = aPixSize; }
wxImage* GetImageData() { return m_image; }
const wxImage* GetImageData() const { return m_image; }
const wxImage* GetOriginalImageData() const { return m_originalImage; }
double GetScale() const { return m_scale; }
void SetScale( double aScale ) { m_scale = aScale; }
KIID GetImageID() const { return m_imageId; }
/**
* Copy aItem image to this object and update #m_bitmap.
*/
void ImportData( BITMAP_BASE& aItem );
/**
* 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.
*/
double GetScalingFactor() const
{
return m_pixelSizeIu * m_scale;
}
/**
* @return the actual size (in user units, not in pixels) of the image
*/
VECTOR2I GetSize() const;
/**
* @return the size in pixels of the image
*/
VECTOR2I GetSizePixels() const
{
if( m_image )
return VECTOR2I( m_image->GetWidth(), m_image->GetHeight() );
else
return VECTOR2I( 0, 0 );
}
/**
* @return the bitmap definition in ppi, the default is 300 ppi.
*/
int GetPPI() const
{
return m_ppi;
}
/**
* 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.
*/
const BOX2I GetBoundingBox() const;
void DrawBitmap( wxDC* aDC, const VECTOR2I& aPos,
const KIGFX::COLOR4D& aBackgroundColor = KIGFX::COLOR4D::UNSPECIFIED ) const;
/**
* Reads and stores in memory an image file.
*
* 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.
*
* @param aFullFilename The full filename of the image file to read.
* @return true if success reading else false.
*/
bool ReadImageFile( const wxString& aFullFilename );
/**
* Reads and stores in memory an image file.
*
* 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.
*
* @param aInStream an input stream containing the file data.
* @return true if success reading else false.
*/
bool ReadImageFile( wxInputStream& aInStream );
/**
* Reads and stores in memory an image file.
*
* 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.
*
* @param aBuf a memory buffer containing the file data.
* @return true if success reading else false.
*/
bool ReadImageFile( wxMemoryBuffer& aBuf );
/**
* Set the image from an existing wxImage.
*/
bool SetImage( const wxImage& aImage );
/**
* 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;
/**
* 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.
* @return true if the bitmap loaded successfully.
*/
bool LoadLegacyData( LINE_READER& aLine, wxString& aErrorMsg );
/**
* Mirror image vertically (i.e. relative to its horizontal X axis ) or horizontally (i.e
* relative to its vertical Y axis).
* @param aFlipDirection the direction to flip the image.
*/
void Mirror( FLIP_DIRECTION aFlipDirection );
/**
* Rotate image CW or CCW.
*
* @param aRotateCCW true to rotate CCW or false to rotate CW.
*/
void Rotate( bool aRotateCCW );
void ConvertToGreyscale();
bool IsMirroredX() const { return m_isMirroredX; }
bool IsMirroredY() const { return m_isMirroredY; }
EDA_ANGLE Rotation() const { return m_rotation; }
/**
* Plot bitmap on plotter.
*
* If the plotter does not support bitmaps, plot a
*
* @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.
*/
void PlotImage( PLOTTER* aPlotter, const VECTOR2I& aPos,
const KIGFX::COLOR4D& aDefaultColor, int aDefaultPensize ) const;
/**
* 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; }
private:
/**
* Resets the image data buffer using the current image data.
*/
void updateImageDataBuffer();
/**
* Rebuild the internal bitmap used to draw/plot image.
*
* This must be called after a #m_image change.
*
* @param aResetID is used to reset the cache ID used for OpenGL rendering.
*/
void rebuildBitmap( bool aResetID = true );
void updatePPI();
double m_scale; ///< The scaling factor of the bitmap
///< with #m_pixelSizeIu, controls the actual draw size.
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.
wxImage* m_originalImage; ///< Raw image data, not transformed by rotate/mirror.
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. This usually
///< does not change.
int m_ppi; ///< The bitmap definition. The default is 300PPI.
KIID m_imageId;
bool m_isMirroredX; // Used for OpenGL rendering only
bool m_isMirroredY; // Used for OpenGL rendering only
EDA_ANGLE m_rotation; // Used for OpenGL rendering only
};
|