KiCad PCB EDA Suite
Loading...
Searching...
No Matches
gfx_import_utils.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 (C) 2023 Alex Shvartzkop <[email protected]>
5 * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include "gfx_import_utils.h"
26
27#include <stdint.h>
28#include <lib_symbol.h>
29#include <lib_shape.h>
32
33
34std::unordered_map<uint32_t, SHAPE_POLY_SET> ConvertImageToPolygons( wxImage img,
35 VECTOR2D pixelScale )
36{
37 // Quantize the image
38 for( int y = 0; y < img.GetHeight(); y++ )
39 {
40 for( int x = 0; x < img.GetWidth(); x++ )
41 {
42 int r = img.GetRed( x, y );
43 int g = img.GetGreen( x, y );
44 int b = img.GetBlue( x, y );
45 int a = img.GetAlpha( x, y );
46
47 int roundBits = 5; // 32
48 r = std::min( r >> roundBits << roundBits, 0xFF );
49 g = std::min( g >> roundBits << roundBits, 0xFF );
50 b = std::min( b >> roundBits << roundBits, 0xFF );
51 a = std::min( a >> roundBits << roundBits, 0xFF );
52
53 img.SetRGB( x, y, r, g, b );
54 img.SetAlpha( x, y, a );
55 }
56 }
57
58 std::unordered_map<uint32_t, SHAPE_POLY_SET> colorPolys;
59
60 // Create polygon sets
61 for( int y = 0; y < img.GetHeight(); y++ )
62 {
63 for( int x = 0; x < img.GetWidth(); x++ )
64 {
65 uint32_t r = img.GetRed( x, y );
66 uint32_t g = img.GetGreen( x, y );
67 uint32_t b = img.GetBlue( x, y );
68 uint32_t a = img.GetAlpha( x, y );
69
70 if( a > 0 )
71 {
72 uint32_t color = r | ( g << 8 ) | ( b << 16 ) | ( a << 24 );
73
74 SHAPE_POLY_SET& colorPoly = colorPolys[color];
75
76 SHAPE_LINE_CHAIN chain;
77 chain.Append( x * pixelScale.x, y * pixelScale.y, true );
78 chain.Append( ( x + 1 ) * pixelScale.x, y * pixelScale.y, true );
79 chain.Append( ( x + 1 ) * pixelScale.x, ( y + 1 ) * pixelScale.y, true );
80 chain.Append( x * pixelScale.x, ( y + 1 ) * pixelScale.y, true );
81 chain.SetClosed( true );
82
83 colorPoly.AddOutline( chain );
84 }
85 }
86 }
87
88 for( auto& [color, polySet] : colorPolys )
89 {
90 polySet.Simplify( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
91
92 for( int i = 0; i < polySet.OutlineCount(); i++ )
93 {
94 SHAPE_POLY_SET::POLYGON& poly = polySet.Polygon( i );
95
96 for( SHAPE_LINE_CHAIN& chain : poly )
97 chain.Simplify();
98 }
99 }
100
101 return colorPolys;
102}
103
104
105void ConvertImageToLibShapes( LIB_SYMBOL* aSymbol, int unit, wxImage img, VECTOR2D pixelScale,
106 VECTOR2D offset )
107{
108 std::unordered_map<uint32_t, SHAPE_POLY_SET> colorPolys =
109 ConvertImageToPolygons( img, pixelScale );
110
111 for( auto& [color, polySet] : colorPolys )
112 {
113 polySet.Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
114
115 for( const SHAPE_POLY_SET::POLYGON& poly : polySet.CPolygons() )
116 {
117 std::unique_ptr<LIB_SHAPE> shape =
118 std::make_unique<LIB_SHAPE>( aSymbol, SHAPE_T::POLY );
119
120 shape->SetPolyShape( poly );
121
122 int r = color & 0xFF;
123 int g = ( color >> 8 ) & 0xFF;
124 int b = ( color >> 16 ) & 0xFF;
125 int a = ( color >> 24 ) & 0xFF;
126
127 shape->SetWidth( -1 );
128 shape->SetFillMode( FILL_T::FILLED_WITH_COLOR );
129 shape->SetFillColor( COLOR4D( r / 255.0, g / 255.0, b / 255.0, a / 255.0 ) );
130
131 shape->SetUnit( unit );
132
133 shape->Offset( offset );
134
135 aSymbol->AddDrawItem( shape.release() );
136 }
137 }
138}
139
140
141void ConvertSVGToLibShapes( LIB_SYMBOL* aSymbol, int unit, const wxMemoryBuffer& aImageData,
142 VECTOR2D pixelScale, VECTOR2D offset )
143{
144 SVG_IMPORT_PLUGIN svgImportPlugin;
145 GRAPHICS_IMPORTER_LIB_SYMBOL libeditImporter( aSymbol, unit );
146
147 libeditImporter.SetScale( pixelScale );
148 libeditImporter.SetImportOffsetMM(
149 VECTOR2D( schIUScale.IUTomm( offset.x ), schIUScale.IUTomm( offset.y ) ) );
150
151 svgImportPlugin.SetImporter( &libeditImporter );
152 svgImportPlugin.LoadFromMemory( aImageData );
153
154 svgImportPlugin.Import();
155}
int color
Definition: DXF_plotter.cpp:58
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:111
void SetImportOffsetMM(const VECTOR2D &aOffset)
Set the offset in millimeters to add to coordinates when importing graphic items.
void SetScale(const VECTOR2D &aScale)
Set the scale factor affecting the imported shapes.
virtual void SetImporter(GRAPHICS_IMPORTER *aImporter)
Set the receiver of the imported shapes.
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
Define a library symbol object.
Definition: lib_symbol.h:99
void AddDrawItem(LIB_ITEM *aItem, bool aSort=true)
Add a new draw aItem to the draw object list and sort according to aSort.
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void SetClosed(bool aClosed)
Mark the line chain as closed (i.e.
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
Represent a set of closed polygons.
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index.
std::vector< SHAPE_LINE_CHAIN > POLYGON
represents a single polygon outline with holes.
bool Import() override
Actually imports the file.
bool LoadFromMemory(const wxMemoryBuffer &aMemBuffer) override
Set memory buffer with content for import.
@ FILLED_WITH_COLOR
void ConvertImageToLibShapes(LIB_SYMBOL *aSymbol, int unit, wxImage img, VECTOR2D pixelScale, VECTOR2D offset)
std::unordered_map< uint32_t, SHAPE_POLY_SET > ConvertImageToPolygons(wxImage img, VECTOR2D pixelScale)
void ConvertSVGToLibShapes(LIB_SYMBOL *aSymbol, int unit, const wxMemoryBuffer &aImageData, VECTOR2D pixelScale, VECTOR2D offset)
constexpr double IUTomm(int iu) const
Definition: base_units.h:87
VECTOR2< double > VECTOR2D
Definition: vector2d.h:587