]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/STLcd/bitmap2Struct.py
Stop requiring editing of example scripts
[kiibohd-controller.git] / Scan / STLcd / bitmap2Struct.py
1 #!/usr/bin/env python3
2
3 # Copyright (C) 2015 by Jacob Alexander
4 #
5 # This file is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This file is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this file.  If not, see <http://www.gnu.org/licenses/>.
17
18 # Imports
19 import sys
20
21 from array import *
22 from PIL import Image
23
24
25 # Convenience class to deal with converting images to a C array
26 class STLcdGraphic:
27         # Some constants for the LCD Driver
28         page_width = 8
29         page_max_length = 132
30
31         array('B')
32
33         def __init__( self, height, width ):
34                 self.height = height
35                 self.width  = width
36
37                 # Calculate number of pages
38                 self.page_count  = int( self.height / self.page_width )
39                 self.page_length = self.width
40
41                 # Initialize pages to 0's
42                 self.page_data = []
43                 for page in range( 0, self.page_count ):
44                         self.page_data.append( array( 'B', [0] * self.page_length ) )
45
46         def setpixel( self, x, y ):
47                 # Calculate which page
48                 page = int( ( self.height - y ) / self.page_width )
49
50                 if page == 4:
51                         print("YAR", (x,y))
52
53                 # Calculate which byte
54                 byte = x
55
56                 # Calculate which bit
57                 bit = int( ( self.height - y ) % self.page_width )
58
59                 # Set pixel bit
60                 self.page_data[ page ][ byte ] |= (1 << bit)
61
62         def getpage( self, page ):
63                 return self.page_data[ page ]
64
65         def getarray( self ):
66                 struct = "{\n"
67
68                 for page in range( 0, self.page_count ):
69                         for elem in self.page_data[ page ]:
70                                 struct += "0x{0:02x}, ".format( elem )
71
72                         if page != self.page_count - 1:
73                                 struct += "\n"
74
75                 struct += "\n}"
76
77                 return struct
78
79         # Prints out what the image will look like on the display
80         def preview( self ):
81                 # Top border first
82                 display = "+"
83                 for pixel in range( 0, self.width ):
84                         display += "-"
85                 display += "+\n"
86
87                 # Each Page
88                 for page in range( self.page_count - 1, -1, -1 ):
89                         # Each Bit (Line)
90                         for line in range( 7, -1, -1 ):
91                                 # Border
92                                 display += "|"
93
94                                 # Each Byte (Column/Pixel)
95                                 for byte in range( 0, self.width ):
96                                         if self.page_data[ page ][ byte ] & (1 << line):
97                                                 display += "*"
98                                         else:
99                                                 display += " "
100
101                                 # Border
102                                 display += "|\n"
103
104                 # Bottom border
105                 display += "+"
106                 for pixel in range( 0, self.width ):
107                         display += "-"
108                 display += "+\n"
109
110                 return display
111
112
113 filename = sys.argv[1]
114 if filename is None:
115         print( "You must specify a bitmap filename. Try './bitmap2Struct.py ic_logo_lcd.bmp'" )
116         sys.exit( 1 )
117 max_height = 32
118 max_width = 128
119 x_offset = 0
120 y_offset = 0
121 output_image = STLcdGraphic( max_height, max_width )
122
123
124
125 # Load the input filename and convert to black & white
126 try:
127         input_image = Image.open( filename ).convert('1')
128 except:
129         print( "Unable to load image '{0}'".format( filename ) )
130
131 # Check the image size to see if within the bounds of the display
132 if input_image.size[0] > max_width or input_image.size[1] > max_height:
133         print( "ERROR: '{0}:{1}' is too large, must be no larger than {2}x{3}".format(
134                 filename,
135                 ( input_image.format, input_image.size, input_image.mode ),
136                 max_width,
137                 max_height )
138         )
139         sys.exit( 1 )
140
141 # Center the image
142 height_start = int( ( max_height - input_image.size[1] ) / 2 )
143 height_end   = int( height_start + input_image.size[1] )
144 width_start  = int( ( max_width - input_image.size[0] ) / 2 )
145 width_end    = int( width_start + input_image.size[0] )
146
147 #print( height_start )
148 #print( height_end )
149 #print( width_start )
150 #print( width_end )
151
152 # Iterate over all of the pixels
153 # Also prepare the debug view of the image (disp_test)
154 disp_test = "+"
155 for pixel in range( 0, max_width ):
156         disp_test += "-"
157 disp_test += "+\n"
158
159 for y in range( 0, max_height ):
160         disp_test += "|"
161
162         # Check if within height range
163         if not ( y >= height_start and y < height_end ):
164                 disp_test += " " * max_width + "|\n|"
165                 continue
166
167         for x in range( 0, max_width ):
168                 # Check if within width range
169                 if not ( x >= width_start and x < width_end ):
170                         disp_test += " "
171                         continue
172
173                 # Use image value to determine pixel
174                 try:
175                         if input_image.getpixel( (x - width_start, y - height_start) ) == 0:
176                                 disp_test += "*"
177                                 output_image.setpixel( x, y + 1 ) # +1 is due to page boundary
178                         else:
179                                 disp_test += " "
180                 except IndexError:
181                         print( (x - width_start,y - height_start) )
182                         pass
183
184         disp_test += "|\n"
185
186 disp_test += "+"
187 for pixel in range( 0, max_width ):
188         disp_test += "-"
189 disp_test += "+\n"
190
191 # BMP Conversion preview
192 print( disp_test )
193
194
195 print ( output_image.preview() )
196 #print ()
197 print( "uint8_t array[] = {0};".format( output_image.getarray() ) )
198