1
+ // Licensed to the .NET Foundation under one or more agreements.
2
+ // The .NET Foundation licenses this file to you under the MIT license.
3
+
4
+ using System ;
5
+ using System . Drawing ;
6
+
7
+ namespace Iot . Device . Ws28xx . Esp32
8
+ {
9
+ /// <summary>
10
+ /// Special 24bit RGB format for Neo pixel LEDs where each bit is converted to 3 bits.
11
+ /// A one is converted to 110, a zero is converted to 100.
12
+ /// </summary>
13
+ internal class BitmapImageNeo3 : BitmapImage
14
+ {
15
+ protected const int BytesPerComponent = 3 ;
16
+ protected const int BytesPerPixel = BytesPerComponent * 3 ;
17
+
18
+ public BitmapImageNeo3 ( int width , int height )
19
+ : base ( new byte [ width * height * BytesPerPixel ] , width , height , width * BytesPerPixel )
20
+ {
21
+ }
22
+
23
+ public override void SetPixel ( int x , int y , Color c )
24
+ {
25
+ var offset = y * Stride + x * BytesPerPixel ;
26
+ Data [ offset ++ ] = _lookup [ c . G * BytesPerComponent + 0 ] ;
27
+ Data [ offset ++ ] = _lookup [ c . G * BytesPerComponent + 1 ] ;
28
+ Data [ offset ++ ] = _lookup [ c . G * BytesPerComponent + 2 ] ;
29
+ Data [ offset ++ ] = _lookup [ c . R * BytesPerComponent + 0 ] ;
30
+ Data [ offset ++ ] = _lookup [ c . R * BytesPerComponent + 1 ] ;
31
+ Data [ offset ++ ] = _lookup [ c . R * BytesPerComponent + 2 ] ;
32
+ Data [ offset ++ ] = _lookup [ c . B * BytesPerComponent + 0 ] ;
33
+ Data [ offset ++ ] = _lookup [ c . B * BytesPerComponent + 1 ] ;
34
+ Data [ offset ++ ] = _lookup [ c . B * BytesPerComponent + 2 ] ;
35
+ }
36
+
37
+ public override void Clear ( int x , int y )
38
+ {
39
+ var offset = y * Stride + x * BytesPerPixel ;
40
+ Array . Clear ( Data , offset , 9 ) ;
41
+ }
42
+
43
+ public override void Clear ( )
44
+ {
45
+ Array . Clear ( Data , 0 , Data . Length ) ;
46
+ }
47
+
48
+ public override void SetPixel ( int x , int y , byte r , byte g , byte b )
49
+ {
50
+ var offset = y * Stride + x * BytesPerPixel ;
51
+ Data [ offset ++ ] = _lookup [ g * BytesPerComponent + 0 ] ;
52
+ Data [ offset ++ ] = _lookup [ g * BytesPerComponent + 1 ] ;
53
+ Data [ offset ++ ] = _lookup [ g * BytesPerComponent + 2 ] ;
54
+ Data [ offset ++ ] = _lookup [ r * BytesPerComponent + 0 ] ;
55
+ Data [ offset ++ ] = _lookup [ r * BytesPerComponent + 1 ] ;
56
+ Data [ offset ++ ] = _lookup [ r * BytesPerComponent + 2 ] ;
57
+ Data [ offset ++ ] = _lookup [ b * BytesPerComponent + 0 ] ;
58
+ Data [ offset ++ ] = _lookup [ b * BytesPerComponent + 1 ] ;
59
+ Data [ offset ++ ] = _lookup [ b * BytesPerComponent + 2 ] ;
60
+ }
61
+
62
+ protected static readonly byte [ ] _lookup = new byte [ 256 * BytesPerComponent ] ;
63
+
64
+ static BitmapImageNeo3 ( )
65
+ {
66
+ for ( int i = 0 ; i < 256 ; i ++ )
67
+ {
68
+ int data = 0 ;
69
+ for ( int j = 7 ; j >= 0 ; j -- )
70
+ {
71
+ data = ( data << 3 ) | 0b100 | ( ( i >> j ) << 1 ) & 2 ;
72
+ }
73
+
74
+ _lookup [ i * BytesPerComponent + 0 ] = unchecked ( ( byte ) ( data >> 16 ) ) ;
75
+ _lookup [ i * BytesPerComponent + 1 ] = unchecked ( ( byte ) ( data >> 8 ) ) ;
76
+ _lookup [ i * BytesPerComponent + 2 ] = unchecked ( ( byte ) ( data >> 0 ) ) ;
77
+ }
78
+ }
79
+ }
80
+ }
0 commit comments