30#include <wx/imagjpeg.h>
31#include <wx/mstream.h>
36static wxImage
makeImage(
int aWidth,
int aHeight,
unsigned char aFill )
38 wxImage
image( aWidth, aHeight );
39 image.SetRGB( wxRect( 0, 0, aWidth, aHeight ), aFill, aFill, aFill );
44static void fillRect( wxImage& aImage,
const wxRect& aRect,
unsigned char aValue )
46 aImage.SetRGB( aRect, aValue, aValue, aValue );
79 for(
int y = 0; y < 16; y++ )
81 for(
int x = 0; x < 16; x++ )
82 image.SetAlpha( x, y, x < 8 ? wxIMAGE_ALPHA_TRANSPARENT : wxIMAGE_ALPHA_OPAQUE );
87 BOOST_CHECK_MESSAGE( bilevel.GetGreen( 2, 8 ) == 255,
"a transparent dark pixel must not be traced" );
88 BOOST_CHECK_MESSAGE( bilevel.GetGreen( 12, 8 ) == 0,
"an opaque dark pixel must be traced" );
108 const double expectedArea = ( 40.0 * 40.0 ) - ( 20.0 * 20.0 );
109 BOOST_CHECK_CLOSE(
std::abs( traced.
Area() ), expectedArea, 5.0 );
121 if( !wxImage::FindHandler( wxBITMAP_TYPE_PNG ) )
122 wxImage::AddHandler(
new wxPNGHandler );
127 wxMemoryOutputStream stream;
129 BOOST_REQUIRE( aImage.SaveFile( stream, wxBITMAP_TYPE_PNG ) );
131 size_t length = stream.GetLength();
132 wxMemoryBuffer buffer( length );
134 stream.CopyTo( buffer.GetWriteBuf( length ), length );
135 buffer.SetDataLen( length );
170 for(
const std::unique_ptr<IMPORTED_SHAPE>& shape : importer.
GetShapes() )
174 BOOST_REQUIRE_MESSAGE( polygon,
"a traced area must arrive as a polygon" );
175 BOOST_CHECK_MESSAGE( polygon->
IsFilled(),
"a traced area must arrive filled" );
178 BOOST_CHECK_MESSAGE( polygon->
GetStroke().
GetWidth() == -1,
"a traced area must arrive with no outline" );
187 for(
int bar = 0; bar < 3; bar++ )
200 for(
const std::unique_ptr<IMPORTED_SHAPE>& shape : importer.
GetShapes() )
202 BOX2D bbox = shape->GetBoundingBox();
204 BOOST_CHECK_CLOSE( bbox.
GetWidth(), 4.0 * 25.4 / 300.0, 15.0 );
205 BOOST_CHECK_CLOSE( bbox.
GetHeight(), 100.0 * 25.4 / 300.0, 5.0 );
213 if( !wxImage::FindHandler( wxBITMAP_TYPE_JPEG ) )
214 wxImage::AddHandler(
new wxJPEGHandler );
216 wxMemoryOutputStream jpeg;
217 BOOST_REQUIRE( aImage.SaveFile( jpeg, wxBITMAP_TYPE_JPEG ) );
219 wxMemoryInputStream stream( jpeg );
227 wxMemoryOutputStream out;
230 std::string bytes( out.GetLength(),
'\0' );
231 out.CopyTo( bytes.data(), bytes.size() );
252 BOOST_CHECK_MESSAGE( bitmap.
GetImageID() !=
id,
"a preview must not keep the image id" );
256 BOOST_CHECK_MESSAGE(
storedImageData( bitmap ) == stored,
"a preview must not change the stored data" );
260 BOOST_CHECK_MESSAGE( bitmap.
GetImageID() ==
id,
"ending a preview must restore the image id" );
265 BOOST_CHECK_MESSAGE(
storedImageData( bitmap ) == stored,
"ending a preview must restore the stored data" );
284 "a preview already holding the rotation must not be turned again" );
288 BOOST_CHECK_MESSAGE( bitmap.
Rotation() !=
ANGLE_0,
"ending a preview must restore the rotation" );
307 bool foundBlock =
false;
317 BOOST_CHECK_MESSAGE( foundBlock,
"a region traced after a failing one must survive" );
321 "a region that could not be traced must be reported" );
wxImage ConvertImageToBilevel(const wxImage &aImage, double aThreshold, bool aInvert)
Reduce an image to pure black and white.
SHAPE_POLY_SET TraceBilevelImage(const wxImage &aBilevelImage, double aScaleX, double aScaleY, REPORTER &aReporter)
Trace the black regions of a black and white image into filled polygons.
This class handle bitmap images in KiCad.
void Rotate(bool aRotateCCW)
Rotate image CW or CCW.
void BeginPreviewImage(const wxImage &aImage)
Show finished pixels in place of the image, without disturbing the scale or the resolution.
const wxImage * GetOriginalImageData() const
VECTOR2I GetSizePixels() const
EDA_ANGLE Rotation() const
void EndPreviewImage()
Put back the image a preview stood in for, and do nothing if none is showing.
bool SaveImageData(wxOutputStream &aOutStream) const
Write the bitmap data to aOutStream.
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.
Import a raster image by tracing its dark areas into filled polygons.
double GetImageWidth() const override
Return image width from original imported file.
bool LoadFromMemory(const wxMemoryBuffer &aMemBuffer) override
Set memory buffer with content for import.
bool Import() override
Actually imports the file.
double GetImageHeight() const override
Return image height from original imported file.
constexpr size_type GetWidth() const
constexpr size_type GetHeight() const
std::list< std::unique_ptr< IMPORTED_SHAPE > > & GetShapes()
virtual void SetImporter(GRAPHICS_IMPORTER *aImporter)
Set the receiver of the imported shapes.
const IMPORTED_STROKE & GetStroke() const
A singleton reporter that reports to nowhere.
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Represent a set of closed polygons.
double Area()
Return the area of this poly set.
bool IsEmpty() const
Return true if the set is empty (no polygons at all)
int OutlineCount() const
Return the number of outlines in the set.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
A wrapper for reporting to a wxString object.
static constexpr EDA_ANGLE ANGLE_0
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
wxMemoryBuffer EncodeAsPng(const wxImage &aImage)
static std::string storedImageData(const BITMAP_BASE &aBitmap)
The bytes the image would be written to a board file with.
static void fillRect(wxImage &aImage, const wxRect &aRect, unsigned char aValue)
static void loadAsJpeg(BITMAP_BASE &aBitmap, const wxImage &aImage)
Load an image the way a board does, through encoded file data.
static wxImage makeImage(int aWidth, int aHeight, unsigned char aFill)
BOOST_FIXTURE_TEST_CASE(PluginReportsSizeInMillimetres, PNG_FIXTURE)
BOOST_AUTO_TEST_CASE(ThresholdSplitsLightFromDark)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
IbisParser parser & reporter
BOOST_CHECK_EQUAL(result, "25.4")