summaryrefslogtreecommitdiff
path: root/NxWidgets/tools/bitmap_converter.py
blob: 2cb7e8869ded926bab3e488136ace13404b645c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python

'''This script converts from any image type supported by
Python imaging library to the RLE-encoded format used by
NxWidgets.
'''

from PIL import Image

def get_palette(img, maxcolors = 255):
  '''Returns a list of colors. If there are too many colors in the image,
  the least used are removed.
  '''
  img = img.convert("RGB")
  colors = img.getcolors(65536)
  colors.sort(key = lambda c: -c[0])
  return [c[1] for c in colors[:maxcolors]]

def write_palette(outfile, palette):
  '''Write the palette (normal and hilight) to the output file.'''
  
  outfile.write('static const NXWidgets::nxwidget_pixel_t palette[BITMAP_PALETTESIZE] =\n');
  outfile.write('{\n')
  
  for i in range(0, len(palette), 4):
    outfile.write('  ');
    for r, g, b in palette[i:i+4]:
      outfile.write('MKRGB(%3d,%3d,%3d), ' % (r, g, b))
    outfile.write('\n');
  
  outfile.write('};\n\n')
  
  outfile.write('static const NXWidgets::nxwidget_pixel_t hilight_palette[BITMAP_PALETTESIZE] =\n');
  outfile.write('{\n')
  
  for i in range(0, len(palette), 4):
    outfile.write('  ');
    for r, g, b in palette[i:i+4]:
      r = min(255, r + 50)
      g = min(255, g + 50)
      b = min(255, b + 50)
      outfile.write('MKRGB(%3d,%3d,%3d), ' % (r, g, b))
    outfile.write('\n');
  
  outfile.write('};\n\n')
  

def quantize(color, palette):
  '''Return the color index to closest match in the palette.'''
  try:
    return palette.index(color)
  except ValueError:
    # No exact match, search for the closest
    def distance(color2):
      return sum([(a - b)**2 for a, b in zip(color, color2)])
    
    return palette.index(min(palette, key = distance));

def encode_row(img, palette, y):
  '''RLE-encode one row of image data.'''
  entries = []
  color = None
  repeats = 0
  
  for x in range(0, img.size[0]):
    c = quantize(img.getpixel((x, y)), palette)
    if c == color:
      repeats += 1
    else:
      if color is not None:
        entries.append((repeats, color))
      
      repeats = 1
      color = c

  if color is not None:
    entries.append((repeats, color))
      
  return entries

def write_image(outfile, img, palette):
  '''Write the image contents to the output file.'''

  outfile.write('static const NXWidgets::SRlePaletteBitmapEntry bitmap[] =\n');
  outfile.write('{\n');
  
  for y in range(0, img.size[1]):
    entries = encode_row(img, palette, y)
    row = ""
    for r, c in entries:
      if len(row) > 60:
        outfile.write('  ' + row + '\n')
        row = ""
      
      row += '{%3d, %3d}, ' % (r, c)
    
    row += ' ' * (73 - len(row))
    outfile.write('  ' + row + '/* Row %d */\n' % y)
  
  outfile.write('};\n\n');

def write_descriptor(outfile, name):
  '''Write the public descriptor structure for the image.'''
  
  outfile.write('extern const struct NXWidgets::SRlePaletteBitmap g_%s =\n' % name)
  outfile.write('{\n')
  outfile.write('  CONFIG_NXWIDGETS_BPP,\n')
  outfile.write('  CONFIG_NXWIDGETS_FMT,\n')
  outfile.write('  BITMAP_PALETTESIZE,\n')
  outfile.write('  BITMAP_WIDTH,\n')
  outfile.write('  BITMAP_HEIGHT,\n')
  outfile.write('  {palette, hilight_palette},\n')
  outfile.write('  bitmap\n')
  outfile.write('};\n')
  
if __name__ == '__main__':
  import sys
  import os.path
  
  if len(sys.argv) != 3:
    print "Usage: bitmap_converter.py source.png output.cxx"
    sys.exit(1)
  
  img = Image.open(sys.argv[1])
  outfile = open(sys.argv[2], 'w')
  palette = get_palette(img)
  
  outfile.write(
'''
/* Automatically NuttX bitmap file. */
/* Generated from %(src)s by bitmap_converter.py. */

#include <nxconfig.hxx>
#include <crlepalettebitmap.hxx>

#define BITMAP_WIDTH %(width)s
#define BITMAP_HEIGHT %(height)s
#define BITMAP_PALETTESIZE %(palettesize)s

''' % {'src': sys.argv[1], 'width': img.size[0], 'height': img.size[1],
       'palettesize': len(palette)}
  )

  name = os.path.splitext(os.path.basename(sys.argv[1]))[0]
  
  write_palette(outfile, palette)
  write_image(outfile, img, palette)
  write_descriptor(outfile, name)