SPH
Bitmap.cpp
Go to the documentation of this file.
1 #include "gui/objects/Bitmap.h"
2 #include "io/FileSystem.h"
3 #include "io/Path.h"
4 #include "objects/Exceptions.h"
5 #include "thread/Scheduler.h"
6 #include <wx/bitmap.h>
7 #include <wx/log.h>
8 #include <wx/rawbmp.h>
9 
11 
12 void toWxBitmap(const Bitmap<Rgba>& bitmap, wxBitmap& wx) {
13  const Pixel size = bitmap.size();
14  if (!wx.IsOk() || wx.GetSize() != wxSize(size.x, size.y)) {
15  wx.Create(size.x, size.y, 32);
16  }
17  SPH_ASSERT(wx.IsOk());
18 
19  wxAlphaPixelData pixels(wx);
20  SPH_ASSERT(pixels);
21 
22  wxAlphaPixelData::Iterator iterator(pixels);
23  SPH_ASSERT(iterator.IsOk());
24  for (int y = 0; y < bitmap.size().y; ++y) {
25  for (int x = 0; x < bitmap.size().x; ++x) {
26  const Rgba rgba = bitmap[Pixel(x, y)];
27  wxColour color(rgba);
28  iterator.Red() = color.Red();
29  iterator.Green() = color.Green();
30  iterator.Blue() = color.Blue();
31  iterator.Alpha() = clamp(int(255.f * rgba.a()), 0, 255);
32 
33  ++iterator;
34  }
35  }
36  SPH_ASSERT(wx.IsOk());
37 }
38 
39 Bitmap<Rgba> toBitmap(wxBitmap& wx) {
40  Bitmap<Rgba> bitmap(Pixel(wx.GetWidth(), wx.GetHeight()));
41 
42  wxNativePixelData pixels(wx);
43  SPH_ASSERT(pixels);
44  wxNativePixelData::Iterator iterator(pixels);
45  SPH_ASSERT(iterator.IsOk());
46  static_assert(
47  std::is_same<std::decay_t<decltype(iterator.Red())>, unsigned char>::value, "expected unsigned char");
48 
49  for (int y = 0; y < bitmap.size().y; ++y) {
50  for (int x = 0; x < bitmap.size().x; ++x) {
51  bitmap[Pixel(x, y)] = Rgba(wxColour(iterator.Red(), iterator.Green(), iterator.Blue()));
52  ++iterator;
53  }
54  }
55 
56  return bitmap;
57 }
58 
59 void saveToFile(const wxBitmap& wx, const Path& path) {
61  wx.SaveFile(path.native().c_str(), wxBITMAP_TYPE_PNG);
62 }
63 
64 void saveToFile(const Bitmap<Rgba>& bitmap, const Path& path) {
65  wxBitmap wx;
66  toWxBitmap(bitmap, wx);
67  saveToFile(wx, path);
68 }
69 
71  wxLogNull logNullGuard; // we have custom error reporting
72  wxBitmap wx;
73  if (!wx.LoadFile(path.native().c_str())) {
74  throw IoError("Cannot load bitmap '" + path.native() + "'");
75  }
76 
77  if (!wx.IsOk()) {
78  throw IoError("Bitmap '" + path.native() + "' failed to load correctly");
79  }
80  return toBitmap(wx);
81 }
82 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Bitmap< Rgba > loadBitmapFromFile(const Path &path)
Definition: Bitmap.cpp:70
Bitmap< Rgba > toBitmap(wxBitmap &wx)
Definition: Bitmap.cpp:39
void saveToFile(const wxBitmap &wx, const Path &path)
Definition: Bitmap.cpp:59
NAMESPACE_SPH_BEGIN void toWxBitmap(const Bitmap< Rgba > &bitmap, wxBitmap &wx)
Definition: Bitmap.cpp:12
Wrapper of wxBitmap, will be possibly replaced by custom implementation.
constexpr INLINE T clamp(const T &f, const T &f1, const T &f2)
Definition: MathBasic.h:35
#define NAMESPACE_SPH_END
Definition: Object.h:12
Object representing a path on a filesystem, similar to std::filesystem::path in c++17.
Interface for executing tasks (potentially) asynchronously.
Pixel size() const
Definition: Bitmap.h:72
Exception thrown when file cannot be read, it has invalid format, etc.
Definition: Exceptions.h:31
Object representing a path on a filesystem.
Definition: Path.h:17
std::string native() const
Returns the native version of the path.
Definition: Path.cpp:71
Path parentPath() const
Returns the parent directory. If the path is empty or root, return empty path.
Definition: Path.cpp:35
Definition: Color.h:8
float & a()
Definition: Color.h:63
Outcome createDirectory(const Path &path, const Flags< CreateDirectoryFlag > flags=CreateDirectoryFlag::ALLOW_EXISTING)
Creates a directory with given path. Creates all parent directories as well.
Definition: FileSystem.cpp:119
Definition: Point.h:101