#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/types.h>
#include <linux/videodev.h>
#include <string.h>
#include <math.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <time.h>
#include <curses.h>

#include "ccvt.h"
#include "fileio.h"

#define DEF_WIDTH	640
#define DEF_HEIGHT	480

#define IN_TV		0
#define IN_COMPOSITE	1
#define IN_COMPOSITE2	2
#define IN_SVIDEO	3
#define IN_DEFAULT	8

#define NORM_PAL	0
#define NORM_NTSC	1
#define NORM_SECAM	2
#define NORM_DEFAULT	0

#define TRUE  1
#define FALSE 0

struct alarm_zone
{
	int x1;						// 0..319 1st pixel with alarm blob
	int x2;						// 0..319 last pixel with alarm blob
	int y1;						// 0..239 1st pixel with alarm blob
	int y2;						// 0..239 last pixel with alarm blob
};

char *get_rgb_addr(void);
char *get_rgb1_addr(void);
char *get_gray_addr(void);
char *get_gray1_addr(void);
char *get_ref_addr(void);
char *get_res_addr(void);
char *get_grf_addr(void);
char *get_hist_addr(void);

int v4l_open(char *device);
void v4l_close(int dev);
int get_vid_cap(int dev);

int get_brigthness(int dev,int channel);
int get_hue(int dev,int channel);
int get_contras(int dev,int channel);
int get_color(int dev,int channel);

int set_brigthness(int dev,int channel,int value);
int set_hue(int dev,int channel,int value);
int set_contras(int dev,int channel,int value);
int set_color(int dev,int channel,int value);

char *get_image(int dev, int *width, int *height, int input,int norm,int fmt,int *size);
int retrieve_data(int dev,int chan,int p_width,int p_height,int g_width,int g_height,int gray_sample,int do_motion,int char_detect);
void down_sample_image(int vid_width,int vid_height,int prevX,int prevY);
void generate_grayscale(int vid_width,int vid_height );
void one_2_tree_bytes(int vid_width,int vid_height,char *in_pic,char *out_pic);
void update_history_graph(int percent, int alarm_level,int prevX,int prevY);
float do_motion_analysis(int vid_width,int vid_height,int prevX,int prevY,int *oblob_cnt,int *ograph_scale,int *oalarm_threshold,int *opixcnt,int *oimage_cnt,float *ofresult);
void mark_alarm_zone( struct alarm_zone *zone,int vid_width,int vid_height );
void update_reference_image(int vid_width,int vid_height,int prevX,int prevY);
void calculate_histogram(int vid_width,int vid_height );
