Path: blob/master/thirdparty/msdfgen/core/BitmapRef.hpp
20901 views
1#pragma once23#include "YAxisOrientation.h"45namespace msdfgen {67/// Reference to a 2D image bitmap or a buffer acting as one. Pixel storage not owned or managed by the object.8template <typename T, int N = 1>9struct BitmapRef;10/// Constant reference to a 2D image bitmap or a buffer acting as one. Pixel storage not owned or managed by the object.11template <typename T, int N = 1>12struct BitmapConstRef;13/// Reference to a 2D image bitmap with non-contiguous rows of pixels. Pixel storage not owned or managed by the object. Can represent e.g. a section of a larger bitmap, bitmap with padded rows, or vertically flipped bitmap (rowStride can be negative).14template <typename T, int N = 1>15struct BitmapSection;16/// Constant reference to a 2D image bitmap with non-contiguous rows of pixels. Pixel storage not owned or managed by the object. Can represent e.g. a section of a larger bitmap, bitmap with padded rows, or vertically flipped bitmap (rowStride can be negative).17template <typename T, int N = 1>18struct BitmapConstSection;1920template <typename T, int N>21struct BitmapRef {2223T *pixels;24int width, height;25YAxisOrientation yOrientation;2627inline BitmapRef() : pixels(NULL), width(0), height(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { }28inline BitmapRef(T *pixels, int width, int height, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), yOrientation(yOrientation) { }2930inline T *operator()(int x, int y) const {31return pixels+N*(width*y+x);32}3334/// Returns a reference to a rectangular section of the bitmap specified by bounds (excluding xMax, yMax).35inline BitmapSection<T, N> getSection(int xMin, int yMin, int xMax, int yMax) const {36return BitmapSection<T, N>(pixels+N*(width*yMin+xMin), xMax-xMin, yMax-yMin, N*width, yOrientation);37}3839/// Returns a constant reference to a rectangular section of the bitmap specified by bounds (excluding xMax, yMax).40inline BitmapConstSection<T, N> getConstSection(int xMin, int yMin, int xMax, int yMax) const {41return BitmapConstSection<T, N>(pixels+N*(width*yMin+xMin), xMax-xMin, yMax-yMin, N*width, yOrientation);42}4344};4546template <typename T, int N>47struct BitmapConstRef {4849const T *pixels;50int width, height;51YAxisOrientation yOrientation;5253inline BitmapConstRef() : pixels(NULL), width(0), height(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { }54inline BitmapConstRef(const T *pixels, int width, int height, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), yOrientation(yOrientation) { }55inline BitmapConstRef(const BitmapRef<T, N> &orig) : pixels(orig.pixels), width(orig.width), height(orig.height), yOrientation(orig.yOrientation) { }5657inline const T *operator()(int x, int y) const {58return pixels+N*(width*y+x);59}6061/// Returns a constant reference to a rectangular section of the bitmap specified by bounds (excluding xMax, yMax).62inline BitmapConstSection<T, N> getSection(int xMin, int yMin, int xMax, int yMax) const {63return BitmapConstSection<T, N>(pixels+N*(width*yMin+xMin), xMax-xMin, yMax-yMin, N*width, yOrientation);64}6566/// Returns a constant reference to a rectangular section of the bitmap specified by bounds (excluding xMax, yMax).67inline BitmapConstSection<T, N> getConstSection(int xMin, int yMin, int xMax, int yMax) const {68return getSection(xMin, yMin, xMax, yMax);69}7071};7273template <typename T, int N>74struct BitmapSection {7576T *pixels;77int width, height;78/// Specifies the difference between the beginnings of adjacent pixel rows as the number of T elements, can be negative.79int rowStride;80YAxisOrientation yOrientation;8182inline BitmapSection() : pixels(NULL), width(0), height(0), rowStride(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { }83inline BitmapSection(T *pixels, int width, int height, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), rowStride(N*width), yOrientation(yOrientation) { }84inline BitmapSection(T *pixels, int width, int height, int rowStride, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), rowStride(rowStride), yOrientation(yOrientation) { }85inline BitmapSection(const BitmapRef<T, N> &orig) : pixels(orig.pixels), width(orig.width), height(orig.height), rowStride(N*orig.width), yOrientation(orig.yOrientation) { }8687inline T *operator()(int x, int y) const {88return pixels+rowStride*y+N*x;89}9091/// Returns a reference to a rectangular subsection of the bitmap specified by bounds (excluding xMax, yMax).92inline BitmapSection<T, N> getSection(int xMin, int yMin, int xMax, int yMax) const {93return BitmapSection<T, N>(pixels+rowStride*yMin+N*xMin, xMax-xMin, yMax-yMin, rowStride, yOrientation);94}9596/// Returns a constant reference to a rectangular subsection of the bitmap specified by bounds (excluding xMax, yMax).97inline BitmapConstSection<T, N> getConstSection(int xMin, int yMin, int xMax, int yMax) const {98return BitmapConstSection<T, N>(pixels+rowStride*yMin+N*xMin, xMax-xMin, yMax-yMin, rowStride, yOrientation);99}100101/// Makes sure that the section's Y-axis orientation matches the argument by potentially reordering its rows.102inline void reorient(YAxisOrientation newYAxisOrientation) {103if (yOrientation != newYAxisOrientation) {104pixels += rowStride*(height-1);105rowStride = -rowStride;106yOrientation = newYAxisOrientation;107}108}109110};111112template <typename T, int N>113struct BitmapConstSection {114115const T *pixels;116int width, height;117/// Specifies the difference between the beginnings of adjacent pixel rows as the number of T elements, can be negative.118int rowStride;119YAxisOrientation yOrientation;120121inline BitmapConstSection() : pixels(NULL), width(0), height(0), rowStride(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { }122inline BitmapConstSection(const T *pixels, int width, int height, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), rowStride(N*width), yOrientation(yOrientation) { }123inline BitmapConstSection(const T *pixels, int width, int height, int rowStride, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), rowStride(rowStride), yOrientation(yOrientation) { }124inline BitmapConstSection(const BitmapRef<T, N> &orig) : pixels(orig.pixels), width(orig.width), height(orig.height), rowStride(N*orig.width), yOrientation(orig.yOrientation) { }125inline BitmapConstSection(const BitmapConstRef<T, N> &orig) : pixels(orig.pixels), width(orig.width), height(orig.height), rowStride(N*orig.width), yOrientation(orig.yOrientation) { }126inline BitmapConstSection(const BitmapSection<T, N> &orig) : pixels(orig.pixels), width(orig.width), height(orig.height), rowStride(orig.rowStride), yOrientation(orig.yOrientation) { }127128inline const T *operator()(int x, int y) const {129return pixels+rowStride*y+N*x;130}131132/// Returns a constant reference to a rectangular subsection of the bitmap specified by bounds (excluding xMax, yMax).133inline BitmapConstSection<T, N> getSection(int xMin, int yMin, int xMax, int yMax) const {134return BitmapConstSection<T, N>(pixels+rowStride*yMin+N*xMin, xMax-xMin, yMax-yMin, rowStride, yOrientation);135}136137/// Returns a constant reference to a rectangular subsection of the bitmap specified by bounds (excluding xMax, yMax).138inline BitmapConstSection<T, N> getConstSection(int xMin, int yMin, int xMax, int yMax) const {139return getSection(xMin, yMin, xMax, yMax);140}141142/// Makes sure that the section's Y-axis orientation matches the argument by potentially reordering its rows.143inline void reorient(YAxisOrientation newYAxisOrientation) {144if (yOrientation != newYAxisOrientation) {145pixels += rowStride*(height-1);146rowStride = -rowStride;147yOrientation = newYAxisOrientation;148}149}150151};152153}154155156