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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <boost/test/unit_test.hpp>
25
26#include <wx/filename.h>
27#include <wx/image.h>
28
31#include <gal/color4d.h>
32
33
34BOOST_AUTO_TEST_SUITE( PngPlotter )
35
36
38{
39 PNG_PLOTTER plotter;
40 plotter.SetPixelSize( 100, 100 );
41 plotter.SetResolution( 300 );
42
43 BOOST_CHECK( plotter.StartPlot( wxEmptyString ) );
44 BOOST_CHECK( plotter.EndPlot() );
45
46 wxString tempFile = wxFileName::CreateTempFileName( wxS( "png_test" ) ) + wxS( ".png" );
47 BOOST_CHECK( plotter.SaveFile( tempFile ) );
48
49 // Verify file exists
50 BOOST_CHECK( wxFileExists( tempFile ) );
51
52 // Verify dimensions via wxImage
53 wxImage img( tempFile );
54 BOOST_CHECK( img.IsOk() );
55 BOOST_CHECK_EQUAL( img.GetWidth(), 100 );
56 BOOST_CHECK_EQUAL( img.GetHeight(), 100 );
57
58 wxRemoveFile( tempFile );
59}
60
61
63{
64 PNG_PLOTTER plotter;
65 plotter.SetPixelSize( 100, 100 );
66 plotter.SetResolution( 100 );
67
68 plotter.StartPlot( wxEmptyString );
69 plotter.SetColor( KIGFX::COLOR4D( 1.0, 0.0, 0.0, 1.0 ) ); // Red
70 plotter.Rect( VECTOR2I( 10, 10 ), VECTOR2I( 90, 90 ), FILL_T::FILLED_SHAPE, 0 );
71 plotter.EndPlot();
72
73 wxString tempFile = wxFileName::CreateTempFileName( wxS( "png_rect" ) ) + wxS( ".png" );
74 BOOST_CHECK( plotter.SaveFile( tempFile ) );
75
76 wxImage img( tempFile );
77 BOOST_CHECK( img.IsOk() );
78
79 // Center pixel should be red (or close to it, accounting for anti-aliasing)
80 unsigned char r = img.GetRed( 50, 50 );
81 unsigned char g = img.GetGreen( 50, 50 );
82 unsigned char b = img.GetBlue( 50, 50 );
83 BOOST_CHECK_GT( r, 200 ); // Should be mostly red
84 BOOST_CHECK_LT( g, 50 );
85 BOOST_CHECK_LT( b, 50 );
86
87 wxRemoveFile( tempFile );
88}
89
90
91BOOST_AUTO_TEST_CASE( DrawShapePolySet )
92{
93 // Create a simple square polygon
94 SHAPE_POLY_SET poly;
95 poly.NewOutline();
96 poly.Append( VECTOR2I( 0, 0 ) );
97 poly.Append( VECTOR2I( 1000000, 0 ) ); // 1mm in nm
98 poly.Append( VECTOR2I( 1000000, 1000000 ) );
99 poly.Append( VECTOR2I( 0, 1000000 ) );
100
101 PNG_PLOTTER plotter;
102 plotter.SetPixelSize( 100, 100 );
103 plotter.SetResolution( 254 ); // 254 DPI = 0.1mm per pixel
104
105 // Set viewport for 1mm x 1mm area
106 plotter.SetViewport( VECTOR2I( 0, 0 ), 254000, 1.0, false );
107
108 plotter.StartPlot( wxEmptyString );
110
111 // Render the polygon
112 for( int i = 0; i < poly.OutlineCount(); i++ )
113 {
114 const SHAPE_LINE_CHAIN& outline = poly.Outline( i );
115 std::vector<VECTOR2I> pts;
116
117 for( int j = 0; j < outline.PointCount(); j++ )
118 pts.push_back( outline.CPoint( j ) );
119
120 plotter.PlotPoly( pts, FILL_T::FILLED_SHAPE, 0 );
121 }
122
123 plotter.EndPlot();
124
125 wxString tempFile = wxFileName::CreateTempFileName( wxS( "png_polyset" ) ) + wxS( ".png" );
126 BOOST_CHECK( plotter.SaveFile( tempFile ) );
127
128 wxImage img( tempFile );
129 BOOST_CHECK( img.IsOk() );
130
131 wxRemoveFile( tempFile );
132}
133
134
135BOOST_AUTO_TEST_CASE( AntialiasControl )
136{
137 PNG_PLOTTER plotterAA;
138 plotterAA.SetPixelSize( 100, 100 );
139 plotterAA.SetAntialias( true );
140
141 PNG_PLOTTER plotterNoAA;
142 plotterNoAA.SetPixelSize( 100, 100 );
143 plotterNoAA.SetAntialias( false );
144
145 // Draw diagonal line on both
146 plotterAA.StartPlot( wxEmptyString );
147 plotterAA.SetCurrentLineWidth( 2 );
148 plotterAA.SetColor( KIGFX::COLOR4D::BLACK );
149 plotterAA.MoveTo( VECTOR2I( 0, 0 ) );
150 plotterAA.FinishTo( VECTOR2I( 100, 100 ) );
151 plotterAA.EndPlot();
152
153 plotterNoAA.StartPlot( wxEmptyString );
154 plotterNoAA.SetCurrentLineWidth( 2 );
155 plotterNoAA.SetColor( KIGFX::COLOR4D::BLACK );
156 plotterNoAA.MoveTo( VECTOR2I( 0, 0 ) );
157 plotterNoAA.FinishTo( VECTOR2I( 100, 100 ) );
158 plotterNoAA.EndPlot();
159
160 wxString tempAA = wxFileName::CreateTempFileName( wxS( "png_aa" ) ) + wxS( ".png" );
161 wxString tempNoAA = wxFileName::CreateTempFileName( wxS( "png_noaa" ) ) + wxS( ".png" );
162
163 BOOST_CHECK( plotterAA.SaveFile( tempAA ) );
164 BOOST_CHECK( plotterNoAA.SaveFile( tempNoAA ) );
165
166 // Both files should exist
167 BOOST_CHECK( wxFileExists( tempAA ) );
168 BOOST_CHECK( wxFileExists( tempNoAA ) );
169
170 wxRemoveFile( tempAA );
171 wxRemoveFile( tempNoAA );
172}
173
174
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:105
static const COLOR4D BLACK
Definition color4d.h:406
void MoveTo(const VECTOR2I &pos)
Definition plotter.h:308
void FinishTo(const VECTOR2I &pos)
Definition plotter.h:318
PNG rasterization plotter using Cairo graphics library.
Definition plotter_png.h:34
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:59
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:51
virtual bool EndPlot() override
void SetAntialias(bool aEnable)
Enable or disable anti-aliasing.
Definition plotter_png.h:79
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:60
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:687