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 "precomp.h"
33 #include "Cell.h"
34 #include "Version.h"
35 #include <wx/image.h>
36 
37 #include <wx/filesys.h>
38 #include <wx/fs_arc.h>
39 #include <wx/buffer.h>
40 #define NANOSVG_ALL_COLOR_KEYWORDS
41 #include "nanoSVG/nanosvg.h"
42 #include "nanoSVG/nanosvgrast.h"
43 
44 
68 class Image final
69 {
70 public:
72  explicit Image(Configuration **config);
73 
75  Image(Configuration **config, wxMemoryBuffer image, wxString type);
76 
82  Image(Configuration **config, const wxBitmap &bitmap);
83 
91  Image(Configuration **config, wxString image, std::shared_ptr<wxFileSystem> filesystem, bool remove = true);
92 
93  Image(Configuration **config, const Image &image);
94  Image(const Image &image) = delete;
95 
96  ~Image();
97 
98  void SetConfiguration(Configuration **config){ m_configuration = config; }
100  int GetPPI() const {return m_ppi;}
102  void SetPPI(int ppi) {m_ppi = ppi;}
103 
105  void InvalidBitmap();
106 
113  void GnuplotSource(wxString gnuplotFilename, wxString dataFilename, std::shared_ptr<wxFileSystem> filesystem);
114 
116  void GnuplotSource(wxString gnuplotFilename, wxString dataFilename)
117  {
118  GnuplotSource(gnuplotFilename, dataFilename, {} /* system fs */);
119  }
120 
130  wxString GnuplotSource();
140  wxString GnuplotData();
141 
143  wxMemoryBuffer GetGnuplotSource();
145  wxMemoryBuffer GetGnuplotData();
146 
151  void ClearCache()
152  {
153  if ((m_scaledBitmap.GetWidth() > 1) || (m_scaledBitmap.GetHeight() > 1))
154  m_scaledBitmap.Create(1, 1);
155  }
156 
158  wxString GetExtension();
160  double GetMaxWidth() const {return m_maxWidth;}
162  double GetHeightList() const {return m_maxHeight;}
164  void SetMaxWidth(double width){m_maxWidth = width;}
166  void SetMaxHeight(double height){m_maxHeight = height;}
167 
169  void LoadImage(const wxBitmap &bitmap);
170 
172  wxSize ToImageFile(wxString filename);
173 
175  wxBitmap GetBitmap(double scale = 1.0);
176 
178  bool IsOk();
179 
181  wxBitmap GetUnscaledBitmap();
182 
184  void Recalculate(double scale = 1.0);
185 
187  long m_width;
189  long m_height;
190 
192  wxMemoryBuffer GetCompressedImage();
193 
195  size_t GetOriginalWidth();
196 
198  size_t GetOriginalHeight();
199 
200 
201 
203  wxMemoryBuffer m_compressedImage;
204 
206  bool CanExportSVG() const {return m_svgRast != nullptr;}
207 
209  static const wxString &GetBadImageToolTip();
210 
211 private:
213  wxMemoryBuffer m_gnuplotSource_Compressed;
215  wxMemoryBuffer m_gnuplotData_Compressed;
217  size_t m_originalWidth;
219  size_t m_originalHeight;
221  wxBitmap m_scaledBitmap;
223  wxString m_extension;
225  bool m_isOk;
227  wxString m_gnuplotSource;
229  wxString m_gnuplotData;
230  void LoadImage_Backgroundtask(wxString image, std::shared_ptr<wxFileSystem> filesystem, bool remove);
231  void LoadGnuplotSource_Backgroundtask(wxString gnuplotFilename, wxString dataFilename, std::shared_ptr<wxFileSystem> filesystem);
233  void LoadImage(wxString image, std::shared_ptr<wxFileSystem> filesystem, bool remove = true);
235  static wxMemoryBuffer ReadCompressedImage(wxInputStream *data);
236  Configuration **m_configuration;
241  double m_maxWidth;
243  double m_maxHeight;
245  wxString m_imageName;
247  double m_ppi = 72;
248  struct free_deleter { void operator()(void *p) const { std::free(p); } };
249  NSVGimage* m_svgImage = {};
250  std::unique_ptr<struct NSVGrasterizer, free_deleter> m_svgRast{nullptr};
251 
252  std::shared_ptr<wxFileSystem> m_fs_keepalive_gnuplotdata;
253  std::shared_ptr<wxFileSystem> m_fs_keepalive_imagedata;
254 };
255 
256 #endif // IMAGE_H
Cell.h
Image::GetUnscaledBitmap
wxBitmap GetUnscaledBitmap()
Returns the image in its unscaled form.
Definition: Image.cpp:172
Image::SetMaxWidth
void SetMaxWidth(double width)
Set the maximum width this image shall be displayed with.
Definition: Image.h:164
Image::GetCompressedImage
wxMemoryBuffer GetCompressedImage()
Returns the original image in its compressed form.
Definition: Image.cpp:201
Image::GetPPI
int GetPPI() const
Return the image's resolution.
Definition: Image.h:100
Image::GetBitmap
wxBitmap GetBitmap(double scale=1.0)
Returns the bitmap being displayed with custom scale.
Definition: Image.cpp:650
Image::m_width
long m_width
The width of the scaled image.
Definition: Image.h:187
Image
Definition: Image.h:68
Image::m_compressedImage
wxMemoryBuffer m_compressedImage
The image in its original compressed form.
Definition: Image.h:203
Image::m_height
long m_height
The height of the scaled image.
Definition: Image.h:189
Image::GnuplotSource
wxString GnuplotSource()
Definition: Image.cpp:532
Image::GetGnuplotSource
wxMemoryBuffer GetGnuplotSource()
Returns the gnuplot source of this image.
Definition: Image.cpp:424
Image::LoadImage
void LoadImage(const wxBitmap &bitmap)
"Loads" an image from a bitmap
Definition: Image.cpp:736
Image::InvalidBitmap
void InvalidBitmap()
Creates a bitmap showing an error message.
Definition: Image.cpp:709
Image::Recalculate
void Recalculate(double scale=1.0)
Can be called to specify a specific scale.
Definition: Image.cpp:923
Image::GetBadImageToolTip
static const wxString & GetBadImageToolTip()
The tooltip to use wherever an image that's not Ok is shown.
Definition: Image.cpp:988
Image::ClearCache
void ClearCache()
Definition: Image.h:151
Image::IsOk
bool IsOk()
Does the image show an actual image or an "broken image" symbol?
Definition: Image.cpp:216
Image::GnuplotSource
void GnuplotSource(wxString gnuplotFilename, wxString dataFilename)
Load the gnuplot source file from the system's filesystem.
Definition: Image.h:116
Image::SetPPI
void SetPPI(int ppi)
Set the image's resolution.
Definition: Image.h:102
Image::GetGnuplotData
wxMemoryBuffer GetGnuplotData()
Returns the gnuplot data of this image.
Definition: Image.cpp:459
Image::GetHeightList
double GetHeightList() const
The maximum height this image shall be displayed with.
Definition: Image.h:162
Configuration
Definition: Configuration.h:83
Image::GetExtension
wxString GetExtension()
Returns the file name extension of the current image.
Definition: Image.cpp:755
Image::SetMaxHeight
void SetMaxHeight(double height)
Set the maximum height this image shall be displayed with.
Definition: Image.h:166
Image::GnuplotData
wxString GnuplotData()
Definition: Image.cpp:494
Image::GetOriginalWidth
size_t GetOriginalWidth()
Returns the original width.
Definition: Image.cpp:206
Image::Image
Image(Configuration **config)
A constructor that generates an empty image. See LoadImage()
Definition: Image.cpp:39
Image::GetOriginalHeight
size_t GetOriginalHeight()
Returns the original height.
Definition: Image.cpp:211
Image::GetMaxWidth
double GetMaxWidth() const
The maximum width this image shall be displayed with.
Definition: Image.h:160
NSVGimage
Definition: nanosvg.h:158
Image::CanExportSVG
bool CanExportSVG() const
Can this image be exported in SVG format?
Definition: Image.h:206
Image::ToImageFile
wxSize ToImageFile(wxString filename)
Saves the image in its original form, or as .png if it originates in a bitmap.
Definition: Image.cpp:577