mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-06 15:47:39 +03:00
update
This commit is contained in:
parent
7cee115d43
commit
a63812707d
@ -4,7 +4,8 @@
|
||||
#include "util/StringUtils.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
|
||||
if (!SdMan.exists(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() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
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::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());
|
||||
}
|
||||
|
||||
|
||||
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::string coverBmpPath;
|
||||
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.
|
||||
// We should ideally generate the thumbnail directly to the cache path.
|
||||
// For now, this will do.
|
||||
if(SdMan.copy(coverBmpPath.c_str(), cache_path.c_str())) {
|
||||
FsFile file;
|
||||
if (SdMan.openFileForRead("CACHE", cache_path, file)) {
|
||||
auto bitmap = std::make_shared<Bitmap>(file);
|
||||
if (bitmap->parseHeaders() == BmpReaderError::Ok) {
|
||||
return bitmap;
|
||||
}
|
||||
FsFile source_file;
|
||||
FsFile dest_file;
|
||||
if (SdMan.openFileForRead("THUMB", coverBmpPath, source_file) &&
|
||||
SdMan.openFileForWrite("THUMB", cache_path, dest_file)) {
|
||||
uint8_t buffer[512];
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -18,7 +18,9 @@ class BookCoverCache {
|
||||
|
||||
private:
|
||||
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;
|
||||
GfxRenderer* renderer;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user