32#include <wx/mstream.h>
35#include <wx/wfstream.h>
118 wxString resStr =
m_originalImage->GetOption( wxIMAGE_OPTION_RESOLUTIONX );
120 resStr.ToCDouble( &dpiX );
124 if(
m_originalImage->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONUNIT ) == wxIMAGE_RESOLUTION_CM )
139 int dpiX =
m_originalImage->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONX );
143 if(
m_originalImage->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONUNIT ) == wxIMAGE_RESOLUTION_CM )
174 size_t dataSize = aInStream.GetLength();
179 std::unique_ptr<wxImage> new_image = std::make_unique<wxImage>();
182 wxMemoryInputStream mem_stream(
m_imageData.GetData(), dataSize );
183 if( !new_image->LoadFile( mem_stream ) )
195 std::unique_ptr<wxImage> new_image = std::make_unique<wxImage>();
200 if( !new_image->LoadFile( mem_stream ) )
209 wxFileInputStream file_stream(aFullFilename);
212 if (!file_stream.IsOk())
221 if( !aImage.IsOk() || aImage.GetWidth() == 0 || aImage.GetHeight() == 0 )
225 m_image =
new wxImage( aImage );
244 wxBitmapType type =
m_imageType == wxBITMAP_TYPE_JPEG ? wxBITMAP_TYPE_JPEG
247 if( !
m_image->SaveFile( aOutStream, type ) )
264 wxMemoryOutputStream stream;
271 aErrorMsg = wxT(
"Unexpected end of data" );
277 if( strncasecmp( line,
"EndData", 4 ) == 0 )
282 wxMemoryInputStream istream( stream );
283 m_image->LoadFile( istream, wxBITMAP_TYPE_ANY );
293 int len = strlen( line );
295 for( ; len > 0; len -= 3, line += 3 )
299 if( sscanf( line,
"%X", &value ) == 1 )
300 stream.PutC( (
char) value );
333 if( ( size.
x == 0 ) || ( size.
y == 0 ) )
341 int logicalOriginX, logicalOriginY;
343 aDC->GetLogicalOrigin( &logicalOriginX, &logicalOriginY );
350 bool useTransform = aDC->CanUseTransformMatrix();
352 wxAffineMatrix2D init_matrix = aDC->GetTransformMatrix();
366 wxAffineMatrix2D matrix = aDC->GetTransformMatrix();
367 matrix.Translate( pos.
x, pos.
y );
369 aDC->SetTransformMatrix( matrix );
374 clipAreaPos.x = pos.
x;
375 clipAreaPos.y = pos.
y;
389 clipAreaPos.x = pos.
x;
390 clipAreaPos.y = pos.
y;
394 aDC->DestroyClippingRegion();
395 aDC->SetClippingRegion( clipAreaPos, wxSize( size.
x, size.
y ) );
406 wxImage
image( w, h );
407 wxColour bgColor = aBackgroundColor.
ToColour();
409 image.SetRGB( wxRect( 0, 0, w, h ), bgColor.Red(), bgColor.Green(), bgColor.Blue() );
410 image.Paste(
m_bitmap->ConvertToImage(), 0, 0, wxIMAGE_ALPHA_BLEND_COMPOSE );
415 aDC->DrawBitmap( wxBitmap(
image ), pos.
x, pos.
y,
true );
419 wxBitmap
result(
m_bitmap->ConvertToImage().ConvertToGreyscale() );
420 aDC->DrawBitmap(
result, pos.
x, pos.
y,
true );
424 aDC->DrawBitmap( *
m_bitmap, pos.
x, pos.
y,
true );
428 aDC->SetTransformMatrix( init_matrix );
432 aDC->SetLogicalOrigin( logicalOriginX, logicalOriginY );
436 aDC->DestroyClippingRegion();
457 const int w = aImage.GetWidth();
458 const int h = aImage.GetHeight();
460 if( w == 0 || h == 0 )
467 unsigned char* rgb = aImage.GetData();
468 unsigned char* alpha = aImage.HasAlpha() ? aImage.GetAlpha() :
nullptr;
474 for(
int y = 0; y < h; ++y )
476 unsigned char* rowRgb = rgb + y * w * bpp;
478 for(
int lo = 0, hi = w - 1; lo < hi; ++lo, --hi )
480 std::swap( rowRgb[lo * bpp + 0], rowRgb[hi * bpp + 0] );
481 std::swap( rowRgb[lo * bpp + 1], rowRgb[hi * bpp + 1] );
482 std::swap( rowRgb[lo * bpp + 2], rowRgb[hi * bpp + 2] );
487 unsigned char* rowAlpha = alpha + y * w;
489 for(
int lo = 0, hi = w - 1; lo < hi; ++lo, --hi )
490 std::swap( rowAlpha[lo], rowAlpha[hi] );
497 const size_t rowBytes = w * bpp;
498 std::vector<unsigned char> tmpRgb( rowBytes );
500 for(
int lo = 0, hi = h - 1; lo < hi; ++lo, --hi )
502 unsigned char* rowLo = rgb + lo * rowBytes;
503 unsigned char* rowHi = rgb + hi * rowBytes;
504 memcpy( tmpRgb.data(), rowLo, rowBytes );
505 memcpy( rowLo, rowHi, rowBytes );
506 memcpy( rowHi, tmpRgb.data(), rowBytes );
511 std::vector<unsigned char> tmpAlpha( w );
513 for(
int lo = 0, hi = h - 1; lo < hi; ++lo, --hi )
515 unsigned char* aLo = alpha + lo * w;
516 unsigned char* aHi = alpha + hi * w;
517 memcpy( tmpAlpha.data(), aLo, w );
518 memcpy( aLo, aHi, w );
519 memcpy( aHi, tmpAlpha.data(), w );
528 const int w = aImage.GetWidth();
529 const int h = aImage.GetHeight();
531 if( w == 0 || h == 0 )
536 unsigned char* rgb = aImage.GetData();
538 for(
int i = 0; i < w * h * 3; ++i )
539 rgb[i] = 255 - rgb[i];
566 wxString resX =
m_image->GetOption( wxIMAGE_OPTION_RESOLUTIONX );
567 wxString resY =
m_image->GetOption( wxIMAGE_OPTION_RESOLUTIONY );
568 int unit =
m_image->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONUNIT );
573 m_image->SetOption( wxIMAGE_OPTION_RESOLUTIONUNIT, unit );
574 m_image->SetOption( wxIMAGE_OPTION_RESOLUTIONX, resX );
575 m_image->SetOption( wxIMAGE_OPTION_RESOLUTIONY, resY );
625 int aDefaultPensize )
const
632 aPlotter->
SetColor( aDefaultColor );
642 wxMemoryOutputStream stream;
643 wxBitmapType type =
m_imageType == wxBITMAP_TYPE_JPEG ? wxBITMAP_TYPE_JPEG
646 if( !
m_image->SaveFile( stream, type ) )
650 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 InvertColors()
Invert the colours of the image (e.g.
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.).
void ConvertColourToAlpha(const wxColour &aColour)
Convert aColour to transparent in the image, proportionally to the 'distance' of each pixel from aCol...
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))
static void invertImageInPlace(wxImage &aImage)
Invert the colour of every pixel of the wxImage in-place without allocating a new image.
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)
void ConvertColourToAlphaInPlace(wxImage &aImage, const wxColour &aColour)
Apply the "Color to Alpha" algorithm in place, converting aColour to transparent.
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I