36template <
typename PixelType>
41 std::shared_ptr<PixelType[]> data;
42 using Ptr = std::shared_ptr<Image<PixelType>>;
44 static std::size_t checkedSize(
int rows,
int cols) {
46 throw std::invalid_argument(
"Image dimensions must be positive.");
50 if (
rowCount >
static_cast<std::size_t
>(std::numeric_limits<int>::max()) /
colCount) {
51 throw std::overflow_error(
"Image dimensions exceed the supported int-indexed size.");
80 return std::make_shared<Image>(
rows,
cols);
114 throw std::invalid_argument(
"Image::fromExternal requires a non-null raw pointer.");
135 throw std::invalid_argument(
"Image::fromRaw requires a non-null raw pointer.");
149 std::fill_n(data.get(), numRows * numCols,
value);
161 if (numRows !=
other->numRows || numCols !=
other->numCols)
163 int n = numRows * numCols;
164 for (
int i = 0;
i < n; ++
i) {
180 std::copy(data.get(), data.get() + (numRows * numCols),
newImg->data.get());
214 int getSize()
const {
return numRows * numCols; }
252template <
typename PixelType>
272 inline static int to1D(
int row,
int col,
int numCols)
noexcept{
273 return row * numCols + col;
283 inline static std::pair<int, int>
to2D(
int index,
int numCols)
noexcept {
284 int row = index / numCols;
285 int col = index - row * numCols;
310 std::unique_ptr<int[]>
r(
new int[
max + 1]);
311 std::unique_ptr<int[]>
g(
new int[
max + 1]);
312 std::unique_ptr<int[]>
b(
new int[
max + 1]);
316 for (
int i = 1;
i <=
max;
i++){
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 std::pair< int, int > to2D(int index, int numCols) noexcept
Converts a row-major linear index to (row, col).
static int to1D(int row, int col, int numCols) noexcept
Converts (row, col) to a row-major linear index.
static ImageUInt8Ptr createRandomColor(int *img, int numRowsOfImage, int numColsOfImage)
Creates a random-colour visualisation from an integer-labelled image.
Generic row-major 2D image with contiguous storage and shared ownership.
const PixelType & operator[](int index) const
Returns immutable linear access to pixel index.
int getSize() const
Returns the total number of pixels.
Image(int rows, int cols)
Allocates an owned row-major image buffer.
int getNumCols() const
Returns the number of image columns.
void fill(PixelType value)
Fills every pixel with value.
bool isEqual(const Ptr &other) const
Returns true when shape and pixel values match other.
static Ptr create(int rows, int cols, PixelType initValue)
Creates an owned image and fills every pixel with initValue.
PixelType & operator[](int index)
Returns mutable linear access to pixel index.
static Ptr fromRaw(PixelType *rawPtr, int rows, int cols)
Wraps row-major memory and takes ownership through delete[].
Ptr clone() const
Returns a deep copy with its own owned buffer.
PixelType * rawData()
Returns a mutable pointer to the contiguous row-major buffer.
static Ptr create(int rows, int cols)
Creates an owned image with uninitialised pixel values.
std::shared_ptr< PixelType[]> rawDataPtr()
Returns the shared pointer that owns or wraps the raw buffer.
int getNumRows() const
Returns the number of image rows.
static Ptr fromExternal(PixelType *rawPtr, int rows, int cols)
Wraps external row-major memory without taking ownership.
Owning result for one computed scalar attribute layout and buffer.