# include "data.h"

int tv (int argc, char **argv) {
  
  int N, Ximage, Nimage;
  Coords coords;
  Buffer *buf;
  KiiImage image;
  KiiDisplayMode mode;

  Nimage = -1;
  if ((N = get_argument (argc, argv, "-n"))) {
    remove_argument (N, &argc, argv);
    Nimage = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }
  if (!GetImage (&Ximage, &Nimage)) return (FALSE);

  /* shell exits on pipe close, FIX */
  if ((N = get_argument (argc, argv, "-kill"))) {
    KiiClose (Ximage);
    Ximage = 0;
    return (TRUE);
  }

  mode.logflux = FALSE;
  if ((N = get_argument (argc, argv, "-log"))) {
    remove_argument (N, &argc, argv);
    mode.logflux = TRUE;
  }

  if ((argc != 2) && (argc != 4)) {
    gprint (GP_ERR, "USAGE: tv <buffer> [zero range] [-n Nimage] [-log] [-kill]\n");
    return (FALSE);
  }

  GetImageScale (&mode.zero, &mode.range);
  if (argc == 4) {
    mode.zero = atof (argv[2]);
    mode.range = atof (argv[3]);
    if (mode.range == 0.0) mode.range = 0.001;
    SetImageScale (mode.zero, mode.range);
  }

  if ((buf = SelectBuffer (argv[1], OLDBUFFER, TRUE)) == NULL) return (FALSE);
  GetCoords (&coords, &buf[0].header);
  
  image.data1d = (float *) buf[0].matrix.buffer;
  image.Nx = buf[0].matrix.Naxis[0];
  image.Ny = buf[0].matrix.Naxis[1];
  image.file = buf[0].file;
  image.name = buf[0].name;

  KiiNewPicture1D (Ximage, &image, &mode, &coords);

  SetImageName (argv[1]);
  set_str_variable ("TV", argv[1]);
  return (TRUE);
}


/* 
projections and coordinates:

IRAS:
CTYPE1  = 'LL      '                    /TANGENT PLANE (L)                     
CRVAL1  =         0.100000D+02          /R.A. OF MAP CENTER  *** SEE COMMENT   
CDELT1  =        -0.133333D-01          /SEPARATION IN L (DEG)                 
CRPIX1  =                  128          /MAPCENTER IN L                        
CROTA1  =         0.000000E+00          /NO ROTATION            
                                                                               
CTYPE2  = 'MM      '                    /TANGENT PLANE (M)                     
CRVAL2  =         0.410000D+02          /DEC. OF MAP CENTER  *** SEE COMMENT   
CDELT2  =         0.133333D-01          /SEPARATION IN M (DEG)                 
CRPIX2  =                  128          /MAP CENTER IN M                       
CROTA2  =         0.000000E+00          /NO ROTATION                           

HIRAS:
CTYPE1  = 'RA---TAN          ' / PRIMARY AXIS NAME                             
CRVAL1  =   1.000000000000E+01 / PRIMARY REFERENCE VALUE                       
CDELT1  =  -5.555555555556E-03 / PRIMARY PIXEL SEPARATION                      
CRPIX1  =   2.560000000000E+02 / PRIMARY REFERENCE PIXEL                       
CUNIT1  = 'DEGREE            ' / PRIMARY AXIS UNITS                            

CDELT2  =   5.555555555556E-03 / PRIMARY PIXEL SEPARATION                      
CRPIX2  =   2.560000000000E+02 / PRIMARY REFERENCE PIXEL                       
CRVAL2  =   4.100000000000E+01 / PRIMARY REFERENCE VALUE                       
CTYPE2  = 'DEC--TAN          ' / PRIMARY AXIS NAME                             
CUNIT2  = 'DEGREE            ' / PRIMARY AXIS UNITS                            

PROJTYPE= 'GNOMON    '                                                         
COORDSYS= 'EQUATORIAL     '                                                    

gene (small FOV linear):
RA_O
RA_X 
RA_Y 
DEC_O
DEC_X
DEC_Y


*/

# ifdef NEWTVMODE  
  {
    double *pixvalues, slope;

    ALLOCATE (pixvalues, double, Ncolors + 1);
    range = fabs (range);
    for (i = 0; i < Ncolors + 1; i++) {
      if (ISLOG) {
	pixvalues[i] = i*range / NNcol + zero;
      }
      if (!ISLOG) {
	pixvalues[i] = pow (10.0, i*log10(range - 1.0) / NNcol) + zero - 1.0;
      }      
    } 
    for (j = 0; j < Npix; j++, in++, out++) {
      if (*in < pixvalues[0]) {
	i = 0;
      } else {
	if (*in > pixvalues[NNcol]) {
	  i = NNcol;
	} else {
	  for (i = 0; (*in < pixvalues[i]) || (*in > pixvalues[i+1]); ) {
	    slope = 1.0 / (pixvalues[i+1] - pixvalues[i]);
	    i = MIN (MAX (0, i + (*in - pixvalues[i]) * slope), NNcol);
	  }
	}	
      }
      /* for (i = 0; (i < Ncolors - 1) && (pixvalues[i] < *in); i++); */
      *out = i;
    }
    free (pixvalues);
  }
# endif
