@@ -6,17 +6,119 @@ class Converters {
6
6
//then adds the hexstring to the list
7
7
//thus generating the hex value of the message
8
8
static List <String > messageTohex (String message) {
9
+ DataToByteArrayConverter converter = DataToByteArrayConverter ();
9
10
List <String > messages = [];
10
11
int i = 0 ;
11
12
while (i < message.length) {
12
13
var ch = message[i];
13
- logger.d ("ch = $ch " );
14
- if (charCodes.containsKey (ch)) {
15
- messages.add (charCodes[ch]! );
14
+ if (converter.charCodes.containsKey (ch)) {
15
+ messages.add (converter.charCodes[ch]! );
16
16
}
17
17
i++ ;
18
18
}
19
- logger.d ("message to hex = $message " );
20
19
return messages;
21
20
}
21
+
22
+ //function to convert the bitmap to the LED hex format
23
+ //it takes the 2D list of pixels and converts it to the LED hex format
24
+ static List <String > convertBitmapToLEDHex (List <List <int >> image) {
25
+ // Determine the height and width of the image
26
+ int height = image.length;
27
+ int width = image.isNotEmpty ? image[0 ].length : 0 ;
28
+
29
+ // Initialize variables to calculate padding and offsets
30
+ int finalSum = 0 ;
31
+
32
+ // Calculate and adjust for right-side padding
33
+ for (int j = 0 ; j < width; j++ ) {
34
+ int sum = 0 ;
35
+ for (int i = 0 ; i < height; i++ ) {
36
+ sum += image[i][j]; // Sum up pixel values in each column
37
+ }
38
+ if (sum == 0 ) {
39
+ // If column sum is zero, mark all pixels in that column as -1
40
+ for (int i = 0 ; i < height; i++ ) {
41
+ image[i][j] = - 1 ;
42
+ }
43
+ } else {
44
+ // Otherwise, update finalSum and exit loop
45
+ finalSum += j;
46
+ break ;
47
+ }
48
+ }
49
+
50
+ // Calculate and adjust for left-side padding
51
+ for (int j = width - 1 ; j >= 0 ; j-- ) {
52
+ int sum = 0 ;
53
+ for (int i = 0 ; i < height; i++ ) {
54
+ sum += image[i]
55
+ [j]; // Sum up pixel values in each column (from right to left)
56
+ }
57
+ if (sum == 0 ) {
58
+ // If column sum is zero, mark all pixels in that column as -1
59
+ for (int i = 0 ; i < height; i++ ) {
60
+ image[i][j] = - 1 ;
61
+ }
62
+ } else {
63
+ // Otherwise, update finalSum and exit loop
64
+ finalSum += (height - j - 1 );
65
+ break ;
66
+ }
67
+ }
68
+
69
+ // Calculate padding difference to align height to a multiple of 8
70
+ int diff = 0 ;
71
+ if ((height - finalSum) % 8 > 0 ) {
72
+ diff = 8 - (height - finalSum) % 8 ;
73
+ }
74
+
75
+ // Calculate left and right offsets for padding
76
+ int rOff = (diff / 2 ).floor ();
77
+ int lOff = (diff / 2 ).ceil ();
78
+
79
+ // Initialize a new list to accommodate the padded image
80
+ List <List <int >> list =
81
+ List .generate (height, (i) => List .filled (width + rOff + lOff, 0 ));
82
+
83
+ // Fill the new list with the padded image data
84
+ for (int i = 0 ; i < height; i++ ) {
85
+ int k = 0 ;
86
+ for (int j = 0 ; j < rOff; j++ ) {
87
+ list[i][k++ ] = 0 ; // Fill right-side padding
88
+ }
89
+ for (int j = 0 ; j < width; j++ ) {
90
+ if (image[i][j] != - 1 ) {
91
+ list[i][k++ ] = image[i][j]; // Copy non-padded pixels
92
+ }
93
+ }
94
+ for (int j = 0 ; j < lOff; j++ ) {
95
+ list[i][k++ ] = 0 ; // Fill left-side padding
96
+ }
97
+ }
98
+
99
+ // Convert each 8-bit segment into hexadecimal strings
100
+ List <String > allHexs = [];
101
+ for (int i = 0 ; i < list[0 ].length ~ / 8 ; i++ ) {
102
+ StringBuffer lineHex = StringBuffer ();
103
+
104
+ for (int k = 0 ; k < height; k++ ) {
105
+ StringBuffer stBuilder = StringBuffer ();
106
+
107
+ // Construct 8-bit segments for each row
108
+ for (int j = i * 8 ; j < i * 8 + 8 ; j++ ) {
109
+ stBuilder.write (list[k][j]);
110
+ }
111
+
112
+ // Convert binary string to hexadecimal
113
+ String hex = int .parse (stBuilder.toString (), radix: 2 )
114
+ .toRadixString (16 )
115
+ .padLeft (2 , '0' );
116
+ lineHex.write (hex); // Append hexadecimal to line
117
+ }
118
+
119
+ allHexs.add (lineHex.toString ()); // Store completed hexadecimal line
120
+ }
121
+
122
+ return allHexs; // Return list of hexadecimal strings
123
+ }
22
124
}
0 commit comments