Skip to content

Commit

Permalink
Bitmap refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Chlumsky committed Apr 20, 2019
1 parent fe19381 commit 997e42f
Show file tree
Hide file tree
Showing 23 changed files with 498 additions and 352 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

## Version 1.6 (2019-04-06)
## Version 1.6 (2019-04-08)

- Core algorithm rewritten to split up advanced edge selection logic into modular template arguments.
- Pseudo-distance evaluation reworked to eliminate discontinuities at the midpoint between edges.
Expand Down
4 changes: 3 additions & 1 deletion Msdfgen.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@
<ItemGroup>
<ClInclude Include="core\arithmetics.hpp" />
<ClInclude Include="core\Bitmap.h" />
<ClInclude Include="core\Bitmap.hpp" />
<ClInclude Include="core\BitmapRef.hpp" />
<ClInclude Include="core\contour-combiners.h" />
<ClInclude Include="core\Contour.h" />
<ClInclude Include="core\edge-coloring.h" />
Expand All @@ -299,6 +301,7 @@
<ClInclude Include="core\EdgeColor.h" />
<ClInclude Include="core\EdgeHolder.h" />
<ClInclude Include="core\equation-solver.h" />
<ClInclude Include="core\pixel-conversion.hpp" />
<ClInclude Include="core\rasterization.h" />
<ClInclude Include="core\render-sdf.h" />
<ClInclude Include="core\save-bmp.h" />
Expand All @@ -315,7 +318,6 @@
<ClInclude Include="resource.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="core\Bitmap.cpp" />
<ClCompile Include="core\contour-combiners.cpp" />
<ClCompile Include="core\Contour.cpp" />
<ClCompile Include="core\edge-coloring.cpp" />
Expand Down
12 changes: 9 additions & 3 deletions Msdfgen.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,20 @@
<ClInclude Include="core\rasterization.h">
<Filter>Core</Filter>
</ClInclude>
<ClInclude Include="core\BitmapRef.hpp">
<Filter>Core</Filter>
</ClInclude>
<ClInclude Include="core\Bitmap.hpp">
<Filter>Core</Filter>
</ClInclude>
<ClInclude Include="core\pixel-conversion.hpp">
<Filter>Core</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Standalone</Filter>
</ClCompile>
<ClCompile Include="core\Bitmap.cpp">
<Filter>Core</Filter>
</ClCompile>
<ClCompile Include="core\Contour.cpp">
<Filter>Core</Filter>
</ClCompile>
Expand Down
77 changes: 0 additions & 77 deletions core/Bitmap.cpp

This file was deleted.

33 changes: 19 additions & 14 deletions core/Bitmap.h
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@

#pragma once

namespace msdfgen {
#include "BitmapRef.hpp"

/// A floating-point RGB pixel.
struct FloatRGB {
float r, g, b;
};
namespace msdfgen {

/// A 2D image bitmap.
template <typename T>
/// A 2D image bitmap with N channels of type T. Pixel memory is managed by the class.
template <typename T, int N = 1>
class Bitmap {

public:
Bitmap();
Bitmap(int width, int height);
Bitmap(const Bitmap<T> &orig);
Bitmap(const BitmapConstRef<T, N> &orig);
Bitmap(const Bitmap<T, N> &orig);
#ifdef MSDFGEN_USE_CPP11
Bitmap(Bitmap<T> &&orig);
Bitmap(Bitmap<T, N> &&orig);
#endif
~Bitmap();
Bitmap<T> & operator=(const Bitmap<T> &orig);
Bitmap<T, N> & operator=(const BitmapConstRef<T, N> &orig);
Bitmap<T, N> & operator=(const Bitmap<T, N> &orig);
#ifdef MSDFGEN_USE_CPP11
Bitmap<T> & operator=(Bitmap<T> &&orig);
Bitmap<T, N> & operator=(Bitmap<T, N> &&orig);
#endif
/// Bitmap width in pixels.
int width() const;
/// Bitmap height in pixels.
int height() const;
T & operator()(int x, int y);
const T & operator()(int x, int y) const;
T * operator()(int x, int y);
const T * operator()(int x, int y) const;
explicit operator T *();
explicit operator const T *() const;
operator BitmapRef<T, N>();
operator BitmapConstRef<T, N>() const;

private:
T *content;
T *pixels;
int w, h;

};

}

#include "Bitmap.hpp"
117 changes: 117 additions & 0 deletions core/Bitmap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@

#include "Bitmap.h"

#include <cstdlib>
#include <cstring>

namespace msdfgen {

template <typename T, int N>
Bitmap<T, N>::Bitmap() : pixels(NULL), w(0), h(0) { }

template <typename T, int N>
Bitmap<T, N>::Bitmap(int width, int height) : w(width), h(height) {
pixels = new T[N*w*h];
}

template <typename T, int N>
Bitmap<T, N>::Bitmap(const BitmapConstRef<T, N> &orig) : w(orig.width), h(orig.height) {
pixels = new T[N*w*h];
memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
}

template <typename T, int N>
Bitmap<T, N>::Bitmap(const Bitmap<T, N> &orig) : w(orig.w), h(orig.h) {
pixels = new T[N*w*h];
memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
}

#ifdef MSDFGEN_USE_CPP11
template <typename T, int N>
Bitmap<T, N>::Bitmap(Bitmap<T, N> &&orig) : pixels(orig.pixels), w(orig.w), h(orig.h) {
orig.pixels = NULL;
orig.w = 0, orig.h = 0;
}
#endif

template <typename T, int N>
Bitmap<T, N>::~Bitmap() {
delete [] pixels;
}

template <typename T, int N>
Bitmap<T, N> & Bitmap<T, N>::operator=(const BitmapConstRef<T, N> &orig) {
if (pixels != orig.pixels) {
delete [] pixels;
w = orig.width, h = orig.height;
pixels = new T[N*w*h];
memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
}
return *this;
}

template <typename T, int N>
Bitmap<T, N> & Bitmap<T, N>::operator=(const Bitmap<T, N> &orig) {
if (this != &orig) {
delete [] pixels;
w = orig.w, h = orig.h;
pixels = new T[N*w*h];
memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
}
return *this;
}

#ifdef MSDFGEN_USE_CPP11
template <typename T, int N>
Bitmap<T, N> & Bitmap<T, N>::operator=(Bitmap<T, N> &&orig) {
if (this != &orig) {
delete [] pixels;
pixels = orig.pixels;
w = orig.w, h = orig.h;
orig.pixels = NULL;
}
return *this;
}
#endif

template <typename T, int N>
int Bitmap<T, N>::width() const {
return w;
}

template <typename T, int N>
int Bitmap<T, N>::height() const {
return h;
}

template <typename T, int N>
T * Bitmap<T, N>::operator()(int x, int y) {
return pixels+N*(w*y+x);
}

template <typename T, int N>
const T * Bitmap<T, N>::operator()(int x, int y) const {
return pixels+N*(w*y+x);
}

template <typename T, int N>
Bitmap<T, N>::operator T *() {
return pixels;
}

template <typename T, int N>
Bitmap<T, N>::operator const T *() const {
return pixels;
}

template <typename T, int N>
Bitmap<T, N>::operator BitmapRef<T, N>() {
return BitmapRef<T, N>(pixels, w, h);
}

template <typename T, int N>
Bitmap<T, N>::operator BitmapConstRef<T, N>() const {
return BitmapConstRef<T, N>(pixels, w, h);
}

}
43 changes: 43 additions & 0 deletions core/BitmapRef.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

#pragma once

#include <cstdlib>

namespace msdfgen {

typedef unsigned char byte;

/// Reference to a 2D image bitmap or a buffer acting as one. Pixel storage not owned or managed by the object.
template <typename T, int N = 1>
struct BitmapRef {

T *pixels;
int width, height;

inline BitmapRef() : pixels(NULL), width(0), height(0) { }
inline BitmapRef(T *pixels, int width, int height) : pixels(pixels), width(width), height(height) { }

inline T * operator()(int x, int y) const {
return pixels+N*(width*y+x);
}

};

/// Constant reference to a 2D image bitmap or a buffer acting as one. Pixel storage not owned or managed by the object.
template <typename T, int N = 1>
struct BitmapConstRef {

const T *pixels;
int width, height;

inline BitmapConstRef() : pixels(NULL), width(0), height(0) { }
inline BitmapConstRef(const T *pixels, int width, int height) : pixels(pixels), width(width), height(height) { }
inline BitmapConstRef(const BitmapRef<T, N> &orig) : pixels(orig.pixels), width(orig.width), height(orig.height) { }

inline const T * operator()(int x, int y) const {
return pixels+N*(width*y+x);
}

};

}
4 changes: 2 additions & 2 deletions core/contour-combiners.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void SimpleContourCombiner<EdgeSelector>::reset(const Point2 &p) {
}

template <class EdgeSelector>
void SimpleContourCombiner<EdgeSelector>::setContourEdge(int i, const EdgeSelector &edgeSelector) {
void SimpleContourCombiner<EdgeSelector>::setContourEdgeSelection(int i, const EdgeSelector &edgeSelector) {
shapeEdgeSelector.merge(edgeSelector);
}

Expand Down Expand Up @@ -61,7 +61,7 @@ void OverlappingContourCombiner<EdgeSelector>::reset(const Point2 &p) {
}

template <class EdgeSelector>
void OverlappingContourCombiner<EdgeSelector>::setContourEdge(int i, const EdgeSelector &edgeSelector) {
void OverlappingContourCombiner<EdgeSelector>::setContourEdgeSelection(int i, const EdgeSelector &edgeSelector) {
DistanceType edgeDistance = edgeSelector.distance();
edgeSelectors[i] = edgeSelector;
shapeEdgeSelector.merge(edgeSelector);
Expand Down
4 changes: 2 additions & 2 deletions core/contour-combiners.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SimpleContourCombiner {

explicit SimpleContourCombiner(const Shape &shape);
void reset(const Point2 &p);
void setContourEdge(int i, const EdgeSelector &edgeSelector);
void setContourEdgeSelection(int i, const EdgeSelector &edgeSelector);
DistanceType distance() const;

private:
Expand All @@ -34,7 +34,7 @@ class OverlappingContourCombiner {

explicit OverlappingContourCombiner(const Shape &shape);
void reset(const Point2 &p);
void setContourEdge(int i, const EdgeSelector &edgeSelector);
void setContourEdgeSelection(int i, const EdgeSelector &edgeSelector);
DistanceType distance() const;

private:
Expand Down
Loading

0 comments on commit 997e42f

Please sign in to comment.