Skip to content

Commit

Permalink
added indexed scaling for Pixelcade and PUP
Browse files Browse the repository at this point in the history
  • Loading branch information
mkalkbrenner committed Apr 1, 2024
1 parent 4b6439a commit ed0b39c
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 26 deletions.
2 changes: 1 addition & 1 deletion platforms/android/arm64-v8a/external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CARGS_SHA=5949a20a926e902931de4a32adaad9f19c76f251
LIBZEDMD_SHA=42d95ed6f1fe2065ecbd247502d177d7e5eb7e4c
LIBSERUM_SHA=b69d2b436bc93570a2e7e78d0946cd3c43f7aed5
SOCKPP_SHA=e6c4688a576d95f42dd7628cefe68092f6c5cd0f
LIBPUPDMD_SHA=5ed99b7c02a3873ba2ef0bd582b6e9fcf8061222
LIBPUPDMD_SHA=8dedc8c81ded2f6b8ca4614752d395aae0332c6c

if [[ $(uname) == "Linux" ]]; then
NUM_PROCS=$(nproc)
Expand Down
2 changes: 1 addition & 1 deletion platforms/ios/arm64/external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CARGS_SHA=5949a20a926e902931de4a32adaad9f19c76f251
LIBZEDMD_SHA=42d95ed6f1fe2065ecbd247502d177d7e5eb7e4c
LIBSERUM_SHA=b69d2b436bc93570a2e7e78d0946cd3c43f7aed5
SOCKPP_SHA=e6c4688a576d95f42dd7628cefe68092f6c5cd0f
LIBPUPDMD_SHA=5ed99b7c02a3873ba2ef0bd582b6e9fcf8061222
LIBPUPDMD_SHA=8dedc8c81ded2f6b8ca4614752d395aae0332c6c

NUM_PROCS=$(sysctl -n hw.ncpu)

Expand Down
2 changes: 1 addition & 1 deletion platforms/linux/aarch64/external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CARGS_SHA=5949a20a926e902931de4a32adaad9f19c76f251
LIBZEDMD_SHA=42d95ed6f1fe2065ecbd247502d177d7e5eb7e4c
LIBSERUM_SHA=b69d2b436bc93570a2e7e78d0946cd3c43f7aed5
SOCKPP_SHA=e6c4688a576d95f42dd7628cefe68092f6c5cd0f
LIBPUPDMD_SHA=5ed99b7c02a3873ba2ef0bd582b6e9fcf8061222
LIBPUPDMD_SHA=8dedc8c81ded2f6b8ca4614752d395aae0332c6c

NUM_PROCS=$(nproc)

Expand Down
2 changes: 1 addition & 1 deletion platforms/linux/x64/external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CARGS_SHA=5949a20a926e902931de4a32adaad9f19c76f251
LIBZEDMD_SHA=42d95ed6f1fe2065ecbd247502d177d7e5eb7e4c
LIBSERUM_SHA=b69d2b436bc93570a2e7e78d0946cd3c43f7aed5
SOCKPP_SHA=e6c4688a576d95f42dd7628cefe68092f6c5cd0f
LIBPUPDMD_SHA=5ed99b7c02a3873ba2ef0bd582b6e9fcf8061222
LIBPUPDMD_SHA=8dedc8c81ded2f6b8ca4614752d395aae0332c6c

NUM_PROCS=$(nproc)

Expand Down
2 changes: 1 addition & 1 deletion platforms/macos/arm64/external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CARGS_SHA=5949a20a926e902931de4a32adaad9f19c76f251
LIBZEDMD_SHA=42d95ed6f1fe2065ecbd247502d177d7e5eb7e4c
LIBSERUM_SHA=b69d2b436bc93570a2e7e78d0946cd3c43f7aed5
SOCKPP_SHA=e6c4688a576d95f42dd7628cefe68092f6c5cd0f
LIBPUPDMD_SHA=5ed99b7c02a3873ba2ef0bd582b6e9fcf8061222
LIBPUPDMD_SHA=8dedc8c81ded2f6b8ca4614752d395aae0332c6c

NUM_PROCS=$(sysctl -n hw.ncpu)

Expand Down
2 changes: 1 addition & 1 deletion platforms/macos/x64/external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CARGS_SHA=5949a20a926e902931de4a32adaad9f19c76f251
LIBZEDMD_SHA=42d95ed6f1fe2065ecbd247502d177d7e5eb7e4c
LIBSERUM_SHA=b69d2b436bc93570a2e7e78d0946cd3c43f7aed5
SOCKPP_SHA=e6c4688a576d95f42dd7628cefe68092f6c5cd0f
LIBPUPDMD_SHA=5ed99b7c02a3873ba2ef0bd582b6e9fcf8061222
LIBPUPDMD_SHA=8dedc8c81ded2f6b8ca4614752d395aae0332c6c

NUM_PROCS=$(sysctl -n hw.ncpu)

Expand Down
2 changes: 1 addition & 1 deletion platforms/tvos/arm64/external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CARGS_SHA=5949a20a926e902931de4a32adaad9f19c76f251
LIBZEDMD_SHA=42d95ed6f1fe2065ecbd247502d177d7e5eb7e4c
LIBSERUM_SHA=b69d2b436bc93570a2e7e78d0946cd3c43f7aed5
SOCKPP_SHA=e6c4688a576d95f42dd7628cefe68092f6c5cd0f
LIBPUPDMD_SHA=5ed99b7c02a3873ba2ef0bd582b6e9fcf8061222
LIBPUPDMD_SHA=8dedc8c81ded2f6b8ca4614752d395aae0332c6c

NUM_PROCS=$(sysctl -n hw.ncpu)

Expand Down
2 changes: 1 addition & 1 deletion platforms/win/x64/external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CARGS_SHA=5949a20a926e902931de4a32adaad9f19c76f251
LIBZEDMD_SHA=42d95ed6f1fe2065ecbd247502d177d7e5eb7e4c
LIBSERUM_SHA=b69d2b436bc93570a2e7e78d0946cd3c43f7aed5
SOCKPP_SHA=e6c4688a576d95f42dd7628cefe68092f6c5cd0f
LIBPUPDMD_SHA=5ed99b7c02a3873ba2ef0bd582b6e9fcf8061222
LIBPUPDMD_SHA=8dedc8c81ded2f6b8ca4614752d395aae0332c6c

echo "Building libraries..."
echo " CARGS_SHA: ${CARGS_SHA}"
Expand Down
2 changes: 1 addition & 1 deletion platforms/win/x86/external.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CARGS_SHA=5949a20a926e902931de4a32adaad9f19c76f251
LIBZEDMD_SHA=42d95ed6f1fe2065ecbd247502d177d7e5eb7e4c
LIBSERUM_SHA=b69d2b436bc93570a2e7e78d0946cd3c43f7aed5
SOCKPP_SHA=e6c4688a576d95f42dd7628cefe68092f6c5cd0f
LIBPUPDMD_SHA=5ed99b7c02a3873ba2ef0bd582b6e9fcf8061222
LIBPUPDMD_SHA=8dedc8c81ded2f6b8ca4614752d395aae0332c6c

echo "Building libraries..."
echo " CARGS_SHA: ${CARGS_SHA}"
Expand Down
68 changes: 51 additions & 17 deletions src/DMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,11 +745,13 @@ void DMD::PixelcadeDMDThread()
{
if (++bufferPosition >= DMDUTIL_FRAME_BUFFER_SIZE) bufferPosition = 0;

// @todo scaling
if (m_pUpdateBufferQueue[bufferPosition]->width == 128 && m_pUpdateBufferQueue[bufferPosition]->height == 32 &&
(m_pUpdateBufferQueue[bufferPosition]->hasData || m_pUpdateBufferQueue[bufferPosition]->hasSegData))
if (m_pUpdateBufferQueue[bufferPosition]->hasData || m_pUpdateBufferQueue[bufferPosition]->hasSegData)
{
int length = m_pUpdateBufferQueue[bufferPosition]->width * m_pUpdateBufferQueue[bufferPosition]->height;
uint16_t width = m_pUpdateBufferQueue[bufferPosition]->width;
uint8_t height = m_pUpdateBufferQueue[bufferPosition]->height;
int length = width * height;
uint8_t depth = m_pUpdateBufferQueue[bufferPosition]->depth;

bool update = false;
if (m_pUpdateBufferQueue[bufferPosition]->depth != 24)
{
Expand All @@ -758,7 +760,8 @@ void DMD::PixelcadeDMDThread()
m_pUpdateBufferQueue[bufferPosition]->b);
}

if (m_pUpdateBufferQueue[bufferPosition]->mode == Mode::RGB24)
// @todo scaling / centering
if (m_pUpdateBufferQueue[bufferPosition]->mode == Mode::RGB24 && width == 128 && height == 32)
{
uint8_t rgb24Data[128 * 32 * 3];
AdjustRGB24Depth(m_pUpdateBufferQueue[bufferPosition]->data, rgb24Data, length, palette,
Expand All @@ -774,14 +777,16 @@ void DMD::PixelcadeDMDThread()
}
update = true;
}
else if (m_pUpdateBufferQueue[bufferPosition]->mode == Mode::RGB16)
// @todo scaling / centering
else if (m_pUpdateBufferQueue[bufferPosition]->mode == Mode::RGB16 && width == 128 && height == 32)
{
memcpy(rgb565Data, m_pUpdateBufferQueue[bufferPosition]->segData, 128 * 32 * sizeof(uint16_t));
update = true;
}
else
else if (m_pUpdateBufferQueue[bufferPosition]->mode == Mode::Data ||
m_pUpdateBufferQueue[bufferPosition]->mode == Mode::AlphaNumeric)
{
uint8_t renderBuffer[128 * 32];
uint8_t renderBuffer[256 * 64];

if (m_pUpdateBufferQueue[bufferPosition]->mode == Mode::Data)
{
Expand All @@ -805,6 +810,23 @@ void DMD::PixelcadeDMDThread()
m_pUpdateBufferQueue[bufferPosition]->width * m_pUpdateBufferQueue[bufferPosition]->height);
update = true;
}

if (update)
{
uint8_t scaledBuffer[128 * 32];
if (width == 128 && height == 32)
memcpy(scaledBuffer, renderBuffer, 128 * 32);
else if (width == 128 && height == 16)
FrameUtil::CenterIndexed(scaledBuffer, 128, 32, renderBuffer, 128, 16);
else if (width == 192 && height == 64)
FrameUtil::ScaleDownIndexed(scaledBuffer, 128, 32, renderBuffer, 192, 64);
else if (width == 256 && height == 64)
FrameUtil::ScaleDownIndexed(scaledBuffer, 128, 32, renderBuffer, 256, 64);
else
continue;

memcpy(renderBuffer, scaledBuffer, 128 * 32);
}
}
else if (m_pUpdateBufferQueue[bufferPosition]->mode == Mode::AlphaNumeric)
{
Expand Down Expand Up @@ -1332,33 +1354,45 @@ void DMD::PupDMDThread()
}
}

// @todo scaling/centering 128x16 and 192x64 or check how PUP deals with it.
if (m_pPUPDMD && m_pUpdateBufferQueue[bufferPosition]->width == 128 &&
m_pUpdateBufferQueue[bufferPosition]->height == 32 && m_pUpdateBufferQueue[bufferPosition]->hasData &&
if (m_pPUPDMD && m_pUpdateBufferQueue[bufferPosition]->hasData &&
m_pUpdateBufferQueue[bufferPosition]->mode == Mode::Data && m_pUpdateBufferQueue[bufferPosition]->depth != 24)
{
int length = m_pUpdateBufferQueue[bufferPosition]->width * m_pUpdateBufferQueue[bufferPosition]->height;
uint16_t width = m_pUpdateBufferQueue[bufferPosition]->width;
uint8_t height = m_pUpdateBufferQueue[bufferPosition]->height;
int length = width * height;

if (memcmp(renderBuffer, m_pUpdateBufferQueue[bufferPosition]->data, length) != 0)
{
memcpy(renderBuffer, m_pUpdateBufferQueue[bufferPosition]->data, length);
uint8_t depth = m_pUpdateBufferQueue[bufferPosition]->depth;

uint8_t scaledBuffer[128 * 32];
if (width == 128 && height == 32)
memcpy(scaledBuffer, renderBuffer, 128 * 32);
else if (width == 128 && height == 16)
FrameUtil::CenterIndexed(scaledBuffer, 128, 32, renderBuffer, 128, 16);
else if (width == 192 && height == 64)
FrameUtil::ScaleDownPUP(scaledBuffer, 128, 32, renderBuffer, 192, 64);
else
return;

uint16_t triggerID = 0;
if (Config::GetInstance()->IsPUPExactColorMatch())
{
triggerID = m_pPUPDMD->MatchIndexed(renderBuffer);
triggerID = m_pPUPDMD->MatchIndexed(scaledBuffer, width, height);
}
else
{
UpdatePalette(palette, m_pUpdateBufferQueue[bufferPosition]->depth, m_pUpdateBufferQueue[bufferPosition]->r,
m_pUpdateBufferQueue[bufferPosition]->g, m_pUpdateBufferQueue[bufferPosition]->b);
// apply a standard orange palette
UpdatePalette(palette, depth, 255, 69, 0);

uint8_t* pFrame = (uint8_t*)malloc(length * 3);
for (uint16_t i = 0; i < length; i++)
{
uint16_t pos = renderBuffer[i] * 3;
uint16_t pos = scaledBuffer[i] * 3;
memcpy(&pFrame[i * 3], &palette[pos], 3);
}
triggerID = m_pPUPDMD->Match(pFrame, false);
triggerID = m_pPUPDMD->Match(pFrame, width, height, false);
free(pFrame);
}

Expand Down
124 changes: 124 additions & 0 deletions src/FrameUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cstring>
#include <iomanip>
#include <sstream>
#include <vector>

namespace DMDUtil
{
Expand Down Expand Up @@ -230,4 +231,127 @@ std::string FrameUtil::HexDump(const uint8_t* data, size_t size)
return ss.str();
}

void FrameUtil::ScaleDownIndexed(uint8_t* pDestFrame, const uint8_t destWidth, const uint8_t destHeight,
const uint8_t* pSrcFrame, const uint16_t srcWidth, const uint8_t srcHeight)
{
memset(pDestFrame, 0, destWidth * destHeight);
uint8_t xOffset = (destWidth - (srcWidth / 2)) / 2;
uint8_t yOffset = (destHeight - (srcHeight / 2)) / 2;

// for half scaling we take the 4 points and look if there is one color repeated
for (uint8_t y = 0; y < srcHeight; y += 2)
{
std::vector<uint8_t> row;
row.reserve(srcWidth / 2);

for (uint16_t x = 0; x < srcWidth; x += 2)
{
uint8_t upper_left = pSrcFrame[y * srcWidth + x];
uint8_t upper_right = pSrcFrame[y * srcWidth + x + 1];
uint8_t lower_left = pSrcFrame[(y + 1) * srcWidth + x];
uint8_t lower_right = pSrcFrame[(y + 1) * srcWidth + x + 1];

if (x < srcWidth / 2)
{
if (y < srcHeight / 2)
{
if (upper_left == upper_right || upper_left == lower_left || upper_left == lower_right)
row.push_back(upper_left);
else if (upper_right == lower_left || upper_right == lower_right)
row.push_back(upper_right);
else if (lower_left == lower_right)
row.push_back(lower_left);
else
row.push_back(upper_left);
}
else
{
if (lower_left == lower_right || lower_left == upper_left || lower_left == upper_right)
row.push_back(lower_right);
else if (lower_right == upper_left || lower_right == upper_right)
row.push_back(lower_right);
else if (upper_left == upper_right)
row.push_back(upper_left);
else
row.push_back(lower_left);
}
}
else
{
if (y < srcHeight / 2)
{
if (upper_right == upper_left || upper_right == lower_right || upper_right == lower_left)
row.push_back(upper_right);
else if (upper_left == lower_right || upper_left == lower_left)
row.push_back(upper_left);
else if (lower_right == lower_left)
row.push_back(lower_right);
else
row.push_back(upper_right);
}
else
{
if (lower_right == lower_left || lower_right == upper_right || lower_right == upper_left)
row.push_back(lower_right);
else if (lower_left == upper_right || lower_left == upper_left)
row.push_back(lower_left);
else if (upper_right == upper_left)
row.push_back(upper_right);
else
row.push_back(lower_right);
}
}
}

memcpy(&pDestFrame[(yOffset + (y / 2)) * destWidth + xOffset], row.data(), srcWidth / 2);
}
}

void FrameUtil::ScaleDownPUP(uint8_t* pDestFrame, const uint8_t destWidth, const uint8_t destHeight,
const uint8_t* pSrcFrame, const uint8_t srcWidth, const uint8_t srcHeight)
{
memset(pDestFrame, 0, destWidth * destHeight);
uint8_t xOffset = (destWidth - (srcWidth / 2)) / 2;
uint8_t yOffset = (destHeight - (srcHeight / 2)) / 2;

// for half scaling we take the 4 points and look if there is one color repeated
for (uint8_t y = 0; y < srcHeight; y += 2)
{
std::vector<uint8_t> row;
row.reserve(srcWidth / 2);

for (uint8_t x = 0; x < srcWidth; x += 2)
{
uint8_t pixel1 = pSrcFrame[y * srcWidth + x];
uint8_t pixel2 = pSrcFrame[y * srcWidth + x + 1];
uint8_t pixel3 = pSrcFrame[(y + 1) * srcWidth + x];
uint8_t pixel4 = pSrcFrame[(y + 1) * srcWidth + x + 1];

if (pixel1 == pixel2 || pixel1 == pixel3 || pixel1 == pixel4)
row.push_back(pixel1);
else if (pixel2 == pixel3 || pixel2 == pixel4)
row.push_back(pixel2);
else if (pixel3 == pixel4)
row.push_back(pixel3);
else
row.push_back(pixel1);
}

memcpy(&pDestFrame[(yOffset + (y / 2)) * destWidth + xOffset], row.data(), srcWidth / 2);
}
}

void FrameUtil::CenterIndexed(uint8_t* pDestFrame, const uint8_t destWidth, const uint8_t destHeight,
const uint8_t* pSrcFrame, const uint8_t srcWidth, const uint8_t srcHeight)
{
memset(pDestFrame, 0, destWidth * destHeight);
uint8_t xOffset = (destWidth - srcWidth) / 2;
uint8_t yOffset = (destHeight - srcHeight) / 2;

for (uint8_t y = 0; y < srcHeight; y++)
{
memcpy(&pDestFrame[(yOffset + y) * destWidth + xOffset], &pSrcFrame[y * srcWidth], srcWidth);
}
}

} // namespace DMDUtil
6 changes: 6 additions & 0 deletions src/FrameUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class FrameUtil
int destHeight);
static float CalcBrightness(float x);
static std::string HexDump(const uint8_t* data, size_t size);
static void ScaleDownIndexed(uint8_t* pDestFrame, const uint8_t destWidth, const uint8_t destHeight,
const uint8_t* pSrcFrame, const uint16_t srcWidth, const uint8_t srcHeight);
static void ScaleDownPUP(uint8_t* pDestFrame, const uint8_t destWidth, const uint8_t destHeight,
const uint8_t* pSrcFrame, const uint8_t srcWidth, const uint8_t srcHeight);
static void CenterIndexed(uint8_t* pDestFrame, const uint8_t destWidth, const uint8_t destHeight,
const uint8_t* pSrcFrame, const uint8_t srcWidth, const uint8_t srcHeight);
};

} // namespace DMDUtil

0 comments on commit ed0b39c

Please sign in to comment.