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#include <qa_utils/file_utils.h>
22
23#include <wx/filefn.h>
24#include <wx/image.h>
25
28#include <gal/color4d.h>
29
30
31BOOST_AUTO_TEST_SUITE( PngPlotter )
32
33
35{
36 PNG_PLOTTER plotter;
37 plotter.SetPixelSize( 100, 100 );
38 plotter.SetResolution( 300 );
39
40 BOOST_CHECK( plotter.StartPlot( wxEmptyString ) );
41 BOOST_CHECK( plotter.EndPlot() );
42
43 KI_TEST::SCOPED_TEMP_DIR tempDir( "png_test" );
44 const wxString tempFile = tempDir.CreateChildFileStr( "plot.png" );
45
46 BOOST_CHECK( plotter.SaveFile( tempFile ) );
47
48 // Verify file exists
49 BOOST_CHECK( wxFileExists( tempFile ) );
50
51 // Verify dimensions via wxImage
52 wxImage img( tempFile );
53 BOOST_CHECK( img.IsOk() );
54 BOOST_CHECK_EQUAL( img.GetWidth(), 100 );
55 BOOST_CHECK_EQUAL( img.GetHeight(), 100 );
56}
57
58
60{
61 PNG_PLOTTER plotter;
62 plotter.SetPixelSize( 100, 100 );
63 plotter.SetResolution( 100 );
64 plotter.SetColorMode( true );
65
66 plotter.StartPlot( wxEmptyString );
67 plotter.SetColor( KIGFX::COLOR4D( 1.0, 0.0, 0.0, 1.0 ) ); // Red
68 plotter.Rect( VECTOR2I( 10, 10 ), VECTOR2I( 90, 90 ), FILL_T::FILLED_SHAPE, 0 );
69 plotter.EndPlot();
70
71 KI_TEST::SCOPED_TEMP_DIR tempDir( "png_rect" );
72 const wxString tempFile = tempDir.CreateChildFileStr( "plot.png" );
73
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
88
89BOOST_AUTO_TEST_CASE( DrawShapePolySet )
90{
91 // Create a simple square polygon
92 SHAPE_POLY_SET poly;
93 poly.NewOutline();
94 poly.Append( VECTOR2I( 0, 0 ) );
95 poly.Append( VECTOR2I( 1000000, 0 ) ); // 1mm in nm
96 poly.Append( VECTOR2I( 1000000, 1000000 ) );
97 poly.Append( VECTOR2I( 0, 1000000 ) );
98
99 PNG_PLOTTER plotter;
100 plotter.SetPixelSize( 100, 100 );
101 plotter.SetResolution( 254 ); // 254 DPI = 0.1mm per pixel
102
103 // Set viewport for 1mm x 1mm area
104 plotter.SetViewport( VECTOR2I( 0, 0 ), 254000, 1.0, false );
105
106 plotter.StartPlot( wxEmptyString );
108
109 // Render the polygon
110 for( int i = 0; i < poly.OutlineCount(); i++ )
111 {
112 const SHAPE_LINE_CHAIN& outline = poly.Outline( i );
113 std::vector<VECTOR2I> pts;
114
115 for( int j = 0; j < outline.PointCount(); j++ )
116 pts.push_back( outline.CPoint( j ) );
117
118 plotter.PlotPoly( pts, FILL_T::FILLED_SHAPE, 0 );
119 }
120
121 plotter.EndPlot();
122
123 KI_TEST::SCOPED_TEMP_DIR tempDir( "png_polyset" );
124 const wxString tempFile = tempDir.CreateChildFileStr( "plot.png" );
125
126 BOOST_CHECK( plotter.SaveFile( tempFile ) );
127
128 wxImage img( tempFile );
129 BOOST_CHECK( img.IsOk() );
130}
131
132
133BOOST_AUTO_TEST_CASE( AntialiasControl )
134{
135 PNG_PLOTTER plotterAA;
136 plotterAA.SetPixelSize( 100, 100 );
137 plotterAA.SetAntialias( true );
138
139 PNG_PLOTTER plotterNoAA;
140 plotterNoAA.SetPixelSize( 100, 100 );
141 plotterNoAA.SetAntialias( false );
142
143 // Draw diagonal line on both
144 plotterAA.StartPlot( wxEmptyString );
145 plotterAA.SetCurrentLineWidth( 2 );
146 plotterAA.SetColor( KIGFX::COLOR4D::BLACK );
147 plotterAA.MoveTo( VECTOR2I( 0, 0 ) );
148 plotterAA.FinishTo( VECTOR2I( 100, 100 ) );
149 plotterAA.EndPlot();
150
151 plotterNoAA.StartPlot( wxEmptyString );
152 plotterNoAA.SetCurrentLineWidth( 2 );
153 plotterNoAA.SetColor( KIGFX::COLOR4D::BLACK );
154 plotterNoAA.MoveTo( VECTOR2I( 0, 0 ) );
155 plotterNoAA.FinishTo( VECTOR2I( 100, 100 ) );
156 plotterNoAA.EndPlot();
157
158 KI_TEST::SCOPED_TEMP_DIR tempDir( "png_aa" );
159 const wxString tempAA = tempDir.CreateChildFileStr( "aa.png" );
160 const wxString tempNoAA = tempDir.CreateChildFileStr( "noaa.png" );
161
162 BOOST_CHECK( plotterAA.SaveFile( tempAA ) );
163 BOOST_CHECK( plotterNoAA.SaveFile( tempNoAA ) );
164
165 // Both files should exist
166 BOOST_CHECK( wxFileExists( tempAA ) );
167 BOOST_CHECK( wxFileExists( tempNoAA ) );
168}
169
170
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
static const COLOR4D BLACK
Definition color4d.h:403
wxString CreateChildFileStr(const wxString &aName) const
Create and return the path to a direct child file as a wxString.
void MoveTo(const VECTOR2I &pos)
Definition plotter.h:308
void FinishTo(const VECTOR2I &pos)
Definition plotter.h:318
virtual void SetColorMode(bool aColorMode)
Plot in B/W or color.
Definition plotter.h:163
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_fill.h:31
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