source: proiecte/pmake3d/segment/image.h @ 77

Last change on this file since 77 was 77, checked in by (none), 14 years ago

Added parallelized code

  • Property svn:executable set to *
File size: 4.2 KB
Line 
1/*
2Copyright (C) 2006 Pedro Felzenszwalb
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17*/
18
19/* a simple image class */
20
21#ifndef IMAGE_H
22#define IMAGE_H
23
24#include <cstring>
25#include <misc.h>
26/*
27template <class T>
28class image {
29 public:
30 
31  image(const int width, const int height, const bool init = true);
32
33 
34  ~image();
35
36 
37  void init(const T &val);
38
39  image<T> *copy() const;
40 
41  int width() const { return w; }
42 
43  int height() const { return h; }
44 
45  T *data;
46 
47  T **access;
48 
49 private:
50  int w, h;
51};
52*/
53
54typedef struct{
55        int w,h;
56        rgb *data;
57        rgb **access;
58}image;
59
60typedef struct{
61        int w,h;
62        float *data;
63        float **access;
64}imagef;
65
66image *image_construct(const int width, const int height, const bool init);
67void image_destruct(image *self){
68        free(self->data);
69        free(self->access);
70        free(self);
71}
72void image_init(image *self,const rgb *val);
73image *image_copy(image *self);
74int image_width(image *self){return self->w;}
75int image_height(image *self){return self->h;}
76
77/* use imRef to access image data. */
78#define imRef(im, x, y) (im->access[y][x])
79 
80/* use imPtr to get pointer to image data. */
81#define imPtr(im, x, y) &(im->access[y][x])
82
83image *image_construct(const int width, const int height, const bool init) {
84  image *self = (image *)malloc(sizeof(image));
85  self->w = width;
86  self->h = height;
87  self->data = (rgb *)malloc(self->w * self->h * sizeof(rgb));  // allocate space for image data
88  self->access = (rgb **)malloc(self->h* sizeof (rgb *));   // allocate space for row pointers
89 
90  // initialize row pointers
91  for (int i = 0; i < self->h; i++)
92    self->access[i] = self->data + (i * self->w); 
93 
94  if (init)
95    memset(self->data, 0, self->w * self->h * sizeof(rgb));
96  return self;
97}
98
99void image_init(image *self,const rgb *val){
100        rgb *ptr = imPtr(self,0,0);
101        rgb *end = imPtr(self,self->w-1, self->h -1);
102        while (ptr <=end)
103           *ptr++ = *val;
104}
105
106image * image_copy(image *self){
107        image *im = image_construct(self->w, self->h,false);
108        memcpy(im->data, self->data, self->w * self->h *sizeof(rgb));
109        return im;
110}
111
112//imagef
113
114imagef *imagef_construct(const int width, const int height, const bool init);
115void imagef_destruct(imagef *self){
116        free(self->data);
117        free(self->access);
118        free(self);
119}
120void imagef_init(imagef *self,const float *val);
121imagef *imagef_copy(imagef *self);
122int imagef_width(imagef *self){return self->w;}
123int imagef_height(imagef *self){return self->h;}
124
125/* use imRef to access image data. */
126#define imRef(im, x, y) (im->access[y][x])
127
128/* use imPtr to get pointer to image data. */
129#define imPtr(im, x, y) &(im->access[y][x])
130
131imagef *imagef_construct(const int width, const int height, const bool init) {
132  imagef *self = (imagef *)malloc(sizeof(imagef));
133  self->w = width;
134  self->h = height;
135  self->data = (float *)malloc(self->w * self->h * sizeof(float));  // allocate space for image data
136  self->access = (float **)malloc(self->h* sizeof (float *));   // allocate space for row pointers
137
138  // initialize row pointers
139  for (int i = 0; i < self->h; i++)
140    self->access[i] = self->data + (i * self->w);
141 
142  if (init)
143    memset(self->data, 0, self->w * self->h * sizeof(float));
144  return self;
145}
146 
147void imagef_init(imagef *self,const float *val){
148  float *ptr = imPtr(self,0,0);
149  float *end = imPtr(self,self->w-1, self->h -1);
150  while (ptr <=end)
151    *ptr++ = *val;
152                                                 
153}
154
155imagef * imagef_copy(image *self){
156        imagef *im = imagef_construct(self->w, self->h,false);
157        memcpy(im->data, self->data, self->w * self->h *sizeof(float));
158        return im;
159}
160
161#endif
162 
Note: See TracBrowser for help on using the repository browser.