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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include "gfx_import_utils.h"
22
23#include <stdint.h>
24#include <lib_symbol.h>
25#include <sch_shape.h>
28
29
30std::unordered_map<uint32_t, SHAPE_POLY_SET> ConvertImageToPolygons( wxImage img,
31 VECTOR2D pixelScale )
32{
33 bool hasAlpha = img.HasAlpha();
34
35 // Quantize the image
36 for( int y = 0; y < img.GetHeight(); y++ )
37 {
38 for( int x = 0; x < img.GetWidth(); x++ )
39 {
40 int r = img.GetRed( x, y );
41 int g = img.GetGreen( x, y );
42 int b = img.GetBlue( x, y );
43 int a = hasAlpha ? img.GetAlpha( x, y ) : 255;
44
45 int roundBits = 5; // 32
46 r = std::min( r >> roundBits << roundBits, 0xFF );
47 g = std::min( g >> roundBits << roundBits, 0xFF );
48 b = std::min( b >> roundBits << roundBits, 0xFF );
49 a = std::min( a >> roundBits << roundBits, 0xFF );
50
51 img.SetRGB( x, y, r, g, b );
52
53 if( hasAlpha )
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 = hasAlpha ? img.GetAlpha( x, y ) : 255;
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
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();
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();
114
115 for( const SHAPE_POLY_SET::POLYGON& poly : polySet.CPolygons() )
116 {
117 auto shape = std::make_unique<SCH_SHAPE>( SHAPE_T::POLY, LAYER_DEVICE );
118
119 shape->SetPolyShape( poly );
120
121 int r = color & 0xFF;
122 int g = ( color >> 8 ) & 0xFF;
123 int b = ( color >> 16 ) & 0xFF;
124 int a = ( color >> 24 ) & 0xFF;
125
126 shape->SetWidth( -1 );
127 shape->SetFillMode( FILL_T::FILLED_WITH_COLOR );
128 shape->SetFillColor( COLOR4D( r / 255.0, g / 255.0, b / 255.0, a / 255.0 ) );
129
130 shape->SetUnit( unit );
131
132 shape->Move( offset );
133
134 aSymbol->AddDrawItem( shape.release(), false );
135 }
136 }
137
138 aSymbol->GetDrawItems().sort();
139}
140
141
142void ConvertSVGToLibShapes( LIB_SYMBOL* aSymbol, int unit, const wxMemoryBuffer& aImageData,
143 VECTOR2D pixelScale, VECTOR2D offset )
144{
145 SVG_IMPORT_PLUGIN svgImportPlugin;
146 GRAPHICS_IMPORTER_LIB_SYMBOL libeditImporter( aSymbol, unit );
147
148 libeditImporter.SetScale( pixelScale );
149 libeditImporter.SetImportOffsetMM(
150 VECTOR2D( schIUScale.IUTomm( offset.x ), schIUScale.IUTomm( offset.y ) ) );
151
152 svgImportPlugin.SetImporter( &libeditImporter );
153 svgImportPlugin.LoadFromMemory( aImageData );
154
155 svgImportPlugin.Import();
156}
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
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:101
Define a library symbol object.
Definition lib_symbol.h:79
LIB_ITEMS_CONTAINER & GetDrawItems()
Return a reference to the draw item list.
Definition lib_symbol.h:709
void AddDrawItem(SCH_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...
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
Definition eda_shape.h:63
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)
@ LAYER_DEVICE
Definition layer_ids.h:464
const SHAPE_LINE_CHAIN chain
VECTOR2< double > VECTOR2D
Definition vector2d.h:682