Skip to content

Commit

Permalink
perf: use compressed images while loading from server
Browse files Browse the repository at this point in the history
Signed-off-by: Aditya Agarwal <[email protected]>
  • Loading branch information
Aditya-A-garwal committed Feb 19, 2024
1 parent 4b56ec6 commit ec55b80
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 31 deletions.
35 changes: 34 additions & 1 deletion include/compressor.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class Compressor {
unsigned compress(uint8_t *codes, unsigned count);
unsigned uncompress(uint8_t *codes);

bool pushSegment(uint16_t segment);

void clear();

unsigned getPrefixSize() const;
unsigned getSegmentCount() const;

Expand Down Expand Up @@ -99,6 +103,31 @@ unsigned Compressor<MAX_SEGMENTS>::uncompress(uint8_t *codes) {
return pixelCount;
}

template <unsigned MAX_SEGMENTS>
bool Compressor<MAX_SEGMENTS>::pushSegment(uint16_t segment) {

uint8_t segmentCount = segmentInfo.segmentCount;
uint16_t pixelCount = segmentInfo.pixelCount;

if (segmentCount >= MAX_SEGMENTS) {
return false;
}

*(uint16_t *)&segments[segmentCount] = segment;

pixelCount += segments[segmentCount].size;
segmentCount += 1;

return true;
}

template <unsigned MAX_SEGMENTS>
void Compressor<MAX_SEGMENTS>::clear() {

segmentInfo.segmentCount = 0;
segmentInfo.pixelCount = 0;
}

template <unsigned MAX_SEGMENTS>
unsigned Compressor<MAX_SEGMENTS>::getPrefixSize() const {
return segmentInfo.pixelCount;
Expand All @@ -116,7 +145,11 @@ void Compressor<MAX_SEGMENTS>::transfer(Compressor<NUM_SEGMENTS_OTHER> *other) {
segmentInfo.pixelCount = other->getPrefixSize();
segmentInfo.segmentCount = other->getSegmentCount();

memcpy(segments, other->getRawSegmentAr(), sizeof(uint16_t) * segmentInfo.segmentCount);
for (unsigned s = 0; s < other->getSegmentCount(); ++s) {
segments[s] = other->getRawSegmentAr()[s];
}

// memcpy(segments, other->getRawSegmentAr(), sizeof(uint16_t) * segmentInfo.segmentCount);
}

template <unsigned MAX_SEGMENTS>
Expand Down
72 changes: 42 additions & 30 deletions src/canvasClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,56 +54,73 @@ void CanvasClient::disconnect() {

void CanvasClient::loadCanvas(uint8_t id, Compressor<CANVAS_BUFFER_MAX_SEGMENTS> *compressed) {

uint16_t imageHeight = canvas->heightInternal();
uint16_t imageWidth = canvas->widthInternal();

if (!client.connect(IPAddress(serverIP), serverPort)) {
Serial.println("Connection Failed");
return;
}

uint16_t imageHeight = canvas->heightInternal();
uint16_t imageWidth = canvas->widthInternal();

client.write("\x02", 1);
client.write(&id, 1);
client.write((const char *)&imageHeight, sizeof(imageHeight));
client.write((const char *)&imageWidth, sizeof(imageWidth));

client.flush();

for (int row = 0; row < imageHeight; ++row) {
for (unsigned row = 0; row < imageHeight; ++row) {

for (int col = 0; col < imageWidth; ++col) {
while (!client.available());
uint8_t mode = client.read();

while (client.connected() && !client.available());
if (mode == 0) {

if (!client.connected()) {
Serial.print("Communication with client failed at row ");
Serial.println(row);
for (unsigned col = 0; col < imageWidth; ++col) {

return;
while (!client.available());
rowbuf.code[col] = client.read();
}

rowbuf.color[col] = code2color(client.read());
}
else {

for (uint16_t j = 0; j < imageWidth; ++j) {
canvas->writePixel(row, j, rowbuf.color[j]);
}
uint8_t lo, hi, code;
uint16_t segment, size;

compressor.clear();
for (unsigned s = 0, idx = 0; s < mode; ++s) {

while (!client.available());
lo = client.read();

for (uint16_t j = 0; j < imageWidth; ++j) {
rowbuf.code[j] = color2code(rowbuf.color[j]); //! this is very risky code
while (!client.available());
hi = client.read();

compressor.pushSegment(((uint16_t)hi << 8) | (uint16_t)lo);

segment = ((uint16_t)hi << 8) | (uint16_t)lo;
code = ((segment_t *)&segment)->code;
size = ((segment_t *)&segment)->size;

while (size--) {
rowbuf.code[idx++] = code;
}
}
}

compressed[row].compress(rowbuf.code, imageWidth); //! this code can be optimized
for (unsigned j = 0; j < imageWidth; ++j) {
canvas->writePixel(row, j, code2color(rowbuf.code[j]));
}
compressed[row].compress(rowbuf.code, imageWidth);
}

while (client.connected()) {}
client.stop();
}

void CanvasClient::saveCanvas(uint8_t id, Compressor<CANVAS_BUFFER_MAX_SEGMENTS> *compressed) {

uint16_t imageHeight = canvas->height() - 2;
uint16_t imageWidth = canvas->width() - 2;
uint16_t imageHeight = canvas->heightInternal();
uint16_t imageWidth = canvas->widthInternal();

if (client.connect(IPAddress(serverIP), serverPort) == 0) {
Serial.println("Connection Failed");
Expand All @@ -118,17 +135,12 @@ void CanvasClient::saveCanvas(uint8_t id, Compressor<CANVAS_BUFFER_MAX_SEGMENTS>

for (unsigned i = 0; i < imageHeight; ++i) {

bool compressable = true;
bool compressable;

if (compressed[i].getPrefixSize() == imageWidth) {
compressor.transfer(compressed);
}
else {
for (unsigned j = compressed[i].uncompress(rowbuf.code); j < imageWidth; ++j) {
rowbuf.code[j] = color2code(canvas->readPixel(i, j));
}
compressable = compressor.compress(rowbuf.code, imageWidth);
for (unsigned j = compressed[i].uncompress(rowbuf.code); j < imageWidth; ++j) {
rowbuf.code[j] = color2code(canvas->readPixel(i, j));
}
compressable = compressor.compress(rowbuf.code, imageWidth);

if (compressable) {

Expand Down

0 comments on commit ec55b80

Please sign in to comment.