X-Boost  2.3.8
Image.h
Go to the documentation of this file.
1 /* XBoost: Ada-Boost and Friends on Haar/ICF/HOG Features, Library and ToolBox
2  *
3  * Copyright (c) 2008-2014 Paolo Medici <medici@ce.unipr.it>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 #ifndef _IMAGE_H
22 #define _IMAGE_H
23 
24 
25 #include "Types.h"
26 #include <algorithm> // swap
27 #include <string.h>
28 
33 struct ImageHandle {
37  unsigned int bpp;
39  unsigned int width, height;
41  long stride;
43  unsigned char *data;
44 
45 public:
47  ImageHandle crop(int x0, int y0, int x1, int y1) const {
48  ImageHandle out;
49 
50  out.bpp = bpp;
51  out.stride = stride;
52  out.width = x1 - x0;
53  out.height = y1 - y0;
54  out.data = data + x0 * bpp + y0 * stride;
55 
56  return out;
57  }
58 
60  ImageHandle crop(const rect & roi) const {
61  ImageHandle out;
62 
63  out.bpp = bpp;
64  out.stride = stride;
65  out.width = roi.x1 - roi.x0;
66  out.height = roi.y1 - roi.y0;
67  out.data = data + roi.x0 * bpp + roi.y0 * stride;
68 
69  return out;
70  }
71 };
72 
74 struct Image: public ImageHandle {
75 
76 private:
77 
78  // uncopiable
79  Image(const Image & src) { }
80 
81 public:
82 
83  Image(int w, int h, int b) { data=0; alloc(w,h,b); }
84  Image() { data = 0 ; }
85  ~Image() { delete [] data; }
86 
88  unsigned char *delegate() {
89  unsigned char *ptr = data;
90  data =0;
91  return ptr;
92  }
93 
95  void release() { delete [] data; data = 0; }
96 
98  void alloc(unsigned int w, unsigned int h, unsigned int b) { release(); width = w; height = h; bpp = b; stride = width * bpp; data = new unsigned char [width * height * bpp]; }
99 
101  void alloc_as(const ImageHandle & src) { alloc(src.width, src.height, src.bpp); }
102 
104  void clone(const ImageHandle & src)
105  {
106  alloc_as(src);
107  for(unsigned int j=0;j<src.height;++j)
108  memcpy(data + j * stride, src.data + j * src.stride, src.width * src.bpp);
109  }
110 
111 };
112 
113 namespace std {
114 
116 inline void swap(::Image &a, ::Image &b)
117 {
118  std::swap(a.bpp, b.bpp);
119  std::swap(a.width, b.width);
120  std::swap(a.height, b.height);
121  std::swap(a.stride, b.stride);
122  std::swap(a.data, b.data);
123 }
124 }
125 
126 #endif
void clone(const ImageHandle &src)
clone an image
Definition: Image.h:104
unsigned char * delegate()
method to delegate the destroy of the data to a third player.
Definition: Image.h:88
void alloc_as(const ImageHandle &src)
allocate this image with the same geometry with a source image
Definition: Image.h:101
Types involved in boosting.
ImageHandle crop(const rect &roi) const
return a sub part of the image (without copy)
Definition: Image.h:60
unsigned int bpp
byte per pixel (1 for grayscale image, 2 for 16bit greyscale image, 3 for RGB/YUV/etc color image) ...
Definition: Image.h:37
Definition: Image.h:35
a structure to hold image data (memory)
Definition: Image.h:74
ImageHandle crop(int x0, int y0, int x1, int y1) const
return a subpart of the image (without copy)
Definition: Image.h:47
void release()
release manully the memory
Definition: Image.h:95
long stride
line stride, the delta offset, in bytes, between two different scanline
Definition: Image.h:41
unsigned int width
image geometry
Definition: Image.h:39
a rectangle structure
Definition: Types.h:55
unsigned char * data
initial address of the first pixel. It must be cast to correct format (uint8, uint16, rgb, etc etc)
Definition: Image.h:43
void alloc(unsigned int w, unsigned int h, unsigned int b)
reserve memory for the image
Definition: Image.h:98