bmp2col.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import os
  2. import re
  3. import string
  4. import sys
  5. import binascii
  6. import math
  7. class color:
  8. def __init__(self,r,g,b):
  9. self.r = r >> 3
  10. self.g = g >> 3
  11. self.b = b >> 3
  12. self.snes = "$%04x" % (( self.b << 10 ) | (self.g << 5) | self.r )
  13. def get(self):
  14. return self.snes
  15. def __str__(self):
  16. return "r=0x%02x g=0x%02x b=0x%02x snes=%s" % ( self.r,self.g,self.b,self.snes)
  17. def openfile(filename):
  18. return open(filename)
  19. def mk_sint(s):
  20. a = ord(s[1])
  21. b = ord(s[0])
  22. return (a<<8) + b
  23. def mk_int(s):
  24. a = ord(s[3])
  25. b = ord(s[2])
  26. c = ord(s[1])
  27. d = ord(s[0])
  28. return (a<<32) + (b<<16) + (c<<8) + d
  29. def main():
  30. file = sys.argv[1]
  31. asmfile = string.replace(file,".bmp",".s")
  32. basename = string.replace(file,".bmp","")
  33. if len(sys.argv) >= 3 and sys.argv[2] =='list' :
  34. do_list = 1
  35. else:
  36. do_list = 0
  37. if len(sys.argv) >= 4 and sys.argv[3] =='compress':
  38. do_compress = 1
  39. else:
  40. do_compress = 0
  41. fp =openfile(file)
  42. header_offset = 14 + 40
  43. data = fp.read()
  44. fp.close()
  45. type = mk_sint(data[0:2])
  46. size = mk_int(data[2:6])
  47. width = mk_int(data[18:22])
  48. height = mk_int(data[22:26])
  49. bits_per_pixel = mk_int(data[28:32])
  50. num_of_colors = mk_int(data[46:50])
  51. bytes_per_line = width / 8
  52. bytes_per_line_padded = bytes_per_line + (4-(bytes_per_line % 4))
  53. header = data[0:header_offset]
  54. colors = data[header_offset: header_offset + (num_of_colors * 4)]
  55. data = data[header_offset + (num_of_colors * 4):]
  56. raw = []
  57. for i in data:
  58. raw.append(ord(i))
  59. raw_len = len(raw)
  60. palette = []
  61. for i in range(0,len(colors),4):
  62. palette.append(color(ord(colors[i+2]),ord(colors[i+1]),ord(colors[i])))
  63. print "file:\t\t%s" % file
  64. print "basename:\t%s" % asmfile
  65. print "header info"
  66. print "type: \t\t%04X " % type
  67. print "size: \t\t%i bytes" % size
  68. print "width: \t\t%i pixel" % width
  69. print "height: \t%i pixel" % height
  70. print "bit per pixel:\t%i" % bits_per_pixel
  71. print "num of colors:\t%i" % num_of_colors
  72. print "imagedata: \t%s bytes" % raw_len
  73. print "per line: \t%i bytes" % bytes_per_line
  74. print "per line pad: \t%i bytes" % bytes_per_line_padded
  75. out='';
  76. fp = open(asmfile,'w')
  77. tile_cnt = 0
  78. color_list = str()
  79. value_list = str()
  80. color_list="\n\n\n%s_color_list:\n\n" % (basename)
  81. value_list ="\n\n\n%s_color_values:\n\n" % (basename)
  82. out = " .db "
  83. cnt=0
  84. last = "";
  85. repeat = 1;
  86. for i in range(raw_len-1,-1,-width):
  87. idx = raw[i]
  88. col = palette[idx].get()
  89. if col == last and do_compress:
  90. repeat += 1
  91. continue
  92. else:
  93. #print palette[idx]
  94. last = col
  95. if do_list:
  96. value_list += " .db $%02x\n" % repeat
  97. value_list += " .dw %s ;line=0x%02x\n" % (col, (height - (i/width)) )
  98. out += "$%02x,$00," % repeat
  99. repeat = 1
  100. cnt +=1
  101. if cnt == width/20:
  102. cnt=0
  103. out = out[:-1]
  104. out +="\n"
  105. color_list += out
  106. out = " .db "
  107. value_list += " .db 0\n"
  108. color_list += " .db 0\n"
  109. if do_list:
  110. fp.write(color_list)
  111. fp.write(value_list)
  112. fp.close()
  113. if __name__ == '__main__':
  114. if len(sys.argv)>= 2:
  115. main()
  116. else:
  117. print "usage: %s in [list] [compress]" % sys.argv[0]