Skip to content

Commit

Permalink
Added variable threshold to makemask
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurence Bank authored and Laurence Bank committed May 20, 2021
1 parent fef4684 commit faf03e7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/PNGdec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
// forward references
PNG_STATIC int PNGInit(PNGIMAGE *pPNG);
PNG_STATIC int DecodePNG(PNGIMAGE *pImage, void *pUser, int iOptions);
PNG_STATIC void PNGMakeMask(PNGDRAW *pDraw, uint8_t *pMask);
PNG_STATIC void PNGMakeMask(PNGDRAW *pDraw, uint8_t *pMask, uint8_t ucThreshold);
// Include the C code which does the actual work
#include "png.inl"

Expand Down Expand Up @@ -203,7 +203,7 @@ void PNG::getLineAsRGB565(PNGDRAW *pDraw, uint16_t *pPixels, int iEndianness, ui
PNGRGB565(pDraw, pPixels, iEndianness, u32Bkgd, hasAlpha());
} /* getLineAsRGB565() */

void PNG::getAlphaMask(PNGDRAW *pDraw, uint8_t *pMask)
void PNG::getAlphaMask(PNGDRAW *pDraw, uint8_t *pMask, uint8_t ucThreshold)
{
PNGMakeMask(pDraw, pMask);
PNGMakeMask(pDraw, pMask, ucThreshold);
} /* getAlphaMask() */
2 changes: 1 addition & 1 deletion src/PNGdec.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class PNG
int getBufferSize();
uint8_t *getBuffer();
void setBuffer(uint8_t *pBuffer);
void getAlphaMask(PNGDRAW *pDraw, uint8_t *pMask);
void getAlphaMask(PNGDRAW *pDraw, uint8_t *pMask, uint8_t ucThreshold);
void getLineAsRGB565(PNGDRAW *pDraw, uint16_t *pPixels, int iEndianness, uint32_t u32Bkgd);

private:
Expand Down
9 changes: 4 additions & 5 deletions src/png.inl
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,10 @@ uint8_t * PNG_getBuffer(PNGIMAGE *pPNG)
} /* PNG_getBuffer() */

#endif // !__cplusplus
PNG_STATIC void PNGMakeMask(PNGDRAW *pDraw, uint8_t *pMask)
PNG_STATIC void PNGMakeMask(PNGDRAW *pDraw, uint8_t *pMask, uint8_t ucThreshold)
{
uint8_t alpha, c, *s, *d, *pPal;
int i, x;
const uint8_t ALPHA_THRESHOLD = 127;

switch (pDraw->iPixelType) {
case PNG_PIXEL_TRUECOLOR_ALPHA: // truecolor + alpha
Expand All @@ -108,7 +107,7 @@ PNG_STATIC void PNGMakeMask(PNGDRAW *pDraw, uint8_t *pMask)
for (i=0; i<8; i++) {
c <<= 1;
alpha = s[3];
if (alpha > ALPHA_THRESHOLD) // if opaque 'enough', set the bit
if (alpha >= ucThreshold) // if opaque 'enough', set the bit
c |= 1;
s += 4;
}
Expand All @@ -123,7 +122,7 @@ PNG_STATIC void PNGMakeMask(PNGDRAW *pDraw, uint8_t *pMask)
for (i=0; i<8; i++) {
c <<= 1;
alpha = s[1];
if (alpha > ALPHA_THRESHOLD) // if opaque 'enough', set the bit
if (alpha >= ucThreshold) // if opaque 'enough', set the bit
c |= 1;
s += 2;
}
Expand All @@ -139,7 +138,7 @@ PNG_STATIC void PNGMakeMask(PNGDRAW *pDraw, uint8_t *pMask)
for (i=0; i<8; i++) {
c <<= 1;
alpha = pPal[s[0]]; // get palette alpha for this color
if (alpha > ALPHA_THRESHOLD) // if opaque 'enough', set the bit
if (alpha >= ucThreshold) // if opaque 'enough', set the bit
c |= 1;
s++;
}
Expand Down

0 comments on commit faf03e7

Please sign in to comment.