/*
 * class Histogram
 *
 * $Date$
 * $Revision$
 *
 */

#ifndef HISTOGRAM_HH__
#define HISTOGRAM_HH__

#include "WTools/ImageInformation.hh"

/** 
 * @addtogroup WTools 
 * @{ */

/**
 * A class for histograms.
 */
class 
Histogram 
{
public:
  /** Constructor. Creates the histogram from an Image object.
   * @exception invalid_argument
   * a zero value for {\em dlt} was specified
   * @param img
   * the image to look at
   * @param dlt
   * the quantization step (necessary for histograms in the DWT domain */
  Histogram (Image &img, double dlt = 1.0);
  /** Constructor. Creates the histogram from an ImageInformation object.
   * @exception invalid_argument
   * a zero value for {\em dlt} was specified
   * @param ii
   * the image data to look at (will be duplicated)
   * @param dlt
   * the quantization step (necessary for histograms in the DWT domain */
  Histogram (ImageInformation &ii, double dlt = 1.0);
  /** Destructor. Releases allocated memory */
  ~Histogram (void);
  /** Get a value via an index.
   * @exception invalid_argument
   * the index {\em pos} is out of bounds.
   * @param pos
   * The index position.
   * @param lower
   * The lower greyscale boundary (returned value).
   * @return the value */
  int at (int pos, coeff &lower);
  /** Get a value via a greyscale value.
   * @param value
   * the greyscale value. 
   * @return the value */
  int at (coeff value);
  /** Return the number of numbers stored.
   * @return the number of numbers. */
  inline int size (void) { return m_nvals; }
  /** Get the histogram's delta.
   * @return the delta */
  inline double delta (void) { return m_dlt; } 
  /** Update the Histogram. 
   * This may be necessary after changes to the image. */
  void update (void);
  /** Print the histogram to stdout. */
  void print (void);
  /** Add an occurrence number for a given value.
   * @exception invalid_argument
   * adding number would make the result negative
   * @param value
   * the value
   * @param number
   * the number to be added */
  void add (coeff value, int number);
protected:
  /** A pointer as a reference to the image. */
  Image *m_image;
  /** A pointer as a reference to the ImageInformation object (needs
   * to be stored in order to be able to update the histogram after 
   * changes to the original image). */
  ImageInformation *m_data;
  /** A helper object for getting the image's coefficients sorted. */
  ImageInformation *m_ii;
  /** The delta between two histogram slots. */
  double m_dlt;
  /** An internal structure for the histogram */
  struct hist {
    /** The lower bound (value > lower) */
    coeff lower;
    /** The upper bound (value >= lower) */
    coeff upper;
    /** The number of occurrences */
    int number;
  };
  /** The internal histogram (an array of hist structs). */
  struct hist *m_values;
  /** The number of histogram slots. */
  int m_nvals;
}; /* class Histogram */

/** @} */

#endif /* HISTOGRAM_HH__ */

