Compare commits

...

1 commit

Author SHA1 Message Date
Djuri Baars
b65ca37485 First attempt of gzip decompression with miniz 2024-12-19 22:09:33 +01:00
5 changed files with 116 additions and 7 deletions

View file

@ -165,8 +165,41 @@ void refreshFromMemory()
} }
} }
const unsigned char* decIcons[5];
void setupDisplays() void setupDisplays()
{ {
for (int i = 0; i < 1; i++) {
const size_t compressedSize = 408;
// Get required output buffer size
size_t decompressedSize = GzipDecompressor::getDecompressedSize(epd_icons_pickaxe, compressedSize);
if (decompressedSize == 0) {
Serial.println("Failed to get decompressed size");
continue;
}
// Allocate output buffer
uint8_t* decompressedData = (uint8_t*)malloc(decompressedSize);
if (!decompressedData) {
Serial.println("Failed to allocate memory");
continue;
}
// Decompress
if (GzipDecompressor::decompressData(epd_icons_pickaxe, compressedSize,
decompressedData, &decompressedSize)) {
Serial.println("Decompression successful");
// Use decompressedData here
} else {
Serial.printf("Decompression failed, Compressed: %d, Decompressed: %d\n", compressedSize, decompressedSize);
}
free(decompressedData);
}
std::lock_guard<std::mutex> lockMcp(mcpMutex); std::lock_guard<std::mutex> lockMcp(mcpMutex);
for (uint i = 0; i < NUM_SCREENS; i++) for (uint i = 0; i < NUM_SCREENS; i++)
@ -614,10 +647,7 @@ void renderIcon(const uint dispNum, const String &text, bool partial)
iconIndex = 4; iconIndex = 4;
} }
// displays[dispNum].drawInvertedBitmap(0,0, decIcons[iconIndex], 122, 250, getFgColor());
displays[dispNum].drawInvertedBitmap(0,0, epd_icons_allArray[iconIndex], 122, 250, getFgColor());
// displays[dispNum].drawInvertedBitmap(0,0, getOceanIcon(), 122, 250, getFgColor()); // displays[dispNum].drawInvertedBitmap(0,0, getOceanIcon(), 122, 250, getFgColor());

View file

@ -3,7 +3,7 @@
#include <Fonts/FreeSans9pt7b.h> #include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSansBold9pt7b.h> #include <Fonts/FreeSansBold9pt7b.h>
#include <GxEPD2_BW.h> #include <GxEPD2_BW.h>
#include "gzip_decompressor.hpp"
#include <mcp23x17_pin.hpp> #include <mcp23x17_pin.hpp>
#include <mutex> #include <mutex>

View file

@ -0,0 +1,55 @@
#include "rom/miniz.h"
class GzipDecompressor {
private:
static const size_t CHUNK_SIZE = 1024; // Adjust based on your needs
public:
// Returns decompressed size or 0 if failed
static size_t getDecompressedSize(const uint8_t* input, size_t inputSize) {
size_t decompSize = 0;
// Last 4 bytes of gzip contain decompressed size
if (inputSize >= 4) {
decompSize = (input[inputSize - 1] << 24) |
(input[inputSize - 2] << 16) |
(input[inputSize - 3] << 8) |
(input[inputSize - 4]);
}
return decompSize;
}
static bool decompressData(const uint8_t* input, size_t inputSize,
uint8_t* output, size_t* outputSize) {
tinfl_decompressor decomp;
tinfl_init(&decomp);
size_t inPos = 0;
size_t outPos = 0;
// Skip gzip header (usually 10 bytes)
if (inputSize > 10) {
inPos = 10;
}
while (inPos < inputSize - 8) { // -8 for footer
size_t inBytes = inputSize - inPos - 8; // Don't read footer
size_t outBytes = *outputSize - outPos;
tinfl_status status = tinfl_decompress(&decomp,
&input[inPos], &inBytes,
output, &output[outPos], &outBytes,
TINFL_FLAG_HAS_MORE_INPUT | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF);
inPos += inBytes;
outPos += outBytes;
if (status == TINFL_STATUS_DONE) {
*outputSize = outPos;
return true;
} else if (status < 0) {
return false;
}
}
return false;
}
};

View file

@ -152,3 +152,24 @@ String calculateSHA256(WiFiClient *stream, size_t contentLength) {
// pUncompressed = (uint8_t *)malloc(iUncompSize+4); // pUncompressed = (uint8_t *)malloc(iUncompSize+4);
// zt.gunzip((uint8_t *)ocean_logo_comp, ocean_logo_size, pUncompressed); // zt.gunzip((uint8_t *)ocean_logo_comp, ocean_logo_size, pUncompressed);
// } // }
void decompressData(const uint8_t* input, size_t inputSize, uint8_t* output, size_t outputSize) {
tinfl_decompressor decomp;
tinfl_init(&decomp);
size_t inPos = 0;
size_t outPos = 0;
while (inPos < inputSize) {
size_t inBytes = inputSize - inPos;
size_t outBytes = outputSize - outPos;
tinfl_decompress(&decomp,
&input[inPos], &inBytes,
output, &output[outPos], &outBytes,
TINFL_FLAG_PARSE_ZLIB_HEADER);
inPos += inBytes;
outPos += outBytes;
}
}

View file

@ -12,6 +12,7 @@
#include <mbedtls/md.h> #include <mbedtls/md.h>
#include "esp_crt_bundle.h" #include "esp_crt_bundle.h"
#include <Update.h> #include <Update.h>
#include <rom/miniz.h>
#include <mutex> #include <mutex>
#include <utils.hpp> #include <utils.hpp>
@ -92,3 +93,5 @@ struct ScreenMapping {
String calculateSHA256(uint8_t* data, size_t len); String calculateSHA256(uint8_t* data, size_t len);
String calculateSHA256(WiFiClient *stream, size_t contentLength); String calculateSHA256(WiFiClient *stream, size_t contentLength);
void decompressData(const uint8_t* input, size_t inputSize, uint8_t* output, size_t outputSize);