#include "ImagePacket.h" /* * Construct a packet from a raw array of unsigned chars, and a beginning index. It will take PACKET_SIZE bytes from the * start and add it to its slice. This will be more useful for embedded scenarios where OpenCV will likely not be used. */ ImagePacket::ImagePacket(const uchar *image, const int begin) { this->begin = begin; this->slice = new uchar[SLICE_SIZE]; const uchar *target = &image[begin]; // TODO: handle out of index cases, pad with zeroes. probably also instantiate our byte array with zeroes for (int i = 0; i < SLICE_SIZE; i++) { this->slice[i] = target[begin + i]; } } /* * destructor frees memory */ ImagePacket::~ImagePacket() = default; unsigned long ImagePacket::getBegin() const { return this->begin; } /* * Apply the packet to an OpenCV Mat. */ int ImagePacket::apply(const cv::Mat *image) const { uchar *target = &image->data[this->begin]; // TODO: handle out of index cases for (int i = 0; i < SLICE_SIZE; i++) { target[this->begin + i] = this->slice[i]; } // TODO: return the actual written size of the packet return SLICE_SIZE; } /* * Serialize the object into a byte array */ void ImagePacket::serialize(char* buffer) const { // TODO: keep the endianness consistent between systems std::memcpy(buffer, &this->begin, sizeof(this->begin)); std::memcpy(buffer + sizeof(this->begin), &this->slice, SLICE_SIZE * sizeof(uchar)); } /* * Deserialize the object from a byte array */ void ImagePacket::deserialize(char* buffer) { // TODO: keep the endianness consistent between systems std::memcpy(&this->begin, buffer, sizeof(this->begin)); std::memcpy(this->slice, buffer + sizeof(this->begin), SLICE_SIZE * sizeof(uchar)); }