23class CommittedImageAccess;
43template <
typename PixelType>
class Image {
45 friend class detail::CommittedImageAccess;
46 struct EstablishedDimensionsTag {};
52 std::shared_ptr<PixelType[]> data;
54 using Ptr = std::shared_ptr<Image<PixelType>>;
63 static std::size_t checkedSize(
int rows,
int columns) {
65 const auto rowCount =
static_cast<std::size_t
>(rows);
66 const auto columnCount =
static_cast<std::size_t
>(columns);
68 throw std::overflow_error(
"Image dimensions exceed the supported int-indexed size."));
79 : numRows(rows), numColumns(columns),
80 data(
new PixelType[
static_cast<std::size_t
>(rows) *
static_cast<std::size_t
>(columns)], [](
PixelType* pixels) {
delete[] pixels; }) {}
94 Image(
int rows,
int columns) : numRows(rows), numColumns(columns), data(
new PixelType[checkedSize(rows, columns)], [](
PixelType* pixels) {
delete[] pixels; }) {}
105 [[
nodiscard]]
static Ptr
create(
int rows,
int columns) {
return std::make_shared<Image>(rows, columns); }
158 img->data = std::shared_ptr<PixelType[]>(
rawPtr, [](
PixelType* pixels) {
delete[] pixels; });
169 void fill(
PixelType value) { std::fill_n(data.get(), numRows * numColumns, value); }
180 if (numRows !=
other->numRows || numColumns !=
other->numColumns)
182 int n = numRows * numColumns;
183 for (
int i = 0;
i < n; ++
i) {
199 std::copy(data.get(), data.get() + (numRows * numColumns),
newImg->data.get());
243 int getSize()
const {
return numRows * numColumns; }
285template <
typename PixelType>
using ImagePtr = std::shared_ptr<Image<PixelType>>;
303 inline static PixelId to1D(
int row,
int column,
int numColumns)
noexcept {
return row * numColumns + column; }
312 inline static std::pair<int, int>
to2D(
PixelId index,
int numColumns)
noexcept {
313 int row = index / numColumns;
314 int column = index - row * numColumns;
315 return {row, column};
339 std::unique_ptr<int[]>
r(
new int[
max + 1]);
340 std::unique_ptr<int[]>
g(
new int[
max + 1]);
341 std::unique_ptr<int[]>
b(
new int[
max + 1]);
345 for (
int i = 1;
i <=
max;
i++) {
Shared scalar conventions used by the C++ API.
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.
std::shared_ptr< ImageFloat > ImageFloatPtr
Shared pointer to a single-precision floating-point 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.
std::shared_ptr< ImageUInt8 > ImageUInt8Ptr
Shared pointer to an 8-bit unsigned 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.
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.
Owning result for one computed scalar attribute layout and buffer.