wxMaxima
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 
70 class Image final
71 {
72 public:
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  const int &scaleFactor = 1);
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 
131  void GnuplotSource(wxString gnuplotFilename, wxString dataFilename,
132  const wxString &wxmxFile = wxEmptyString);
133 
137  void CompressedGnuplotSource(wxString gnuplotFilename, wxString dataFilename,
138  const wxString &wxmxFile = wxEmptyString);
139 
149  wxString GnuplotSource();
159  wxString GnuplotData();
160 
162  const wxMemoryBuffer GetGnuplotSource();
163  const wxMemoryBuffer GetCompressedGnuplotSource();
165  const wxMemoryBuffer GetGnuplotData();
166  const wxMemoryBuffer GetCompressedGnuplotData();
167 
172  void ClearCache()
173  {
174  if(m_loadImageTask.joinable())
175  m_loadImageTask.join();
176  if ((m_scaledBitmap.GetWidth() > 1) || (m_scaledBitmap.GetHeight() > 1))
177  m_scaledBitmap.Create(1, 1);
178  }
179 
181  wxString GetExtension() const;
183  double GetMaxWidth() const {
184  if(m_loadImageTask.joinable())
185  m_loadImageTask.join();
186  return m_maxWidth;}
188  double GetHeightList() const {
189  if(m_loadImageTask.joinable())
190  m_loadImageTask.join();
191  return m_maxHeight;}
193  void SetMaxWidth(double width){
194  if(m_loadImageTask.joinable())
195  m_loadImageTask.join();
196  m_maxWidth = width;
197  }
199  void SetMaxHeight(double height){
200  if(m_loadImageTask.joinable())
201  m_loadImageTask.join();
202  m_maxHeight = height;
203  }
204 
206  void LoadImage(const wxBitmap &bitmap);
207 
209  wxSize ToImageFile(wxString filename);
210 
212  wxBitmap GetBitmap(double scale = 1.0);
213 
215  wxBitmap GetUnscaledBitmap();
216 
218  void Recalculate(double scale = 1.0);
219 
221  long m_width = 1;
223  long m_height = 1;
224 
226  const wxMemoryBuffer GetCompressedImage() const;
227 
229  std::size_t GetOriginalWidth() const;
230 
232  std::size_t GetOriginalHeight() const;
233 
234 
235 
237  wxMemoryBuffer m_compressedImage;
238 
240  bool CanExportSVG() const {
241  if(m_loadImageTask.joinable())
242  m_loadImageTask.join();
243  return m_svgRast != nullptr;}
244 
246  static const wxString GetBadImageToolTip();
247 
248  class WxmxStream: public wxZipInputStream
249  {
250  public:
251  WxmxStream(wxInputStream &wxmxFile, const wxString &fileInWxmx);
252  };
253 
254  bool HasGnuplotSource() const {return m_gnuplotSource_Compressed.GetDataLen() > 20;}
255 private:
256  bool m_fromWxFS = false;
257  bool m_gnuplotDataThreadRunning = false;
259  wxMemoryBuffer m_gnuplotSource_Compressed;
261  wxMemoryBuffer m_gnuplotData_Compressed;
263  std::size_t m_originalWidth = 640;
265  std::size_t m_originalHeight = 480;
267  wxBitmap m_scaledBitmap;
269  wxString m_extension;
271  wxString m_gnuplotSource;
273  wxString m_gnuplotData;
274  mutable jthread m_loadImageTask;
275  void LoadImage_Backgroundtask(std::unique_ptr<ThreadNumberLimiter> limiter,
276  wxString image, wxString wxmxFile,
277  bool remove);
278  jthread m_loadGnuplotSourceTask;
279  void LoadGnuplotSource_Backgroundtask(
280  std::unique_ptr<ThreadNumberLimiter> limiter,
281  wxString gnuplotFile, wxString dataFile, wxString wxmxFile);
282  void LoadGnuplotSource(wxInputStream *source);
283  void LoadGnuplotData(wxInputStream *data);
284  void LoadGnuplotSource_Backgroundtask_internal(
285  wxInputStream *source,
286  wxInputStream *data);
287 
288 
289  void LoadCompressedGnuplotSource_Backgroundtask(std::unique_ptr<ThreadNumberLimiter> limiter,
290  wxString sourcefile,
291  wxString datafile,
292  wxString wxmxFile
293  );
295  void LoadImage(wxString image, const wxString &wxmxFile, bool remove = true);
297  static wxMemoryBuffer ReadCompressedImage(wxInputStream *data);
298  Configuration *m_configuration = NULL;
301  wxCoord m_maxWidth = -1;
303  wxCoord m_maxHeight = -1;
305  wxString m_imageName;
307  double m_ppi = 72;
308  struct free_deleter { void operator()(void *p) const { std::free(p); } };
309  wxm_NSVGimage* m_svgImage = {};
310  std::unique_ptr<struct wxm_NSVGrasterizer, free_deleter> m_svgRast{nullptr};
311 };
312 
313 #endif // IMAGE_H
A mutex that locks if we have too many background threads.
The configuration storage for the current worksheet.
Definition: Configuration.h:95
Definition: Image.h:249
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:193
double GetHeightList() const
The maximum height this image shall be displayed with.
Definition: Image.h:188
void LoadImage(const wxBitmap &bitmap)
"Loads" an image from a bitmap
Definition: Image.cpp:743
void Recalculate(double scale=1.0)
Can be called to specify a specific scale.
Definition: Image.cpp:921
int GetPPI() const
Return the image's resolution.
Definition: Image.h:112
Image(Configuration *config)
A constructor that generates an empty image. See LoadImage()
Definition: Image.cpp:53
wxString GnuplotSource()
Returns the gnuplot source file name of this image.
Definition: Image.cpp:522
const wxMemoryBuffer GetGnuplotSource()
Returns the gnuplot source of this image.
Definition: Image.cpp:412
std::size_t GetOriginalWidth() const
Returns the original width.
Definition: Image.cpp:188
std::size_t GetOriginalHeight() const
Returns the original height.
Definition: Image.cpp:195
static const wxString GetBadImageToolTip()
The tooltip to use wherever an image that's not Ok is shown.
Definition: Image.cpp:992
long m_height
The height of the scaled image.
Definition: Image.h:223
const wxMemoryBuffer GetGnuplotData()
Returns the gnuplot data of this image.
Definition: Image.cpp:457
long m_width
The width of the scaled image.
Definition: Image.h:221
wxString GetExtension() const
Returns the file name extension of the current image.
Definition: Image.cpp:762
void InvalidBitmap(const wxString &message=wxEmptyString)
Creates a bitmap showing an error message.
Definition: Image.cpp:691
const wxMemoryBuffer GetCompressedImage() const
Returns the original image in its compressed form.
Definition: Image.cpp:182
bool CanExportSVG() const
Can this image be exported in SVG format?
Definition: Image.h:240
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:237
void SetMaxHeight(double height)
Set the maximum height this image shall be displayed with.
Definition: Image.h:199
wxSize ToImageFile(wxString filename)
Saves the image in its original form, or as .png if it originates in a bitmap.
Definition: Image.cpp:563
static wxBitmap RGBA2wxBitmap(const unsigned char imgdata[], const int &width, const int &height, const int &scaleFactor=1)
Converts rgba data to a wxBitmap.
Definition: Image.cpp:1002
double GetMaxWidth() const
The maximum width this image shall be displayed with.
Definition: Image.h:183
wxBitmap GetBitmap(double scale=1.0)
Returns the bitmap being displayed with custom scale.
Definition: Image.cpp:633
wxString GnuplotData()
Returns the gnuplot data file name of this image.
Definition: Image.cpp:487
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:254
void ClearCache()
Temporarily forget the scaled image in order to save memory.
Definition: Image.h:172