Skip to content

Commit 6a33d0d

Browse files
committed
added output filter support
1 parent a3c9809 commit 6a33d0d

8 files changed

Lines changed: 157 additions & 5 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ set(DMDUTIL_SOURCES
9999
src/DMD.cpp
100100
src/LevelDMD.cpp
101101
src/RGB24DMD.cpp
102+
src/OutputFilters.cpp
102103
src/ConsoleDMD.cpp
103104
src/Logger.cpp
104105
src/AlphaNumeric.cpp

include/DMDUtil/Config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class DMDUTILAPI Config
9191
{
9292
m_filterTransitionalFrames = filterTransitionalFrames;
9393
}
94+
int GetRoundedCorners() const { return m_roundedCorners; }
95+
void SetRoundedCorners(int roundedCorners) { m_roundedCorners = roundedCorners; }
9496
bool IsZeDMD() const { return m_zedmd; }
9597
void SetZeDMD(bool zedmd) { m_zedmd = zedmd; }
9698
const char* GetZeDMDDevice() const { return m_zedmdDevice.c_str(); }
@@ -168,6 +170,7 @@ class DMDUTILAPI Config
168170
std::string m_dumpPath;
169171
bool m_dumpZip;
170172
bool m_filterTransitionalFrames;
173+
int m_roundedCorners;
171174
bool m_zedmd;
172175
std::string m_zedmdDevice;
173176
bool m_zedmdDebug;

src/Config.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Config::Config()
4141
m_dumpFrames = false;
4242
m_dumpZip = false;
4343
m_filterTransitionalFrames = false;
44+
m_roundedCorners = 0;
4445
m_zedmd = true;
4546
m_zedmdDevice.clear();
4647
m_zedmdDebug = false;
@@ -381,6 +382,16 @@ void Config::parseConfigFile(const char* path)
381382
{
382383
SetFilterTransitionalFrames(false);
383384
}
385+
386+
// OutputFilters
387+
try
388+
{
389+
SetRoundedCorners(r.Get<int>("OutputFilters", "RoundedCorners", 0));
390+
}
391+
catch (const std::exception&)
392+
{
393+
SetRoundedCorners(0);
394+
}
384395
}
385396

386397
} // namespace DMDUtil

src/DMD.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "AlphaNumeric.h"
4343
#include "FrameUtil.h"
4444
#include "DMDUtil/Logger.h"
45+
#include "OutputFilters.h"
4546
#include "TimeUtils.h"
4647
#include "ZeDMD.h"
4748
#include "komihash/komihash.h"
@@ -1237,6 +1238,7 @@ void DMD::ZeDMDThread()
12371238
Config* const pConfig = Config::GetInstance();
12381239
bool showNotColorizedFrames = pConfig->IsShowNotColorizedFrames();
12391240
bool excludeColorizedFrames = pConfig->IsExcludeColorizedFramesForZeDMD();
1241+
const int roundedCorners = pConfig->GetRoundedCorners();
12401242

12411243
while (true)
12421244
{
@@ -1332,12 +1334,16 @@ void DMD::ZeDMDThread()
13321334

13331335
AdjustRGB24Depth(m_pUpdateBufferQueue[bufferPositionMod]->data, rgb24Data, (size_t)width * height, palette,
13341336
m_pUpdateBufferQueue[bufferPositionMod]->depth);
1337+
ApplyRoundedCornersRGB24(rgb24Data, width, height, roundedCorners);
13351338
m_pZeDMD->RenderRgb888(rgb24Data);
13361339
}
13371340
else if (m_pUpdateBufferQueue[bufferPositionMod]->mode == Mode::RGB16 ||
13381341
(m_pSerum && IsSerumV2Mode(m_pUpdateBufferQueue[bufferPositionMod]->mode)))
13391342
{
1340-
m_pZeDMD->RenderRgb565(m_pUpdateBufferQueue[bufferPositionMod]->segData);
1343+
uint16_t rgb565Data[256 * 64];
1344+
memcpy(rgb565Data, m_pUpdateBufferQueue[bufferPositionMod]->segData, (size_t)frameSize * sizeof(uint16_t));
1345+
ApplyRoundedCornersRGB565(rgb565Data, width, height, roundedCorners);
1346+
m_pZeDMD->RenderRgb565(rgb565Data);
13411347
}
13421348
else
13431349
{
@@ -1397,7 +1403,11 @@ void DMD::ZeDMDThread()
13971403
}
13981404
}
13991405

1400-
if (update) m_pZeDMD->RenderRgb888(renderBuffer);
1406+
if (update)
1407+
{
1408+
ApplyRoundedCornersRGB24(renderBuffer, width, height, roundedCorners);
1409+
m_pZeDMD->RenderRgb888(renderBuffer);
1410+
}
14011411
}
14021412
}
14031413
}
@@ -2081,6 +2091,7 @@ void DMD::PIN2DMDThread()
20812091
Config* const pConfig = Config::GetInstance();
20822092
bool showNotColorizedFrames = pConfig->IsShowNotColorizedFrames();
20832093
bool excludeColorizedFrames = pConfig->IsExcludeColorizedFramesForPIN2DMD();
2094+
const int roundedCorners = pConfig->GetRoundedCorners();
20842095

20852096
auto scaleToTarget = [&](const uint8_t* src, uint16_t width, uint16_t height, uint8_t* dst) -> bool
20862097
{
@@ -2267,6 +2278,7 @@ void DMD::PIN2DMDThread()
22672278

22682279
if (update && scaleToTarget(rgb24Data, width, height, scaledBuffer))
22692280
{
2281+
ApplyRoundedCornersRGB24(scaledBuffer, targetWidth, targetHeight, roundedCorners);
22702282
PIN2DMDRenderRaw(targetWidth, targetHeight, scaledBuffer, 1);
22712283
}
22722284
}
@@ -2292,6 +2304,7 @@ void DMD::PixelcadeDMDThread()
22922304
Config* const pConfig = Config::GetInstance();
22932305
bool showNotColorizedFrames = pConfig->IsShowNotColorizedFrames();
22942306
bool excludeColorizedFrames = pConfig->IsExcludeColorizedFramesForPixelcade();
2307+
const int roundedCorners = pConfig->GetRoundedCorners();
22952308

22962309
while (true)
22972310
{
@@ -2357,6 +2370,7 @@ void DMD::PixelcadeDMDThread()
23572370

23582371
if (m_pPixelcadeDMD->GetIsV2())
23592372
{
2373+
ApplyRoundedCornersRGB24(scaledBuffer, targetWidth, targetHeight, roundedCorners);
23602374
m_pPixelcadeDMD->UpdateRGB24(scaledBuffer);
23612375
}
23622376
else
@@ -2476,7 +2490,11 @@ void DMD::PixelcadeDMDThread()
24762490
}
24772491
}
24782492

2479-
if (update) m_pPixelcadeDMD->Update(rgb565Data);
2493+
if (update)
2494+
{
2495+
ApplyRoundedCornersRGB565(rgb565Data, targetWidth, targetHeight, roundedCorners);
2496+
m_pPixelcadeDMD->Update(rgb565Data);
2497+
}
24802498
}
24812499
}
24822500
}

src/OutputFilters.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include "OutputFilters.h"
2+
3+
#include <algorithm>
4+
#include <cstring>
5+
6+
namespace DMDUtil
7+
{
8+
9+
namespace
10+
{
11+
int ClampCornerRadius(uint16_t width, uint16_t height, int radius)
12+
{
13+
if (radius <= 0 || width == 0 || height == 0)
14+
{
15+
return 0;
16+
}
17+
18+
return std::min<int>(radius, std::min<int>(width, height) / 2);
19+
}
20+
} // namespace
21+
22+
void ApplyRoundedCornersRGB24(uint8_t* pData, uint16_t width, uint16_t height, int radius)
23+
{
24+
if (pData == nullptr)
25+
{
26+
return;
27+
}
28+
29+
const int maxRadius = ClampCornerRadius(width, height, radius);
30+
if (maxRadius <= 0)
31+
{
32+
return;
33+
}
34+
35+
const float circleRadius = static_cast<float>(maxRadius);
36+
for (int y = 0; y < maxRadius; ++y)
37+
{
38+
for (int x = 0; x < maxRadius; ++x)
39+
{
40+
const float dx = circleRadius - (static_cast<float>(x) + 0.5f);
41+
const float dy = circleRadius - (static_cast<float>(y) + 0.5f);
42+
if (dx * dx + dy * dy <= circleRadius * circleRadius)
43+
{
44+
continue;
45+
}
46+
47+
const size_t topLeft = ((size_t)y * width + x) * 3u;
48+
const size_t topRight = ((size_t)y * width + (width - 1 - x)) * 3u;
49+
const size_t bottomLeft = (((size_t)height - 1 - y) * width + x) * 3u;
50+
const size_t bottomRight = (((size_t)height - 1 - y) * width + (width - 1 - x)) * 3u;
51+
52+
memset(pData + topLeft, 0, 3);
53+
memset(pData + topRight, 0, 3);
54+
memset(pData + bottomLeft, 0, 3);
55+
memset(pData + bottomRight, 0, 3);
56+
}
57+
}
58+
}
59+
60+
void ApplyRoundedCornersRGB565(uint16_t* pData, uint16_t width, uint16_t height, int radius)
61+
{
62+
if (pData == nullptr)
63+
{
64+
return;
65+
}
66+
67+
const int maxRadius = ClampCornerRadius(width, height, radius);
68+
if (maxRadius <= 0)
69+
{
70+
return;
71+
}
72+
73+
const float circleRadius = static_cast<float>(maxRadius);
74+
for (int y = 0; y < maxRadius; ++y)
75+
{
76+
for (int x = 0; x < maxRadius; ++x)
77+
{
78+
const float dx = circleRadius - (static_cast<float>(x) + 0.5f);
79+
const float dy = circleRadius - (static_cast<float>(y) + 0.5f);
80+
if (dx * dx + dy * dy <= circleRadius * circleRadius)
81+
{
82+
continue;
83+
}
84+
85+
pData[(size_t)y * width + x] = 0;
86+
pData[(size_t)y * width + (width - 1 - x)] = 0;
87+
pData[((size_t)height - 1 - y) * width + x] = 0;
88+
pData[((size_t)height - 1 - y) * width + (width - 1 - x)] = 0;
89+
}
90+
}
91+
}
92+
93+
} // namespace DMDUtil

src/OutputFilters.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
5+
namespace DMDUtil
6+
{
7+
8+
void ApplyRoundedCornersRGB24(uint8_t* pData, uint16_t width, uint16_t height, int radius);
9+
void ApplyRoundedCornersRGB565(uint16_t* pData, uint16_t width, uint16_t height, int radius);
10+
11+
} // namespace DMDUtil

src/RGB24DMD.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#include <cstring>
55
#include <string>
66

7+
#include "DMDUtil/Config.h"
78
#include "FrameUtil.h"
9+
#include "OutputFilters.h"
810

911
namespace DMDUtil
1012
{
@@ -49,6 +51,11 @@ void RGB24DMD::Update(uint8_t* pData, uint16_t width, uint16_t height)
4951
FrameUtil::Helper::ScaleUp(m_pData, pData, width, height, 24);
5052
m_update = true;
5153
}
54+
55+
if (m_update)
56+
{
57+
ApplyRoundedCornersRGB24(m_pData, m_width, m_height, Config::GetInstance()->GetRoundedCorners());
58+
}
5259
}
5360

5461
uint8_t* RGB24DMD::GetData()

src/test.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,11 @@ void run(DMDUtil::DMD* pDmd)
164164
printf("Render raw: %s\n", filename);
165165
fileptr = fopen(filename, "rb");
166166
if (!fileptr) continue;
167-
fread(buffer, size, 1, fileptr);
167+
if (fread(buffer, size, 1, fileptr) != 1)
168+
{
169+
fclose(fileptr);
170+
continue;
171+
}
168172
fclose(fileptr);
169173

170174
memcpy(rgb565, buffer, size);
@@ -182,7 +186,11 @@ void run(DMDUtil::DMD* pDmd)
182186
printf("Render raw: %s\n", filename);
183187
fileptr = fopen(filename, "rb");
184188
if (!fileptr) continue;
185-
fread(buffer, size, 1, fileptr);
189+
if (fread(buffer, size, 1, fileptr) != 1)
190+
{
191+
fclose(fileptr);
192+
continue;
193+
}
186194
fclose(fileptr);
187195

188196
memcpy(rgb565, buffer, size);

0 commit comments

Comments
 (0)