wxMaxima
Loading...
Searching...
No Matches
Image.h
Go to the documentation of this file.
1// -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2//
3// Copyright (C) 2004-2015 Andrej Vodopivec <andrej.vodopivec@gmail.com>
4// (C) 2014-2015 Gunter Königsmann <wxMaxima@physikbuch.de>
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (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//
17// You should have received a copy of the GNU General Public License
18// along with this program; if not, write to the Free Software
19// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20//
21// SPDX-License-Identifier: GPL-2.0+
22
29#ifndef IMAGE_H
30#define IMAGE_H
31
32#include <memory>
33#include <thread>
34#include "ThreadNumberLimiter.h"
35#include "precomp.h"
36#include "Version.h"
37#include "Configuration.h"
38#include <wx/image.h>
39#include <wx/buffer.h>
40#include <wx/zipstrm.h>
41#include <wx/wfstream.h>
42#define NANOSVG_ALL_COLOR_KEYWORDS
43#include "nanosvg_private.h"
44#include "nanosvgrast_private.h"
45
46
70class Image final
71{
72public:
74 explicit Image(Configuration *config);
75
77 Image(Configuration *config, const wxMemoryBuffer &image, const wxString &type);
78
84 Image(Configuration *config, const wxBitmap &bitmap);
85
94 Image(Configuration *config, const wxString &image,
95 const wxString &wxmxFile, bool remove = true);
96
97 Image(Configuration *config, const Image &image);
98 Image(const Image &image) = delete;
99
100 virtual ~Image();
101
103 static wxBitmap RGBA2wxBitmap(const unsigned char imgdata[],
104 const int &width, const int &height);
105
106 void SetConfiguration(Configuration *config){
107 if(m_loadImageTask.joinable())
108 m_loadImageTask.join();
109 m_configuration = config; }
111 int GetPPI() const {
112 if(m_loadImageTask.joinable())
113 m_loadImageTask.join();
114 return m_ppi;}
116 void SetPPI(int ppi) {
117 if(m_loadImageTask.joinable())
118 m_loadImageTask.join();
119 m_ppi = ppi;}
120
122 void InvalidBitmap(const wxString &message = wxEmptyString);
123
130 void GnuplotSource(wxString gnuplotFilename, wxString dataFilename,
131 const wxString &wxmxFile = wxEmptyString);
132
136 void CompressedGnuplotSource(wxString gnuplotFilename, wxString dataFilename,
137 const wxString &wxmxFile = wxEmptyString);
138
148 wxString GnuplotSource();
158 wxString GnuplotData();
159
161 const wxMemoryBuffer GetGnuplotSource();
162 const wxMemoryBuffer GetCompressedGnuplotSource();
164 const wxMemoryBuffer GetGnuplotData();
165 const wxMemoryBuffer GetCompressedGnuplotData();
166
172 {
173 if(m_loadImageTask.joinable())
174 m_loadImageTask.join();
175 if ((m_scaledBitmap.GetWidth() > 1) || (m_scaledBitmap.GetHeight() > 1))
176 m_scaledBitmap.Create(1, 1);
177 }
178
180 wxString GetExtension() const;
182 double GetMaxWidth() const {
183 if(m_loadImageTask.joinable())
184 m_loadImageTask.join();
185 return m_maxWidth;}
187 double GetHeightList() const {
188 if(m_loadImageTask.joinable())
189 m_loadImageTask.join();
190 return m_maxHeight;}
192 void SetMaxWidth(double width){
193 if(m_loadImageTask.joinable())
194 m_loadImageTask.join();
195 m_maxWidth = width;
196 }
198 void SetMaxHeight(double height){
199 if(m_loadImageTask.joinable())
200 m_loadImageTask.join();
201 m_maxHeight = height;
202 }
203
205 void LoadImage(const wxBitmap &bitmap);
206
208 wxSize ToImageFile(wxString filename);
209
211 wxBitmap GetBitmap(double scale = 1.0);
212
214 wxBitmap GetUnscaledBitmap();
215
217 void Recalculate(double scale = 1.0);
218
220 long m_width = 1;
222 long m_height = 1;
223
225 const wxMemoryBuffer GetCompressedImage() const;
226
228 std::size_t GetOriginalWidth() const;
229
231 std::size_t GetOriginalHeight() const;
232
233
234
236 wxMemoryBuffer m_compressedImage;
237
239 bool CanExportSVG() const {
240 if(m_loadImageTask.joinable())
241 m_loadImageTask.join();
242 return m_svgRast != nullptr;}
243
245 static const wxString GetBadImageToolTip();
246
247 class WxmxStream: public wxZipInputStream
248 {
249 public:
250 WxmxStream(wxInputStream &wxmxFile, const wxString &fileInWxmx);
251 };
252
253 bool HasGnuplotSource() const {return m_gnuplotSource_Compressed.GetDataLen() > 20;}
254private:
255 bool m_fromWxFS = false;
257 wxMemoryBuffer m_gnuplotSource_Compressed;
259 wxMemoryBuffer m_gnuplotData_Compressed;
261 std::size_t m_originalWidth = 640;
263 std::size_t m_originalHeight = 480;
265 wxBitmap m_scaledBitmap;
267 wxString m_extension;
269 wxString m_gnuplotSource;
271 wxString m_gnuplotData;
272 mutable jthread m_loadImageTask;
273 void LoadImage_Backgroundtask(std::unique_ptr<ThreadNumberLimiter> limiter,
274 wxString image, wxString wxmxFile,
275 bool remove);
276 jthread m_loadGnuplotSourceTask;
277 void LoadGnuplotSource_Backgroundtask(
278 std::unique_ptr<ThreadNumberLimiter> limiter,
279 wxString gnuplotFile, wxString dataFile, wxString wxmxFile);
280 void LoadGnuplotSource(wxInputStream *source);
281 void LoadGnuplotData(wxInputStream *data);
282 void LoadGnuplotSource_Backgroundtask_internal(
283 wxInputStream *source,
284 wxInputStream *data);
285
286
287 void LoadCompressedGnuplotSource_Backgroundtask(std::unique_ptr<ThreadNumberLimiter> limiter,
288 wxString sourcefile,
289 wxString datafile,
290 wxString wxmxFile
291 );
293 void LoadImage(wxString image, const wxString &wxmxFile, bool remove = true);
295 static wxMemoryBuffer ReadCompressedImage(wxInputStream *data);
296 Configuration *m_configuration = NULL;
299 wxCoord m_maxWidth = -1;
301 wxCoord m_maxHeight = -1;
303 wxString m_imageName;
305 double m_ppi = 72;
306 struct free_deleter { void operator()(void *p) const { std::free(p); } };
307 wxm_NSVGimage* m_svgImage = {};
308 std::unique_ptr<struct wxm_NSVGrasterizer, free_deleter> m_svgRast{nullptr};
309};
310
311#endif // IMAGE_H
A mutex that locks if we have too many background threads.
The configuration storage for the current worksheet.
Definition: Configuration.h:84
Definition: Image.h:248
Manages an auto-scaling image.
Definition: Image.h:71
void SetMaxWidth(double width)
Set the maximum width this image shall be displayed with.
Definition: Image.h:192
double GetHeightList() const
The maximum height this image shall be displayed with.
Definition: Image.h:187
void LoadImage(const wxBitmap &bitmap)
"Loads" an image from a bitmap
Definition: Image.cpp:732
void Recalculate(double scale=1.0)
Can be called to specify a specific scale.
Definition: Image.cpp:909
int GetPPI() const
Return the image's resolution.
Definition: Image.h:111
wxString GnuplotSource()
Returns the gnuplot source file name of this image.
Definition: Image.cpp:511
const wxMemoryBuffer GetGnuplotSource()
Returns the gnuplot source of this image.
Definition: Image.cpp:401
std::size_t GetOriginalWidth() const
Returns the original width.
Definition: Image.cpp:184
std::size_t GetOriginalHeight() const
Returns the original height.
Definition: Image.cpp:191
static const wxString GetBadImageToolTip()
The tooltip to use wherever an image that's not Ok is shown.
Definition: Image.cpp:982
long m_height
The height of the scaled image.
Definition: Image.h:222
const wxMemoryBuffer GetGnuplotData()
Returns the gnuplot data of this image.
Definition: Image.cpp:446
long m_width
The width of the scaled image.
Definition: Image.h:220
wxString GetExtension() const
Returns the file name extension of the current image.
Definition: Image.cpp:751
void InvalidBitmap(const wxString &message=wxEmptyString)
Creates a bitmap showing an error message.
Definition: Image.cpp:680
static wxBitmap RGBA2wxBitmap(const unsigned char imgdata[], const int &width, const int &height)
Converts rgba data to a wxBitmap.
Definition: Image.cpp:992
const wxMemoryBuffer GetCompressedImage() const
Returns the original image in its compressed form.
Definition: Image.cpp:178
bool CanExportSVG() const
Can this image be exported in SVG format?
Definition: Image.h:239
void SetPPI(int ppi)
Set the image's resolution.
Definition: Image.h:116
wxMemoryBuffer m_compressedImage
The image in its original compressed form.
Definition: Image.h:236
void SetMaxHeight(double height)
Set the maximum height this image shall be displayed with.
Definition: Image.h:198
wxSize ToImageFile(wxString filename)
Saves the image in its original form, or as .png if it originates in a bitmap.
Definition: Image.cpp:552
double GetMaxWidth() const
The maximum width this image shall be displayed with.
Definition: Image.h:182
wxBitmap GetBitmap(double scale=1.0)
Returns the bitmap being displayed with custom scale.
Definition: Image.cpp:622
wxString GnuplotData()
Returns the gnuplot data file name of this image.
Definition: Image.cpp:476
wxBitmap GetUnscaledBitmap()
Returns the image in its unscaled form.
Definition: Image.cpp:157
void CompressedGnuplotSource(wxString gnuplotFilename, wxString dataFilename, const wxString &wxmxFile=wxEmptyString)
Loads the compressed gnuplot source and data file for this image.
Definition: Image.cpp:247
void ClearCache()
Temporarily forget the scaled image in order to save memory.
Definition: Image.h:171