From f1a8e4e00ac33f224d621b39a7c47808d989f969 Mon Sep 17 00:00:00 2001 From: Dave Allie Date: Sun, 21 Dec 2025 17:52:33 +1100 Subject: [PATCH] Add Epub::generateCoverBmp Add Epub::generateCoverBmp, only supports JPG for now --- lib/Epub/Epub.cpp | 34 +++++++++++++++++++++++++++++++++- lib/Epub/Epub.h | 2 +- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/Epub/Epub.cpp b/lib/Epub/Epub.cpp index cc3bc90..b04c771 100644 --- a/lib/Epub/Epub.cpp +++ b/lib/Epub/Epub.cpp @@ -1,6 +1,7 @@ #include "Epub.h" #include +#include #include #include @@ -218,7 +219,38 @@ const std::string& Epub::getPath() const { return filepath; } const std::string& Epub::getTitle() const { return title; } -const std::string& Epub::getCoverImageItem() const { return coverImageItem; } +bool Epub::generateCoverBmp() const { + // Already generated, return true + if (SD.exists((getCachePath() + "/cover.bmp").c_str())) { + return true; + } + + if (coverImageItem.empty()) { + Serial.printf("[%lu] [EBP] No known cover image\n", millis()); + return false; + } + + if (coverImageItem.substr(coverImageItem.length() - 4) == ".jpg" || + coverImageItem.substr(coverImageItem.length() - 5) == ".jpeg") { + Serial.printf("[%lu] [EBP] Generating BMP from JPG cover image\n", millis()); + File coverJpg = SD.open((getCachePath() + "/.cover.jpg").c_str(), FILE_WRITE, true); + readItemContentsToStream(coverImageItem, coverJpg, 1024); + coverJpg.close(); + + coverJpg = SD.open((getCachePath() + "/.cover.jpg").c_str(), FILE_READ); + File coverBmp = SD.open((getCachePath() + "/cover.bmp").c_str(), FILE_WRITE, true); + const bool success = JpegToBmpConverter::jpegFileToBmpStream(coverJpg, coverBmp); + coverJpg.close(); + coverBmp.close(); + SD.remove((getCachePath() + "/.cover.jpg").c_str()); + Serial.printf("[%lu] [EBP] Generated BMP from JPG cover image, success: %s\n", millis(), success ? "yes" : "no"); + return success; + } else { + Serial.printf("[%lu] [EBP] Cover image is not a JPG, skipping\n", millis()); + } + + return false; +} std::string normalisePath(const std::string& path) { std::vector components; diff --git a/lib/Epub/Epub.h b/lib/Epub/Epub.h index 3115303..f11e388 100644 --- a/lib/Epub/Epub.h +++ b/lib/Epub/Epub.h @@ -48,7 +48,7 @@ class Epub { const std::string& getCachePath() const; const std::string& getPath() const; const std::string& getTitle() const; - const std::string& getCoverImageItem() const; + bool generateCoverBmp() const; uint8_t* readItemContentsToBytes(const std::string& itemHref, size_t* size = nullptr, bool trailingNullByte = false) const; bool readItemContentsToStream(const std::string& itemHref, Print& out, size_t chunkSize) const;