#include "colourspace.h"
#include "image_dc1394input.h"
#include "rgba.h"

namespace mimas {

template<>
void image_dc1394input< rgba< unsigned char > >::
read( image< rgba< unsigned char > > &img ) throw (mimasexception)
{
  MMERROR( m_handle != NULL, mimasexception, ,
           "Camera device was closed already." );
  if ( m_bufferUsed ) {
    dc1394_dma_done_with_buffer( &m_camera );
    m_bufferUsed = false;
  };

  // Perform image capture.
  MMERROR( dc1394_dma_single_capture( &m_camera ) == DC1394_SUCCESS,
           mimasexception, ,
           "Failed capturing frame from firewire digital camera." );
  m_bufferUsed = true;

  img.init( m_camera.frame_width, m_camera.frame_height );

  switch ( m_colourSpace ) {
  case UYVY:
    uyvy_to_rgba( (const char *)m_camera.capture_buffer,
                  m_camera.frame_width, m_camera.frame_height,
                  (char *)img.rawData() );
    break;
  case RGB24:
    rgb_to_rgba( (const char *)m_camera.capture_buffer,
                 m_camera.frame_width, m_camera.frame_height,
                 (char *)img.rawData() );    break;
  default:
    assert( false );
    MMERROR( false, mimasexception, ,
             "Colour-grabbing with colourspace number " << m_colourSpace
             << " not supported." );
  };
}

};