3 # Copyright (C) 2015 by Jacob Alexander
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.
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.
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/>.
25 # Convenience class to deal with converting images to a C array
27 # Some constants for the LCD Driver
33 def __init__( self, height, width ):
37 # Calculate number of pages
38 self.page_count = int( self.height / self.page_width )
39 self.page_length = self.width
41 # Initialize pages to 0's
43 for page in range( 0, self.page_count ):
44 self.page_data.append( array( 'B', [0] * self.page_length ) )
46 def setpixel( self, x, y ):
47 # Calculate which page
48 page = int( ( self.height - y ) / self.page_width )
53 # Calculate which byte
57 bit = int( ( self.height - y ) % self.page_width )
60 self.page_data[ page ][ byte ] |= (1 << bit)
62 def getpage( self, page ):
63 return self.page_data[ page ]
68 for page in range( 0, self.page_count ):
69 for elem in self.page_data[ page ]:
70 struct += "0x{0:02x}, ".format( elem )
72 if page != self.page_count - 1:
79 # Prints out what the image will look like on the display
83 for pixel in range( 0, self.width ):
88 for page in range( self.page_count - 1, -1, -1 ):
90 for line in range( 7, -1, -1 ):
94 # Each Byte (Column/Pixel)
95 for byte in range( 0, self.width ):
96 if self.page_data[ page ][ byte ] & (1 << line):
106 for pixel in range( 0, self.width ):
113 filename = "ic_logo_lcd.bmp"
118 output_image = STLcdGraphic( max_height, max_width )
122 # Load the input filename and convert to black & white
124 input_image = Image.open( filename ).convert('1')
126 print( "Unable to load image '{0}'".format( filename ) )
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(
132 ( input_image.format, input_image.size, input_image.mode ),
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] )
144 #print( height_start )
146 #print( width_start )
149 # Iterate over all of the pixels
150 # Also prepare the debug view of the image (disp_test)
152 for pixel in range( 0, max_width ):
156 for y in range( 0, max_height ):
159 # Check if within height range
160 if not ( y >= height_start and y < height_end ):
161 disp_test += " " * max_width + "|\n|"
164 for x in range( 0, max_width ):
165 # Check if within width range
166 if not ( x >= width_start and x < width_end ):
170 # Use image value to determine pixel
172 if input_image.getpixel( (x - width_start, y - height_start) ) == 0:
174 output_image.setpixel( x, y + 1 ) # +1 is due to page boundary
178 print( (x - width_start,y - height_start) )
184 for pixel in range( 0, max_width ):
188 # BMP Conversion preview
192 print ( output_image.preview() )
194 print( "uint8_t array[] = {0};".format( output_image.getarray() ) )