Skip to content

Commit f058023

Browse files
committed
pwm: Add gray8 -> gray4/2 conversion.
1 parent d694d44 commit f058023

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

LibDmd/Common/FrameUtil.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,20 @@ public static byte[] ConvertGrayToGray(byte[] srcFrame, params byte[] mapping)
513513
}
514514
}
515515

516+
public static byte[] ConvertGray8ToGray(byte[] srcFrame, int bitLength)
517+
{
518+
using (Profiler.Start("FrameUtil.ConvertGray8ToGray")) {
519+
var numDestColors = Math.Pow(2, bitLength);
520+
var factor = 256 / numDestColors;
521+
522+
var destFrame = new byte[srcFrame.Length];
523+
for (var i = 0; i < destFrame.Length; i++) {
524+
destFrame[i] = (byte)Math.Floor(srcFrame[i] / factor);
525+
}
526+
return destFrame;
527+
}
528+
}
529+
516530
public static byte[] NewPlane(Dimensions dim)
517531
{
518532
var count = dim.Width / 8 * dim.Height;

LibDmd/Frame/DmdFrame.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ public DmdFrame ConvertToGray2()
248248
return Update(FrameUtil.ConvertGrayToGray(Data, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x1, 0x1, 0x2, 0x2, 0x2, 0x2, 0x3, 0x3, 0x3, 0x3), 2);
249249
case 6:
250250
return Update(FrameUtil.ConvertGrayToGray(Data, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3), 2);
251+
case 8:
252+
return Update(FrameUtil.ConvertGray8ToGray(Data, 2), 2);
251253
case 16:
252254
return Update(ImageUtil.ConvertRgb565ToGray(Dimensions, Data, 4), 2);
253255
case 24:
@@ -275,6 +277,8 @@ public DmdFrame ConvertToGray4()
275277
throw new ArgumentException("Frame is already gray4.");
276278
case 6:
277279
return Update(FrameUtil.ConvertGrayToGray(Data, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x1, 0x1, 0x2, 0x2, 0x2, 0x2, 0x3, 0x3, 0x3, 0x3, 0x4, 0x4, 0x4, 0x4, 0x5, 0x5, 0x5, 0x5, 0x6, 0x6, 0x6, 0x6, 0x7, 0x7, 0x7, 0x7, 0x8, 0x8, 0x8, 0x8, 0x9, 0x9, 0x9, 0x9, 0xa, 0xa, 0xa, 0xa, 0xb, 0xb, 0xb, 0xb, 0xc, 0xc, 0xc, 0xc, 0xd, 0xd, 0xd, 0xd, 0xe, 0xe, 0xe, 0xe, 0xf, 0xf, 0xf, 0xf), 4);
280+
case 8:
281+
return Update(FrameUtil.ConvertGray8ToGray(Data, 4), 4);
278282
case 16:
279283
return Update(ImageUtil.ConvertRgb565ToGray(Dimensions, Data, 16), 4);
280284
case 24:

LibDmd/RenderGraph.cs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,11 +588,16 @@ public IDisposable StartRendering(Action onCompleted, Action<Exception> onError
588588
Connect(Source, dest, FrameFormat.Gray8, FrameFormat.Rgb565);
589589
continue;
590590
}
591-
// gray4 -> rgb24
591+
// gray8 -> rgb24
592592
if (sourceGray8 != null && destRgb24 != null) {
593593
Connect(Source, dest, FrameFormat.Gray8, FrameFormat.Rgb24);
594594
continue;
595595
}
596+
// gray8 -> bitmap
597+
if (sourceGray8 != null && destBitmap != null) {
598+
Connect(Source, dest, FrameFormat.Gray8, FrameFormat.Bitmap);
599+
continue;
600+
}
596601
// rgb565 -> rgb24
597602
if (sourceRgb565 != null && destRgb24 != null) {
598603
Connect(Source, dest, FrameFormat.Rgb565, FrameFormat.Rgb24);
@@ -650,6 +655,16 @@ public IDisposable StartRendering(Action onCompleted, Action<Exception> onError
650655
Connect(Source, dest, FrameFormat.Gray4, FrameFormat.Gray2);
651656
continue;
652657
}
658+
// gray8 -> gray4
659+
if (sourceGray8 != null && destGray4 != null) {
660+
Connect(Source, dest, FrameFormat.Gray8, FrameFormat.Gray4);
661+
continue;
662+
}
663+
// gray8 -> gray2
664+
if (sourceGray8 != null && destGray2 != null) {
665+
Connect(Source, dest, FrameFormat.Gray8, FrameFormat.Gray2);
666+
continue;
667+
}
653668
// colored gray6 -> rgb565
654669
if (sourceColoredGray6 != null && destRgb565 != null) {
655670
Connect(Source, dest, FrameFormat.ColoredGray6, FrameFormat.Rgb565);
@@ -960,6 +975,30 @@ private void Connect(ISource source, IDestination dest, FrameFormat from, FrameF
960975
var sourceGray8 = source as IGray8Source;
961976
switch (to) {
962977

978+
// gray8 -> gray4
979+
case FrameFormat.Gray4:
980+
AssertCompatibility(source, sourceGray8, dest, destGray4, from, to);
981+
Subscribe(
982+
sourceGray8.GetGray8Frames(!dest.NeedsDuplicateFrames),
983+
frame => frame
984+
.TransformHdScaling(destFixedSize, ScalerMode)
985+
.ConvertToGray4()
986+
.TransformGray(this, destFixedSize, destMultiSize),
987+
destGray4.RenderGray4);
988+
break;
989+
990+
// gray8 -> gray2
991+
case FrameFormat.Gray2:
992+
AssertCompatibility(source, sourceGray8, dest, destGray2, from, to);
993+
Subscribe(
994+
sourceGray8.GetGray8Frames(!dest.NeedsDuplicateFrames),
995+
frame => frame
996+
.TransformHdScaling(destFixedSize, ScalerMode)
997+
.ConvertToGray2()
998+
.TransformGray(this, destFixedSize, destMultiSize),
999+
destGray2.RenderGray2);
1000+
break;
1001+
9631002
// gray8 -> gray8
9641003
case FrameFormat.Gray8:
9651004
AssertCompatibility(source, sourceGray8, dest, destGray8, from, to);
@@ -984,7 +1023,7 @@ private void Connect(ISource source, IDestination dest, FrameFormat from, FrameF
9841023
);
9851024
break;
9861025

987-
// gray4 -> rgb24
1026+
// gray8 -> rgb24
9881027
case FrameFormat.Rgb24:
9891028
AssertCompatibility(source, sourceGray8, dest, destRgb24, from, to);
9901029
Subscribe(
@@ -996,6 +1035,19 @@ private void Connect(ISource source, IDestination dest, FrameFormat from, FrameF
9961035
destRgb24.RenderRgb24);
9971036
break;
9981037

1038+
// gray8 -> bitmap
1039+
case FrameFormat.Bitmap:
1040+
AssertCompatibility(source, sourceGray8, dest, destBitmap, from, to);
1041+
Subscribe(
1042+
sourceGray8.GetGray8Frames(!dest.NeedsDuplicateFrames),
1043+
frame => frame
1044+
.TransformHdScaling(destFixedSize, ScalerMode)
1045+
.ConvertGrayToBmp(_gray8Palette ?? _gray8Colors)
1046+
.Transform(this, destFixedSize, destMultiSize),
1047+
destBitmap.RenderBitmap);
1048+
break;
1049+
1050+
9991051
default:
10001052
throw new ArgumentOutOfRangeException(nameof(to), to, null);
10011053
}

0 commit comments

Comments
 (0)