#include <userint.h>
 
#include "CAEN_V729_CVI.h"
 
#include "HDraw.h"
 
#include "H3D.h"
 
#include "H2D.h"
 
#include "H1D.h"
 
 
 
HColorMap *colormap = NULL;
 
 
 
int hmapinit=0;
 
 
 
int InitColors(void) {
 
  if (!hmapinit) {
 
    colormap 
= malloc(sizeof(HColorMap
)); 
 
 
    colormap->numberofColors = 5;
 
    colormap
->array 
= malloc(colormap
->numberofColors
*sizeof(ColorMapEntry
)); 
 
 
    colormap->array[0].color = 0x0000ff; //BLUE
 
    colormap->array[1].color = 0x00ff00; //GREEN
 
    colormap->array[2].color = 0xffff00; //YELLOW
 
    colormap->array[3].color = 0xff8000; //ORANGE
 
    colormap->array[4].color = 0xff0000; //RED
 
 
 
    colormap->HiColor =colormap->array[colormap->numberofColors-1].color ;
 
    hmapinit=1;
 
 
 
    return 0;
 
  }
 
  return -1;
 
}
 
 
 
 
 
 
 
HColorMap *GetColorMapPtr(void) {
 
  return colormap;
 
}
 
 
 
 
 
int RangeColors( double min, double max) {
 
  int i;
 
  if (!hmapinit) InitColors();
 
 
 
//if (zerosuppress){
 
//  if (min>0) min=0;
 
//}
 
  if (colormap->numberofColors<2) return -1;
 
  for  (i=0; i<colormap->numberofColors; i++) {
 
 
 
    colormap->array[i].dataValue.valDouble=i*(max-min)/(colormap->numberofColors-1)+min;
 
    //sa02Printf("Colors %d ;%f %f %f\n",i, min,max,colormap->array[i].dataValue.valDouble );
 
 
 
  }
 
  return 0;
 
}
 
 
 
int H3DDrawSliceXY(int histogram,int slice,int panel, int control, int *plothandle) {
 
  int i=0;
 
  if (!H2DExist(histogram)) {
 
    printf("2D Histogram %d does not exist!\n",histogram
);  
    return 0;
 
  }
 
 
 
 
 
  if (H3DGetNbinsY(histogram)==1|| H3DGetNbinsX(histogram)==1) {
 
    if (H3DGetNbinsY(histogram)==1) {
 
      H1DInit(499,"projection","projection", H3DGetNbinsX(histogram), H3DGetMinX(histogram),H3DGetStepX(histogram));
 
      for (i=0; i < H3DGetNbinsX(histogram); i++ ) H1DSetBinContent(499,i,H3DGetBinContent(histogram,i,0,slice));
 
    } else {
 
      H1DInit(499,"projection","projection", H3DGetNbinsY(histogram), H3DGetMinY(histogram),H3DGetStepY(histogram));
 
      for (i=0; i < H3DGetNbinsY(histogram); i++ ) H1DSetBinContent(499,i,H3DGetBinContent(histogram,0,i,slice));
 
    }
 
    H1DDraw(499, panel, control, plothandle);
 
  } else {
 
 
 
    if (*plothandle> 0 ) DeleteGraphPlot (panel, control, *plothandle, VAL_IMMEDIATE_DRAW);
 
    RangeColors(H3DGetMin(histogram),H3DGetMax(histogram));
 
    *plothandle = PlotScaledIntensity (panel, control,
 
                                       H3DGetSliceXYData(histogram, slice),
 
                                       H3DGetNbinsX(histogram),
 
                                       H3DGetNbinsY(histogram),
 
                                       VAL_DOUBLE,
 
                                       H3DGetStepY(histogram),
 
                                       H3DGetMinY(histogram),
 
                                       H3DGetStepX(histogram),
 
                                       H3DGetMinX(histogram),
 
                                       colormap->array,
 
                                       colormap->HiColor,
 
                                       colormap->numberofColors, 1, 0);
 
  }
 
  ProcessSystemEvents ();
 
  return *plothandle;
 
 
 
}
 
 
 
 
 
 
 
 
 
 
 
int H2DDraw(int histogram,int panel, int control, int *plothandle) {
 
  int i=0;
 
  if (!H2DExist(histogram)) {
 
    printf("2D Histogram %d does not exist!\n",histogram
);  
    return 0;
 
  }
 
 
 
 
 
  if (H2DGetNbinsY(histogram)==1|| H2DGetNbinsX(histogram)==1) {
 
    if (H2DGetNbinsY(histogram)==1) {
 
      H1DInit(499,"projection","projection", H2DGetNbinsX(histogram), H2DGetMinX(histogram),H2DGetStepX(histogram));
 
      for (i=0; i < H2DGetNbinsX(histogram); i++ ) H1DSetBinContent(499,i,H2DGetBinContent(histogram,i,0));
 
    } else {
 
      H1DInit(499,"projection","projection", H2DGetNbinsY(histogram), H2DGetMinY(histogram),H2DGetStepY(histogram));
 
      for (i=0; i < H2DGetNbinsY(histogram); i++ ) H1DSetBinContent(499,i,H2DGetBinContent(histogram,0,i));
 
    }
 
    H1DDraw(499, panel, control, plothandle);
 
  } else {
 
    if (*plothandle> 0 ) DeleteGraphPlot (panel, control, *plothandle, VAL_IMMEDIATE_DRAW);
 
    double min = H2DGetMin(histogram); 
 
    double max = H2DGetMax(histogram);
 
    RangeColors(min,max);
 
    *plothandle = PlotScaledIntensity (panel, control,
 
                                       H2DGetData(histogram),
 
                                       H2DGetNbinsX(histogram),
 
                                       H2DGetNbinsY(histogram),
 
                                       VAL_DOUBLE,
 
                                       H2DGetStepY(histogram),
 
                                       H2DGetMinY(histogram),
 
                                       H2DGetStepX(histogram),
 
                                       H2DGetMinX(histogram),
 
                                       colormap->array,
 
                                       colormap->HiColor,
 
                                       colormap->numberofColors, 1, 0);
 
  }
 
  ProcessSystemEvents ();
 
  return *plothandle;
 
 
 
}
 
 
 
 
 
unsigned int hcolors[8]={VAL_RED, VAL_GREEN, VAL_BLUE, VAL_CYAN, VAL_MAGENTA, VAL_YELLOW, VAL_GRAY, VAL_BLACK};
 
 
 
int H1DDraw(int histogram,int panel, int control, int *plothandle) {
 
  if (!H1DExist(histogram)) {
 
    printf("1D Histogram %d does not exist!\n",histogram
);  
    return 0;
 
  }
 
  if (*plothandle> 0 ) DeleteGraphPlot (panel, control, *plothandle, VAL_IMMEDIATE_DRAW);
 
 
 
  *plothandle = PlotWaveform (panel, control, H1DGetData(histogram), H1DGetNbinsX(histogram), VAL_DOUBLE, 1, 0, H1DGetMinX(histogram), H1DGetStepX(histogram), VAL_FAT_LINE, VAL_SMALL_SOLID_SQUARE, VAL_SOLID, 1,
 
                                                          hcolors[histogram%8]);
 
  ProcessSystemEvents ();
 
  return *plothandle;
 
 
 
}
 
 
 
int EstimatedFinish(int panel, int slider,int txt, time_t tstart, double fraction) {
 
 
 
  time_t t, tend=tstart;
 
  if (fraction>0) tend= ((double)(t-tstart))/fraction + tstart;
 
  SetCtrlVal
(panel
, txt
, ctime(&tend
)); 
  SetCtrlVal(panel, slider, 100*fraction);
 
 
 
  return 0;
 
 
 
}