Skip to content

Commit

Permalink
jcore/jx: fix JImage to correctly apply a mask for true color png's
Browse files Browse the repository at this point in the history
  • Loading branch information
jafl committed Jan 3, 2024
1 parent 8316cff commit 5571036
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 10 deletions.
3 changes: 3 additions & 0 deletions libjcore/code/JCoreLibVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ static const char* kCurrentJCoreLibVersionStr = "4.1.0";
// jListUtil:
// *** Removed JCompareIndices(), JCompareSizes(), JCompareCoordinates(),
// JCompareUInt64() in favor of <=>.
// JImage:
// Fixed ReadGD() to correctly apply a mask for true color png's.
// Added pure virtual CreateEmptyMask().

// version 4.0.0:
// *** Upgraded to C++20
Expand Down
20 changes: 20 additions & 0 deletions libjcore/code/JImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ JImage::ReadGD
{
PrepareForImageData();

auto* mask = CreateEmptyMask();
bool setMask = false;

for (JCoordinate x=0; x<itsWidth; x++)
{
for (JCoordinate y=0; y<itsHeight; y++)
Expand All @@ -329,11 +332,28 @@ JImage::ReadGD
gdImageGreen(image, c) * kGDColorScale,
gdImageBlue (image, c) * kGDColorScale));
SetColor(x,y, color);

if ((!hasMask && gdImageAlpha(image, c) == 0) ||
( hasMask && c != maskColor))
{
mask->AddPixel(x,y);
setMask = true;
}
}
}

ImageDataFinished();

if (setMask)
{
SetMask(mask);
}
else
{
jdelete mask;
mask = nullptr;
}

gdImageDestroy(image);
image = nullptr;

Expand Down
4 changes: 3 additions & 1 deletion libjcore/code/JImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ class JImage
virtual void SetColor(const JCoordinate x, const JCoordinate y,
const JColorID color) = 0;

virtual bool GetMask(JImageMask** mask) const = 0;
virtual bool GetMask(JImageMask** mask) const = 0;
virtual void SetMask(JImageMask* mask) = 0;
virtual JImageMask* CreateEmptyMask() const = 0;

JError WriteGIF(const JString& fileName,
const bool compressColorsToFit,
Expand Down
20 changes: 19 additions & 1 deletion libjx/code/JXImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,17 @@ JXImage::GetMask // virtual
return itsMask != nullptr;
}

void
JXImage::SetMask
(
JImageMask* mask
)
{
JXImageMask* xmask = dynamic_cast<JXImageMask*>(mask);
assert( xmask != nullptr );
SetMask(xmask);
}

void
JXImage::SetMask
(
Expand All @@ -728,6 +739,13 @@ JXImage::ClearMask()
itsMask = nullptr;
}

JImageMask*
JXImage::CreateEmptyMask()
const
{
return jnew JXImageMask(itsDisplay, GetWidth(), GetHeight(), false);
}

/******************************************************************************
Draw
Expand Down Expand Up @@ -1138,7 +1156,7 @@ JXImage::SetImageData
const JSize colorCount,
const JColorID* colorTable,
unsigned short** imageData,
const bool hasMask,
const bool hasMask,
const unsigned long maskColor
)
{
Expand Down
12 changes: 7 additions & 5 deletions libjx/code/JXImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ class JXImage : public JImage, public virtual JBroadcaster
JXDisplay* GetDisplay() const;
JSize GetDepth() const;

bool HasMask() const;
bool GetMask(JImageMask** mask) const override;
bool GetMask(JXImageMask** mask) const;
void SetMask(JXImageMask* mask);
void ClearMask();
bool HasMask() const;
bool GetMask(JImageMask** mask) const override;
bool GetMask(JXImageMask** mask) const;
void SetMask(JImageMask* mask) override;
void SetMask(JXImageMask* mask);
void ClearMask();
JImageMask* CreateEmptyMask() const override;

void Draw(const Drawable drawable, JXGC* gc,
const JRect& srcRect, const JRect& destRect) const;
Expand Down
2 changes: 1 addition & 1 deletion libjx/code/JXImageMask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ JXImageMask::JXImageMask
JXDisplay* display,
const JCoordinate width,
const JCoordinate height,
const bool filled
const bool filled
)
:
JXImage(display, width, height,
Expand Down
1 change: 0 additions & 1 deletion libjx/code/JXImage_JPEG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ JXImage::CreateFromJPEG
)
{
*image = jnew JXImage(display);
assert( *image != nullptr );

const JError err = (**image).ReadJPEG(fileName);

Expand Down
1 change: 0 additions & 1 deletion libjx/code/JXImage_PNG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ JXImage::CreateFromPNG
)
{
*image = jnew JXImage(display);
assert( *image != nullptr );

const JError err = (**image).ReadPNG(fileName);

Expand Down
1 change: 1 addition & 0 deletions libjx/testui/testjx.jcc
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ T "./" "&make_default ; /bin/sh -c 'JMM_INITIALIZE=yes JMM_SHRED=yes JMM_RECORD_
T "./" "&make_default ; /bin/sh -c 'JMM_INITIALIZE=yes JMM_SHRED=yes JMM_RECORD_ALLOCATED=yes JMM_PRINT_EXIT_STATS=yes ./$program --debug-ftc-vert' ; echo \"Remaining memory should be 1008 bytes\"" "" FFFTTFF "Debug FTC Vert" "Shift-F11" "" T
T "./" "testjx -MDI" "" FFFFTTF "" "" "$svHlVFP`<D)#Cpy3gRi::CBCommandManager" F
T "./" "testjx --pseudotranslate" "" FFFFTTF "" "" "$svHlVFP`<D)#Cpy3gRi::CBCommandManager" F
T "./" "make jx.test.skip=true -kw" "" TFTTTFF "" "" "" F
F
# build settings
1
Expand Down

0 comments on commit 5571036

Please sign in to comment.