/* Copyright (C) 2006 Pedro Felzenszwalb This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* a simple image class */ #ifndef IMAGE_H #define IMAGE_H #include #include /* template class image { public: image(const int width, const int height, const bool init = true); ~image(); void init(const T &val); image *copy() const; int width() const { return w; } int height() const { return h; } T *data; T **access; private: int w, h; }; */ typedef struct{ int w,h; rgb *data; rgb **access; }image; typedef struct{ int w,h; float *data; float **access; }imagef; image *image_construct(const int width, const int height, const bool init); void image_destruct(image *self){ free(self->data); free(self->access); free(self); } void image_init(image *self,const rgb *val); image *image_copy(image *self); int image_width(image *self){return self->w;} int image_height(image *self){return self->h;} /* use imRef to access image data. */ #define imRef(im, x, y) (im->access[y][x]) /* use imPtr to get pointer to image data. */ #define imPtr(im, x, y) &(im->access[y][x]) image *image_construct(const int width, const int height, const bool init) { image *self = (image *)malloc(sizeof(image)); self->w = width; self->h = height; self->data = (rgb *)malloc(self->w * self->h * sizeof(rgb)); // allocate space for image data self->access = (rgb **)malloc(self->h* sizeof (rgb *)); // allocate space for row pointers // initialize row pointers for (int i = 0; i < self->h; i++) self->access[i] = self->data + (i * self->w); if (init) memset(self->data, 0, self->w * self->h * sizeof(rgb)); return self; } void image_init(image *self,const rgb *val){ rgb *ptr = imPtr(self,0,0); rgb *end = imPtr(self,self->w-1, self->h -1); while (ptr <=end) *ptr++ = *val; } image * image_copy(image *self){ image *im = image_construct(self->w, self->h,false); memcpy(im->data, self->data, self->w * self->h *sizeof(rgb)); return im; } //imagef imagef *imagef_construct(const int width, const int height, const bool init); void imagef_destruct(imagef *self){ free(self->data); free(self->access); free(self); } void imagef_init(imagef *self,const float *val); imagef *imagef_copy(imagef *self); int imagef_width(imagef *self){return self->w;} int imagef_height(imagef *self){return self->h;} /* use imRef to access image data. */ #define imRef(im, x, y) (im->access[y][x]) /* use imPtr to get pointer to image data. */ #define imPtr(im, x, y) &(im->access[y][x]) imagef *imagef_construct(const int width, const int height, const bool init) { imagef *self = (imagef *)malloc(sizeof(imagef)); self->w = width; self->h = height; self->data = (float *)malloc(self->w * self->h * sizeof(float)); // allocate space for image data self->access = (float **)malloc(self->h* sizeof (float *)); // allocate space for row pointers // initialize row pointers for (int i = 0; i < self->h; i++) self->access[i] = self->data + (i * self->w); if (init) memset(self->data, 0, self->w * self->h * sizeof(float)); return self; } void imagef_init(imagef *self,const float *val){ float *ptr = imPtr(self,0,0); float *end = imPtr(self,self->w-1, self->h -1); while (ptr <=end) *ptr++ = *val; } imagef * imagef_copy(image *self){ imagef *im = imagef_construct(self->w, self->h,false); memcpy(im->data, self->data, self->w * self->h *sizeof(float)); return im; } #endif