Mercurial > louis > kiibohd-controller
comparison Scan/STLcd/bitmap2Struct.py @ 332:2e0074f75855
Adding example logo to the lcdtest and bmp conversion script.
author | Jacob Alexander <haata@kiibohd.com> |
---|---|
date | Sat, 18 Apr 2015 11:24:15 -0700 |
parents | |
children | 63b1d003fd50 cba3cefc6e56 |
comparison
equal
deleted
inserted
replaced
331:9e31d92caf12 | 332:2e0074f75855 |
---|---|
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 = "ic_logo_lcd.bmp" | |
114 max_height = 32 | |
115 max_width = 128 | |
116 x_offset = 0 | |
117 y_offset = 0 | |
118 output_image = STLcdGraphic( max_height, max_width ) | |
119 | |
120 | |
121 | |
122 # Load the input filename and convert to black & white | |
123 try: | |
124 input_image = Image.open( filename ).convert('1') | |
125 except: | |
126 print( "Unable to load image '{0}'".format( filename ) ) | |
127 | |
128 # Check the image size to see if within the bounds of the display | |
129 if input_image.size[0] > max_width or input_image.size[1] > max_height: | |
130 print( "ERROR: '{0}:{1}' is too large, must be no larger than {2}x{3}".format( | |
131 filename, | |
132 ( input_image.format, input_image.size, input_image.mode ), | |
133 max_width, | |
134 max_height ) | |
135 ) | |
136 sys.exit( 1 ) | |
137 | |
138 # Center the image | |
139 height_start = int( ( max_height - input_image.size[1] ) / 2 ) | |
140 height_end = int( height_start + input_image.size[1] ) | |
141 width_start = int( ( max_width - input_image.size[0] ) / 2 ) | |
142 width_end = int( width_start + input_image.size[0] ) | |
143 | |
144 #print( height_start ) | |
145 #print( height_end ) | |
146 #print( width_start ) | |
147 #print( width_end ) | |
148 | |
149 # Iterate over all of the pixels | |
150 # Also prepare the debug view of the image (disp_test) | |
151 disp_test = "+" | |
152 for pixel in range( 0, max_width ): | |
153 disp_test += "-" | |
154 disp_test += "+\n" | |
155 | |
156 for y in range( 0, max_height ): | |
157 disp_test += "|" | |
158 | |
159 # Check if within height range | |
160 if not ( y >= height_start and y < height_end ): | |
161 disp_test += " " * max_width + "|\n|" | |
162 continue | |
163 | |
164 for x in range( 0, max_width ): | |
165 # Check if within width range | |
166 if not ( x >= width_start and x < width_end ): | |
167 disp_test += " " | |
168 continue | |
169 | |
170 # Use image value to determine pixel | |
171 try: | |
172 if input_image.getpixel( (x - width_start, y - height_start) ) == 0: | |
173 disp_test += "*" | |
174 output_image.setpixel( x, y + 1 ) # +1 is due to page boundary | |
175 else: | |
176 disp_test += " " | |
177 except IndexError: | |
178 print( (x - width_start,y - height_start) ) | |
179 pass | |
180 | |
181 disp_test += "|\n" | |
182 | |
183 disp_test += "+" | |
184 for pixel in range( 0, max_width ): | |
185 disp_test += "-" | |
186 disp_test += "+\n" | |
187 | |
188 # BMP Conversion preview | |
189 print( disp_test ) | |
190 | |
191 | |
192 print ( output_image.preview() ) | |
193 #print () | |
194 print( "uint8_t array[] = {0};".format( output_image.getarray() ) ) | |
195 |