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(const wxString& gnuplotFilename, const 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
250 double GetUserScale() const { return m_userScale; }
252 void SetUserScale(double scale) {
253 if (scale > 0)
254 m_userScale = scale;
255 }
262 bool GetFitToWindow() const { return m_fitToWindow; }
264 void SetFitToWindow(bool fit) { m_fitToWindow = fit; }
265
267 void LoadImage(const wxBitmap &bitmap);
268
270 wxSize ToImageFile(const wxString& filename);
271
273 wxBitmap GetBitmap(double scale = 1.0);
274
276 wxBitmap GetUnscaledBitmap();
277
279 void Recalculate(double scale = 1.0);
280
282 long m_width = 1;
284 long m_height = 1;
285
287 const wxMemoryBuffer GetCompressedImage() const;
288
290 std::size_t GetOriginalWidth() const;
291
293 std::size_t GetOriginalHeight() const;
294
295
296
298 wxMemoryBuffer m_compressedImage;
299
301 bool CanExportSVG() const {
302 if(m_loadImageTask)
303 m_loadImageTask->Wait();
304 return IsSvg();}
305
307 static const wxString GetBadImageToolTip();
308
310 static bool IsSafePath(const wxString &path);
311
312 bool HasGnuplotSource() const {return m_gnuplotSource_Compressed.GetDataLen() > 20;}
313private:
314 bool m_fromWxFS = false;
316 wxMemoryBuffer m_gnuplotSource_Compressed;
318 wxMemoryBuffer m_gnuplotData_Compressed;
320 std::size_t m_originalWidth = 640;
322 std::size_t m_originalHeight = 480;
324 wxBitmap m_scaledBitmap;
326 wxString m_extension;
328 wxString m_gnuplotSource;
330 wxString m_gnuplotData;
332 mutable std::shared_ptr<BackgroundTask> m_loadImageTask;
333 void LoadImage_Backgroundtask(const stop_token& stopToken,
334 const wxString& image, const AttachmentSource& attachments,
335 bool remove);
338 std::shared_ptr<BackgroundTask> m_loadGnuplotSourceTask;
339 void LoadGnuplotSource_Backgroundtask(
340 const stop_token& stopToken,
341 const wxString& gnuplotFile, const wxString& dataFile, const AttachmentSource& attachments);
342 void LoadGnuplotSource(wxInputStream *source);
343 void LoadGnuplotData(wxInputStream *data);
344 void LoadGnuplotSource_Backgroundtask_internal(
345 wxInputStream *source,
346 wxInputStream *data);
347
348
349 void LoadCompressedGnuplotSource_Backgroundtask(const stop_token& stopToken,
350 const wxString& sourcefile,
351 const wxString& datafile,
352 const AttachmentSource& attachments
353 );
355 void LoadImage(wxString image, const AttachmentSource &attachments, bool remove = true);
357 wxMemoryBuffer ReadCompressedImage(const stop_token& stopToken, wxInputStream *data);
359 void RecordDataUnavailable(const wxString &message);
367 void RecordDecodeFailure(const wxString &message, bool missingCodec);
368 LoadFailureKind m_loadFailureKind = LoadFailureKind::None;
369 Configuration *m_configuration = nullptr;
372 wxCoord m_maxWidth = -1;
374 wxCoord m_maxHeight = -1;
376 double m_userScale = 1.0;
378 bool m_fitToWindow = true;
380 wxString m_imageName;
382 double m_ppi = 72;
384 bool IsSvg() const { return m_extension == wxS("svgz"); }
392 const wxBitmapBundle &SvgBundle();
394 wxBitmap SvgBitmapAt(wxSize size);
396 wxBitmapBundle m_svgBundle;
397};
398
399#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:102
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:788
void Recalculate(double scale=1.0)
Can be called to specify a specific scale.
Definition: Image.cpp:1001
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:518
const wxMemoryBuffer GetGnuplotSource()
Returns the gnuplot source of this image.
Definition: Image.cpp:408
std::size_t GetOriginalWidth() const
Returns the original width.
Definition: Image.cpp:205
std::size_t GetOriginalHeight() const
Returns the original height.
Definition: Image.cpp:212
static const wxString GetBadImageToolTip()
The tooltip to use wherever an image that's not Ok is shown.
Definition: Image.cpp:1101
long m_height
The height of the scaled image.
Definition: Image.h:284
const wxMemoryBuffer GetGnuplotData()
Returns the gnuplot data of this image.
Definition: Image.cpp:453
void CompressedGnuplotSource(wxString gnuplotFilename, wxString dataFilename, const AttachmentSource &attachments={})
Loads the compressed gnuplot source and data file for this image.
Definition: Image.cpp:281
long m_width
The width of the scaled image.
Definition: Image.h:282
wxString GetExtension() const
Returns the file name extension of the current image.
Definition: Image.cpp:807
void InvalidBitmap(const wxString &message=wxEmptyString)
Creates a bitmap showing an error message.
Definition: Image.cpp:709
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:199
bool CanExportSVG() const
Can this image be exported in SVG format?
Definition: Image.h:301
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:1082
bool GetFitToWindow() const
Is this image shrunk to fit into the window?
Definition: Image.h:262
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:298
void SetMaxHeight(double height)
Set the maximum height this image shall be displayed with.
Definition: Image.h:238
void SetUserScale(double scale)
Sets the factor GetUserScale() returns; values <= 0 are ignored.
Definition: Image.h:252
void SetFitToWindow(bool fit)
Sets what GetFitToWindow() returns.
Definition: Image.h:264
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:629
wxString GnuplotData()
Returns the gnuplot data file name of this image.
Definition: Image.cpp:483
wxSize ToImageFile(const wxString &filename)
Saves the image in its original form, or as .png if it originates in a bitmap.
Definition: Image.cpp:559
wxBitmap GetUnscaledBitmap()
Returns the image in its unscaled form.
Definition: Image.cpp:179
void ClearCache()
Temporarily forget the scaled image in order to save memory.
Definition: Image.h:202
double GetUserScale() const
The factor the user wants this image displayed larger or smaller with.
Definition: Image.h:250
Definition: Compat.h:47