<commit_before>/** @file
    @brief Implementation

    @date 2015

    @author
    Sensics, Inc.
    <http://sensics.com/osvr>
*/

// Copyright 2015 Sensics, Inc.
//
// All rights reserved.
//
// (Final version intended to be licensed under
// the Apache License, Version 2.0)

// Internal Includes
#include <osvr/Common/ImagingComponent.h>
#include <osvr/Common/BaseDevice.h>
#include <osvr/Common/Serialization.h>
#include <osvr/Common/Buffer.h>
#include <osvr/Util/OpenCVTypeDispatch.h>

#include <osvr/Util/Verbosity.h>

// Library/third-party includes
#include <opencv2/core/core.hpp>

// Standard includes
// - none

namespace osvr {
namespace common {
    namespace messages {
        class ImageRegion::MessageSerialization {
          public:
            MessageSerialization(OSVR_ImagingMetadata const &meta,
                                 OSVR_ImageBufferElement *imageData,
                                 OSVR_ChannelCount sensor)
                : m_meta(meta),
                  m_imgBuf(imageData,
                           [](OSVR_ImageBufferElement *) {
                           }), // That's a null-deleter right there for you.
                  m_sensor(sensor) {}

            MessageSerialization() : m_imgBuf(nullptr) {}

            template <typename T>
            void allocateBuffer(T &, size_t bytes, std::true_type const &) {
                m_imgBuf.reset(reinterpret_cast<OSVR_ImageBufferElement *>(
                                   cv::fastMalloc(bytes)),
                               &cv::fastFree);
            }

            template <typename T>
            void allocateBuffer(T &, size_t, std::false_type const &) {
                // Does nothing if we're serializing.
            }

            template <typename T> void processMessage(T &p) {
                p(m_meta.height);
                p(m_meta.width);
                p(m_meta.channels);
                p(m_meta.depth);
                p(m_meta.type,
                  serialization::EnumAsIntegerTag<OSVR_ImagingValueType,
                                                  uint8_t>());

                auto bytes = m_meta.height * m_meta.width * m_meta.depth *
                             m_meta.channels;

                /// Allocate the matrix backing data, if we're deserializing
                /// only.
                allocateBuffer(p, bytes, p.isDeserialize());
                p(m_imgBuf.get(),
                  serialization::AlignedDataBufferTag(bytes, m_meta.depth));
            }
            ImageData getData() const {
                ImageData ret;
                ret.sensor = m_sensor;
                ret.metadata = m_meta;
                ret.buffer = m_imgBuf;
                return ret;
            }

          private:
            OSVR_ImagingMetadata m_meta;
            ImageBufferPtr m_imgBuf;
            OSVR_ChannelCount m_sensor;
        };
        const char *ImageRegion::identifier() {
            return "com.osvr.imaging.imageregion";
        }
    } // namespace messages

    shared_ptr<ImagingComponent>
    ImagingComponent::create(OSVR_ChannelCount numChan) {
        shared_ptr<ImagingComponent> ret(new ImagingComponent(numChan));
        return ret;
    }
    ImagingComponent::ImagingComponent(OSVR_ChannelCount numChan)
        : m_numSensor(numChan) {}

    void ImagingComponent::sendImageData(OSVR_ImagingMetadata metadata,
                                         OSVR_ImageBufferElement *imageData,
                                         OSVR_ChannelCount sensor,
                                         OSVR_TimeValue const &timestamp) {
        Buffer<> buf;
        messages::ImageRegion::MessageSerialization msg(metadata, imageData,
                                                        sensor);
        serialize(buf, msg);
        m_getParent().packMessage(buf, imageRegion.getMessageType(), timestamp);
        m_getParent().sendPending();
        m_checkFirst(metadata);
    }

    int VRPN_CALLBACK
    ImagingComponent::m_handleImageRegion(void *userdata, vrpn_HANDLERPARAM p) {
        auto self = static_cast<ImagingComponent *>(userdata);
        auto bufwrap = ExternalBufferReadingWrapper<unsigned char>(
            reinterpret_cast<unsigned char const *>(p.buffer), p.payload_len);
        auto bufReader = BufferReader<decltype(bufwrap)>(bufwrap);

        messages::ImageRegion::MessageSerialization msg;
        deserialize(bufReader, msg);
        auto data = msg.getData();
        auto timestamp = util::time::fromStructTimeval(p.msg_time);

        self->m_checkFirst(data.metadata);
        for (auto const &cb : self->m_cb) {
            cb(data, timestamp);<commit_msg>Imaging: check buffer size before sending.<commit_after>/** @file
    @brief Implementation

    @date 2015

    @author
    Sensics, Inc.
    <http://sensics.com/osvr>
*/

// Copyright 2015 Sensics, Inc.
//
// All rights reserved.
//
// (Final version intended to be licensed under
// the Apache License, Version 2.0)

// Internal Includes
#include <osvr/Common/ImagingComponent.h>
#include <osvr/Common/BaseDevice.h>
#include <osvr/Common/Serialization.h>
#include <osvr/Common/Buffer.h>
#include <osvr/Util/OpenCVTypeDispatch.h>

#include <osvr/Util/Verbosity.h>

// Library/third-party includes
#include <opencv2/core/core.hpp>

// Standard includes
// - none

namespace osvr {
namespace common {
    namespace messages {
        class ImageRegion::MessageSerialization {
          public:
            MessageSerialization(OSVR_ImagingMetadata const &meta,
                                 OSVR_ImageBufferElement *imageData,
                                 OSVR_ChannelCount sensor)
                : m_meta(meta),
                  m_imgBuf(imageData,
                           [](OSVR_ImageBufferElement *) {
                           }), // That's a null-deleter right there for you.
                  m_sensor(sensor) {}

            MessageSerialization() : m_imgBuf(nullptr) {}

            template <typename T>
            void allocateBuffer(T &, size_t bytes, std::true_type const &) {
                m_imgBuf.reset(reinterpret_cast<OSVR_ImageBufferElement *>(
                                   cv::fastMalloc(bytes)),
                               &cv::fastFree);
            }

            template <typename T>
            void allocateBuffer(T &, size_t, std::false_type const &) {
                // Does nothing if we're serializing.
            }

            template <typename T> void processMessage(T &p) {
                p(m_meta.height);
                p(m_meta.width);
                p(m_meta.channels);
                p(m_meta.depth);
                p(m_meta.type,
                  serialization::EnumAsIntegerTag<OSVR_ImagingValueType,
                                                  uint8_t>());

                auto bytes = m_meta.height * m_meta.width * m_meta.depth *
                             m_meta.channels;

                /// Allocate the matrix backing data, if we're deserializing
                /// only.
                allocateBuffer(p, bytes, p.isDeserialize());
                p(m_imgBuf.get(),
                  serialization::AlignedDataBufferTag(bytes, m_meta.depth));
            }
            ImageData getData() const {
                ImageData ret;
                ret.sensor = m_sensor;
                ret.metadata = m_meta;
                ret.buffer = m_imgBuf;
                return ret;
            }

          private:
            OSVR_ImagingMetadata m_meta;
            ImageBufferPtr m_imgBuf;
            OSVR_ChannelCount m_sensor;
        };
        const char *ImageRegion::identifier() {
            return "com.osvr.imaging.imageregion";
        }
    } // namespace messages

    shared_ptr<ImagingComponent>
    ImagingComponent::create(OSVR_ChannelCount numChan) {
        shared_ptr<ImagingComponent> ret(new ImagingComponent(numChan));
        return ret;
    }
    ImagingComponent::ImagingComponent(OSVR_ChannelCount numChan)
        : m_numSensor(numChan) {}

    void ImagingComponent::sendImageData(OSVR_ImagingMetadata metadata,
                                         OSVR_ImageBufferElement *imageData,
                                         OSVR_ChannelCount sensor,
                                         OSVR_TimeValue const &timestamp) {
        Buffer<> buf;
        messages::ImageRegion::MessageSerialization msg(metadata, imageData,
                                                        sensor);
        serialize(buf, msg);
        if (buf.size() > vrpn_CONNECTION_TCP_BUFLEN) {
            OSVR_DEV_VERBOSE("Skipping imaging message: size is "
                             << buf.size() << " vs the maximum of "
                             << vrpn_CONNECTION_TCP_BUFLEN);
            return;
        }
        m_getParent().packMessage(buf, imageRegion.getMessageType(), timestamp);
        m_getParent().sendPending();
        m_checkFirst(metadata);
    }

    int VRPN_CALLBACK
    ImagingComponent::m_handleImageRegion(void *userdata, vrpn_HANDLERPARAM p) {
        auto self = static_cast<ImagingComponent *>(userdata);
        auto bufwrap = ExternalBufferReadingWrapper<unsigned char>(
            reinterpret_cast<unsigned char const *>(p.buffer), p.payload_len);
        auto bufReader = BufferReader<decltype(bufwrap)>(bufwrap);

        messages::ImageRegion::MessageSerialization msg;
        deserialize(bufReader, msg);
        auto data = msg.getData();
        auto timestamp = util::time::fromStructTimeval(p.msg_time);

        self->m_checkFirst(data.metadata);
        for (auto const &cb : self->m_cb) {
            cb(data, timestamp);
{'fraction_non_alphanumeric': 0.09427121102248005, 'fraction_numerical': 0.002486273697296177, 'mean_word_length': 1.7356191555681497, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4875350', 'n_tokens_mistral': 2617, 'n_tokens_neox': 2501, 'n_words': 497}