diff options
Diffstat (limited to 'glcdskin/cache.c')
-rw-r--r-- | glcdskin/cache.c | 70 |
1 files changed, 61 insertions, 9 deletions
diff --git a/glcdskin/cache.c b/glcdskin/cache.c index 83629a0..f0543d9 100644 --- a/glcdskin/cache.c +++ b/glcdskin/cache.c @@ -12,20 +12,25 @@ #include <glcdgraphics/image.h> #include <glcdgraphics/glcd.h> #include <glcdgraphics/pbm.h> +#include <glcdgraphics/extformats.h> #include <stdlib.h> #include <string.h> +#include <syslog.h> + #include "cache.h" #include "skin.h" namespace GLCD { -cImageItem::cImageItem(const std::string & path, cImage * image) +cImageItem::cImageItem(const std::string & path, cImage * image, uint16_t scalew, uint16_t scaleh) : path(path), counter(0), - image(image) + image(image), + scale_width(scalew), + scale_height(scaleh) { } @@ -43,25 +48,40 @@ cImageCache::cImageCache(cSkin * Parent, int Size) cImageCache::~cImageCache() { + Clear(); +} + +void cImageCache::Clear(void) +{ for (unsigned int i = 0; i < images.size(); i++) { delete images[i]; } images.clear(); + failedpaths.clear(); } -cImage * cImageCache::Get(const std::string & path) +cImage * cImageCache::Get(const std::string & path, uint16_t & scalew, uint16_t & scaleh) { std::vector <cImageItem *>::iterator it; cImageItem * item; uint64_t maxCounter; std::vector <cImageItem *>::iterator oldest; + // test if this path has already been stored as invalid path / invalid/non-existent image + for (size_t i = 0; i < failedpaths.size(); i++) { + if (failedpaths[i] == path) { + return NULL; + } + } + maxCounter = 0; item = NULL; for (it = images.begin(); it != images.end(); it++) { - if (item == NULL && path == (*it)->Path()) + uint16_t scw = 0, sch = 0; + (*it)->ScalingGeometry(scw, sch); + if (item == NULL && path == (*it)->Path() && ( (scw == 0 && sch == 0 && ! (scalew || scaleh)) || (scw == scalew && sch == scaleh))) { (*it)->ResetCounter(); item = (*it); @@ -81,21 +101,25 @@ cImage * cImageCache::Get(const std::string & path) return item->Image(); } - item = LoadImage(path); + item = LoadImage(path, scalew, scaleh); if (item) { + syslog(LOG_INFO, "INFO: graphlcd: successfully loaded image '%s'\n", path.c_str()); if (images.size() == size) { images.erase(oldest); } images.push_back(item); return item->Image(); + } else { + failedpaths.push_back(path); } return NULL; } -cImageItem * cImageCache::LoadImage(const std::string & path) +cImageItem * cImageCache::LoadImage(const std::string & path, uint16_t scalew, uint16_t scaleh) { + //fprintf(stderr, "### loading image %s\n", path.c_str()); cImageItem * item; cImage * image; char str[8]; @@ -138,6 +162,28 @@ cImageItem * cImageCache::LoadImage(const std::string & path) } file += path; } + + cImageFile* imgFile = NULL; + + if (strcmp(str, "PBM") == 0) { + imgFile = new cPBMFile(); + } else if (strcmp(str, "GLCD") == 0) { + imgFile = new cGLCDFile(); + } else { + imgFile = new cExtFormatFile(); + } + + uint16_t scale_width = scalew; + uint16_t scale_height = scaleh; + // scale_width and scale_height are set to 0 if image was NOT scaled + if (!imgFile || (imgFile->LoadScaled(*image, file, scalew /*scale_width*/, scaleh /*scale_height*/) == false) ) { + delete image; + if (imgFile) delete imgFile; + return NULL; + } + delete imgFile; + +#if 0 if (strcmp(str, "PBM") == 0) { cPBMFile pbm; @@ -160,11 +206,17 @@ cImageItem * cImageCache::LoadImage(const std::string & path) } else { - delete image; - return NULL; + cExtFormatFile extformat; + + if (extformat.Load(*image, file) == false) + { + delete image; + return NULL; + } } +#endif - item = new cImageItem(path, image); + item = new cImageItem(path, image, scale_width, scale_height); if (!item) { delete image; |