Image Utilities (IU)
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Groups Pages
image_allocator_cpu.h
1 #pragma once
2 
3 #include <cstring>
4 #include <math.h>
5 #include "coredefs.h"
6 
8 namespace iuprivate {
9 
10 //--------------------------------------------------------------------------
11 template <typename PixelType>
13 {
14 public:
15  static PixelType* alloc(unsigned int width, unsigned int height, size_t *pitch)
16  {
17  if ((width == 0) || (height == 0)) throw IuException("width or height is 0", __FILE__,__FUNCTION__, __LINE__);
18 
19  // manually pitch the memory to 32-byte alignment (for better support of eg. IPP functions)
20  *pitch = width * sizeof(PixelType);
21 
22  unsigned int elements_to_pitch = (32-(*pitch % 32))/sizeof(PixelType);
23 
24  // n*32 % 32 = 0 -> elements_to_pitch according to above formula would be (unnecessarily) 32 in that case
25  // alternative formula: elements_to_pitch = ( 31 - ( ((*pitch) - 1) % 32) ) / sizeof(PixelType);
26  if(*pitch % 32 == 0)
27  elements_to_pitch = 0;
28 
29  width += elements_to_pitch;
30  PixelType *buffer = new PixelType[width * height];
31  *pitch = width * sizeof(PixelType);
32  return buffer;
33  }
34 
35  static void free(PixelType *buffer)
36  {
37  delete[] buffer;
38  }
39 
40  static void copy(const PixelType *src, size_t src_pitch,
41  PixelType *dst, size_t dst_pitch, iu::Size<2> size)
42  {
43  size_t src_stride = src_pitch/sizeof(PixelType);
44  size_t dst_stride = dst_pitch/sizeof(PixelType);
45 
46  for(unsigned int y=0; y< size.height; ++y)
47  {
48  for(unsigned int x=0; x<size.width; ++x)
49  {
50  dst[y*dst_stride+x] = src[y*src_stride+x];
51  }
52  }
53  }
54 };
55 
56 } // namespace iuprivate
57 
Definition: image_allocator_cpu.h:12
unsigned int & height
Definition: vector.h:531
Exceptions with additional error information.
Definition: coredefs.h:32
unsigned int & width
Definition: vector.h:529
Template specialization for 2-d unsigned int vectors (size vectors).
Definition: vector.h:525