35#include <wx/mstream.h>
38#include <wx/wfstream.h>
116 int dpiX =
m_originalImage->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONX );
120 if(
m_originalImage->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONUNIT ) == wxIMAGE_RESOLUTION_CM )
149 size_t dataSize = aInStream.GetLength();
154 std::unique_ptr<wxImage> new_image = std::make_unique<wxImage>();
157 wxMemoryInputStream mem_stream(
m_imageData.GetData(), dataSize );
158 if( !new_image->LoadFile( mem_stream ) )
170 std::unique_ptr<wxImage> new_image = std::make_unique<wxImage>();
175 if( !new_image->LoadFile( mem_stream ) )
184 wxFileInputStream file_stream(aFullFilename);
187 if (!file_stream.IsOk())
196 if( !aImage.IsOk() || aImage.GetWidth() == 0 || aImage.GetHeight() == 0 )
200 m_image =
new wxImage( aImage );
219 wxBitmapType type =
m_imageType == wxBITMAP_TYPE_JPEG ? wxBITMAP_TYPE_JPEG
222 if( !
m_image->SaveFile( aOutStream, type ) )
239 wxMemoryOutputStream stream;
246 aErrorMsg = wxT(
"Unexpected end of data" );
252 if( strncasecmp( line,
"EndData", 4 ) == 0 )
257 wxMemoryInputStream istream( stream );
258 m_image->LoadFile( istream, wxBITMAP_TYPE_ANY );
268 int len = strlen( line );
270 for( ; len > 0; len -= 3, line += 3 )
274 if( sscanf( line,
"%X", &value ) == 1 )
275 stream.PutC( (
char) value );
308 if( ( size.
x == 0 ) || ( size.
y == 0 ) )
316 int logicalOriginX, logicalOriginY;
318 aDC->GetLogicalOrigin( &logicalOriginX, &logicalOriginY );
325 bool useTransform = aDC->CanUseTransformMatrix();
327 wxAffineMatrix2D init_matrix = aDC->GetTransformMatrix();
341 wxAffineMatrix2D matrix = aDC->GetTransformMatrix();
342 matrix.Translate( pos.
x, pos.
y );
344 aDC->SetTransformMatrix( matrix );
349 clipAreaPos.x = pos.
x;
350 clipAreaPos.y = pos.
y;
364 clipAreaPos.x = pos.
x;
365 clipAreaPos.y = pos.
y;
369 aDC->DestroyClippingRegion();
370 aDC->SetClippingRegion( clipAreaPos, wxSize( size.
x, size.
y ) );
381 wxImage
image( w, h );
382 wxColour bgColor = aBackgroundColor.
ToColour();
384 image.SetRGB( wxRect( 0, 0, w, h ), bgColor.Red(), bgColor.Green(), bgColor.Blue() );
385 image.Paste(
m_bitmap->ConvertToImage(), 0, 0, wxIMAGE_ALPHA_BLEND_COMPOSE );
390 aDC->DrawBitmap( wxBitmap(
image ), pos.
x, pos.
y,
true );
394 wxBitmap
result(
m_bitmap->ConvertToImage().ConvertToGreyscale() );
395 aDC->DrawBitmap(
result, pos.
x, pos.
y,
true );
399 aDC->DrawBitmap( *
m_bitmap, pos.
x, pos.
y,
true );
403 aDC->SetTransformMatrix( init_matrix );
407 aDC->SetLogicalOrigin( logicalOriginX, logicalOriginY );
411 aDC->DestroyClippingRegion();
432 const int w = aImage.GetWidth();
433 const int h = aImage.GetHeight();
435 if( w == 0 || h == 0 )
438 unsigned char* rgb = aImage.GetData();
439 unsigned char* alpha = aImage.HasAlpha() ? aImage.GetAlpha() :
nullptr;
445 for(
int y = 0; y < h; ++y )
447 unsigned char* rowRgb = rgb + y * w * bpp;
449 for(
int lo = 0, hi = w - 1; lo < hi; ++lo, --hi )
451 std::swap( rowRgb[lo * bpp + 0], rowRgb[hi * bpp + 0] );
452 std::swap( rowRgb[lo * bpp + 1], rowRgb[hi * bpp + 1] );
453 std::swap( rowRgb[lo * bpp + 2], rowRgb[hi * bpp + 2] );
458 unsigned char* rowAlpha = alpha + y * w;
460 for(
int lo = 0, hi = w - 1; lo < hi; ++lo, --hi )
461 std::swap( rowAlpha[lo], rowAlpha[hi] );
468 const size_t rowBytes = w * bpp;
469 std::vector<unsigned char> tmpRgb( rowBytes );
471 for(
int lo = 0, hi = h - 1; lo < hi; ++lo, --hi )
473 unsigned char* rowLo = rgb + lo * rowBytes;
474 unsigned char* rowHi = rgb + hi * rowBytes;
475 memcpy( tmpRgb.data(), rowLo, rowBytes );
476 memcpy( rowLo, rowHi, rowBytes );
477 memcpy( rowHi, tmpRgb.data(), rowBytes );
482 std::vector<unsigned char> tmpAlpha( w );
484 for(
int lo = 0, hi = h - 1; lo < hi; ++lo, --hi )
486 unsigned char* aLo = alpha + lo * w;
487 unsigned char* aHi = alpha + hi * w;
488 memcpy( tmpAlpha.data(), aLo, w );
489 memcpy( aLo, aHi, w );
490 memcpy( aHi, tmpAlpha.data(), w );
519 int resX =
m_image->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONX );
520 int resY =
m_image->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONY );
521 int unit =
m_image->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONUNIT );
526 m_image->SetOption( wxIMAGE_OPTION_RESOLUTIONUNIT, unit );
527 m_image->SetOption( wxIMAGE_OPTION_RESOLUTIONX, resX );
528 m_image->SetOption( wxIMAGE_OPTION_RESOLUTIONY, resY );
552 int aDefaultPensize )
const
559 aPlotter->
SetColor( aDefaultColor );
569 wxMemoryOutputStream stream;
570 wxBitmapType type =
m_imageType == wxBITMAP_TYPE_JPEG ? wxBITMAP_TYPE_JPEG
573 if( !
m_image->SaveFile( stream, type ) )
577 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
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