////////////////////////////////////////////////////////////////////////////
// Fast Track example II
//
// Usage: seev4l2
//
// This program captures and displays one video frame from a
// Video2Linux2 capture device.
//
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2004  Joao Hespanha
//
// This program 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.
//
// This program 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 this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Modification history:
//
// Before August 27, 2006 
// No idea about what happened before this date...
//
// August 27, 2006
// GNU GPL added
// 
///////////////////////////////////////////////////////////////////////////

#include "../lib/FastTrack.h"

FT_16GrayImage_T Image;
FT_8GrayImage_T  Image8;

int main(int argc, char *argv[])
{
  int width=640,height=480;
  int vidfd,i,j,mn,mx,rc,videoseq;
  struct timeval timeout,tstamp,t0,t1;

  ////// initialize gtk /////////////////////////////////////////////
  gtk_init(&argc, &argv);
  gdk_rgb_init();

  // Initialize device and start capture
  vidfd=v4l2Init("/dev/video0",          // device
		 0,                      // source
		 V4L2_STD_NTSC_M,        // standard
		 V4L2_PIX_FMT_GREY,      // pixel format
		 // V4L2_PIX_FMT_BGR24,     // pixel format
		 V4L2_FIELD_INTERLACED,  // field order
		 // V4L2_FIELD_BOTTOM,         // field order
		 width,                  // window capture width
		 height,                 // window capture height
		 4                       // number of buffers
		 );

  int contrast=32767,brightness=32767;     // normal contrast
  for (i=0;i<100;i++) {
    v4l2SetParameters(vidfd,
		      brightness,    // brightness
		      contrast,      // contrast
		      32767,         // saturation
		      32767);        // hue
    
  }

  timeout.tv_sec = 2;timeout.tv_usec = 0;
  rc=v4l2GetImage(vidfd,           // device file handle for ioctl
		  &Image,          // Image
		  &tstamp,         // time stamp with start-of-capture time
		  &videoseq,       // frame video sequence number
		  &timeout,        // maximum waiting time 
		  &mn,             // minimum grey level
		  &mx,             // maximum grey level
		  1);              // get latest image
    
  if (rc<0) fprintf(stderr,"v4l2GetImage: rc = %d\n",rc);

  close(vidfd);

  Convert16to8(&Image,&Image8);
  DisplayNewImage(&Image8,"/dev/video0");
  
  ////// gtk_main() loop ///////////////////////////////////////////
  gtk_main();
}


