first attempt at networking (does not work)

This commit is contained in:
2025-03-26 15:28:16 -05:00
parent a134513b9c
commit c85581749b
5 changed files with 126 additions and 31 deletions

View File

@@ -1,38 +1,56 @@
#include "ImagePacket.h"
/*
* Construct a packet from an OpenCV Mat, and a beginning index. It will take PACKET_SIZE bytes from the start and add
* it to its slice
*/
ImagePacket::ImagePacket(const cv::Mat *image, const int begin) {
const uchar *target = &image->data[begin];
// TODO: handle out of index cases, pad with zeroes. probably also instantiate our byte array with zeroes
for (int i = 0; i < PACKET_SIZE; i++) {
slice[i] = target[begin + i];
}
}
/*
* 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 < PACKET_SIZE; i++) {
slice[i] = target[begin + i];
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[begin];
uchar *target = &image->data[this->begin];
// TODO: handle out of index cases
for (int i = 0; i < PACKET_SIZE; i++) {
target[begin + i] = slice[i];
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 PACKET_SIZE;
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));
}

View File

@@ -2,17 +2,23 @@
#define IMAGEPACKET_H
#include <opencv2/core/mat.hpp>
#define PACKET_SIZE 768
#define SLICE_SIZE 768
#define IMAGEPACKET_SIZE sizeof(int) + (SLICE_SIZE * sizeof(uchar))
class ImagePacket {
public:
ImagePacket(const cv::Mat *image, int begin);
ImagePacket(const cv::Mat *image, int begin) : ImagePacket(image->data, begin) {}
ImagePacket(const uchar *image, int begin);
~ImagePacket();
unsigned long getBegin() const;
int apply(const cv::Mat *image) const;
void serialize(char* buffer) const;
void deserialize(char* buffer);
private:
int begin;
uchar slice[PACKET_SIZE];
unsigned long begin;
uchar *slice;
};
#endif //IMAGEPACKET_H