KinectSensor::KinectSensor()
{
    // get resolution as DWORDS, but store as LONGs to avoid casts later
    DWORD width = 0;
    DWORD height = 0;

    NuiImageResolutionToSize(cDepthResolution, width, height);
    unsigned int depthWidth = static_cast<unsigned int>(width);
    unsigned int depthHeight = static_cast<unsigned int>(height);

    NuiImageResolutionToSize(cColorResolution, width, height);
    unsigned int colorWidth  = static_cast<unsigned int>(width);
    unsigned int colorHeight = static_cast<unsigned int>(height);

    DepthSensor::init(depthWidth, depthHeight, colorWidth, colorHeight);

    m_colorToDepthDivisor = colorWidth/depthWidth;

    m_bDepthReceived = false;
    m_bColorReceived = false;

    m_hNextDepthFrameEvent = INVALID_HANDLE_VALUE;
    m_pDepthStreamHandle = INVALID_HANDLE_VALUE;
    m_hNextColorFrameEvent = INVALID_HANDLE_VALUE;
    m_pColorStreamHandle = INVALID_HANDLE_VALUE;

    m_colorCoordinates = new LONG[depthWidth*depthHeight*2];

    m_bDepthImageIsUpdated = false;
    m_bDepthImageCameraIsUpdated = false;
    m_bNormalImageCameraIsUpdated = false;

    initializeIntrinsics(2.0f*NUI_CAMERA_SKELETON_TO_DEPTH_IMAGE_MULTIPLIER_320x240, 2.0f*NUI_CAMERA_SKELETON_TO_DEPTH_IMAGE_MULTIPLIER_320x240, 320.0f, 240.0f);
}
HRESULT BinaryDumpReader::createFirstConnected()
{
	std::string filename = GlobalAppState::getInstance().s_BinaryDumpReaderSourceFile;
	std::cout << "Start loading binary dump" << std::endl;
	//BinaryDataStreamZLibFile inputStream(filename, false);
	BinaryDataStreamFile inputStream(filename, false);
	CalibratedSensorData sensorData;
	inputStream >> sensorData;
	std::cout << "Loading finished" << std::endl;
	std::cout << sensorData << std::endl;

	DepthSensor::init(sensorData.m_DepthImageWidth, sensorData.m_DepthImageHeight, std::max(sensorData.m_ColorImageWidth,1u), std::max(sensorData.m_ColorImageHeight,1u));
	mat4f intrinsics(sensorData.m_CalibrationDepth.m_Intrinsic);
	initializeIntrinsics(sensorData.m_CalibrationDepth.m_Intrinsic(0,0), sensorData.m_CalibrationDepth.m_Intrinsic(1,1), sensorData.m_CalibrationDepth.m_Intrinsic(0,2), sensorData.m_CalibrationDepth.m_Intrinsic(1,2));

	m_NumFrames = sensorData.m_DepthNumFrames;
	assert(sensorData.m_ColorNumFrames == sensorData.m_DepthNumFrames || sensorData.m_ColorNumFrames == 0);		
	releaseData();
	m_DepthD16Array = new USHORT*[m_NumFrames];
	for (unsigned int i = 0; i < m_NumFrames; i++) {
		m_DepthD16Array[i] = new USHORT[getDepthWidth()*getDepthHeight()];
		for (unsigned int k = 0; k < getDepthWidth()*getDepthHeight(); k++) {
			m_DepthD16Array[i][k] = (USHORT)(sensorData.m_DepthImages[i][k]*1000.0f + 0.5f);
		}
	}

	std::cout << "loading depth done" << std::endl;
	if (sensorData.m_ColorImages.size() > 0) {
		m_bHasColorData = true;
		m_ColorRGBXArray = new BYTE*[m_NumFrames];
		for (unsigned int i = 0; i < m_NumFrames; i++) {
			m_ColorRGBXArray[i] = new BYTE[getColorWidth()*getColorHeight()*getColorBytesPerPixel()];
			for (unsigned int k = 0; k < getColorWidth()*getColorHeight(); k++) {
				const BYTE* c = (BYTE*)&(sensorData.m_ColorImages[i][k]); 
				m_ColorRGBXArray[i][k*getColorBytesPerPixel()+0] = c[0];
				m_ColorRGBXArray[i][k*getColorBytesPerPixel()+1] = c[1];
				m_ColorRGBXArray[i][k*getColorBytesPerPixel()+2] = c[2];
				m_ColorRGBXArray[i][k*getColorBytesPerPixel()+3] = 255;
				//I don't know really why this has to be swapped...
			}
			//std::string outFile = "colorout//color" + std::to_string(i) + ".png";
			//ColorImageR8G8B8A8 image(getColorHeight(), getColorWidth(), (vec4uc*)m_ColorRGBXArray[i]);
			//FreeImageWrapper::saveImage(outFile, image);
		}
	} else {
		m_bHasColorData = false;
	}
	sensorData.deleteData();

	std::cout << "loading color done" << std::endl;


	return S_OK;
}
HRESULT ImageReaderSensor::createFirstConnected()
{
	HRESULT hr = S_OK;

	//what Qian-Yi / Vladlen tell us
	float focalLengthX = 525.0f;
	float focalLengthY = 525.0f;
	//float cx = 319.5f;
	//float cy = 239.5f;

	//what the oni framework gives us
	//float focalLengthX = 570.34f;
	//float focalLengthY = 570.34f;
	float cx = 320.0f;
	float cy = 240.0f;
	initializeIntrinsics(focalLengthX, focalLengthY, cx, cy);

	m_CurrentFrameNumberColor = 0;
	m_CurrentFrameNumberDepth = 0;
	return hr;
}