Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   Related Pages   Examples  

Image.h

00001 /* Copyright (c) 2001 C. Grigorescu S.E. Grigorescu A. Jalba */
00002 
00003 #ifndef IMAGE_H
00004 #define IMAGE_H
00005 
00006 #include <stdlib.h>
00007 #include <string.h>
00008 #include <malloc.h>
00009 #include <signal.h> //only for kill
00010 #include "imlib_io.h"
00011 #include "Exceptions.h"
00012 #include <stdio.h>
00013 
00018 template< class T > class Image 
00019 {
00023  protected: 
00025   int width;
00026 
00028   int height; 
00029 
00031   T **pixels;
00032 
00034   char img_name[256]; 
00035 
00036   int pid; //only to close a window 
00037   
00038  public:
00040   Image(char * name = "");
00041 
00043   Image(int w, int h, char *name = "");
00044 
00046   Image(const Image<T>&);
00047 
00049   virtual ~Image();
00050 
00052   int getWidth() const { return width; }
00053 
00055   int getHeight() const { return height; }
00056 
00058   int getSize() { return width*height; }
00059 
00061   char *getName() { return img_name; }
00062 
00064   void setName(const char *name);
00065 
00067   void makeImage(int w, int h);
00068 
00070   void destroyImage();
00071 
00073   T *getPixels() { return pixels[0]; }
00074 
00076   void setPixels(int w, int h, T *data);
00077 
00080   T min();
00081 
00084   T max();
00085 
00088   float avg();
00089 
00092   float dev();
00093   
00097   T *operator[](int i) {
00098     if ((i > height) || (i < 0))
00099       throw Fatal_error("Index out of range"); 
00100     else        
00101       return pixels[i];
00102   }
00103 
00107   T &operator()(int i) { 
00108     if ((i > getSize()) || (i < 0))
00109       throw Fatal_error("Index out of range"); 
00110     else        
00111       return pixels[0][i]; 
00112   }
00113 
00114 /* Addressing of the internal image buffer with two indices (i-row, j-column). */
00115 /*    T &operator()(int i, int j) {  */
00116 /*      if ((i>getHeight()) || (i < 0)) */
00117 /*        throw Fatal_error("Height index out of range");  */
00118 /*      else  */
00119 /*        if ((j>getWidth()) || (j < 0)) */
00120 /*      throw Fatal_error("Width index out of range");  */
00121 /*        else */
00122 /*      return pixels[i][j];  */
00123 /*    } */
00124 
00128   Image< T > &operator=(const Image< T > &img); 
00129 
00133   int operator==(const Image< T > &img);
00134 
00138   int operator!=(const Image< T > &img);
00139 
00142   Image< T > &operator=(const T val);
00143 
00147   void clearImage(const T val);
00148 
00152   Image< T > &operator+=(const T val);
00153 
00157   Image< T > &operator-=(const T val);
00158 
00162   Image< T > &operator*=(const T val);
00163 
00167   Image< T > &operator+=(const Image< T > & img);
00168 
00172   Image< T > &operator-=(const Image< T > & img);
00173 
00177   Image< T > &operator*=(const Image< T > & img);
00178   
00185   void pad(int w, int h, PADDING_TYPE p);
00186 
00189   void crop(int x1, int y1, int x2, int y2);
00190 
00193   void makeEvenSized(void);
00194   
00197   void closeWindow();
00198 
00201   void readImage(char *name) { 
00202     T *d = read_image< T >(name, &width, &height);
00203     setPixels(width, height, d); 
00204     setName(name);    
00205   }
00206 
00209   void writeImage()
00210   { 
00211     if (width * height != 0) 
00212       write_image(img_name, width, height, pixels[0]); 
00213     else
00214       ERROR("Empty image; nothing to write");
00215   }
00216 
00219   void writeImage(char *name)
00220   { 
00221     setName(name);
00222     if (width * height != 0) 
00223       write_image(img_name, width, height, pixels[0]); 
00224     else
00225       ERROR("Empty image; nothing to write");
00226   }
00227 
00230   int showImage()  { 
00231     if (width * height != 0) {
00232       char name[128];
00233       strncpy(name, img_name, 110);
00234       closeWindow();  
00235       pid = show_image< T > (pixels[0], width, height, name);     
00236     }
00237     else {
00238       pid = 0;
00239       ERROR("Empty image; cannot display it!");
00240     }    
00241     return(pid);
00242   }
00243 
00244   // Bellow are some functions intended to be used BUT
00245   // not used currently for various reasons:
00246 
00247   // "+","-", "*" operators: as friends functions 
00248   //  friend Image< T > operator+< T >(const Image< T >& img1, const Image< T >& img2);
00249   //  friend Image< T > operator*< T >(const Image< T >& img1, const Image< T >& img2);
00250   //  friend Image< T > operator-< T >(const Image< T >& img1, const Image< T >& img2);
00251   // - not used because they allocate memory without freeing it - a garbage
00252   //   collection mechanism would be necessary (see Stroustroup p. 282)
00253 
00254   //  "+", "-", "*" operators as members of the class 
00255   //  Image< T > &operator+(const Image< T > &img);
00256   //  Image< T > &operator-(const Image< T > &img);
00257   //  Image< T > &operator*(const Image< T > &img);
00258   // - not used because they allocate memory without freeing it - a garbage
00259   //   collection mechanism would be necessary (see Stroustroup p. 282)
00260 };
00261 
00262 /*  template< class T > Image<T> operator+(const Image<T>&, const Image<T>&); */
00263 /*  template< class T > Image<T> operator-(const Image<T>&, const Image<T>&); */
00264 /*  template< class T > Image<T> operator*(const Image<T>&, const Image<T>&); */
00265 
00266 #endif
00267