From 7ef1ad19448670543bece35c3a6119e6a440c778 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 15 Oct 2013 14:29:06 -0600 Subject: Use NxWidgets::CScaledBitmap to scale icons in the NxWM taskbar --- NxWidgets/libnxwidgets/include/cscaledbitmap.hxx | 4 +- NxWidgets/libnxwidgets/src/crlepalettebitmap.cxx | 2 +- NxWidgets/libnxwidgets/src/cscaledbitmap.cxx | 62 ++++++++++++++++-------- 3 files changed, 45 insertions(+), 23 deletions(-) (limited to 'NxWidgets/libnxwidgets') diff --git a/NxWidgets/libnxwidgets/include/cscaledbitmap.hxx b/NxWidgets/libnxwidgets/include/cscaledbitmap.hxx index 4c5d8f729..3c0594a1d 100644 --- a/NxWidgets/libnxwidgets/include/cscaledbitmap.hxx +++ b/NxWidgets/libnxwidgets/include/cscaledbitmap.hxx @@ -75,7 +75,7 @@ namespace NXWidgets FAR IBitmap *m_bitmap; /**< The bitmap that is being scaled */ struct nxgl_size_s m_size; /**< Scaled size of the image */ FAR uint8_t *m_rowCache[2]; /**< Two cached rows of the image */ - unsigned int m_row; /**< Row number of the first cached row */ + int m_row; /**< Row number of the first cached row */ b16_t m_xScale; /**< X scale factor */ b16_t m_yScale; /**< Y scale factor */ @@ -167,7 +167,7 @@ namespace NXWidgets * @return The bitmap's height (in rows). */ - inline const nxgl_coord_t getHeight(void) const; + const nxgl_coord_t getHeight(void) const; /** * Get the bitmap's width (in bytes). diff --git a/NxWidgets/libnxwidgets/src/crlepalettebitmap.cxx b/NxWidgets/libnxwidgets/src/crlepalettebitmap.cxx index 241736add..1fb2518a5 100644 --- a/NxWidgets/libnxwidgets/src/crlepalettebitmap.cxx +++ b/NxWidgets/libnxwidgets/src/crlepalettebitmap.cxx @@ -192,7 +192,7 @@ bool CRlePaletteBitmap::getRun(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t widt { // Check ranges. Casts to unsigned int are ugly but permit one-sided comparisons - if (((unsigned int)x < (unsigned int)width) && + if (((unsigned int)x < (unsigned int)m_bitmap->width) && ((unsigned int)(x + width) <= (unsigned int)m_bitmap->width)) { // Seek to the requested row diff --git a/NxWidgets/libnxwidgets/src/cscaledbitmap.cxx b/NxWidgets/libnxwidgets/src/cscaledbitmap.cxx index 6fe211ca4..e274bbb0a 100644 --- a/NxWidgets/libnxwidgets/src/cscaledbitmap.cxx +++ b/NxWidgets/libnxwidgets/src/cscaledbitmap.cxx @@ -81,16 +81,17 @@ CScaledBitmap::CScaledBitmap(IBitmap *bitmap, struct nxgl_size_s &newSize) // yImage = yRequested * oldHeight / newHeight // = yRequested * yScale - m_xScale = itob16((uint32_t)m_bitmap->getHeight()) / newSize.h; + m_yScale = itob16((uint32_t)m_bitmap->getHeight()) / newSize.h; // Allocate and initialize the row cache size_t stride = bitmap->getStride(); - m_rowCache[0] = new uint8_t(stride); - m_rowCache[1] = new uint8_t(stride); + m_rowCache[0] = new uint8_t[stride]; + m_rowCache[1] = new uint8_t[stride]; // Read the first two rows into the cache + m_row = m_bitmap->getWidth(); // Set to an impossible value cacheRows(0); } @@ -100,6 +101,8 @@ CScaledBitmap::CScaledBitmap(IBitmap *bitmap, struct nxgl_size_s &newSize) CScaledBitmap::~CScaledBitmap(void) { + // Delete the allocated row cache memory + if (m_rowCache[0]) { delete m_rowCache[0]; @@ -108,6 +111,13 @@ CScaledBitmap::~CScaledBitmap(void) if (m_rowCache[1]) { delete m_rowCache[1]; + } + + // We are also responsible for deleting the contained IBitmap + + if (m_bitmap) + { + delete m_bitmap; } } @@ -372,7 +382,7 @@ bool CScaledBitmap::cacheRows(unsigned int row) row = bitmapHeight - 1; } - if (!m_bitmap->getRun(0, row, bitmapWidth, &m_rowCache[1])) + if (!m_bitmap->getRun(0, row, bitmapWidth, m_rowCache[1])) { gdbg("Failed to read bitmap row %d\n", row); return false; @@ -391,7 +401,7 @@ bool CScaledBitmap::cacheRows(unsigned int row) row = bitmapHeight - 1; } - if (!m_bitmap->getRun(0, row, bitmapWidth, &m_rowCache[0])) + if (!m_bitmap->getRun(0, row, bitmapWidth, m_rowCache[0])) { gdbg("Failed to read bitmap row %d\n", row); return false; @@ -408,7 +418,7 @@ bool CScaledBitmap::cacheRows(unsigned int row) row = bitmapHeight - 1; } - if (!m_bitmap->getRun(0, row, bitmapWidth, &m_rowCache[1])) + if (!m_bitmap->getRun(0, row, bitmapWidth, m_rowCache[1])) { gdbg("Failed to read bitmap row %d\n", row); return false; @@ -432,21 +442,33 @@ bool CScaledBitmap::scaleColor(FAR const struct rgbcolor_s &incolor1, FAR const struct rgbcolor_s &incolor2, b16_t fraction, FAR struct rgbcolor_s &outcolor) { - unsigned int red; - unsigned int green; - unsigned int blue; + uint8_t component; + b16_t red; + b16_t green; + b16_t blue; + + // A fraction of < 0.5 would mean to use use mostly color1; a fraction + // greater than 0.5 would men to use mostly color2 + b16_t remainder = b16ONE - fraction; - - red = (unsigned int)incolor1.r * fraction + - (unsigned int)incolor2.r * remainder; - green = (unsigned int)incolor1.g * fraction + - (unsigned int)incolor2.g * remainder; - blue = (unsigned int)incolor1.b * fraction + - (unsigned int)incolor2.b * remainder; - - outcolor.r = red < 256 ? red : 255; - outcolor.g = green < 256 ? green : 255; - outcolor.b = blue < 256 ? blue : 255; + + // Interpolate each color value (converting to b15) + + red = (b16_t)incolor1.r * remainder + (b16_t)incolor2.r * fraction; + green = (b16_t)incolor1.g * remainder + (b16_t)incolor2.g * fraction; + blue = (b16_t)incolor1.b * remainder + (b16_t)incolor2.b * fraction; + + // Return the integer, interpolated values, clipping to the range of + // uint8_t + + component = b16toi(red); + outcolor.r = component < 256 ? component : 255; + + component = b16toi(green); + outcolor.g = component < 256 ? component : 255; + + component = b16toi(blue); + outcolor.b = component < 256 ? component : 255; return true; } -- cgit v1.2.3