#ifndef __IMAGE3D_H__
#define __IMAGE3D_H__

#include <math.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <vector>
#include "image2d.h"
#include <pthread.h>

#ifndef _NOMAGICK_
#include <Magick++.h>
#endif

struct filterjob
{
  double *data;
  double *dst;
  int h;
  int w;
  int d;
  int s;
  int sd;
  int ed;
};

class image3d
{
 public:
  image3d();
  ~image3d();
  int del();
  int create( int h, int w, int d );

  /// load a slice to an image
  int loadd( image2d &im, int d );

  double getpixel( int h, int w, int d);
  int setpixel( int h, int w, int d, double v );

  /// get a set of pixels
  int getpixels( int i, int j, int k, 
                 int s, double *v );

  int normalizeminmax();

  int gamma( double g );

  int medianfilter( int s );

  int getwidth()
  {
    return depth;
  }
  int getheight()
  {
    return height;
  }
  int getdepth()
  {
    return depth;
  }

#ifndef _NOMAGICK_
  int write( std::string base );
#endif

 private:

  int calcoffset( int h, int w, int d )
  {
    if( h < 0 || h >= height ||
        w < 0 || w >= width ||
        d < 0 || d >= depth )
      {
        std::cout << "offset error\n";
        return -1;
      }
    return d*height*width+h*width+w;
  }

  int calcpmf(double *hist, int sz );

#ifndef _NOMAGICK_
  int getslice( Magick::Image &im, int s);
#endif

  double *data;
  int height;
  int width;
  int depth;
};

#endif
