From a3916e6c29491024c7204e6a994e156241c5ff42 Mon Sep 17 00:00:00 2001 From: mrwastl Date: Sun, 16 Oct 2011 00:31:30 +0200 Subject: scaling images: added 'auto'; images are now centered (when scale=['auto', 'autox', 'autoy']); bug fixes --- glcdskin/cache.c | 8 +++----- glcdskin/object.c | 37 +++++++++++++++++++++++++++++++++++-- glcdskin/object.h | 1 + 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/glcdskin/cache.c b/glcdskin/cache.c index 59a5f26..a0b2929 100644 --- a/glcdskin/cache.c +++ b/glcdskin/cache.c @@ -72,7 +72,6 @@ cImage * cImageCache::Get(const std::string & path, uint16_t & scalew, uint16_t return NULL; } } - maxCounter = 0; item = NULL; @@ -80,7 +79,7 @@ cImage * cImageCache::Get(const std::string & path, uint16_t & scalew, uint16_t { uint16_t scw = 0, sch = 0; (*it)->ScalingGeometry(scw, sch); - if (item == NULL && path == (*it)->Path() && ( (scw == 0 && sch == 0) || (scw == scalew && sch == scaleh))) + if (item == NULL && path == (*it)->Path() && ( (scw == 0 && sch == 0 && ! (scalew || scaleh)) || (scw == scalew && sch == scaleh))) { (*it)->ResetCounter(); item = (*it); @@ -108,8 +107,6 @@ cImage * cImageCache::Get(const std::string & path, uint16_t & scalew, uint16_t images.erase(oldest); } images.push_back(item); - scalew = item->Image()->Width(); - scaleh = item->Image()->Height(); return item->Image(); } else { failedpaths.push_back(path); @@ -119,6 +116,7 @@ cImage * cImageCache::Get(const std::string & path, uint16_t & scalew, uint16_t 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]; @@ -175,7 +173,7 @@ cImageItem * cImageCache::LoadImage(const std::string & path, uint16_t scalew, u 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, scale_width, scale_height) == false) ) { + if (!imgFile || (imgFile->LoadScaled(*image, file, scalew /*scale_width*/, scaleh /*scale_height*/) == false) ) { delete image; if (imgFile) delete imgFile; return NULL; diff --git a/glcdskin/object.c b/glcdskin/object.c index 7c6b75d..7e99092 100644 --- a/glcdskin/object.c +++ b/glcdskin/object.c @@ -233,6 +233,8 @@ bool cSkinObject::ParseScale(const std::string & Text) { if (Text == "none") mScale = tscNone; + else if (Text == "auto") + mScale = tscAuto; else if (Text == "autox") mScale = tscAutoX; else if (Text == "autoy") @@ -434,6 +436,27 @@ void cSkinObject::Render(GLCD::cBitmap * screen) uint16_t scaleh = 0; switch (mScale) { + case tscAuto: + { + uint16_t w_temp = 0; + uint16_t h_temp = 0; + // get dimensions of unscaled image + GLCD::cImage * image = cache->Get(evalPath, w_temp, h_temp); + if (image) { + w_temp = image->Width(); + h_temp = image->Height(); + if (w_temp != Size().w || h_temp != Size().h) { + double fw = (double)Size().w / (double)w_temp; + double fh = (double)Size().h / (double)h_temp; + if (fw < fh) { + scalew = Size().w; + } else { + scaleh = Size().h; + } + } + } + } + break; case tscAutoX: scalew = Size().w; break; @@ -458,10 +481,20 @@ void cSkinObject::Render(GLCD::cBitmap * screen) if (bitmap) { + uint16_t xoff = 0; + uint16_t yoff = 0; + if (scalew || scaleh) { + if (image->Width() < Size().w) { + xoff = (Size().w - image->Width() ) / 2; + } else if (image->Height() < Size().h) { + yoff = (Size().h - image->Height() ) / 2; + } + } + if (mColor == cColor::ERRCOL) - screen->DrawBitmap(Pos().x, Pos().y, *bitmap); + screen->DrawBitmap(Pos().x + xoff, Pos().y + yoff, *bitmap); else - screen->DrawBitmap(Pos().x, Pos().y, *bitmap, mColor, mBackgroundColor, mOpacity); + screen->DrawBitmap(Pos().x + xoff, Pos().y + yoff, *bitmap, mColor, mBackgroundColor, mOpacity); } if (mScrollLoopMode != -1) // if == -1: currScrollLoopMode already contains correct value diff --git a/glcdskin/object.h b/glcdskin/object.h index a82bedb..694d552 100644 --- a/glcdskin/object.h +++ b/glcdskin/object.h @@ -70,6 +70,7 @@ enum eEffect enum eScale { tscNone, + tscAuto, tscAutoX, tscAutoY, tscFill -- cgit v1.2.3