39template <
typename PixelType>
class Image {
46 std::shared_ptr<PixelType[]> data;
48 using Ptr = std::shared_ptr<Image<PixelType>>;
57 static std::size_t checkedSize(
int rows,
int columns) {
59 const auto rowCount =
static_cast<std::size_t
>(rows);
60 const auto columnCount =
static_cast<std::size_t
>(columns);
62 throw std::overflow_error(
"Image dimensions exceed the supported int-indexed size."));
63 return rowCount * columnCount;
78 Image(
int rows,
int columns) : numRows(rows), numColumns(columns), data(new PixelType[checkedSize(rows, columns)], [](PixelType* pixels) {
delete[] pixels; }) {}
89 [[nodiscard]]
static Ptr
create(
int rows,
int columns) {
return std::make_shared<Image>(rows, columns); }
101 [[nodiscard]]
static Ptr
create(
int rows,
int columns, PixelType initValue) {
102 auto img =
create(rows, columns);
103 img->fill(initValue);
120 [[nodiscard]]
static Ptr
fromExternal(PixelType* rawPtr,
int rows,
int columns) {
121 MMCFILTERS_CONTRACT_REQUIRE(rawPtr !=
nullptr,
throw std::invalid_argument(
"Image::fromExternal requires a non-null raw pointer."));
122 auto img =
create(rows, columns);
123 img->data = std::shared_ptr<PixelType[]>(rawPtr, [](PixelType*) {
139 [[nodiscard]]
static Ptr
fromRaw(PixelType* rawPtr,
int rows,
int columns) {
141 auto img =
create(rows, columns);
142 img->data = std::shared_ptr<PixelType[]>(rawPtr, [](PixelType* pixels) {
delete[] pixels; });
153 void fill(PixelType value) { std::fill_n(data.get(), numRows * numColumns, value); }
164 if (numRows != other->numRows || numColumns != other->numColumns)
166 int n = numRows * numColumns;
167 for (
int i = 0; i < n; ++i) {
168 if (data[i] != (*other)[i])
182 auto newImg =
create(numRows, numColumns);
183 std::copy(data.get(), data.get() + (numRows * numColumns), newImg->data.get());
227 int getSize()
const {
return numRows * numColumns; }
269template <
typename PixelType>
using ImagePtr = std::shared_ptr<Image<PixelType>>;
287 inline static PixelId to1D(
int row,
int column,
int numColumns)
noexcept {
return row * numColumns + column; }
296 inline static std::pair<int, int>
to2D(
PixelId index,
int numColumns)
noexcept {
297 int row = index / numColumns;
298 int column = index - row * numColumns;
299 return {row, column};
317 int sizeImage = numColumnsOfImage * numRowsOfImage;
318 for (
int i = 0; i < sizeImage; i++) {
323 std::unique_ptr<int[]> r(
new int[max + 1]);
324 std::unique_ptr<int[]> g(
new int[max + 1]);
325 std::unique_ptr<int[]> b(
new int[max + 1]);
329 for (
int i = 1; i <= max; i++) {
335 int sizeOutput = sizeImage * 3;
338 auto output = outImage->rawData();
340 std::fill_n(output, sizeOutput, 0);
342 for (
int pidx = 0; pidx < sizeImage; pidx++) {
343 int cpidx = pidx * 3;
344 output[cpidx] = r[img[pidx]];
345 output[cpidx + 1] = g[img[pidx]];
346 output[cpidx + 2] = b[img[pidx]];
Shared scalar conventions used by the C++ API.
int PixelId
Pixel identifier type used by source and active construction domains.
Compile-time policy for defensive checks at untrusted API boundaries.
#define MMCFILTERS_CONTRACT_REQUIRE(condition,...)
Evaluates a caller precondition and its failure action only in checked builds.
Image< int32_t > ImageInt32
32-bit signed integer image container.
std::shared_ptr< ImageUInt8 > ImageUInt8Ptr
Shared pointer to an 8-bit unsigned image.
std::shared_ptr< ImageInt32 > ImageInt32Ptr
Shared pointer to a 32-bit signed integer image.
std::shared_ptr< Image< PixelType > > ImagePtr
Shared pointer alias for an image with arbitrary pixel type.
Image< uint8_t > ImageUInt8
8-bit unsigned image container.
Image< float > ImageFloat
Single-precision floating-point image container.
std::shared_ptr< ImageFloat > ImageFloatPtr
Shared pointer to a single-precision floating-point image.
Utility functions for basic image conversion and manipulation.
static ImageUInt8Ptr createRandomColor(int *img, int numRowsOfImage, int numColumnsOfImage)
Creates a random-colour visualisation from an integer-labelled image.
static std::pair< int, int > to2D(PixelId index, int numColumns) noexcept
Converts a row-major linear index to (row, column).
static PixelId to1D(int row, int column, int numColumns) noexcept
Converts (row, column) to a row-major linear index.
Generic row-major 2D image with contiguous storage and shared ownership.
int getNumColumns() const
Returns the number of image columns.
int getSize() const
Returns the total number of pixels.
PixelType & operator[](PixelId index)
Returns mutable linear access to pixel index.
static Ptr fromRaw(PixelType *rawPtr, int rows, int columns)
Wraps row-major memory and takes ownership through delete[].
void fill(PixelType value)
Fills every pixel with value.
bool isEqual(const Ptr &other) const
Returns true when shape and pixel values match other.
Image(int rows, int columns)
Allocates an owned row-major image buffer.
static Ptr create(int rows, int columns, PixelType initValue)
Creates an owned image and fills every pixel with initValue.
Ptr clone() const
Returns a deep copy with its own owned buffer.
static Ptr fromExternal(PixelType *rawPtr, int rows, int columns)
Wraps external row-major memory without taking ownership.
PixelType * rawData()
Returns a mutable pointer to the contiguous row-major buffer.
PixelType Type
Pixel scalar type stored by this image.
static Ptr create(int rows, int columns)
Creates an owned image with uninitialised pixel values.
std::shared_ptr< PixelType[]> rawDataPtr()
Returns the shared pointer that owns or wraps the raw buffer.
const PixelType & operator[](PixelId index) const
Returns immutable linear access to pixel index.
int getNumRows() const
Returns the number of image rows.