Path: blob/master/thirdparty/msdfgen/core/Bitmap.hpp
21070 views
1#include "Bitmap.h"23#include <cstdlib>4#include <cstring>56namespace msdfgen {78template <typename T, int N>9Bitmap<T, N>::Bitmap() : pixels(NULL), w(0), h(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { }1011template <typename T, int N>12Bitmap<T, N>::Bitmap(int width, int height, YAxisOrientation yOrientation) : w(width), h(height), yOrientation(yOrientation) {13pixels = new T[N*w*h];14}1516template <typename T, int N>17Bitmap<T, N>::Bitmap(const BitmapConstRef<T, N> &orig) : w(orig.width), h(orig.height), yOrientation(orig.yOrientation) {18pixels = new T[N*w*h];19memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);20}2122template <typename T, int N>23Bitmap<T, N>::Bitmap(const BitmapConstSection<T, N> &orig) : w(orig.width), h(orig.height), yOrientation(orig.yOrientation) {24pixels = new T[N*w*h];25T *dst = pixels;26const T *src = orig.pixels;27int rowLength = N*w;28for (int y = 0; y < h; ++y) {29memcpy(dst, src, sizeof(T)*rowLength);30dst += rowLength;31src += orig.rowStride;32}33}3435template <typename T, int N>36Bitmap<T, N>::Bitmap(const Bitmap<T, N> &orig) : w(orig.w), h(orig.h), yOrientation(orig.yOrientation) {37pixels = new T[N*w*h];38memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);39}4041#ifdef MSDFGEN_USE_CPP1142template <typename T, int N>43Bitmap<T, N>::Bitmap(Bitmap<T, N> &&orig) : pixels(orig.pixels), w(orig.w), h(orig.h), yOrientation(orig.yOrientation) {44orig.pixels = NULL;45orig.w = 0, orig.h = 0;46}47#endif4849template <typename T, int N>50Bitmap<T, N>::~Bitmap() {51delete[] pixels;52}5354template <typename T, int N>55Bitmap<T, N> &Bitmap<T, N>::operator=(const BitmapConstRef<T, N> &orig) {56if (pixels != orig.pixels) {57delete[] pixels;58w = orig.width, h = orig.height;59yOrientation = orig.yOrientation;60pixels = new T[N*w*h];61memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);62}63return *this;64}6566template <typename T, int N>67Bitmap<T, N> &Bitmap<T, N>::operator=(const BitmapConstSection<T, N> &orig) {68if (orig.pixels && orig.pixels >= pixels && orig.pixels < pixels+N*w*h)69return *this = Bitmap<T, N>(orig);70delete[] pixels;71w = orig.width, h = orig.height;72yOrientation = orig.yOrientation;73pixels = new T[N*w*h];74T *dst = pixels;75const T *src = orig.pixels;76int rowLength = N*w;77for (int y = 0; y < h; ++y) {78memcpy(dst, src, sizeof(T)*rowLength);79dst += rowLength;80src += orig.rowStride;81}82return *this;83}8485template <typename T, int N>86Bitmap<T, N> &Bitmap<T, N>::operator=(const Bitmap<T, N> &orig) {87if (this != &orig) {88delete[] pixels;89w = orig.w, h = orig.h;90yOrientation = orig.yOrientation;91pixels = new T[N*w*h];92memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);93}94return *this;95}9697#ifdef MSDFGEN_USE_CPP1198template <typename T, int N>99Bitmap<T, N> &Bitmap<T, N>::operator=(Bitmap<T, N> &&orig) {100if (this != &orig) {101delete[] pixels;102pixels = orig.pixels;103w = orig.w, h = orig.h;104yOrientation = orig.yOrientation;105orig.pixels = NULL;106}107return *this;108}109#endif110111template <typename T, int N>112int Bitmap<T, N>::width() const {113return w;114}115116template <typename T, int N>117int Bitmap<T, N>::height() const {118return h;119}120121template <typename T, int N>122T *Bitmap<T, N>::operator()(int x, int y) {123return pixels+N*(w*y+x);124}125126template <typename T, int N>127const T *Bitmap<T, N>::operator()(int x, int y) const {128return pixels+N*(w*y+x);129}130131template <typename T, int N>132Bitmap<T, N>::operator T *() {133return pixels;134}135136template <typename T, int N>137Bitmap<T, N>::operator const T *() const {138return pixels;139}140141template <typename T, int N>142Bitmap<T, N>::operator BitmapRef<T, N>() {143return BitmapRef<T, N>(pixels, w, h, yOrientation);144}145146template <typename T, int N>147Bitmap<T, N>::operator BitmapConstRef<T, N>() const {148return BitmapConstRef<T, N>(pixels, w, h, yOrientation);149}150151template <typename T, int N>152Bitmap<T, N>::operator BitmapSection<T, N>() {153return BitmapSection<T, N>(pixels, w, h, yOrientation);154}155156template <typename T, int N>157Bitmap<T, N>::operator BitmapConstSection<T, N>() const {158return BitmapConstSection<T, N>(pixels, w, h, yOrientation);159}160161template <typename T, int N>162BitmapSection<T, N> Bitmap<T, N>::getSection(int xMin, int yMin, int xMax, int yMax) {163return BitmapSection<T, N>(pixels+N*(w*yMin+xMin), xMax-xMin, yMax-yMin, N*w, yOrientation);164}165166template <typename T, int N>167BitmapConstSection<T, N> Bitmap<T, N>::getConstSection(int xMin, int yMin, int xMax, int yMax) const {168return BitmapConstSection<T, N>(pixels+N*(w*yMin+xMin), xMax-xMin, yMax-yMin, N*w, yOrientation);169}170171}172173174