aboutsummaryrefslogtreecommitdiffstats
path: root/tools/ota/convert-to-bmp.py
blob: 446c09da8a8439f40a05acccdd15b3eee1bfd39e (plain)
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
#!/usr/bin/python2.4

"""A simple script to convert asset images to BMP files, that supports
RGBA image."""

import struct
import Image
import sys

infile = sys.argv[1]
outfile = sys.argv[2]

if not outfile.endswith(".bmp"):
  print >> sys.stderr, "Warning: I'm expecting to write BMP files."

im = Image.open(infile)
if im.mode == 'RGB':
  im.save(outfile)
elif im.mode == 'RGBA':
  # Python Imaging Library doesn't write RGBA BMP files, so we roll
  # our own.

  BMP_HEADER_FMT = ("<"      # little-endian
                    "H"      # signature
                    "L"      # file size
                    "HH"     # reserved (set to 0)
                    "L"      # offset to start of bitmap data)
                    )

  BITMAPINFO_HEADER_FMT= ("<"      # little-endian
                          "L"      # size of this struct
                          "L"      # width
                          "L"      # height
                          "H"      # planes (set to 1)
                          "H"      # bit count
                          "L"      # compression (set to 0 for minui)
                          "L"      # size of image data (0 if uncompressed)
                          "L"      # x pixels per meter (1)
                          "L"      # y pixels per meter (1)
                          "L"      # colors used (0)
                          "L"      # important colors (0)
                          )

  fileheadersize = struct.calcsize(BMP_HEADER_FMT)
  infoheadersize = struct.calcsize(BITMAPINFO_HEADER_FMT)

  header = struct.pack(BMP_HEADER_FMT,
                       0x4d42,   # "BM" in little-endian
                       (fileheadersize + infoheadersize +
                        im.size[0] * im.size[1] * 4),
                       0, 0,
                       fileheadersize + infoheadersize)

  info = struct.pack(BITMAPINFO_HEADER_FMT,
                     infoheadersize,
                     im.size[0],
                     im.size[1],
                     1,
                     32,
                     0,
                     0,
                     1,
                     1,
                     0,
                     0)

  f = open(outfile, "wb")
  f.write(header)
  f.write(info)
  data = im.tostring()
  for j in range(im.size[1]-1, -1, -1):   # rows bottom-to-top
    for i in range(j*im.size[0]*4, (j+1)*im.size[0]*4, 4):
      f.write(data[i+2])    # B
      f.write(data[i+1])    # G
      f.write(data[i+0])    # R
      f.write(data[i+3])    # A
  f.close()
else:
  print >> sys.stderr, "Don't know how to handle image mode '%s'." % (im.mode,)