Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

bitmap2Struct.py 4.8KB

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