LArOpenCV  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
Image2D.cxx
Go to the documentation of this file.
1 #include "Image2D.h"
2 #include "larbys.h"
3 #include <iostream>
4 #include <string.h>
5 #include <stdio.h>
6 namespace larcaffe {
7 
8  Image2D::Image2D(size_t width_npixel, size_t height_npixel )
9  : std::vector<float>(height_npixel*width_npixel,0.)
10  , _meta(0.,0.,width_npixel,height_npixel,0.,0.)
11  {}
12 
14  : std::vector<float>(meta.num_pixel_row()*meta.num_pixel_column(),0.)
15  , _meta(meta)
16  {}
17 
19  : std::vector<float>(rhs)
20  , _meta(rhs._meta)
21  {}
22 
23  void Image2D::resize(size_t width_npixel, size_t height_npixel )
24  {
25  std::vector<float>::resize(height_npixel*width_npixel);
26  _meta.update(width_npixel,height_npixel);
27  }
28 
29  void Image2D::clear() {
30  std::vector<float>::clear();
31  _meta.update(1,1);
32  //std::cout << "[Image2D (" << this << ")] Cleared image memory " << std::endl;
33  }
34 
35  void Image2D::clear_data() { for(auto& v : (*this)) v = 0.; }
36 
37  void Image2D::set_pixel( size_t w, size_t h, float value ) {
38  if ( h >= _meta._height_npixel || w >= _meta._width_npixel ) throw larbys("Out-of-bound pixel set request!");
39  return;
40  (*this)[w*_meta._height_npixel + h] = value;
41  }
42 
43  void Image2D::paint(float value)
44  { for(auto& v : (*this)) v=value; }
45 
46  float Image2D::pixel( size_t w, size_t h ) const
47  { return (*this)[index(h,w)]; }
48 
49  size_t Image2D::index( size_t w, size_t h ) const {
50 
51  if ( !isInBounds( h, w ) ) throw larbys("Invalid pixel index queried");
52 
53  return ( w * _meta._height_npixel + h );
54  }
55 
56  void Image2D::copy(size_t w, size_t h, const float* src, size_t num_pixel)
57  {
58  const size_t idx = index(h,w);
59  if(idx+num_pixel >= size()) throw larbys("memcpy size exceeds allocated memory!");
60 
61  memcpy(&((*this)[idx]),src, num_pixel * sizeof(float));
62 
63  }
64 
65  void Image2D::copy(size_t w, size_t h, const std::vector<float>& src, size_t num_pixel)
66  {
67  if(!num_pixel)
68  this->copy(h,w,(float*)(&(src[0])),src.size());
69  else if(num_pixel < src.size())
70  this->copy(h,w,(float*)&src[0],num_pixel);
71  else
72  throw larbys("Not enough pixel in source!");
73  }
74 
75  void Image2D::copy(size_t w, size_t h, const short* src, size_t num_pixel)
76  {
77  const size_t idx = index(h,w);
78  if(idx+num_pixel >= size()) throw larbys("memcpy size exceeds allocated memory!");
79 
80  for(size_t i=0; i<num_pixel; ++i) (*this)[idx+i] = src[num_pixel];
81 
82  }
83 
84  void Image2D::copy(size_t w, size_t h, const std::vector<short>& src, size_t num_pixel)
85  {
86  if(!num_pixel)
87  this->copy(h,w,(short*)(&(src[0])),src.size());
88  else if(num_pixel < src.size())
89  this->copy(h,w,(short*)&src[0],num_pixel);
90  else
91  throw larbys("Not enough pixel in source!");
92  }
93 
94  Image2D Image2D::copy_compress(size_t width, size_t height) const
95  {
96  if(_meta._height_npixel % height || _meta._width_npixel % width)
97  throw larbys("Compression only possible if height/width are modular 0 of compression factor!");
98 
99  size_t width_factor = _meta._width_npixel / width;
100  size_t height_factor = _meta._height_npixel / height;
101  ImageMeta meta(_meta);
102  meta.update(height,width);
103  Image2D result(meta);
104 
105  for(size_t w=0; w<width; ++ w) {
106  for(size_t h=0; h<height; ++h) {
107  float value = 0;
108  //std::cout << w*height << " : " << h << std::endl;
109  for(size_t orig_w=w*width_factor; orig_w<(w+1)*width_factor; ++orig_w)
110  for(size_t orig_h=h*height_factor; orig_h<(h+1)*height_factor; ++orig_h) {
111  //std::cout << " " << (*this)[orig_w * _meta._height_npixel + orig_h] << " @ " << orig_w * _meta._height_npixel + orig_h << std::endl;
112  value += (*this)[orig_w * _meta._height_npixel + orig_h];
113  }
114  //std::cout<<std::endl;
115  result[w*height + h] = value;
116  }
117  }
118  return result;
119  }
120 
121  void Image2D::compress(size_t width, size_t height)
122  { (*this) = copy_compress(height,width); }
123 
124 }