17 #include <cvd/exceptions.h> 18 #include <cvd/image_ref.h> 21 #include <type_traits> 37 using CVD::Exceptions::All::All;
45 :
All(
"Incompatible image sizes in " +
function) {};
53 :
All(
"Input ImageRefs not in image in " +
function) {};
58 #ifndef DOXYGEN_IGNORE_INTERNAL 66 [[noreturn]]
void error_abort(
const char* f,
int l,
const char* code);
71 #define CVD_ASSERT(X) \ 75 CVD::Internal::error_abort(__FILE__, __LINE__, #X); \ 77 #elif defined(_MSC_VER) 78 #define CVD_ASSERT(X) __assume(X) 79 #elif defined(__GNUC__) || defined(__clang__) 80 #define CVD_ASSERT(X) \ 84 __builtin_unreachable(); \ 87 #define CVD_ASSERT(X) \ 93 #ifdef CVD_IMAGE_DEBUG 94 #define CVD_IMAGE_ASSERT(X, Y) \ 97 #define CVD_IMAGE_ASSERT2(X, Y, Z) \ 101 #define CVD_IMAGE_ASSERT(X, Y) 102 #define CVD_IMAGE_ASSERT2(X, Y, Z) 124 inline void memfill(T* data,
int n,
const T val)
127 for(; data < de; data++)
132 inline void memfill(
unsigned char* data,
int n,
const unsigned char val)
134 memset(data, val, n);
138 inline void memfill(
signed char* data,
int n,
const signed char val)
140 memset(data, val, n);
144 inline void memfill(
char* data,
int n,
const char val)
146 memset(data, val, n);
155 static const C&
get();
163 template <
unsigned long S>
169 static One& detect_dummy(...);
172 static typename SFINAE_dummy<sizeof(typename S::dummy)>::Type& detect_dummy(
const S&);
174 static const bool Is = (
sizeof(detect_dummy(get<C>())) == 2);
183 typedef std::forward_iterator_tag iterator_category;
184 typedef T value_type;
185 typedef std::ptrdiff_t difference_type;
187 typedef T& reference;
194 ptr += row_increment;
195 row_end += total_width;
208 T* operator->()
const {
return ptr; }
209 T& operator*()
const {
return *ptr; }
215 if(is_end && s.is_end)
218 return s.end != NULL;
227 return !((*this) != s);
232 if(is_end && s.is_end)
235 return s.end != NULL;
248 , row_end(start + image_width)
251 , row_increment(row_stride - image_width)
252 , total_width(row_stride)
269 int row_increment, total_width;
300 template <class T, bool D = Internal::IsDummy<T>::Is>
317 , data_length(T::bytes_per_pixel::num * sz.
area() / T::bytes_per_pixel::den)
339 using PointerType =
void*;
340 using ConstPointerType =
const void*;
376 return ir.
x >= 0 && ir.
y >= 0 && ir.
x < my_size.x && ir.
y < my_size.y;
384 return ir.
x >= border && ir.
y >= border && ir.
x < my_size.x - border && ir.
y < my_size.y - border;
393 return (my_data[pos.
y * my_stride + pos.
x]);
402 return (my_data[pos.
y * my_stride + pos.
x]);
412 return my_data + row * my_stride;
422 return my_data + row * my_stride;
428 int diff = ptr - my_data;
429 return ImageRef(diff % my_stride, diff / my_size.x);
456 inline const_iterator
end()
const 490 T*
end_ptr() {
return my_data + my_size.y * my_stride; }
493 const T*
end_ptr()
const {
return my_data + my_size.y * my_stride; }
498 using PointerType = T*;
499 using ConstPointerType =
const T*;
522 using typename Internal::ImageData<T>::PointerType;
523 using typename Internal::ImageData<T>::ConstPointerType;
535 inline ConstPointerType
data()
const 549 for(
int y = 0; y < my_size.y; y++)
550 if constexpr(std::is_trivially_copyable_v<T>)
551 std::memcpy((*
this)[y], other[y],
sizeof(T) * my_size.x);
553 std::copy(other[y], other[y] + my_size.x, (*
this)[y]);
566 static_assert(std::is_trivially_copyable<T>::value,
"Error: zero() only works on POD types");
567 for(
int y = 0; y < my_size.y; y++)
568 memset((*
this)[y], 0,
sizeof(T) * my_size.x);
576 for(
int y = 0; y < my_size.y; y++)
577 Internal::memfill((*
this)[y], my_size.x, d);
606 C* ptr = my_data + start.
y * my_stride + start.
x;
626 struct CopyPlaceHolder
641 resize(other.
size());
666 move_from.erase_fields();
680 if(
this != &move_from)
684 move_from.erase_fields();
690 #ifndef DOXYGEN_IGNORE_INTERNAL 772 void resize_(
const ImageRef& size, DD<Dummy>)
785 void resize_(
const ImageRef& size, DD<true>)
795 my_data =
new char[this->datalength()];
800 void delete_(DD<Dummy>)
805 void delete_(DD<true>)
807 delete[]
static_cast<char*
>(my_data);
ImageRef ir(const TooN::Vector< 2 > &v)
Convert a Vector into an image co-ordinate.
Definition: vector_image_ref.h:24
size_t datalength() const
What is the row stride of the image?
Definition: image.h:322
Image(const SubImage< T > &i)
Copy-ish constructor: new allocation and copy the data.
Definition: image.h:648
T value_type
The data type of the pixels in the image.
Definition: image.h:436
constexpr int area() const
Area (product of x and y; signed)
Definition: image_ref.h:213
int row_stride() const
What is the row stride of the image?
Definition: image.h:463
All classes and functions are within the CVD namespace.
Definition: argb.h:6
PointerType data()
Returns the raw image data.
Definition: image.h:541
ImageRef size() const
What is the size of this image?
Definition: image.h:557
const_iterator begin() const
Returns a const iterator referencing the first (top-left) pixel in the image.
Definition: image.h:444
bool in_image_with_border(const ImageRef &ir, int border) const
Is this pixel co-ordinate inside the image, and not too close to the edges?
Definition: image.h:382
void copy(const BasicImage< S > &in, BasicImage< T > &out, ImageRef size=ImageRef(-1, -1), ImageRef begin=ImageRef(), ImageRef dst=ImageRef())
Generic image copy function for copying sub rectangles of images into other images.
Definition: utility.h:30
Image()
Default constructor: everything set to zero.
Definition: image.h:707
void resize(const ImageRef &size)
Resize the image (destroying the data).
Definition: image.h:731
An attempt was made to access a pixel outside the image.
Definition: image.h:116
~Image()
The destructor removes the image data.
Definition: image.h:747
Image(Image &&move_from)
Move constructor: steal the pointer.
Definition: image.h:663
void zero()
Set image data to all zero bytes.
Definition: image.h:564
Input ImageRef not within image dimensions.
Definition: image.h:50
virtual ~BasicImage()
The image data is not destroyed when a BasicImage is destroyed.
Definition: image.h:530
ImageData(T *data, const ImageRef &size, int stride)
Construct an image from a block of data.
Definition: image.h:361
ConstPointerType data() const
Returns the raw image data.
Definition: image.h:535
const T & operator[](const ImageRef &pos) const
Access a pixel from the image.
Definition: image.h:399
int x
The x co-ordinate.
Definition: image_ref.h:172
Base class for all CVD exceptions.
Definition: exceptions.h:15
Definition: image_ref.h:29
Image & operator=(const Image &i)
Copy the data.
Definition: image.h:671
ImageRef pos(const T *ptr) const
Given a pointer, this returns the image position as an ImageRef.
Definition: image.h:426
void resize(const ImageRef &size, const T &val)
Resize the image (destroying the data).
Definition: image.h:740
void fill(const T d)
Set all the pixels in the image to a value.
Definition: image.h:574
iterator begin()
Returns an iterator referencing the first (top-left) pixel in the image.
Definition: image.h:439
A generic image class to manage a block of arbitrarily padded data as an image.
Definition: image.h:273
ImageData(T *data, const ImageRef &size)
Construct an image from a block of data, assuming tight packing.
Definition: image.h:350
Image(const ImageRef &size, const T &val)
Create a filled image of a given size.
Definition: image.h:723
bool in_image(const ImageRef &ir) const
Is this pixel co-ordinate inside the image?
Definition: image.h:374
int y
The y co-ordinate.
Definition: image_ref.h:173
A full image which manages its own data.
Definition: image.h:623
Input images have incompatible dimensions.
Definition: image.h:42
const T * end_ptr() const
Return an off-the-end pointer without ever throwing AccessOutsideImage.
Definition: image.h:493
Image(const ImageRef &size)
Create an empty image of a given size.
Definition: image.h:715
const T * operator[](int row) const
Access pointer to pixel row.
Definition: image.h:419
T * operator[](int row)
Access pointer to pixel row.
Definition: image.h:409
Base class for all Image_IO exceptions.
Definition: image.h:35
T value_type
The data type of the pixels in the image.
Definition: image.h:637
Image(const Image &i)
Copy constructor: new allocation and copy the data.
Definition: image.h:656
T & operator[](const ImageRef &pos)
Access a pixel from the image.
Definition: image.h:390
T * end_ptr()
Return an off-the-end pointer without ever throwing AccessOutsideImage.
Definition: image.h:490
const_iterator end() const
Returns a const iterator pointing to one past the end of the image.
Definition: image.h:456
iterator end()
Returns an iterator pointing to one past the end of the image.
Definition: image.h:450