This commit is contained in:
Eliz 2026-01-24 00:35:36 +00:00
parent 7cee115d43
commit a63812707d
2 changed files with 43 additions and 9 deletions

View File

@ -4,7 +4,8 @@
#include "util/StringUtils.h" #include "util/StringUtils.h"
#include <sys/stat.h> #include <sys/stat.h>
BookCoverCache::BookCoverCache(const std::string& cache_dir) : cache_dir(cache_dir) { BookCoverCache::BookCoverCache(const std::string& cache_dir, GfxRenderer* renderer)
: cache_dir(cache_dir), renderer(renderer) {
// Ensure the cache directory exists // Ensure the cache directory exists
if (!SdMan.exists(cache_dir.c_str())) { if (!SdMan.exists(cache_dir.c_str())) {
SdMan.mkdir(cache_dir.c_str()); SdMan.mkdir(cache_dir.c_str());
@ -14,6 +15,18 @@ BookCoverCache::BookCoverCache(const std::string& cache_dir) : cache_dir(cache_d
BookCoverCache::~BookCoverCache() { BookCoverCache::~BookCoverCache() {
} }
bool BookCoverCache::render(const std::string& book_path, int x, int y, int width,
int height) {
auto bitmap = getCover(book_path);
if (bitmap) {
renderer->drawBitmap(*bitmap, x, y, width, height);
return true;
}
return false;
}
std::shared_ptr<Bitmap> BookCoverCache::getCover(const std::string& book_path) { std::shared_ptr<Bitmap> BookCoverCache::getCover(const std::string& book_path) {
std::string cache_path = getCachePath(book_path); std::string cache_path = getCachePath(book_path);
@ -46,6 +59,12 @@ bool BookCoverCache::isCacheValid(const std::string& book_path) const {
return SdMan.exists(cache_path.c_str()); return SdMan.exists(cache_path.c_str());
} }
void BookCoverCache::setTargetSize(int width, int height) {
target_width = width;
target_height = height;
}
std::shared_ptr<Bitmap> BookCoverCache::generateThumbnail(const std::string& book_path) { std::shared_ptr<Bitmap> BookCoverCache::generateThumbnail(const std::string& book_path) {
std::string coverBmpPath; std::string coverBmpPath;
bool hasCoverImage = false; bool hasCoverImage = false;
@ -70,14 +89,27 @@ std::shared_ptr<Bitmap> BookCoverCache::generateThumbnail(const std::string& boo
// This is not very efficient, we are copying the file. // This is not very efficient, we are copying the file.
// We should ideally generate the thumbnail directly to the cache path. // We should ideally generate the thumbnail directly to the cache path.
// For now, this will do. // For now, this will do.
if(SdMan.copy(coverBmpPath.c_str(), cache_path.c_str())) { FsFile source_file;
FsFile file; FsFile dest_file;
if (SdMan.openFileForRead("CACHE", cache_path, file)) { if (SdMan.openFileForRead("THUMB", coverBmpPath, source_file) &&
auto bitmap = std::make_shared<Bitmap>(file); SdMan.openFileForWrite("THUMB", cache_path, dest_file)) {
if (bitmap->parseHeaders() == BmpReaderError::Ok) { uint8_t buffer[512];
return bitmap; while (source_file.available()) {
} int bytes_read = source_file.read(buffer, sizeof(buffer));
if (bytes_read > 0) {
dest_file.write(buffer, bytes_read);
} }
}
source_file.close();
dest_file.close();
FsFile file;
if (SdMan.openFileForRead("CACHE", cache_path, file)) {
auto bitmap = std::make_shared<Bitmap>(file);
if (bitmap->parseHeaders() == BmpReaderError::Ok) {
return bitmap;
}
}
} }
} }

View File

@ -18,7 +18,9 @@ class BookCoverCache {
private: private:
std::string getCachePath(const std::string& book_path) const; std::string getCachePath(const std::string& book_path) const;
bool generateThumbnail(const std::string& book_path); std::shared_ptr<Bitmap> generateThumbnail(const std::string& book_path);
std::shared_ptr<Bitmap> getCover(const std::string& book_path);
bool isCacheValid(const std::string& book_path) const;
std::string cache_dir; std::string cache_dir;
GfxRenderer* renderer; GfxRenderer* renderer;