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 "AttachmentSource.h"
35#include "BackgroundQueue.h"
36#include "precomp.h"
37#include "Compat.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#include <wx/bmpbndl.h>
44
45
69class Image final
70{
71public:
73 explicit Image(Configuration *config);
74
76 Image(Configuration *config, const wxMemoryBuffer &image, const wxString &type);
77
83 Image(Configuration *config, const wxBitmap &bitmap);
84
93 Image(Configuration *config, const wxString &image,
94 const AttachmentSource &attachments, bool remove = true);
95
96 Image(Configuration *config, const Image &image);
97 Image(const Image &image) = delete;
98
99 ~Image();
100
101
102 void SetConfiguration(Configuration *config){
103 if(m_loadImageTask)
104 m_loadImageTask->Wait();
105 m_configuration = config; }
107 int GetPPI() const {
108 if(m_loadImageTask)
109 m_loadImageTask->Wait();
110 return m_ppi;}
112 void SetPPI(int ppi) {
113 if(m_loadImageTask)
114 m_loadImageTask->Wait();
115 m_ppi = ppi;}
116
118 void InvalidBitmap(const wxString &message = wxEmptyString);
119 void GenerateInvalidBitmap();
120
121 wxString m_invalidBitmapMessage;
122 bool m_isInvalid = false;
123
125 enum class LoadFailureKind {
126 None,
143 };
150 if (m_loadImageTask)
151 m_loadImageTask->Wait();
152 return m_loadFailureKind;
153 }
154
161 void GnuplotSource(wxString gnuplotFilename, wxString dataFilename,
162 const AttachmentSource &attachments = {});
163
167 void CompressedGnuplotSource(wxString gnuplotFilename, wxString dataFilename,
168 const AttachmentSource &attachments = {});
169
179 wxString GnuplotSource();
189 wxString GnuplotData();
190
192 const wxMemoryBuffer GetGnuplotSource();
193 const wxMemoryBuffer GetCompressedGnuplotSource();
195 const wxMemoryBuffer GetGnuplotData();
196 const wxMemoryBuffer GetCompressedGnuplotData();
197
203 {
204 if(m_loadImageTask)
205 m_loadImageTask->Wait();
206 // Querying the size of an invalid bitmap trips a wxWidgets assertion (and,
207 // under a GUI assert handler, hangs). A not-yet-rendered cache is already
208 // "clear", so only shrink a bitmap that actually exists.
209 if (m_scaledBitmap.IsOk() &&
210 ((m_scaledBitmap.GetWidth() > 1) || (m_scaledBitmap.GetHeight() > 1)))
211 m_scaledBitmap.Create(1, 1);
212 // An SVG bundle holds the parsed document plus the last bitmap it
213 // rendered, at full size. SvgBundle() makes a new one when needed.
214 m_svgBundle = wxBitmapBundle();
215 }
216
218 bool HoldsSvgRendering() const { return m_svgBundle.IsOk(); }
220 wxString GetExtension() const;
222 double GetMaxWidth() const {
223 if(m_loadImageTask)
224 m_loadImageTask->Wait();
225 return m_maxWidth;}
227 double GetHeightList() const {
228 if(m_loadImageTask)
229 m_loadImageTask->Wait();
230 return m_maxHeight;}
232 void SetMaxWidth(double width){
233 if(m_loadImageTask)
234 m_loadImageTask->Wait();
235 m_maxWidth = width;
236 }
238 void SetMaxHeight(double height){
239 if(m_loadImageTask)
240 m_loadImageTask->Wait();
241 m_maxHeight = height;
242 }
243
245 void LoadImage(const wxBitmap &bitmap);
246
248 wxSize ToImageFile(wxString filename);
249
251 wxBitmap GetBitmap(double scale = 1.0);
252
254 wxBitmap GetUnscaledBitmap();
255
257 void Recalculate(double scale = 1.0);
258
260 long m_width = 1;
262 long m_height = 1;
263
265 const wxMemoryBuffer GetCompressedImage() const;
266
268 std::size_t GetOriginalWidth() const;
269
271 std::size_t GetOriginalHeight() const;
272
273
274
276 wxMemoryBuffer m_compressedImage;
277
279 bool CanExportSVG() const {
280 if(m_loadImageTask)
281 m_loadImageTask->Wait();
282 return IsSvg();}
283
285 static const wxString GetBadImageToolTip();
286
288 static bool IsSafePath(const wxString &path);
289
290 bool HasGnuplotSource() const {return m_gnuplotSource_Compressed.GetDataLen() > 20;}
291private:
292 bool m_fromWxFS = false;
294 wxMemoryBuffer m_gnuplotSource_Compressed;
296 wxMemoryBuffer m_gnuplotData_Compressed;
298 std::size_t m_originalWidth = 640;
300 std::size_t m_originalHeight = 480;
302 wxBitmap m_scaledBitmap;
304 wxString m_extension;
306 wxString m_gnuplotSource;
308 wxString m_gnuplotData;
310 mutable std::shared_ptr<BackgroundTask> m_loadImageTask;
311 void LoadImage_Backgroundtask(stop_token stopToken,
312 wxString image, AttachmentSource attachments,
313 bool remove);
316 std::shared_ptr<BackgroundTask> m_loadGnuplotSourceTask;
317 void LoadGnuplotSource_Backgroundtask(
318 stop_token stopToken,
319 wxString gnuplotFile, wxString dataFile, AttachmentSource attachments);
320 void LoadGnuplotSource(wxInputStream *source);
321 void LoadGnuplotData(wxInputStream *data);
322 void LoadGnuplotSource_Backgroundtask_internal(
323 wxInputStream *source,
324 wxInputStream *data);
325
326
327 void LoadCompressedGnuplotSource_Backgroundtask(stop_token stopToken,
328 wxString sourcefile,
329 wxString datafile,
330 AttachmentSource attachments
331 );
333 void LoadImage(wxString image, const AttachmentSource &attachments, bool remove = true);
335 wxMemoryBuffer ReadCompressedImage(stop_token stopToken, wxInputStream *data);
337 void RecordDataUnavailable(const wxString &message);
345 void RecordDecodeFailure(const wxString &message, bool missingCodec);
346 LoadFailureKind m_loadFailureKind = LoadFailureKind::None;
347 Configuration *m_configuration = nullptr;
350 wxCoord m_maxWidth = -1;
352 wxCoord m_maxHeight = -1;
354 wxString m_imageName;
356 double m_ppi = 72;
358 bool IsSvg() const { return m_extension == wxS("svgz"); }
366 const wxBitmapBundle &SvgBundle();
368 wxBitmap SvgBitmapAt(wxSize size);
370 wxBitmapBundle m_svgBundle;
371};
372
373#endif // IMAGE_H
Where the files a worksheet's XML refers to (images, gnuplot sources and data) can be read from.
A priority work-queue thread pool for offloading work to background threads.
Small compatibility shims for older toolchains and older wxWidgets.
The files a worksheet's XML refers to by name.
Definition: AttachmentSource.h:49
The configuration storage for the current worksheet.
Definition: Configuration.h:101
Manages an auto-scaling image.
Definition: Image.h:70
void SetMaxWidth(double width)
Set the maximum width this image shall be displayed with.
Definition: Image.h:232
double GetHeightList() const
The maximum height this image shall be displayed with.
Definition: Image.h:227
void LoadImage(const wxBitmap &bitmap)
"Loads" an image from a bitmap
Definition: Image.cpp:786
void Recalculate(double scale=1.0)
Can be called to specify a specific scale.
Definition: Image.cpp:999
int GetPPI() const
Return the image's resolution.
Definition: Image.h:107
wxString GnuplotSource()
Returns the gnuplot source file name of this image.
Definition: Image.cpp:516
const wxMemoryBuffer GetGnuplotSource()
Returns the gnuplot source of this image.
Definition: Image.cpp:406
std::size_t GetOriginalWidth() const
Returns the original width.
Definition: Image.cpp:203
std::size_t GetOriginalHeight() const
Returns the original height.
Definition: Image.cpp:210
static const wxString GetBadImageToolTip()
The tooltip to use wherever an image that's not Ok is shown.
Definition: Image.cpp:1092
long m_height
The height of the scaled image.
Definition: Image.h:262
const wxMemoryBuffer GetGnuplotData()
Returns the gnuplot data of this image.
Definition: Image.cpp:451
void CompressedGnuplotSource(wxString gnuplotFilename, wxString dataFilename, const AttachmentSource &attachments={})
Loads the compressed gnuplot source and data file for this image.
Definition: Image.cpp:279
long m_width
The width of the scaled image.
Definition: Image.h:260
wxString GetExtension() const
Returns the file name extension of the current image.
Definition: Image.cpp:805
void InvalidBitmap(const wxString &message=wxEmptyString)
Creates a bitmap showing an error message.
Definition: Image.cpp:707
LoadFailureKind GetLoadFailureKind() const
Blocks until this image's background load task (if any) has finished, then reports why it ended up in...
Definition: Image.h:149
const wxMemoryBuffer GetCompressedImage() const
Returns the original image in its compressed form.
Definition: Image.cpp:197
bool CanExportSVG() const
Can this image be exported in SVG format?
Definition: Image.h:279
bool HoldsSvgRendering() const
True while this SVG image keeps its parsed document and a rendering in memory.
Definition: Image.h:218
LoadFailureKind
Why the most recent load attempt left this image invalid, if it did.
Definition: Image.h:125
@ MissingCodec
Data was read fine, but this wxWidgets build has no decoder registered for the format at all (e....
@ DecodeFailed
Data was read fine and a decoder exists for the format (or it's an SVG, parsed by our own vendored co...
@ DataUnavailable
Raw data could never be obtained at all (file not found, empty wxmx entry, ...). Always a real failur...
static bool IsSafePath(const wxString &path)
Returns true if the path is in a safe (temporary) directory for deletion.
Definition: Image.cpp:1073
void SetPPI(int ppi)
Set the image's resolution.
Definition: Image.h:112
wxMemoryBuffer m_compressedImage
The image in its original compressed form.
Definition: Image.h:276
void SetMaxHeight(double height)
Set the maximum height this image shall be displayed with.
Definition: Image.h:238
wxSize ToImageFile(wxString filename)
Saves the image in its original form, or as .png if it originates in a bitmap.
Definition: Image.cpp:557
double GetMaxWidth() const
The maximum width this image shall be displayed with.
Definition: Image.h:222
wxBitmap GetBitmap(double scale=1.0)
Returns the bitmap being displayed with custom scale.
Definition: Image.cpp:627
wxString GnuplotData()
Returns the gnuplot data file name of this image.
Definition: Image.cpp:481
wxBitmap GetUnscaledBitmap()
Returns the image in its unscaled form.
Definition: Image.cpp:177
void ClearCache()
Temporarily forget the scaled image in order to save memory.
Definition: Image.h:202
Definition: Compat.h:47