<commit_before>// DepthCameraKinectSDK2.cpp

#include "DepthCameraKinectSDK2.h"
#include <stdio.h>
#include <Unknwn.h>

#if USE_KINECTSDK2

DepthCameraKinectSDK2::DepthCameraKinectSDK2()
: DepthCamera()
, m_pKinectSensor(NULL)
, m_pDepthFrameReader(NULL)
{
    _Initialize();
}

DepthCameraKinectSDK2::~DepthCameraKinectSDK2()
{
    _Uninitialize();
}

int DepthCameraKinectSDK2::_Initialize()
{
    HRESULT hr;

    hr = GetDefaultKinectSensor(&m_pKinectSensor);
    if (FAILED(hr))
    {
        return hr;
    }

    if (m_pKinectSensor)
    {
        // Initialize the Kinect and get the depth reader
        IDepthFrameSource* pDepthFrameSource = NULL;

        hr = m_pKinectSensor->Open();

        if (SUCCEEDED(hr))
        {
            hr = m_pKinectSensor->get_DepthFrameSource(&pDepthFrameSource);
        }

        if (SUCCEEDED(hr))
        {
            hr = pDepthFrameSource->OpenReader(&m_pDepthFrameReader);
        }

        //SafeRelease(pDepthFrameSource);
    }

    //m_currentStatus = OK;

    m_depthWidth = cDepthWidth;
    m_depthHeight = cDepthHeight;

    _AllocateDepthAndColorBuffers();

    return hr;
}


int DepthCameraKinectSDK2::_Uninitialize()
{
#if 0
    // done with depth frame reader
    if (m_pDepthFrameReader)
        m_pDepthFrameReader->Release;

    // close the Kinect Sensor
    if (m_pKinectSensor)
    {
        m_pKinectSensor->Close();
    }

    if (m_pKinectSensor)
        m_pKinectSensor->Release();
#endif
    return 0;
}

/// Copy depth pixels from locked KinectSDK buffer into our local depth buffer.
void DepthCameraKinectSDK2::_ProcessDepth(UINT16* pBuffer)
{
    memcpy(&m_depthBuffer[0], pBuffer, m_depthWidth * m_depthHeight * sizeof(UINT16));

#if 0
    // RGBA8

    // Make sure we've received valid data
    //if (m_pDepthRGBX && pBuffer && (nWidth == cDepthWidth) && (nHeight == cDepthHeight))
    {
        RGBQUAD* pRGBX = m_pDepthRGBX;

        // end pixel is start + width*height - 1
        const UINT16* pBufferEnd = pBuffer + (nWidth * nHeight);

        while (pBuffer < pBufferEnd)
        {
            USHORT depth = *pBuffer;

            // To convert to a byte, we're discarding the most-significant
            // rather than least-significant bits.
            // We're preserving detail, although the intensity will "wrap."
            // Values outside the reliable depth range are mapped to 0 (black).

            // Note: Using conditionals in this loop could degrade performance.
            // Consider using a lookup table instead when writing production code.
            BYTE intensity = static_cast<BYTE>((depth >= nMinDepth) && (depth <= nMaxDepth) ? (depth % 256) : 0);

            pRGBX->rgbRed   = intensity;
            pRGBX->rgbGreen = intensity;
            pRGBX->rgbBlue  = intensity;

            ++pRGBX;
            ++pBuffer;
        }

        // Draw the data with Direct2D
        m_pDrawDepth->Draw(reinterpret_cast<BYTE*>(m_pDepthRGBX), cDepthWidth * cDepthHeight * sizeof(RGBQUAD));
//      m_depthBuffer[i++] = depth;
    }
#endif
}

/// Copy the color image from the Kinect stream into the color buffer.
void DepthCameraKinectSDK2::_ProcessColor()
{
    ///@todo
    //memcpy(&m_colorBuffer[0], static_cast<BYTE *>(LockedRect.pBits), LockedRect.size);
}

void DepthCameraKinectSDK2::UpdateFrame()
{
    if (!m_pDepthFrameReader)
    {
        return;
    }

    IDepthFrame* pDepthFrame = NULL;
<commit_msg>Swap the two bytes of the unsigned short depth value to recover what looks like a number of bits of depth data, but which appear wrapped over a small dynamic range.<commit_after>// DepthCameraKinectSDK2.cpp

#include "DepthCameraKinectSDK2.h"
#include <stdio.h>
#include <Unknwn.h>

#if USE_KINECTSDK2

DepthCameraKinectSDK2::DepthCameraKinectSDK2()
: DepthCamera()
, m_pKinectSensor(NULL)
, m_pDepthFrameReader(NULL)
{
    _Initialize();
}

DepthCameraKinectSDK2::~DepthCameraKinectSDK2()
{
    _Uninitialize();
}

int DepthCameraKinectSDK2::_Initialize()
{
    HRESULT hr;

    hr = GetDefaultKinectSensor(&m_pKinectSensor);
    if (FAILED(hr))
    {
        return hr;
    }

    if (m_pKinectSensor)
    {
        // Initialize the Kinect and get the depth reader
        IDepthFrameSource* pDepthFrameSource = NULL;

        hr = m_pKinectSensor->Open();

        if (SUCCEEDED(hr))
        {
            hr = m_pKinectSensor->get_DepthFrameSource(&pDepthFrameSource);
        }

        if (SUCCEEDED(hr))
        {
            hr = pDepthFrameSource->OpenReader(&m_pDepthFrameReader);
        }

        //SafeRelease(pDepthFrameSource);
    }

    //m_currentStatus = OK;

    m_depthWidth = cDepthWidth;
    m_depthHeight = cDepthHeight;

    _AllocateDepthAndColorBuffers();

    return hr;
}


int DepthCameraKinectSDK2::_Uninitialize()
{
#if 0
    // done with depth frame reader
    if (m_pDepthFrameReader)
        m_pDepthFrameReader->Release;

    // close the Kinect Sensor
    if (m_pKinectSensor)
    {
        m_pKinectSensor->Close();
    }

    if (m_pKinectSensor)
        m_pKinectSensor->Release();
#endif
    return 0;
}

/// Copy depth pixels from locked KinectSDK buffer into our local depth buffer.
void DepthCameraKinectSDK2::_ProcessDepth(UINT16* pBuffer)
{
    //memcpy(&m_depthBuffer[0], pBuffer, m_depthWidth * m_depthHeight * sizeof(UINT16));
    for (int i=0; i<m_depthWidth * m_depthHeight; ++i)
    {
        USHORT d = pBuffer[i];
        USHORT high = d & 0xFF00;
        USHORT low = d & 0xFF;
        USHORT shitfhi = high >> 8;
        USHORT shiftlo = low << 8;
        // It looks like this is the missing depth we're looking for, but it isn't
        // immediately clear to me how to create world space points.
        USHORT dx = shitfhi | shiftlo;
        m_depthBuffer[i] = dx;
    }
}

/// Copy the color image from the Kinect stream into the color buffer.
void DepthCameraKinectSDK2::_ProcessColor()
{
    ///@todo
    //memcpy(&m_colorBuffer[0], static_cast<BYTE *>(LockedRect.pBits), LockedRect.size);
}

void DepthCameraKinectSDK2::UpdateFrame()
{
    if (!m_pDepthFrameReader)
    {
        return;
    }

    IDepthFrame* pDepthFrame = NULL;

{'fraction_non_alphanumeric': 0.10471374979611808, 'fraction_numerical': 0.009460120698091665, 'mean_word_length': 2.72539489671932, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '400527', 'n_tokens_mistral': 2113, 'n_tokens_neox': 2043, 'n_words': 466}