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
|
|
|
*
|
2017-11-16 11:45:53 +00:00
|
|
|
* Copyright (C) 2017 jean-pierre.charras
|
2023-01-22 19:03:29 +00:00
|
|
|
* Copyright (C) 2011-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
|
|
|
|
*/
|
|
|
|
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <bitmap_base.h>
|
|
|
|
#include <gr_basic.h>
|
2021-08-18 20:38:14 +00:00
|
|
|
#include <math/util.h> // for KiROUND
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <memory> // for make_unique, unique_ptr
|
2021-08-18 20:38:14 +00:00
|
|
|
#include <plotters/plotter.h>
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <richio.h>
|
|
|
|
#include <wx/bitmap.h> // for wxBitmap
|
2011-08-31 14:59:20 +00:00
|
|
|
#include <wx/mstream.h>
|
2023-06-08 15:54:36 +00:00
|
|
|
#include <wx/stream.h> // for wxInputStream, wxOutputStream
|
|
|
|
#include <wx/string.h> // for wxString
|
|
|
|
#include <wx/wfstream.h> // for wxFileInputStream
|
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
|
|
|
|
|
2022-01-01 06:04:08 +00:00
|
|
|
BITMAP_BASE::BITMAP_BASE( const VECTOR2I& pos )
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
2020-11-28 09:53:26 +00:00
|
|
|
m_scale = 1.0; // 1.0 = original bitmap size
|
2021-07-15 19:26:35 +00:00
|
|
|
m_bitmap = nullptr;
|
|
|
|
m_image = nullptr;
|
2023-01-03 21:54:20 +00:00
|
|
|
m_originalImage = nullptr;
|
2020-11-28 09:53:26 +00:00
|
|
|
m_ppi = 300; // the bitmap definition. the default is 300PPI
|
|
|
|
m_pixelSizeIu = 254000.0 / m_ppi; // a pixel size value OK for bitmaps using 300 PPI
|
|
|
|
// for Eeschema which uses currently 254000PPI
|
2023-04-23 14:00:05 +00:00
|
|
|
m_isMirroredX = false;
|
|
|
|
m_isMirroredY = false;
|
2023-01-03 21:54:20 +00:00
|
|
|
m_rotation = ANGLE_0;
|
2011-08-31 14:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BITMAP_BASE::BITMAP_BASE( const BITMAP_BASE& aSchBitmap )
|
|
|
|
{
|
2016-07-11 19:48:46 +00:00
|
|
|
m_scale = aSchBitmap.m_scale;
|
2015-02-26 10:33:15 +00:00
|
|
|
m_ppi = aSchBitmap.m_ppi;
|
2020-11-28 09:53:26 +00:00
|
|
|
m_pixelSizeIu = aSchBitmap.m_pixelSizeIu;
|
2023-04-23 14:00:05 +00:00
|
|
|
m_isMirroredX = aSchBitmap.m_isMirroredX;
|
|
|
|
m_isMirroredY = aSchBitmap.m_isMirroredY;
|
2023-01-03 21:54:20 +00:00
|
|
|
m_rotation = aSchBitmap.m_rotation;
|
2019-05-23 15:52:07 +00:00
|
|
|
|
|
|
|
m_image = nullptr;
|
|
|
|
m_bitmap = nullptr;
|
2023-01-03 21:54:20 +00:00
|
|
|
m_originalImage = nullptr;
|
2019-05-23 15:52:07 +00:00
|
|
|
|
|
|
|
if( aSchBitmap.m_image )
|
|
|
|
{
|
2023-01-03 03:14:22 +00:00
|
|
|
m_image = new wxImage( *aSchBitmap.m_image );
|
|
|
|
m_bitmap = new wxBitmap( *m_image );
|
2023-01-03 21:54:20 +00:00
|
|
|
m_originalImage = new wxImage( *aSchBitmap.m_originalImage );
|
2023-06-08 15:54:36 +00:00
|
|
|
m_imageType = aSchBitmap.m_imageType;
|
|
|
|
m_imageData = aSchBitmap.m_imageData;
|
2023-01-03 03:14:22 +00:00
|
|
|
m_imageId = aSchBitmap.m_imageId;
|
2019-05-23 15:52:07 +00:00
|
|
|
}
|
2011-08-31 14:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-03 21:54:20 +00:00
|
|
|
void BITMAP_BASE::rebuildBitmap( bool aResetID )
|
2023-01-03 03:14:22 +00:00
|
|
|
{
|
|
|
|
if( m_bitmap )
|
|
|
|
delete m_bitmap;
|
|
|
|
|
|
|
|
m_bitmap = new wxBitmap( *m_image );
|
2023-01-03 21:54:20 +00:00
|
|
|
|
|
|
|
if( aResetID )
|
|
|
|
m_imageId = KIID();
|
2023-06-08 15:54:36 +00:00
|
|
|
|
2023-01-03 03:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-15 00:41:54 +00:00
|
|
|
void BITMAP_BASE::updatePPI()
|
|
|
|
{
|
|
|
|
// Todo: eventually we need to support dpi / scaling in both dimensions
|
|
|
|
int dpiX = m_originalImage->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONX );
|
|
|
|
|
|
|
|
if( dpiX > 1 )
|
|
|
|
{
|
|
|
|
if( m_originalImage->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONUNIT ) == wxIMAGE_RESOLUTION_CM )
|
|
|
|
m_ppi = KiROUND( dpiX * 2.54 );
|
|
|
|
else
|
|
|
|
m_ppi = dpiX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
void BITMAP_BASE::ImportData( BITMAP_BASE* aItem )
|
|
|
|
{
|
|
|
|
*m_image = *aItem->m_image;
|
|
|
|
*m_bitmap = *aItem->m_bitmap;
|
2023-01-03 21:54:20 +00:00
|
|
|
*m_originalImage = *aItem->m_originalImage;
|
2023-01-03 03:14:22 +00:00
|
|
|
m_imageId = aItem->m_imageId;
|
2016-07-11 19:48:46 +00:00
|
|
|
m_scale = aItem->m_scale;
|
2013-10-19 10:29:54 +00:00
|
|
|
m_ppi = aItem->m_ppi;
|
2020-11-28 09:53:26 +00:00
|
|
|
m_pixelSizeIu = aItem->m_pixelSizeIu;
|
2023-04-23 14:00:05 +00:00
|
|
|
m_isMirroredX = aItem->m_isMirroredX;
|
|
|
|
m_isMirroredY = aItem->m_isMirroredY;
|
2023-01-03 21:54:20 +00:00
|
|
|
m_rotation = aItem->m_rotation;
|
2023-06-08 15:54:36 +00:00
|
|
|
m_imageType = aItem->m_imageType;
|
|
|
|
m_imageData = aItem->m_imageData;
|
2011-08-31 14:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-24 15:35:01 +00:00
|
|
|
bool BITMAP_BASE::ReadImageFile( wxInputStream& aInStream )
|
|
|
|
{
|
2023-06-08 15:54:36 +00:00
|
|
|
// Store the original image data in m_imageData
|
|
|
|
size_t dataSize = aInStream.GetLength();
|
|
|
|
m_imageData.SetBufSize( dataSize );
|
|
|
|
aInStream.Read( m_imageData.GetData(), dataSize );
|
|
|
|
m_imageData.SetDataLen( dataSize );
|
|
|
|
|
2022-07-29 21:02:35 +00:00
|
|
|
std::unique_ptr<wxImage> new_image = std::make_unique<wxImage>();
|
2019-04-24 15:35:01 +00:00
|
|
|
|
2023-06-08 15:54:36 +00:00
|
|
|
// Load the image from the stream into new_image
|
|
|
|
wxMemoryInputStream mem_stream( m_imageData.GetData(), dataSize );
|
|
|
|
if( !new_image->LoadFile( mem_stream ) )
|
2019-04-24 15:35:01 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
delete m_image;
|
2023-06-08 15:54:36 +00:00
|
|
|
m_imageType = new_image->GetType();
|
2019-04-24 15:35:01 +00:00
|
|
|
m_image = new_image.release();
|
2023-06-08 15:54:36 +00:00
|
|
|
|
|
|
|
// Create a new wxImage object from m_image
|
2023-01-03 21:54:20 +00:00
|
|
|
delete m_originalImage;
|
|
|
|
m_originalImage = new wxImage( *m_image );
|
2023-06-08 15:54:36 +00:00
|
|
|
|
2023-01-03 03:14:22 +00:00
|
|
|
rebuildBitmap();
|
2023-02-15 00:41:54 +00:00
|
|
|
updatePPI();
|
2019-04-24 15:35:01 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-08 15:54:36 +00:00
|
|
|
bool BITMAP_BASE::ReadImageFile( wxMemoryBuffer& aBuf )
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
2023-06-08 15:54:36 +00:00
|
|
|
// Store the original image data in m_imageData
|
|
|
|
m_imageData = aBuf;
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2023-06-08 15:54:36 +00:00
|
|
|
std::unique_ptr<wxImage> new_image = std::make_unique<wxImage>();
|
|
|
|
|
|
|
|
// Load the image from the buffer into new_image
|
|
|
|
wxMemoryInputStream mem_stream( m_imageData.GetData(), m_imageData.GetBufSize() );
|
|
|
|
|
|
|
|
if( !new_image->LoadFile( mem_stream ) )
|
2011-08-31 14:59:20 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
delete m_image;
|
2023-06-08 15:54:36 +00:00
|
|
|
m_imageType = new_image->GetType();
|
|
|
|
m_image = new_image.release();
|
|
|
|
|
|
|
|
// Create a new wxImage object from m_image
|
2023-01-03 21:54:20 +00:00
|
|
|
delete m_originalImage;
|
|
|
|
m_originalImage = new wxImage( *m_image );
|
2023-06-08 15:54:36 +00:00
|
|
|
|
2023-01-03 03:14:22 +00:00
|
|
|
rebuildBitmap();
|
2023-02-15 00:41:54 +00:00
|
|
|
updatePPI();
|
2023-01-17 16:19:44 +00:00
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-08 15:54:36 +00:00
|
|
|
bool BITMAP_BASE::ReadImageFile(const wxString& aFullFilename)
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
2023-06-08 15:54:36 +00:00
|
|
|
wxFileInputStream file_stream(aFullFilename);
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2023-06-08 15:54:36 +00:00
|
|
|
// Check if the file could be opened successfully
|
|
|
|
if (!file_stream.IsOk())
|
|
|
|
return false;
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2023-06-08 15:54:36 +00:00
|
|
|
return ReadImageFile(file_stream);
|
2011-08-31 14:59:20 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 21:23:17 +00:00
|
|
|
|
2023-06-08 15:54:36 +00:00
|
|
|
bool BITMAP_BASE::SaveImageData( wxOutputStream& aOutStream ) const
|
2013-10-18 17:38:03 +00:00
|
|
|
{
|
2023-06-08 15:54:36 +00:00
|
|
|
if( m_imageData.IsEmpty() )
|
2013-10-18 17:38:03 +00:00
|
|
|
{
|
2023-06-08 15:54:36 +00:00
|
|
|
// If m_imageData is empty, use wxImage::Save() method to write m_image contents to the stream.
|
|
|
|
wxBitmapType type = m_imageType == wxBITMAP_TYPE_JPEG ? wxBITMAP_TYPE_JPEG : wxBITMAP_TYPE_PNG;
|
2013-10-18 17:38:03 +00:00
|
|
|
|
2023-06-08 15:54:36 +00:00
|
|
|
if( !m_image->SaveFile( aOutStream, type ) )
|
2013-10-18 17:38:03 +00:00
|
|
|
{
|
2023-06-08 15:54:36 +00:00
|
|
|
return false;
|
2013-10-18 17:38:03 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-08 15:54:36 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Write the contents of m_imageData to the stream.
|
|
|
|
aOutStream.Write( m_imageData.GetData(), m_imageData.GetBufSize() );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-10-18 17:38:03 +00:00
|
|
|
}
|
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2023-06-08 15:54:36 +00:00
|
|
|
bool BITMAP_BASE::LoadLegacyData( LINE_READER& aLine, wxString& aErrorMsg )
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
|
|
|
wxMemoryOutputStream stream;
|
|
|
|
char* line;
|
|
|
|
|
|
|
|
while( true )
|
|
|
|
{
|
|
|
|
if( !aLine.ReadLine() )
|
2013-10-18 17:38:03 +00:00
|
|
|
{
|
|
|
|
aErrorMsg = wxT("Unexpected end of data");
|
2011-08-31 14:59:20 +00:00
|
|
|
return false;
|
2013-10-18 17:38:03 +00:00
|
|
|
}
|
2011-08-31 14:59:20 +00:00
|
|
|
|
|
|
|
line = aLine.Line();
|
2013-10-18 17:38:03 +00:00
|
|
|
|
2016-08-16 08:27:09 +00:00
|
|
|
if( strncasecmp( line, "EndData", 4 ) == 0 )
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
|
|
|
// all the PNG date is read.
|
|
|
|
// We expect here m_image and m_bitmap are void
|
|
|
|
m_image = new wxImage();
|
|
|
|
wxMemoryInputStream istream( stream );
|
2023-06-07 02:59:17 +00:00
|
|
|
m_image->LoadFile( istream, wxBITMAP_TYPE_ANY );
|
2011-08-31 14:59:20 +00:00
|
|
|
m_bitmap = new wxBitmap( *m_image );
|
2023-01-04 18:51:48 +00:00
|
|
|
m_originalImage = new wxImage( *m_image );
|
2023-06-08 15:54:36 +00:00
|
|
|
UpdateImageDataBuffer();
|
2011-08-31 14:59:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read PNG data, stored in hexadecimal,
|
|
|
|
// each byte = 2 hexadecimal digits and a space between 2 bytes
|
|
|
|
// and put it in memory stream buffer
|
|
|
|
int len = strlen( line );
|
2016-07-11 19:48:46 +00:00
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
for( ; len > 0; len -= 3, line += 3 )
|
|
|
|
{
|
|
|
|
int value = 0;
|
2016-07-11 19:48:46 +00:00
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
if( sscanf( line, "%X", &value ) == 1 )
|
|
|
|
stream.PutC( (char) value );
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-31 09:15:42 +00:00
|
|
|
const BOX2I BITMAP_BASE::GetBoundingBox() const
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
2022-08-31 09:15:42 +00:00
|
|
|
BOX2I bbox;
|
|
|
|
VECTOR2I size = GetSize();
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2022-08-31 09:15:42 +00:00
|
|
|
bbox.Inflate( size.x / 2, size.y / 2 );
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2022-08-31 09:15:42 +00:00
|
|
|
return bbox;
|
2011-08-31 14:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-02 22:59:38 +00:00
|
|
|
void BITMAP_BASE::DrawBitmap( wxDC* aDC, const VECTOR2I& aPos,
|
|
|
|
const KIGFX::COLOR4D& aBackgroundColor )
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
2021-07-15 19:26:35 +00:00
|
|
|
if( m_bitmap == nullptr )
|
2011-08-31 14:59:20 +00:00
|
|
|
return;
|
|
|
|
|
2022-01-01 06:04:08 +00:00
|
|
|
VECTOR2I pos = aPos;
|
2022-08-31 09:15:42 +00:00
|
|
|
VECTOR2I size = GetSize();
|
2011-08-31 14:59:20 +00:00
|
|
|
|
2016-02-22 21:23:17 +00:00
|
|
|
// This fixes a bug in OSX that should be fixed in the 3.0.3 version or later.
|
|
|
|
if( ( size.x == 0 ) || ( size.y == 0 ) )
|
|
|
|
return;
|
|
|
|
|
2011-08-31 14:59:20 +00:00
|
|
|
// To draw the bitmap, pos is the upper left corner position
|
|
|
|
pos.x -= size.x / 2;
|
|
|
|
pos.y -= size.y / 2;
|
|
|
|
|
|
|
|
double scale;
|
|
|
|
int logicalOriginX, logicalOriginY;
|
|
|
|
aDC->GetUserScale( &scale, &scale );
|
|
|
|
aDC->GetLogicalOrigin( &logicalOriginX, &logicalOriginY );
|
2020-01-11 17:44:54 +00:00
|
|
|
|
2022-08-04 12:25:52 +00:00
|
|
|
// We already have issues to draw a bitmap on the wxDC, depending on wxWidgets version.
|
2023-01-22 19:03:29 +00:00
|
|
|
// Now we have an issue on wxWidgets 3.1.6 to fix the clip area
|
2022-08-04 12:25:52 +00:00
|
|
|
// and the bitmap position when using TransformMatrix
|
2023-01-22 19:03:29 +00:00
|
|
|
// So for version == 3.1.6 do not use it
|
2022-08-04 12:25:52 +00:00
|
|
|
// Be carefull before changing the code.
|
2020-05-24 19:00:11 +00:00
|
|
|
bool useTransform = aDC->CanUseTransformMatrix();
|
2022-08-04 12:25:52 +00:00
|
|
|
|
2020-05-24 19:00:11 +00:00
|
|
|
wxAffineMatrix2D init_matrix = aDC->GetTransformMatrix();
|
|
|
|
|
2023-01-22 19:03:29 +00:00
|
|
|
// Note: clipping bitmap area was made to fix a minor issue in old versions of
|
|
|
|
// Kicad/wxWidgets (5.1 / wx 3.0)
|
|
|
|
// However SetClippingRegion creates a lot of issues (different ways to fix the
|
|
|
|
// position and size of the area, depending on wxWidget version)because it changes with
|
|
|
|
// each versions of wxWigets, so it is now disabled
|
|
|
|
// However the code is still here, just in case
|
|
|
|
// #define USE_CLIP_AREA
|
|
|
|
|
2022-01-20 15:15:39 +00:00
|
|
|
wxPoint clipAreaPos;
|
|
|
|
|
2020-05-24 19:00:11 +00:00
|
|
|
if( useTransform )
|
|
|
|
{
|
|
|
|
wxAffineMatrix2D matrix = aDC->GetTransformMatrix();
|
|
|
|
matrix.Translate( pos.x, pos.y );
|
|
|
|
matrix.Scale( GetScalingFactor(), GetScalingFactor() );
|
|
|
|
aDC->SetTransformMatrix( matrix );
|
2022-08-04 12:25:52 +00:00
|
|
|
// Needed on wx <= 3.1.5, and this is strange...
|
|
|
|
// Nevertheless, this code has problem (the bitmap is not seen)
|
|
|
|
// with wx version > 3.1.5
|
2022-01-20 15:15:39 +00:00
|
|
|
clipAreaPos.x = pos.x;
|
|
|
|
clipAreaPos.y = pos.y;
|
2022-08-04 12:25:52 +00:00
|
|
|
|
2020-05-24 19:00:11 +00:00
|
|
|
pos.x = pos.y = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aDC->SetUserScale( scale * GetScalingFactor(), scale * GetScalingFactor() );
|
|
|
|
aDC->SetLogicalOrigin( logicalOriginX / GetScalingFactor(),
|
|
|
|
logicalOriginY / GetScalingFactor() );
|
|
|
|
|
|
|
|
pos.x = KiROUND( pos.x / GetScalingFactor() );
|
|
|
|
pos.y = KiROUND( pos.y / GetScalingFactor() );
|
2022-01-08 01:15:59 +00:00
|
|
|
size.x = KiROUND( size.x / GetScalingFactor() );
|
|
|
|
size.y = KiROUND( size.y / GetScalingFactor() );
|
2022-01-20 15:15:39 +00:00
|
|
|
clipAreaPos.x = pos.x;
|
|
|
|
clipAreaPos.y = pos.y;
|
2020-05-24 19:00:11 +00:00
|
|
|
}
|
2020-01-11 17:55:30 +00:00
|
|
|
|
2023-01-22 19:03:29 +00:00
|
|
|
#ifdef USE_CLIP_AREA
|
2022-01-08 01:15:59 +00:00
|
|
|
aDC->DestroyClippingRegion();
|
2022-08-31 09:15:42 +00:00
|
|
|
aDC->SetClippingRegion( clipAreaPos, wxSize( size.x, size.y ) );
|
2023-01-22 19:03:29 +00:00
|
|
|
#endif
|
2022-01-08 01:15:59 +00:00
|
|
|
|
2023-07-02 22:59:38 +00:00
|
|
|
if( aBackgroundColor != COLOR4D::UNSPECIFIED && m_bitmap->HasAlpha() )
|
|
|
|
{
|
|
|
|
// Most printers don't support transparent images properly,
|
|
|
|
// so blend the image with background color.
|
|
|
|
|
|
|
|
int w = m_bitmap->GetWidth();
|
|
|
|
int h = m_bitmap->GetHeight();
|
|
|
|
|
|
|
|
wxImage image( w, h );
|
|
|
|
wxColour bgColor = aBackgroundColor.ToColour();
|
|
|
|
|
|
|
|
image.SetRGB( wxRect( 0, 0, w, h ), bgColor.Red(), bgColor.Green(), bgColor.Blue() );
|
|
|
|
image.Paste( m_bitmap->ConvertToImage(), 0, 0, wxIMAGE_ALPHA_BLEND_COMPOSE );
|
|
|
|
|
|
|
|
if( GetGRForceBlackPenState() )
|
|
|
|
image = image.ConvertToGreyscale();
|
|
|
|
|
|
|
|
aDC->DrawBitmap( wxBitmap( image ), pos.x, pos.y, true );
|
|
|
|
}
|
|
|
|
else if( GetGRForceBlackPenState() )
|
2020-01-11 17:55:30 +00:00
|
|
|
{
|
|
|
|
wxBitmap result( m_bitmap->ConvertToImage().ConvertToGreyscale() );
|
|
|
|
aDC->DrawBitmap( result, pos.x, pos.y, true );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aDC->DrawBitmap( *m_bitmap, pos.x, pos.y, true );
|
|
|
|
}
|
|
|
|
|
2020-05-24 19:00:11 +00:00
|
|
|
if( useTransform )
|
|
|
|
aDC->SetTransformMatrix( init_matrix );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aDC->SetUserScale( scale, scale );
|
|
|
|
aDC->SetLogicalOrigin( logicalOriginX, logicalOriginY );
|
|
|
|
}
|
2022-01-08 01:15:59 +00:00
|
|
|
|
2023-01-22 19:03:29 +00:00
|
|
|
#ifdef USE_CLIP_AREA
|
2022-01-08 01:15:59 +00:00
|
|
|
aDC->DestroyClippingRegion();
|
2023-01-22 19:03:29 +00:00
|
|
|
#endif
|
2011-08-31 14:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-31 09:15:42 +00:00
|
|
|
VECTOR2I BITMAP_BASE::GetSize() const
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
2022-08-31 09:15:42 +00:00
|
|
|
VECTOR2I size;
|
2011-08-31 14:59:20 +00:00
|
|
|
|
|
|
|
if( m_bitmap )
|
|
|
|
{
|
|
|
|
size.x = m_bitmap->GetWidth();
|
|
|
|
size.y = m_bitmap->GetHeight();
|
|
|
|
|
// Dick Hollenbeck's KiROUND R&D
// This provides better project control over rounding to int from double
// than wxRound() did. This scheme provides better logging in Debug builds
// and it provides for compile time calculation of constants.
#include <stdio.h>
#include <assert.h>
#include <limits.h>
//-----<KiROUND KIT>------------------------------------------------------------
/**
* KiROUND
* rounds a floating point number to an int using
* "round halfway cases away from zero".
* In Debug build an assert fires if will not fit into an int.
*/
#if defined( DEBUG )
// DEBUG: a macro to capture line and file, then calls this inline
static inline int KiRound( double v, int line, const char* filename )
{
v = v < 0 ? v - 0.5 : v + 0.5;
if( v > INT_MAX + 0.5 )
{
printf( "%s: in file %s on line %d, val: %.16g too ' > 0 ' for int\n", __FUNCTION__, filename, line, v );
}
else if( v < INT_MIN - 0.5 )
{
printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n", __FUNCTION__, filename, line, v );
}
return int( v );
}
#define KiROUND( v ) KiRound( v, __LINE__, __FILE__ )
#else
// RELEASE: a macro so compile can pre-compute constants.
#define KiROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 )
#endif
//-----</KiROUND KIT>-----------------------------------------------------------
// Only a macro is compile time calculated, an inline function causes a static constructor
// in a situation like this.
// Therefore the Release build is best done with a MACRO not an inline function.
int Computed = KiROUND( 14.3 * 8 );
int main( int argc, char** argv )
{
for( double d = double(INT_MAX)-1; d < double(INT_MAX)+8; d += 2.0 )
{
int i = KiROUND( d );
printf( "t: %d %.16g\n", i, d );
}
return 0;
}
2012-04-19 06:55:45 +00:00
|
|
|
size.x = KiROUND( size.x * GetScalingFactor() );
|
|
|
|
size.y = KiROUND( size.y * GetScalingFactor() );
|
2011-08-31 14:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BITMAP_BASE::Mirror( bool aVertically )
|
|
|
|
{
|
|
|
|
if( m_image )
|
|
|
|
{
|
2023-04-23 09:30:02 +00:00
|
|
|
// wxImage::Mirror() clear some parameters of the original image.
|
|
|
|
// We need to restore them, especially resolution and unit, to be
|
|
|
|
// sure image parameters saved in file are the right parameters, not
|
|
|
|
// the defualt values
|
|
|
|
int resX = m_image->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONX );
|
|
|
|
int resY = m_image->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONY );
|
|
|
|
int unit = m_image->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONUNIT );
|
|
|
|
|
2023-01-03 03:14:22 +00:00
|
|
|
*m_image = m_image->Mirror( not aVertically );
|
2023-04-23 09:30:02 +00:00
|
|
|
|
|
|
|
m_image->SetOption( wxIMAGE_OPTION_RESOLUTIONUNIT , unit);
|
|
|
|
m_image->SetOption( wxIMAGE_OPTION_RESOLUTIONX, resX);
|
|
|
|
m_image->SetOption( wxIMAGE_OPTION_RESOLUTIONY, resY);
|
|
|
|
|
2023-04-23 14:00:05 +00:00
|
|
|
if( aVertically )
|
|
|
|
m_isMirroredY = !m_isMirroredY;
|
|
|
|
else
|
|
|
|
m_isMirroredX = !m_isMirroredX;
|
|
|
|
|
2023-01-03 21:54:20 +00:00
|
|
|
rebuildBitmap( false );
|
2023-06-08 15:54:36 +00:00
|
|
|
UpdateImageDataBuffer();
|
2011-08-31 14:59:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BITMAP_BASE::Rotate( bool aRotateCCW )
|
|
|
|
{
|
|
|
|
if( m_image )
|
|
|
|
{
|
2023-04-23 09:30:02 +00:00
|
|
|
// wxImage::Rotate90() clear some parameters of the original image.
|
|
|
|
// We need to restore them, especially resolution and unit, to be
|
|
|
|
// sure image parameters saved in file are the right parameters, not
|
|
|
|
// the defualt values
|
|
|
|
int resX = m_image->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONX );
|
|
|
|
int resY = m_image->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONY );
|
|
|
|
int unit = m_image->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONUNIT );
|
|
|
|
|
2023-01-03 03:14:22 +00:00
|
|
|
*m_image = m_image->Rotate90( aRotateCCW );
|
2023-04-23 09:30:02 +00:00
|
|
|
|
|
|
|
m_image->SetOption( wxIMAGE_OPTION_RESOLUTIONUNIT , unit);
|
|
|
|
m_image->SetOption( wxIMAGE_OPTION_RESOLUTIONX, resX);
|
|
|
|
m_image->SetOption( wxIMAGE_OPTION_RESOLUTIONY, resY);
|
|
|
|
|
2023-01-03 21:54:20 +00:00
|
|
|
m_rotation += ( aRotateCCW ? -ANGLE_90 : ANGLE_90 );
|
|
|
|
rebuildBitmap( false );
|
2023-06-08 15:54:36 +00:00
|
|
|
UpdateImageDataBuffer();
|
2023-01-03 03:14:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BITMAP_BASE::ConvertToGreyscale()
|
|
|
|
{
|
|
|
|
if( m_image )
|
|
|
|
{
|
|
|
|
*m_image = m_image->ConvertToGreyscale();
|
2023-01-12 02:07:50 +00:00
|
|
|
*m_originalImage = m_originalImage->ConvertToGreyscale();
|
2023-01-03 03:14:22 +00:00
|
|
|
rebuildBitmap();
|
2023-06-08 15:54:36 +00:00
|
|
|
UpdateImageDataBuffer();
|
2011-08-31 14:59:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-01 06:04:08 +00:00
|
|
|
void BITMAP_BASE::PlotImage( PLOTTER* aPlotter, const VECTOR2I& aPos,
|
2021-07-26 17:28:37 +00:00
|
|
|
const COLOR4D& aDefaultColor,
|
2021-03-06 09:27:41 +00:00
|
|
|
int aDefaultPensize ) const
|
2011-08-31 14:59:20 +00:00
|
|
|
{
|
2021-07-15 19:26:35 +00:00
|
|
|
if( m_image == nullptr )
|
2011-08-31 14:59:20 +00:00
|
|
|
return;
|
|
|
|
|
2016-02-22 21:23:17 +00:00
|
|
|
// These 2 lines are useful only for plotters that cannot plot a bitmap
|
2012-09-15 12:13:03 +00:00
|
|
|
// and plot a rectangle instead of.
|
2012-05-03 18:37:56 +00:00
|
|
|
aPlotter->SetColor( aDefaultColor );
|
|
|
|
aPlotter->SetCurrentLineWidth( aDefaultPensize );
|
2011-08-31 14:59:20 +00:00
|
|
|
aPlotter->PlotImage( *m_image, aPos, GetScalingFactor() );
|
|
|
|
}
|
2023-06-08 15:54:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
void BITMAP_BASE::UpdateImageDataBuffer()
|
|
|
|
{
|
|
|
|
if( m_image )
|
|
|
|
{
|
|
|
|
wxMemoryOutputStream stream;
|
|
|
|
wxBitmapType type = m_imageType == wxBITMAP_TYPE_JPEG ? wxBITMAP_TYPE_JPEG : wxBITMAP_TYPE_PNG;
|
|
|
|
|
|
|
|
if( !m_image->SaveFile( stream, type ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_imageData.GetWriteBuf( stream.GetLength() );
|
|
|
|
stream.CopyTo( m_imageData.GetData(), stream.GetLength() );
|
|
|
|
m_imageData.SetDataLen( stream.GetLength() );
|
|
|
|
}
|
|
|
|
}
|