www.pudn.com > firev0.01.rar > image.hpp


/*
This file is part of the FIRE -- Flexible Image Retrieval System

FIRE 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.

FIRE 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 FIRE; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
/** Classes to handle images.
 *  @author Thomas Deselaers 
 *
 */

#ifndef __image_h
#define __image_h

#include 

extern "C" {
#include 
#include 
#include "pngfile.h"
// #include 
// #include 
// #include 
// #include 
// #include 
}
#include 
#include 
#include "diag.hpp"
#include "colorpixel.hpp"
#include "basetools.hpp"
#include "basefeature.hpp"
#include "genericfeature.hpp"

using namespace std;
namespace img {
  
  ///class describing a quite general image.
  template  
  class BaseImage {

    //if i inherit this from base feature, rgbimage won't compile
    //because of conflicting types for operator[], because rgbimage is
    //inherited from BaseImage > and thus would have to
    //provide T operator[]() and Colorpixel operator[]()
    
  public:
    
    /// default constructor.
    BaseImage() {
      cls_=0;
    };
    /// standard constructor. Also allocates memory.
    BaseImage(const int yDim,const int xDim) {
      yDim_=yDim;
      xDim_=xDim;
      size_=xDim*yDim;
      cls_=0;
      data_=vector(size_,pixeltype());
    };
    /// copy constructor.
    BaseImage(const BaseImage &img) {
      yDim_=img.yDim_;
      xDim_=img.xDim_;
      size_=img.size_;
      cls_=img.cls_;
      data_=img.data_;
    };

    ///return generic feature for easier use in retriever of direct image data
    
    const GenericFeature genericFeature() const {
      GenericFeature f(this->size());
      for(unsigned int i=0;isize();++i) {
        f[i]=this->data_[i];
      }
      return f;
    }

	
    ///get the class of the image, const version.
    const int cls() const {return cls_;};
    ///get the class of the image.
    int & cls() {return cls_;};
	
    /// get the y dimension of the image (height).
    const unsigned int yDim() const {return yDim_;};
    /// get the x dimension of the image (width).
    const unsigned int xDim() const {return xDim_;};
    /// get the complete size of the image (height*width).
    const unsigned int size() const {return size_;};
    /// get the dimensionality of every pixel
    const unsigned int pixelDim() const {return 1;};
	
    ///get the values one by one from the image, const version.
    inline const pixeltype operator[](const unsigned int i) const { return data_[i];};
    ///get the values one by one from the image.
    inline pixeltype& operator[](const unsigned int i) {return data_[i];};
	
    ///get the values from the image by coordinate (y,x), const version.
    inline const pixeltype operator()(const int y, const int x) const {
      return data_[(y*xDim_)+x];
    };
    

    void load(const string& filename) {
      if(filename.rfind(".jpg") == filename.length()-4) {
        this->readJPG(filename.c_str());
      } else if(filename.rfind(".JPG") == filename.length()-4) {
        this->readJPG(filename.c_str());
      } else if( filename.rfind(".png") ==filename.length()-4) {
        this->readPNG(filename.c_str());
      } else {
        DBG(DBG_ERROR) << "Unable to open file '"<< filename <<"'. Unable to determine filetype." << endl;
      }
    }


    const BaseImage& convolution(vector< vector > filtermatrix) {
      int height=filtermatrix.size();
      int width=filtermatrix[0].size();
      
      int height2=height/2;
      int width2=width/2;
      
      vector copy(xDim_*yDim_,0);
      double tmp;
      
      //double min=999999, max=0;
      
      for(unsigned int y=0;y=0 && xx>=0 && xxoperator()(yy,xx);
              }
            }
          }
          copy[(y*xDim_)+x]=tmp;
        }
      }
      
      for(unsigned int y=0;y(size_);
	DBG(DBG_TALKATIVE) << "Having all the jpeg information, now copying" << endl;
	
	buffer = (*decomp.mem->alloc_sarray)((j_common_ptr) &decomp, JPOOL_IMAGE, row_stride, 1);
	if(buffer==NULL) {
	  ERR << "Fehler!!" << endl;
	}
	int j;
	DBG(DBG_TALKATIVE) <<"averaging " << decomp.output_components << " components per pixel" << endl;
	while(decomp.output_scanline < decomp.output_height) {
	  j=decomp.output_scanline;
	  (void) jpeg_read_scanlines(&decomp,buffer,1);
	  for(unsigned int i=0;i "
				    <<(int)buffer[0][i*decomp.output_components+c]
				    <(xDim_*yDim_);
      
      read_png(filename,rowPointers,&width,&height);
      DBG(DBG_TALKATIVE) << "have information about image now" << endl;
      if(colorType==PNG_COLOR_TYPE_GRAY)  {
	DBG(DBG_VERBOSE) << "Is GRAY image" << endl;
	for(unsigned int j=0;j> x_return;
      
//       XGetGeometry(dis,win,&root_return,&x_return, &y_return, &width_return, &height_return, &border_width_return, &depth_return);
      
//       for(unsigned int y=0;yxDim();
      int height=this->yDim();
      
      png_byte toWrite[height*width];
      for(int y=0;yoperator()(y,x));
        }
      }
      
      write_png(filename, toWrite, this->xDim(), this->yDim(), 8, 1);
      
      DBG(DBG_VERBOSE) << "ready writing png '" << filename << "'." << endl;
    }
        
    BaseImage& normalize() {
      pixeltype min=*min_element(data_.begin(),data_.end());
      pixeltype max=*max_element(data_.begin(),data_.end());
      
      //      cout << int(min) << " " << int(max) << endl;
      
      for(unsigned int i=0;isize();++i) {
        data_[i]=pixeltype((double((data_[i])-min))*double(255)/double(max-min));
      }
      
      return *this;
    }
    
    BaseImage & gammaCorrection(double gamma) {
      for(unsigned int i=0;isize();++i) {
        data_[i]=pixeltype(255.0*exp(gamma*log(double(data_[i])/255.0)));
      }
      return *this;
    }
    
  protected:
    vector data_;
    unsigned int xDim_, yDim_, size_;
    int cls_;
  };
  

  /**
   * Print the image to a stream.
   *
   */
  
  template
  inline ostream& operator<<(ostream& os, const BaseImage & src) {
    os << "xDim: " << src.xDim() << "   yDim: " << src.yDim() << "   cls: " << src.cls() << endl;
    for(unsigned int y=0;y