31#include <wx/mstream.h>
34#include <wx/wfstream.h>
117 wxString resStr =
m_originalImage->GetOption( wxIMAGE_OPTION_RESOLUTIONX );
119 resStr.ToCDouble( &dpiX );
123 if(
m_originalImage->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONUNIT ) == wxIMAGE_RESOLUTION_CM )
138 int dpiX =
m_originalImage->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONX );
142 if(
m_originalImage->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONUNIT ) == wxIMAGE_RESOLUTION_CM )
173 size_t dataSize = aInStream.GetLength();
178 std::unique_ptr<wxImage> new_image = std::make_unique<wxImage>();
181 wxMemoryInputStream mem_stream(
m_imageData.GetData(), dataSize );
182 if( !new_image->LoadFile( mem_stream ) )
194 std::unique_ptr<wxImage> new_image = std::make_unique<wxImage>();
199 if( !new_image->LoadFile( mem_stream ) )
208 wxFileInputStream file_stream(aFullFilename);
211 if (!file_stream.IsOk())
220 if( !aImage.IsOk() || aImage.GetWidth() == 0 || aImage.GetHeight() == 0 )
224 m_image =
new wxImage( aImage );
243 wxBitmapType type =
m_imageType == wxBITMAP_TYPE_JPEG ? wxBITMAP_TYPE_JPEG
246 if( !
m_image->SaveFile( aOutStream, type ) )
263 wxMemoryOutputStream stream;
270 aErrorMsg = wxT(
"Unexpected end of data" );
276 if( strncasecmp( line,
"EndData", 4 ) == 0 )
281 wxMemoryInputStream istream( stream );
282 m_image->LoadFile( istream, wxBITMAP_TYPE_ANY );
292 int len = strlen( line );
294 for( ; len > 0; len -= 3, line += 3 )
298 if( sscanf( line,
"%X", &value ) == 1 )
299 stream.PutC( (
char) value );
332 if( ( size.
x == 0 ) || ( size.
y == 0 ) )
340 int logicalOriginX, logicalOriginY;
342 aDC->GetLogicalOrigin( &logicalOriginX, &logicalOriginY );
349 bool useTransform = aDC->CanUseTransformMatrix();
351 wxAffineMatrix2D init_matrix = aDC->GetTransformMatrix();
365 wxAffineMatrix2D matrix = aDC->GetTransformMatrix();
366 matrix.Translate( pos.
x, pos.
y );
368 aDC->SetTransformMatrix( matrix );
373 clipAreaPos.x = pos.
x;
374 clipAreaPos.y = pos.
y;
388 clipAreaPos.x = pos.
x;
389 clipAreaPos.y = pos.
y;
393 aDC->DestroyClippingRegion();
394 aDC->SetClippingRegion( clipAreaPos, wxSize( size.
x, size.
y ) );
405 wxImage
image( w, h );
406 wxColour bgColor = aBackgroundColor.
ToColour();
408 image.SetRGB( wxRect( 0, 0, w, h ), bgColor.Red(), bgColor.Green(), bgColor.Blue() );
409 image.Paste(
m_bitmap->ConvertToImage(), 0, 0, wxIMAGE_ALPHA_BLEND_COMPOSE );
414 aDC->DrawBitmap( wxBitmap(
image ), pos.
x, pos.
y,
true );
418 wxBitmap
result(
m_bitmap->ConvertToImage().ConvertToGreyscale() );
419 aDC->DrawBitmap(
result, pos.
x, pos.
y,
true );
423 aDC->DrawBitmap( *
m_bitmap, pos.
x, pos.
y,
true );
427 aDC->SetTransformMatrix( init_matrix );
431 aDC->SetLogicalOrigin( logicalOriginX, logicalOriginY );
435 aDC->DestroyClippingRegion();
456 const int w = aImage.GetWidth();
457 const int h = aImage.GetHeight();
459 if( w == 0 || h == 0 )
462 unsigned char* rgb = aImage.GetData();
463 unsigned char* alpha = aImage.HasAlpha() ? aImage.GetAlpha() :
nullptr;
469 for(
int y = 0; y < h; ++y )
471 unsigned char* rowRgb = rgb + y * w * bpp;
473 for(
int lo = 0, hi = w - 1; lo < hi; ++lo, --hi )
475 std::swap( rowRgb[lo * bpp + 0], rowRgb[hi * bpp + 0] );
476 std::swap( rowRgb[lo * bpp + 1], rowRgb[hi * bpp + 1] );
477 std::swap( rowRgb[lo * bpp + 2], rowRgb[hi * bpp + 2] );
482 unsigned char* rowAlpha = alpha + y * w;
484 for(
int lo = 0, hi = w - 1; lo < hi; ++lo, --hi )
485 std::swap( rowAlpha[lo], rowAlpha[hi] );
492 const size_t rowBytes = w * bpp;
493 std::vector<unsigned char> tmpRgb( rowBytes );
495 for(
int lo = 0, hi = h - 1; lo < hi; ++lo, --hi )
497 unsigned char* rowLo = rgb + lo * rowBytes;
498 unsigned char* rowHi = rgb + hi * rowBytes;
499 memcpy( tmpRgb.data(), rowLo, rowBytes );
500 memcpy( rowLo, rowHi, rowBytes );
501 memcpy( rowHi, tmpRgb.data(), rowBytes );
506 std::vector<unsigned char> tmpAlpha( w );
508 for(
int lo = 0, hi = h - 1; lo < hi; ++lo, --hi )
510 unsigned char* aLo = alpha + lo * w;
511 unsigned char* aHi = alpha + hi * w;
512 memcpy( tmpAlpha.data(), aLo, w );
513 memcpy( aLo, aHi, w );
514 memcpy( aHi, tmpAlpha.data(), w );
544 wxString resX =
m_image->GetOption( wxIMAGE_OPTION_RESOLUTIONX );
545 wxString resY =
m_image->GetOption( wxIMAGE_OPTION_RESOLUTIONY );
546 int unit =
m_image->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONUNIT );
551 m_image->SetOption( wxIMAGE_OPTION_RESOLUTIONUNIT, unit );
552 m_image->SetOption( wxIMAGE_OPTION_RESOLUTIONX, resX );
553 m_image->SetOption( wxIMAGE_OPTION_RESOLUTIONY, resY );
577 int aDefaultPensize )
const
584 aPlotter->
SetColor( aDefaultColor );
594 wxMemoryOutputStream stream;
595 wxBitmapType type =
m_imageType == wxBITMAP_TYPE_JPEG ? wxBITMAP_TYPE_JPEG
598 if( !
m_image->SaveFile( stream, type ) )
602 stream.CopyTo(
m_imageData.GetData(), stream.GetLength() );
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
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 #SaveData.
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.
void PlotImage(PLOTTER *aPlotter, const VECTOR2I &aPos, const KIGFX::COLOR4D &aDefaultColor, int aDefaultPensize) const
Plot bitmap on plotter.
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.
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.
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.).
wxBitmap * m_bitmap
The bitmap used to draw/plot image.
void ConvertToGreyscale()
bool SetImage(const wxImage &aImage)
Set the image from an existing wxImage.
bool ReadImageFile(const wxString &aFullFilename)
Reads and stores in memory an image file.
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.
void DrawBitmap(wxDC *aDC, const VECTOR2I &aPos, const KIGFX::COLOR4D &aBackgroundColor=KIGFX::COLOR4D::UNSPECIFIED) const
int GetLegacyPPI() const
Recompute the PPI the way it was computed before the pixels/cm truncation fix.
BITMAP_BASE(const VECTOR2I &pos=VECTOR2I(0, 0))
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
A color representation with 4 components: red, green, blue, alpha.
wxColour ToColour() const
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
virtual char * ReadLine()=0
Read a line of text into the buffer and increments the line number counter.
char * Line() const
Return a pointer to the last line that was read in.
Base plotter engine class.
virtual void PlotImage(const wxImage &aImage, const VECTOR2I &aPos, double aScaleFactor)
Only PostScript plotters can plot bitmaps.
virtual void SetCurrentLineWidth(int width, void *aData=nullptr)=0
Set the line width for the next drawing.
virtual void SetColor(const COLOR4D &color)=0
static constexpr EDA_ANGLE ANGLE_0
static constexpr EDA_ANGLE ANGLE_90
bool GetGRForceBlackPenState(void)
@ LEFT_RIGHT
Flip left to right (around the Y axis)
@ TOP_BOTTOM
Flip top to bottom (around the X axis)
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I