KiCad PCB EDA Suite
Loading...
Searching...
No Matches
bitmap_texture.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <gal/bitmap_texture.h>
21
22#include <math/util.h>
23
24#include <algorithm>
25
26#include <wx/image.h>
27
28
29namespace KIGFX
30{
31
32wxImage FitImageToTexture( const wxImage& aImage, int aMaxTextureSize )
33{
34 const int width = aImage.GetWidth();
35 const int height = aImage.GetHeight();
36
37 if( aMaxTextureSize <= 0 || ( width <= aMaxTextureSize && height <= aMaxTextureSize ) )
38 return aImage;
39
40 const double scale = std::min( static_cast<double>( aMaxTextureSize ) / width,
41 static_cast<double>( aMaxTextureSize ) / height );
42
43 const int newWidth = std::clamp( KiROUND( width * scale ), 1, aMaxTextureSize );
44 const int newHeight = std::clamp( KiROUND( height * scale ), 1, aMaxTextureSize );
45
46 return aImage.Scale( newWidth, newHeight, wxIMAGE_QUALITY_HIGH );
47}
48
49
50std::vector<uint8_t> RgbToRgba( const uint8_t* aRgb, const uint8_t* aAlpha, size_t aPixelCount )
51{
52 std::vector<uint8_t> rgba( aPixelCount * 4 );
53
54 for( size_t ii = 0; ii < aPixelCount; ++ii )
55 {
56 rgba[ii * 4 + 0] = aRgb[ii * 3 + 0];
57 rgba[ii * 4 + 1] = aRgb[ii * 3 + 1];
58 rgba[ii * 4 + 2] = aRgb[ii * 3 + 2];
59 rgba[ii * 4 + 3] = aAlpha ? aAlpha[ii] : wxIMAGE_ALPHA_OPAQUE;
60 }
61
62 return rgba;
63}
64
65
66std::vector<uint8_t> ImageToRgba( const wxImage& aImage )
67{
68 const size_t pixelCount = static_cast<size_t>( aImage.GetWidth() ) * aImage.GetHeight();
69
70 std::vector<uint8_t> rgba =
71 RgbToRgba( aImage.GetData(), aImage.HasAlpha() ? aImage.GetAlpha() : nullptr, pixelCount );
72
73 if( !aImage.HasAlpha() && aImage.HasMask() )
74 {
75 const uint8_t maskRed = aImage.GetMaskRed();
76 const uint8_t maskGreen = aImage.GetMaskGreen();
77 const uint8_t maskBlue = aImage.GetMaskBlue();
78
79 for( size_t ii = 0; ii < pixelCount; ++ii )
80 {
81 uint8_t* px = &rgba[ii * 4];
82
83 if( px[0] == maskRed && px[1] == maskGreen && px[2] == maskBlue )
84 px[3] = wxIMAGE_ALPHA_TRANSPARENT;
85 }
86 }
87
88 return rgba;
89}
90
91} // namespace KIGFX
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:982
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:30
std::vector< uint8_t > ImageToRgba(const wxImage &aImage)
The pixels of aImage as tightly packed RGBA, transparent where it has alpha or a mask.
std::vector< uint8_t > RgbToRgba(const uint8_t *aRgb, const uint8_t *aAlpha, size_t aPixelCount)
Expand packed RGB to tightly packed RGBA.
wxImage FitImageToTexture(const wxImage &aImage, int aMaxTextureSize)
Fit an image into the largest texture a GPU can hold.
const int scale