15 #include <type_traits> 16 #include <boost/exception/info.hpp> 18 namespace duds {
namespace ui {
27 struct ImageDimensions;
57 std::enable_if_t<std::is_integral<Int0>::value,
bool> =
true,
58 std::enable_if_t<std::is_integral<Int1>::value,
bool> =
true 61 x((
std::int16_t)px), y((
std::int16_t)py) { }
66 return (x == il.
x) && (y == il.
y);
72 return (x != il.
x) || (y != il.
y);
149 std::enable_if_t<std::is_integral<Int0>::value,
bool> =
true,
150 std::enable_if_t<std::is_integral<Int1>::value,
bool> =
true 153 w((
std::int16_t)dw), h((
std::int16_t)dh) { }
158 return (w ==
id.w) && (h ==
id.h);
164 return (w !=
id.w) || (h !=
id.h);
183 return (loc.
x >= 0) && (loc.
x < w) && (loc.
y >= 0) && (loc.
y < h);
190 return (
id.w <= w) && (
id.h <= h);
197 return ((loc.
x +
id.w) <= w) && ((loc.
y +
id.h) <= h);
240 std::min(dim.
w - ((loc.x < 0) ? loc.x : 0), w - std::abs(loc.x)),
244 std::min(dim.
h - ((loc.y < 0) ? loc.y : 0), h - std::abs(loc.y)),
287 typedef boost::error_info<struct Info_ImageLocation, ImageLocation>
292 typedef boost::error_info<struct Info_ImageDimensions, ImageDimensions>
297 typedef boost::error_info<struct Info_ImageDimensions, ImageDimensions>
302 typedef boost::error_info<struct Info_FrameDimensions, ImageDimensions>
331 std::vector<PixelBlock>
img;
348 return (w / (
sizeof(PixelBlock) * 8) +
349 ((w % (
sizeof(PixelBlock) * 8)) ? 1 : 0)) * h;
356 return bufferBlockSize(w, h) *
sizeof(PixelBlock);
364 return width / (
sizeof(PixelBlock) * 8) +
365 ((width % (
sizeof(PixelBlock) * 8)) ? 1 : 0);
373 return dim.
w / (
sizeof(PixelBlock) * 8) +
374 ((dim.
w % (
sizeof(PixelBlock) * 8)) ? 1 : 0);
419 Rotate0DCCW = HorizInc,
425 Rotate90DCCW = VertInc,
432 Rotate180DCCW = HorizDec,
438 Rotate270DCCW = VertDec,
622 return (pos.
x == -1) && (pos.
y == -1);
628 return (src != cp.
src) || (pos != cp.
pos);
634 return (pos.
x != -1) || (pos.
y != -1);
689 return orig.
x + pos.
x;
695 return orig.
y + pos.
y;
802 operator bool ()
const {
898 return ConstPixel::state();
950 bool operator = (
bool b) {
1013 BppImage(
const std::vector<char> &data);
1019 return std::make_shared<BppImage>(id);
1025 static std::shared_ptr<BppImage>
make(
int width,
int height) {
1026 return std::make_shared<BppImage>(width, height);
1032 static std::shared_ptr<BppImage>
make(
const char *data) {
1033 return std::make_shared<BppImage>(data);
1039 static std::shared_ptr<BppImage>
make(
const std::vector<char> &data) {
1040 return std::make_shared<BppImage>(data);
1095 return dim.
w * dim.
h;
1119 const PixelBlock *buffer()
const;
1127 (((
const BppImage*)
this)->buffer());
1133 const std::vector<PixelBlock> &
data()
const {
1149 const PixelBlock *bufferLine(
int py)
const;
1162 (((
const BppImage*)
this)->bufferLine(py));
1183 PixelBlock *(&addr),
1216 const PixelBlock *(&addr),
1221 const_cast<BppImage*
>(
this)->bufferSpot(
1222 const_cast<PixelBlock*&>(addr),
1293 return cpixel(x, y);
1474 void invertLines(
int start,
int height);
1482 void patternLines(
int start,
int height, PixelBlock val);
1489 patternLines(start, height, -1);
1497 patternLines(start, height, 0);
1505 void blankImage(
bool state);
1553 typedef bool (*OpBitFunction)(
bool dest,
bool src);
1559 static const OpBitFunction OpBitFunctions[OpTotal];
1560 typedef void (*OpFunction)(PixelBlock *dest,
const PixelBlock &src,
const PixelBlock &mask);
1566 static const OpFunction OpFunctions[OpTotal];
1608 write(src, destLoc,
ImageLocation(0, 0), srcSize, srcDir, op);
1624 const std::shared_ptr<const BppImage> &src,
1631 write(src.get(), destLoc, srcLoc, srcSize, srcDir, op);
1646 const std::shared_ptr<const BppImage> src,
1652 write(src.get(), destLoc,
ImageLocation(0, 0), srcSize, srcDir, op);
1681 const std::shared_ptr<const BppImage> &src,
1686 write(src.get(), dest, srcDir, op);
1723 drawBox(ul,
id, state ? OpSet : OpNot);
1813 const ConstBppImageSptr &i0,
1814 const ConstBppImageSptr &i1
1821 #endif // #ifndef BPPIMAGE_HPP constexpr ImageLocation(Int0 px, Int1 py)
Construct with the given location.
std::uintptr_t PixelBlock
The type used to hold pixel values, one bit per pixel.
constexpr ImageDimensions clip(const ImageDimensions &dim, const ImageLocation &loc=ImageLocation(0, 0)) const
Returns a region clipped to fit within this object's dimensions.
const ImageDimensions & dimensions() const
Returns the dimensions of the image.
void clearPixel(const ImageLocation &il)
Changes the state of a pixel to clear (false).
int y() const
Returns the vertical coordinate of the referenced pixel relative to this object's origin...
boost::error_info< struct Info_ImageDimensions, ImageDimensions > ImageErrorDimensions
Image dimensions relevant to the error.
void bufferSpot(const PixelBlock *(&addr), PixelBlock &mask, const ImageLocation &il) const
Provides the location of the specified pixel inside the image data.
void resize(int width, int height)
Changes the size of the image.
PixelBlock * bufferLine(int py)
Returns a pointer to the start of the given line.
ImageDimensions dim
The dimensions of the image to iterate over; can be used to limit the portion visited by the iterator...
bool empty() const
Returns true if there is no image data.
constexpr ImageLocation operator+(const ImageLocation &il) const
Add locations together.
void write(const std::shared_ptr< const BppImage > &src, const ImageLocation &destLoc, const ImageLocation &srcLoc, const ImageDimensions &srcSize, Direction srcDir=HorizInc, Operation op=OpSet)
Writes the specified portion of the source into this image.
static std::shared_ptr< BppImage > make(const char *data)
Convenience function to make a shared pointer to an image using the BppImage(const char *) constructo...
Stores a location within an image.
void write(const std::shared_ptr< const BppImage > &src, const ImageLocation &dest, Direction srcDir=HorizInc, Operation op=OpSet)
Writes as much of the given source image as will fit into this image.
ConstBoolProxy(const ConstPixel *p)
std::int16_t y
Vertical coordinate.
PixelBlock mask
The mask used to isolate the referenced pixel.
std::int16_t x
Horizontal coordinate.
constexpr bool operator==(const ImageLocation &il) const
Obvious equality operator.
Retreives a pixel's state; used to allow ConstPixel to be dereferenced like any other iterator to get...
constexpr bool empty() const
True if the dimensions indicate zero area.
int originX() const
Returns the X coordinate of this object's origin used to limit the area of the source image that will...
ImageLocation pos
The location of the referenced pixel on the source image.
int originY() const
Returns the Y coordinate of this object's origin used to limit the area of the source image that will...
std::shared_ptr< const BppImage > ConstBppImageSptr
static std::shared_ptr< BppImage > make(int width, int height)
Convenience function to make a shared pointer to an image using the BppImage(int, int) constructor...
const std::vector< PixelBlock > & data() const
Provides access to the internal vector storing the image data.
bool state() const
Returns the state of the referenced pixel.
void setPixel(const ImageLocation &il)
Changes the state of a pixel to set (true).
ConstPixel begin() const
Returns a ConstPixel (iterator) to the upper left of the image.
constexpr ImageDimensions(Int0 dw, Int1 dh)
Construct with the given dimensions.
A forward and output iterator that visits each location of the image.
constexpr ImageDimensions minExtent(const ImageDimensions &dim) const
Returns new dimensions that only cover the union of this dimension and the given dimension.
static constexpr int bufferBlocksPerLine(int width)
Returns the number of PixelBlock objects that will be used for each horizontal line of an image of th...
constexpr ImageDimensions maxExtent(const ImageDimensions &dim) const
Returns new dimensions that are minimally large enough to fit this dimension and the given dimension...
constexpr ConstPixel()
Construct a ConstPixel to nowhere.
void state(bool s)
Sets the state of the pixel.
void drawBox(int x, int y, int w, int h=1, Operation op=OpSet)
Draws a box into this image.
void state(int x, int y, bool s)
Changes the state of a pixel.
Pixel pixel(int x, int y, Direction dir=HorizInc)
Returns a Pixel (iterator) to iterate across the image starting from the given location.
std::ostream & operator<<(std::ostream &os, const ImageLocation &il)
Writes an ImageLocation object to a stream in human readable form.
Direction dir
The direction to move when incremented.
std::int16_t width() const
Returns the width of the image.
const ImageLocation & location() const
Returns the coordinates of the referenced pixel relative to this object's origin. ...
PixelBlock * blk
The PixelBlock containing the referenced pixel.
Stores the dimensions of an image.
static constexpr std::size_t bufferByteSize(int w, int h)
Returns the size of an image buffer in bytes needed for the specified image size. ...
The Y coordinate will be incremented.
static constexpr ConstPixel cend()
Returns a ConstPixel end iterator.
void location(int x, int y)
Changes the location referenced by this ConstPixel relative to its origin.
int absY() const
Returns the absolute vertical coordinate of the referenced pixel.
std::vector< PixelBlock > img
The image data.
void setPixel(int x, int y)
Changes the state of a pixel to set (true).
std::shared_ptr< BppImage > BppImageSptr
Performs a bitwise exclusive-or operation with the destination and source data, and places the result...
int height() const
Returns the height of this object's dimensions used to limit the area of the source image that will b...
boost::error_info< struct Info_ImageDimensions, ImageDimensions > ImageErrorSourceDimensions
Image dimensions for a source image relevant to the error.
BoolProxy operator*()
Dereferences the Pixel; provides the state of the pixel.
void write(const BppImage *const src, const ImageLocation &destLoc, const ImageDimensions &srcSize, Direction srcDir=HorizInc, Operation op=OpSet)
Writes a portion of the source image, starting from (0, 0), into this image.
ConstPixel end() const
Returns a ConstPixel end iterator.
std::size_t bufferSize() const
Returns the number of PixelBlocks, not bytes, that make up the image buffer.
void clear()
Clears (make false) the referenced pixel.
The Y coordinate will be decremented.
static EndPixel endPixel()
Convenience function that returns EndPixel, which can be used as an end iterator with any ConstPixel ...
void drawBox(ImageLocation ul, ImageDimensions id, bool state)
Draws a box into this image.
void clearLines(int start, int height)
Clears all the pixles of the given contiguous horizontal lines.
void swap(ImageLocation &l0, ImageLocation &l1)
Swaps the values of two ImageLocation objects.
int width() const
Returns the width of this object's dimensions used to limit the area of the source image that will be...
static std::shared_ptr< BppImage > make(const std::vector< char > &data)
Convenience function to make a shared pointer to an image using the BppImage(const std::vector<char> ...
PixelBlock * buffer()
Retuns a pointer to the start of image data.
ConstBoolProxy operator*() const
Dereferences the ConstPixel; provides the state of the pixel.
ImageLocation absLocation() const
Returns the absolute coordinates of the referenced pixel.
static constexpr int bufferBlocksPerLine(ImageDimensions dim)
Returns the number of PixelBlock objects that will be used for each horizontal line of an image of th...
constexpr ImageLocation swappedAxes() const
Returns a new location with swapped axes.
ImageDimensions dim
The dimensions of the image.
std::basic_ostream< Char, Traits > & clear(std::basic_ostream< Char, Traits > &os)
Display stream manipulator that clears all text from the display and places the cursor in the upper l...
Pixel(BppImage *img, const ImageLocation &il={ 0, 0 }, Direction d=HorizInc)
Construct a Pixel to reference the requested location of the image.
static constexpr std::size_t bufferBlockSize(int w, int h)
Returns the size of an image buffer as the number of PixelBlock PixelBlocks needed to store an image ...
constexpr ConstPixel(const EndPixel)
Construct a ConstPixel to nowhere.
void direction(Direction d)
Changes the direction used for incrementing.
Can be used as an end iterator to avoid making a whole iterator.
int size() const
Returns the number of pixels that make up the image.
int absX() const
Returns the absolute horizontal coordinate of the referenced pixel.
The X coordinate will be decremented until reaching zero.
void bufferSpot(PixelBlock *(&addr), PixelBlock &mask, int x, int y)
Provides the location of the specified pixel inside the image data.
int blkPerLine
Number of PixelBlocks used for each horizontal line.
void swapAxes()
Swaps the dimensions's axes.
The X coordinate will be incremented until reaching the width limit.
ImageLocation orig
Upper left corner of the image to limit the iteration to a portion of the whole image.
const ImageLocation & origin() const
Returns this object's origin used to limit the area of the source image that will be visited...
constexpr bool fits(const ImageLocation &loc, const ImageDimensions &id) const
Returns true if the given dimensions at the given location relative to this object can fit within thi...
Direction
Controls the direction ConstPixel and Pixel objects will move across the image when the object is inc...
Assigns the pixels in the destination the opposing value of the pixels in the source.
ImageDimensions MaxExtent(const BppImage *i0, const BppImage *i1)
Returns the maximum extent of the dimensions of two bit-per-pixel images.
Performs a bitwise or operation with the destination and source data, and places the result in the de...
Operation
Tells how to modify the destination pixel with the source pixel data.
Direction direction() const
Returns the direction used for incrementing.
BppImage()
Make an empty image with zero size.
void swap(BppImage &bi0, BppImage &bi1)
Support swap operations with the C++ standard library and BppImage objects.
A forward iterator like class that visits each location of the image or a subset of the image...
void swap(BppImage &other)
Swap two images.
void write(const std::shared_ptr< const BppImage > src, const ImageLocation &destLoc, const ImageDimensions &srcSize, Direction srcDir=HorizInc, Operation op=OpSet)
Writes a portion of the source image, starting from (0, 0), into this image.
void clearPixel(int x, int y)
Changes the state of a pixel to clear (false).
std::weak_ptr< BppImage > BppImageWptr
static std::shared_ptr< BppImage > make(const ImageDimensions &id)
Convenience function to make a shared pointer to an image using the BppImage(const ImageDimensions &)...
constexpr ImageDimensions swappedAxes() const
Returns new dimensions with swapped axes.
bool state() const
Returns the state of the referenced pixel.
void clearImage()
Clears every pixel (change to false) in the image.
General graphics related code.
void setLines(int start, int height)
Sets all the pixles of the given contiguous horizontal lines.
constexpr bool withinBounds(const ImageLocation &loc) const
Returns true if the given location is within the bounds specified by this object. ...
bool invertPixel(int x, int y)
Toggles the state of a pixel.
bool state(int x, int y) const
Returns the state of the image pixel of the requested location.
std::weak_ptr< const BppImage > ConstBppImageWptr
boost::error_info< struct Info_ImageLocation, ImageLocation > ImageErrorLocation
An image location relevant to the error.
ConstPixel cpixel(int x, int y, Direction dir=HorizInc) const
Returns a ConstPixel (iterator) to iterate across the image starting from the given location...
int x() const
Returns the horizontal coordinate of the referenced pixel relative to this object's origin...
Retreives a pixel's state; used to allow Pixel to be dereferenced like any other iterator to get a bo...
Pixel(BppImage *img, const ImageLocation &o, const ImageDimensions &s, const ImageLocation &p={ 0, 0 }, Direction d=HorizInc)
Construct a Pixel to iterate over a subset of the image, and start at a given spot.
constexpr ImageLocation operator-(const ImageLocation &il) const
Subtract locations.
BppImage * src
The image to operate upon.
void clear()
Set the dimensions to zero area.
ConstPixel(const BppImage *img, int x, int y, Direction d=HorizInc)
Construct a ConstPixel to reference the requested location of the image.
constexpr bool operator!=(const ImageLocation &il) const
Obvious inequality operator.
Pixel(BppImage *img, int x, int y, Direction d=HorizInc)
Construct a Pixel to reference the requested location of the image.
Assigns the pixels in the destination the same value as the pixels in the source. ...
ImageLocation()=default
Construct uninitialized.
An image that uses a single bit to represent the state of each pixel; a black or white picture...
void drawBox(int x, int y, int w, int h, bool state)
Draws a box into this image.
const ImageDimensions & dimensions() const
Returns this object's dimensions used to limit the area of the source image that will be visited...
int blocksPerLine() const
Returns the number of PixelBlock objects per row in the image data.
boost::error_info< struct Info_FrameDimensions, ImageDimensions > ImageErrorTargetDimensions
Image dimensions for a target image relevant to the error.
constexpr bool fits(const ImageDimensions &id) const
Returns true if the given dimensions are not larger than this object's dimensions.
void swapAxes()
Swaps the location's axes.
void setImage()
Sets every pixel (set to true) in the image.
std::int16_t height() const
Returns the height of the image.
BppImage(int width, int height)
Makes an image of the requested size with uninitialized image data.
Performs a bitwise and operation with the destination and source data, and places the result in the d...