KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_png_plotter.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
21
22#include <wx/filename.h>
23#include <wx/image.h>
24
27#include <gal/color4d.h>
28
29
30BOOST_AUTO_TEST_SUITE( PngPlotter )
31
32
34{
35 PNG_PLOTTER plotter;
36 plotter.SetPixelSize( 100, 100 );
37 plotter.SetResolution( 300 );
38
39 BOOST_CHECK( plotter.StartPlot( wxEmptyString ) );
40 BOOST_CHECK( plotter.EndPlot() );
41
42 wxString tempFile = wxFileName::CreateTempFileName( wxS( "png_test" ) ) + wxS( ".png" );
43 BOOST_CHECK( plotter.SaveFile( tempFile ) );
44
45 // Verify file exists
46 BOOST_CHECK( wxFileExists( tempFile ) );
47
48 // Verify dimensions via wxImage
49 wxImage img( tempFile );
50 BOOST_CHECK( img.IsOk() );
51 BOOST_CHECK_EQUAL( img.GetWidth(), 100 );
52 BOOST_CHECK_EQUAL( img.GetHeight(), 100 );
53
54 wxRemoveFile( tempFile );
55}
56
57
59{
60 PNG_PLOTTER plotter;
61 plotter.SetPixelSize( 100, 100 );
62 plotter.SetResolution( 100 );
63 plotter.SetColorMode( true );
64
65 plotter.StartPlot( wxEmptyString );
66 plotter.SetColor( KIGFX::COLOR4D( 1.0, 0.0, 0.0, 1.0 ) ); // Red
67 plotter.Rect( VECTOR2I( 10, 10 ), VECTOR2I( 90, 90 ), FILL_T::FILLED_SHAPE, 0 );
68 plotter.EndPlot();
69
70 wxString tempFile = wxFileName::CreateTempFileName( wxS( "png_rect" ) ) + wxS( ".png" );
71 BOOST_CHECK( plotter.SaveFile( tempFile ) );
72
73 wxImage img( tempFile );
74 BOOST_CHECK( img.IsOk() );
75
76 // Center pixel should be red (or close to it, accounting for anti-aliasing)
77 unsigned char r = img.GetRed( 50, 50 );
78 unsigned char g = img.GetGreen( 50, 50 );
79 unsigned char b = img.GetBlue( 50, 50 );
80 BOOST_CHECK_GT( r, 200 ); // Should be mostly red
81 BOOST_CHECK_LT( g, 50 );
82 BOOST_CHECK_LT( b, 50 );
83
84 wxRemoveFile( tempFile );
85}
86
87
88BOOST_AUTO_TEST_CASE( DrawShapePolySet )
89{
90 // Create a simple square polygon
91 SHAPE_POLY_SET poly;
92 poly.NewOutline();
93 poly.Append( VECTOR2I( 0, 0 ) );
94 poly.Append( VECTOR2I( 1000000, 0 ) ); // 1mm in nm
95 poly.Append( VECTOR2I( 1000000, 1000000 ) );
96 poly.Append( VECTOR2I( 0, 1000000 ) );
97
98 PNG_PLOTTER plotter;
99 plotter.SetPixelSize( 100, 100 );
100 plotter.SetResolution( 254 ); // 254 DPI = 0.1mm per pixel
101
102 // Set viewport for 1mm x 1mm area
103 plotter.SetViewport( VECTOR2I( 0, 0 ), 254000, 1.0, false );
104
105 plotter.StartPlot( wxEmptyString );
107
108 // Render the polygon
109 for( int i = 0; i < poly.OutlineCount(); i++ )
110 {
111 const SHAPE_LINE_CHAIN& outline = poly.Outline( i );
112 std::vector<VECTOR2I> pts;
113
114 for( int j = 0; j < outline.PointCount(); j++ )
115 pts.push_back( outline.CPoint( j ) );
116
117 plotter.PlotPoly( pts, FILL_T::FILLED_SHAPE, 0 );
118 }
119
120 plotter.EndPlot();
121
122 wxString tempFile = wxFileName::CreateTempFileName( wxS( "png_polyset" ) ) + wxS( ".png" );
123 BOOST_CHECK( plotter.SaveFile( tempFile ) );
124
125 wxImage img( tempFile );
126 BOOST_CHECK( img.IsOk() );
127
128 wxRemoveFile( tempFile );
129}
130
131
132BOOST_AUTO_TEST_CASE( AntialiasControl )
133{
134 PNG_PLOTTER plotterAA;
135 plotterAA.SetPixelSize( 100, 100 );
136 plotterAA.SetAntialias( true );
137
138 PNG_PLOTTER plotterNoAA;
139 plotterNoAA.SetPixelSize( 100, 100 );
140 plotterNoAA.SetAntialias( false );
141
142 // Draw diagonal line on both
143 plotterAA.StartPlot( wxEmptyString );
144 plotterAA.SetCurrentLineWidth( 2 );
145 plotterAA.SetColor( KIGFX::COLOR4D::BLACK );
146 plotterAA.MoveTo( VECTOR2I( 0, 0 ) );
147 plotterAA.FinishTo( VECTOR2I( 100, 100 ) );
148 plotterAA.EndPlot();
149
150 plotterNoAA.StartPlot( wxEmptyString );
151 plotterNoAA.SetCurrentLineWidth( 2 );
152 plotterNoAA.SetColor( KIGFX::COLOR4D::BLACK );
153 plotterNoAA.MoveTo( VECTOR2I( 0, 0 ) );
154 plotterNoAA.FinishTo( VECTOR2I( 100, 100 ) );
155 plotterNoAA.EndPlot();
156
157 wxString tempAA = wxFileName::CreateTempFileName( wxS( "png_aa" ) ) + wxS( ".png" );
158 wxString tempNoAA = wxFileName::CreateTempFileName( wxS( "png_noaa" ) ) + wxS( ".png" );
159
160 BOOST_CHECK( plotterAA.SaveFile( tempAA ) );
161 BOOST_CHECK( plotterNoAA.SaveFile( tempNoAA ) );
162
163 // Both files should exist
164 BOOST_CHECK( wxFileExists( tempAA ) );
165 BOOST_CHECK( wxFileExists( tempNoAA ) );
166
167 wxRemoveFile( tempAA );
168 wxRemoveFile( tempNoAA );
169}
170
171
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
static const COLOR4D BLACK
Definition color4d.h:402
void MoveTo(const VECTOR2I &pos)
Definition plotter.h:305
void FinishTo(const VECTOR2I &pos)
Definition plotter.h:315
virtual void SetColorMode(bool aColorMode)
Plot in B/W or color.
Definition plotter.h:160
PNG rasterization plotter using Cairo graphics library.
Definition plotter_png.h:40
virtual void SetCurrentLineWidth(int aWidth, void *aData=nullptr) override
Set the line width for the next drawing.
void SetPixelSize(int aWidth, int aHeight)
Set the output image dimensions in pixels.
Definition plotter_png.h:64
virtual void Rect(const VECTOR2I &p1, const VECTOR2I &p2, FILL_T aFill, int aWidth, int aCornerRadius=0) override
bool SaveFile(const wxString &aPath)
Save the rendered image to a PNG file.
void SetResolution(int aDPI)
Set the output resolution in dots per inch.
Definition plotter_png.h:56
virtual bool EndPlot() override
void SetAntialias(bool aEnable)
Enable or disable anti-aliasing.
Definition plotter_png.h:84
virtual void SetColor(const COLOR4D &aColor) override
virtual void PlotPoly(const std::vector< VECTOR2I > &aCornerList, FILL_T aFill, int aWidth, void *aData=nullptr) override
Draw a polygon ( filled or not ).
virtual void SetViewport(const VECTOR2I &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the plot offset and scaling for the current plot.
virtual bool StartPlot(const wxString &aPageNumber) override
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int PointCount() const
Return the number of points (vertices) in this line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
Represent a set of closed polygons.
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline)
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int NewOutline()
Creates a new empty polygon in the set and returns its index.
int OutlineCount() const
Return the number of outlines in the set.
@ FILLED_SHAPE
Fill with object color.
Definition eda_shape.h:61
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(BasicOutput)
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683