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 <atomic>
35#include "ThreadNumberLimiter.h"
36#include "precomp.h"
37#include "Version.h"
38#include "Configuration.h"
39#include <wx/image.h>
40#include <wx/buffer.h>
41#include <wx/zipstrm.h>
42#include <wx/wfstream.h>
43#define NANOSVG_ALL_COLOR_KEYWORDS
44#include "nanosvg_private.h"
45#include "nanosvgrast_private.h"
46
47
71class Image final
72{
73public:
75 explicit Image(Configuration *config);
76
78 Image(Configuration *config, const wxMemoryBuffer &image, const wxString &type);
79
85 Image(Configuration *config, const wxBitmap &bitmap);
86
95 Image(Configuration *config, const wxString &image,
96 const wxString &wxmxFile, bool remove = true);
97
98 Image(Configuration *config, const Image &image);
99 Image(const Image &image) = delete;
100
101 virtual ~Image();
102
104 static wxBitmap RGBA2wxBitmap(const unsigned char imgdata[],
105 const int &width, const int &height);
106
107 void SetConfiguration(Configuration *config){
108 if(m_loadImageTask.joinable())
109 m_loadImageTask.join();
110 m_configuration = config; }
112 int GetPPI() const {
113 if(m_loadImageTask.joinable())
114 m_loadImageTask.join();
115 return m_ppi;}
117 void SetPPI(int ppi) {
118 if(m_loadImageTask.joinable())
119 m_loadImageTask.join();
120 m_ppi = ppi;}
121
123 void InvalidBitmap(const wxString &message = wxEmptyString);
124 void GenerateInvalidBitmap();
125
126 wxString m_invalidBitmapMessage;
127 bool m_isInvalid = false;
128
135 void GnuplotSource(wxString gnuplotFilename, wxString dataFilename,
136 const wxString &wxmxFile = wxEmptyString);
137
141 void CompressedGnuplotSource(wxString gnuplotFilename, wxString dataFilename,
142 const wxString &wxmxFile = wxEmptyString);
143
153 wxString GnuplotSource();
163 wxString GnuplotData();
164
166 const wxMemoryBuffer GetGnuplotSource();
167 const wxMemoryBuffer GetCompressedGnuplotSource();
169 const wxMemoryBuffer GetGnuplotData();
170 const wxMemoryBuffer GetCompressedGnuplotData();
171
177 {
178 if(m_loadImageTask.joinable())
179 m_loadImageTask.join();
180 if ((m_scaledBitmap.GetWidth() > 1) || (m_scaledBitmap.GetHeight() > 1))
181 m_scaledBitmap.Create(1, 1);
182 }
183
185 wxString GetExtension() const;
187 double GetMaxWidth() const {
188 if(m_loadImageTask.joinable())
189 m_loadImageTask.join();
190 return m_maxWidth;}
192 double GetHeightList() const {
193 if(m_loadImageTask.joinable())
194 m_loadImageTask.join();
195 return m_maxHeight;}
197 void SetMaxWidth(double width){
198 if(m_loadImageTask.joinable())
199 m_loadImageTask.join();
200 m_maxWidth = width;
201 }
203 void SetMaxHeight(double height){
204 if(m_loadImageTask.joinable())
205 m_loadImageTask.join();
206 m_maxHeight = height;
207 }
208
210 void LoadImage(const wxBitmap &bitmap);
211
213 wxSize ToImageFile(wxString filename);
214
216 wxBitmap GetBitmap(double scale = 1.0);
217
219 wxBitmap GetUnscaledBitmap();
220
222 void Recalculate(double scale = 1.0);
223
225 long m_width = 1;
227 long m_height = 1;
228
230 const wxMemoryBuffer GetCompressedImage() const;
231
233 std::size_t GetOriginalWidth() const;
234
236 std::size_t GetOriginalHeight() const;
237
238
239
241 wxMemoryBuffer m_compressedImage;
242
244 bool CanExportSVG() const {
245 if(m_loadImageTask.joinable())
246 m_loadImageTask.join();
247 return m_svgRast != nullptr;}
248
250 static const wxString GetBadImageToolTip();
251
252 class WxmxStream: public wxZipInputStream
253 {
254 public:
255 WxmxStream(wxInputStream &wxmxFile, const wxString &fileInWxmx);
256 };
257
258 bool HasGnuplotSource() const {return m_gnuplotSource_Compressed.GetDataLen() > 20;}
259private:
260 bool m_fromWxFS = false;
262 wxMemoryBuffer m_gnuplotSource_Compressed;
264 wxMemoryBuffer m_gnuplotData_Compressed;
266 std::size_t m_originalWidth = 640;
268 std::size_t m_originalHeight = 480;
270 wxBitmap m_scaledBitmap;
272 wxString m_extension;
274 wxString m_gnuplotSource;
276 wxString m_gnuplotData;
277 mutable jthread m_loadImageTask;
278 void LoadImage_Backgroundtask(std::unique_ptr<ThreadNumberLimiter> limiter,
279 wxString image, wxString wxmxFile,
280 bool remove);
281 jthread m_loadGnuplotSourceTask;
283 std::atomic_bool m_abortBackgroundTask{false};
284 void LoadGnuplotSource_Backgroundtask(
285 std::unique_ptr<ThreadNumberLimiter> limiter,
286 wxString gnuplotFile, wxString dataFile, wxString wxmxFile);
287 void LoadGnuplotSource(wxInputStream *source);
288 void LoadGnuplotData(wxInputStream *data);
289 void LoadGnuplotSource_Backgroundtask_internal(
290 wxInputStream *source,
291 wxInputStream *data);
292
293
294 void LoadCompressedGnuplotSource_Backgroundtask(std::unique_ptr<ThreadNumberLimiter> limiter,
295 wxString sourcefile,
296 wxString datafile,
297 wxString wxmxFile
298 );
300 void LoadImage(wxString image, const wxString &wxmxFile, bool remove = true);
302 wxMemoryBuffer ReadCompressedImage(wxInputStream *data);
303 Configuration *m_configuration = NULL;
306 wxCoord m_maxWidth = -1;
308 wxCoord m_maxHeight = -1;
310 wxString m_imageName;
312 double m_ppi = 72;
313 struct free_deleter { void operator()(void *p) const { std::free(p); } };
314 wxm_NSVGimage* m_svgImage = {};
315 std::unique_ptr<struct wxm_NSVGrasterizer, free_deleter> m_svgRast{nullptr};
316};
317
318#endif // IMAGE_H
A mutex that locks if we have too many background threads.
The configuration storage for the current worksheet.
Definition: Configuration.h:85
Definition: Image.h:253
Manages an auto-scaling image.
Definition: Image.h:72
void SetMaxWidth(double width)
Set the maximum width this image shall be displayed with.
Definition: Image.h:197
double GetHeightList() const
The maximum height this image shall be displayed with.
Definition: Image.h:192
void LoadImage(const wxBitmap &bitmap)
"Loads" an image from a bitmap
Definition: Image.cpp:764
void Recalculate(double scale=1.0)
Can be called to specify a specific scale.
Definition: Image.cpp:949
int GetPPI() const
Return the image's resolution.
Definition: Image.h:112
wxString GnuplotSource()
Returns the gnuplot source file name of this image.
Definition: Image.cpp:530
const wxMemoryBuffer GetGnuplotSource()
Returns the gnuplot source of this image.
Definition: Image.cpp:420
std::size_t GetOriginalWidth() const
Returns the original width.
Definition: Image.cpp:189
std::size_t GetOriginalHeight() const
Returns the original height.
Definition: Image.cpp:196
static const wxString GetBadImageToolTip()
The tooltip to use wherever an image that's not Ok is shown.
Definition: Image.cpp:1022
long m_height
The height of the scaled image.
Definition: Image.h:227
const wxMemoryBuffer GetGnuplotData()
Returns the gnuplot data of this image.
Definition: Image.cpp:465
long m_width
The width of the scaled image.
Definition: Image.h:225
wxString GetExtension() const
Returns the file name extension of the current image.
Definition: Image.cpp:783
void InvalidBitmap(const wxString &message=wxEmptyString)
Creates a bitmap showing an error message.
Definition: Image.cpp:702
static wxBitmap RGBA2wxBitmap(const unsigned char imgdata[], const int &width, const int &height)
Converts rgba data to a wxBitmap.
Definition: Image.cpp:1032
const wxMemoryBuffer GetCompressedImage() const
Returns the original image in its compressed form.
Definition: Image.cpp:183
bool CanExportSVG() const
Can this image be exported in SVG format?
Definition: Image.h:244
void SetPPI(int ppi)
Set the image's resolution.
Definition: Image.h:117
wxMemoryBuffer m_compressedImage
The image in its original compressed form.
Definition: Image.h:241
void SetMaxHeight(double height)
Set the maximum height this image shall be displayed with.
Definition: Image.h:203
wxSize ToImageFile(wxString filename)
Saves the image in its original form, or as .png if it originates in a bitmap.
Definition: Image.cpp:571
double GetMaxWidth() const
The maximum width this image shall be displayed with.
Definition: Image.h:187
wxBitmap GetBitmap(double scale=1.0)
Returns the bitmap being displayed with custom scale.
Definition: Image.cpp:641
wxString GnuplotData()
Returns the gnuplot data file name of this image.
Definition: Image.cpp:495
wxBitmap GetUnscaledBitmap()
Returns the image in its unscaled form.
Definition: Image.cpp:159
void CompressedGnuplotSource(wxString gnuplotFilename, wxString dataFilename, const wxString &wxmxFile=wxEmptyString)
Loads the compressed gnuplot source and data file for this image.
Definition: Image.cpp:259
void ClearCache()
Temporarily forget the scaled image in order to save memory.
Definition: Image.h:176