// -*- C++ -*-
/*!
 * @file  CameraEye.h
 * @brief CameraEye
 * @date  $Date$
 *
 * $Id$
 */

#ifndef CAMERAEYE_H
#define CAMERAEYE_H

#include <rtm/Manager.h>
#include <rtm/DataFlowComponentBase.h>
#include <rtm/CorbaPort.h>
#include <rtm/DataInPort.h>
#include <rtm/DataOutPort.h>
#include <rtm/idl/BasicDataTypeSkel.h>

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>


using namespace RTC;

class CameraEye
  : public RTC::DataFlowComponentBase
{
 public:
  CameraEye(RTC::Manager* manager);
  ~CameraEye();

  virtual RTC::ReturnCode_t onInitialize();
  virtual RTC::ReturnCode_t onFinalize();
  virtual RTC::ReturnCode_t onStartup(RTC::UniqueId ec_id);
  virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id);
  virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id);
  virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id);
  virtual RTC::ReturnCode_t onAborting(RTC::UniqueId ec_id);
 protected:
  TimedOctetSeq m_ImageData;
  OutPort<TimedOctetSeq> m_ImageDataOut;
  
 private:
  int dummy;
  CvCapture	*cap;
	IplImage	*img;
 	IplImage *hikaku_img;//位置合わせ用画像
	int			m_Flip;
	int			m_CapSerial;
	std::string	m_caribFile;
	std::string m_captureWindow;		//画像のウィンドウ表示(on)/非表示(off)
	char captureWindow[4];
	int m_usbID; // USBカメラのID /dev/video# の#の番号
	int window_flag; //ウィンドウ表示(0), 非表示(1)
	int cap_window;

	CvSeq* m_image_points_seq;
	CvMat* m_image_points_mat;
	CvSize m_img_size;
	CvSize m_board_size;
	float m_square_size;
	float m_aspect_ratio;
	int m_flags;
	CvMat* m_camera_matrix;
	CvMat* m_dist_coeffs;
	CvMat* m_extr_params;
	CvMat* m_reproj_errs;
	double m_avg_reproj_err ;
	double m_magnification;
	CvRect roi;

	double compute_reprojection_error( const CvMat* object_points,
		    const CvMat* rot_vects, const CvMat* trans_vects,
		    const CvMat* camera_matrix, const CvMat* dist_coeffs,
		    const CvMat* image_points, const CvMat* point_counts,
		    CvMat* per_view_errors );
	int load_calibration(std::string caribFile);
	int run_calibration();

};


extern "C"
{
  void CameraEyeInit(RTC::Manager* manager);
};


#endif // CAMERAEYE_H
