/*
    Robot Interface
    (C) 2006 Jason Hunt
    nulluser@gmail.com
    
    File: input.h
*/


#ifndef CAMERA_H
#define CAMERA_H

#include <windows.h>
#include "vfwmod.h"
#include "types.h"


/* Camera class*/
class camera_class
{
  public:
    camera_class(void);
    
    void setup_camera( HWND hwnd, bool ask_info);           // Configure camera

    void start( HWND hwnd );                                // Start camera system
    void stop( void );                                      // Stop camera system

    void update ( void );                                   // Called once a second for housekeeping

    void request_image(void);

    void set_camera_number(unsigned int n) {camera_number = n;};
    int get_update_rate(void) {return (update_rate); };
    
    void send_image(void);
    
  private:
    void compress_image(void);

    void setup_buffer( void );                              // Resize buffers
    
    //void send_image( void );                                // Send image to client

    static int run_thread(void * p_this);                   // Thread starter
    DWORD WINAPI camera_thread( void * ) ;                  // Camera thread

        
    HWND capture_win;                   // Handle to the capture window
    HDC camera_dc;                      // Camera device context

    unsigned int x_size;                // Size of camera image
    unsigned int y_size;

    byte *compressed_buffer;            // Buffer for compressed data
    byte *transcode_buffer;             // Buffer for transcoding
    color_type *camera_buffer;          // Buffer for camera data

    BITMAPINFO camera_buffer_info;      // Bitmap info for camera


    HANDLE thread_handle;               // Thread handle
    unsigned long thread_id;            // Thread ID
    bool thread_running;                // True if thread running

    HANDLE request_image_sem;               // Signal for request image

    int camera_number;                  // Camera index

    int max_compressed;                 // Max compressed size

    unsigned int frames;         // Numbers of frames so far in this second
    unsigned int update_rate;    // Frames per second

    unsigned int compressed_image_size;

    CRITICAL_SECTION camera_thread_lock;  // Lock for serial thread
    CRITICAL_SECTION compressed_buffer_lock;
    
    HANDLE image_ready;;  
};

extern camera_class camera;

#endif
