diff --git a/stim/image/image.h b/stim/image/image.h index 28ce502..4a324b2 100644 --- a/stim/image/image.h +++ b/stim/image/image.h @@ -24,6 +24,8 @@ class image{ size_t Y() const { return R[2]; } size_t C() const { return R[0]; } + size_t bytes(){ return size() * sizeof(T); } + void init(){ //initializes all variables, assumes no memory is allocated memset(R, 0, sizeof(size_t) * 3); //set the resolution and number of channels to zero img = NULL; @@ -31,6 +33,8 @@ class image{ void unalloc(){ //frees any resources associated with the image if(img) free(img); //if memory has been allocated, free it + img=NULL; + } @@ -40,15 +44,17 @@ class image{ } void allocate(){ - img = (T*) malloc( sizeof(T) * R[0] * R[1] * R[2] ); //allocate memory + unalloc(); + img = (T*) malloc( bytes() ); //allocate memory + memset(img, 0, bytes()); } void allocate(size_t x, size_t y, size_t c){ //allocate memory based on the resolution + unalloc(); R[0] = c; R[1] = x; R[2] = y; //set the resolution allocate(); //allocate memory } - size_t bytes(){ return size() * sizeof(T); } size_t idx(size_t x, size_t y, size_t c = 0){ return y * C() * X() + x * C() + c; @@ -89,36 +95,33 @@ public: image(){ init(); } //initialize all variables to zero, don't allocate any memory /// Constructor with a filename - loads the specified file - image(std::string filename){ //constructor initialize the image with an image file + image(std::string filename) : image(){ //constructor initialize the image with an image file load(filename); } /// Create a new image from scratch given a number of samples and channels - image(size_t x, size_t y = 1, size_t c = 1){ + image(size_t x, size_t y = 1, size_t c = 1) : image(){ + init(); allocate(x, y, c); } /// Create a new image with the data given in 'data' - image(T* data, size_t x, size_t y, size_t c = 1){ + image(T* data, size_t x, size_t y, size_t c = 1) : image(){ + init(); allocate(x, y, c); memcpy(img, data, bytes()); } /// Copy constructor - duplicates an image object - image(const stim::image& I){ + image(const stim::image& I) : image(){ + init(); allocate(I.X(), I.Y(), I.C()); - //allocate(I.R[1], I.R[2], I.R[0]); memcpy(img, I.img, bytes()); } /// Destructor - clear memory -// ~image(){ -// free(img); -// } - - void clear_exp(){ //clears all image data - unalloc(); - + ~image(){ + free(img); } stim::image operator=(const stim::image& I){ @@ -142,24 +145,14 @@ public: if(C() == 1) { //if this is a single-color image, just copy the data -/* - THIS DOES NOT WORK PROPERLY. - YOU CANNOT DO MEMCPY LIKE THIS BECAUSE IT CORRUPTS THE POINTREF MECHANIC IN OPENCV AND NEVER ACTUALLY CREATES A DEEP COPY BUT TAKES UP THE MEMORY. - TO TEST run and compile pseudocode - stim::image I; - for(int i = 0; i < file_list.size(); i++) - I.load(file_list[i].srt()) - - - You will run out of memory. -*/ memcpy(img, cv_ptr, bytes()); -// img = cvImage.data; } - if(C() == 3) //if this is a 3-color image, OpenCV uses BGR interleaving + if(C() == 3) + { //if this is a 3-color image, OpenCV uses BGR interleaving set_interleaved_bgr(cv_ptr, X(), Y()); + } -// cvImage.deallocate(); + cvImage.release(); } //save a file @@ -352,6 +345,17 @@ public: return r; //return the inverted image } + + /// Invert an image by calculating I1 = alpha - I0, where alpha is the maximum image value + image invert(){ + size_t N = size(); //calculate the total number of values in the image + image r(X(), Y(), C()); //allocate space for the resulting image + T white_val = maxv(); + for(size_t n = 0; n < N; n++) + r.img[n] = white_val - img[n]; //perform the inversion + + return r; //return the inverted image + } ///crops the image from x1 to x0 and y1 to y0 and returns a new (smaller) image. ///Untested @@ -361,12 +365,12 @@ public: image ret(x1-x0, y1-y0, C()); int newWidth = x1-x0; int destidx, srcidx; -// for(int i = 0; i < (y1-y0); i++) + for(int i = 0; i < (y1-y0); i++) { destidx = i*newWidth*C(); ///destination index one per each row srcidx = ((i+(y0))*X()+x0)*C(); ///source index, one per each row. - ret.set_interleaved_rgb(img[srcidx], newWidth, y1-y0, C()); - memcpy(&ret.img[destidx], &buffer[srcidx], sizeof(T)*newWidth*C()); +// ret.set_interleaved_rgb(img[srcidx], newWidth, y1-y0, C()); + memcpy(&ret.img[destidx], &img[srcidx], sizeof(T)*newWidth*C()); } // for(int i = 0; i < (x1 -x0); i++) -- libgit2 0.21.4