Add a another two examples of patterns
[PoPiPaint.git] / tools / convert_to_RLE_animation.py
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2014  Antonio Ospite <ao2@ao2.it>
4 #
5 # This program 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.
9 #
10 # This program 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.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 import sys
19 import Image
20
21
22 def usage(program_name):
23     sys.stderr.write("usage: %s <image file> <animation file>\n" % program_name)
24     sys.exit(1)
25
26
27 # The code below has been copied from the C implementation in PatternPaint:
28 # https://github.com/Blinkinlabs/PatternPaint
29 def save_animation(input_filename, output_filename):
30     output_file = open(output_filename, "w")
31
32     img = Image.open(input_filename).convert('RGB')
33     w, h = img.size
34     colors = img.getcolors(w*h)
35     palette = img.getpalette()
36     pixels = img.getdata()
37
38     output_file.write("const PROGMEM prog_uint8_t animationData[]  = {\n")
39
40     output_file.write("// Length of the color table - 1, in bytes. length: 1 byte\n")
41     output_file.write("   %d,\n" % (len(colors) - 1))
42
43     output_file.write("// Color table section. Each entry is 3 bytes. length: %d bytes\n" % (len(colors) * 3))
44
45     color_map = {}
46     for i, c in enumerate(colors):
47         output_file.write(" %3d, %3d, %3d,\n" % (c[1][0], c[1][1], c[1][2]))
48         color_map[c[1]] = i
49
50     output_file.write("// Pixel runs section. Each pixel run is 2 bytes. length: -1 bytes\n")
51
52     for x in range(0, w):
53         run_count = 0
54         for y in range(0, h):
55             new_color = color_map[pixels.getpixel((x, y))]
56             if run_count == 0:
57                 current_color = new_color
58
59             if current_color != new_color:
60                 output_file.write(" %3d, %3d,\n" % (run_count, current_color))
61                 run_count = 1
62                 current_color = new_color
63             else:
64                 run_count += 1
65
66         output_file.write(" %3d, %3d,\n" % (run_count, current_color))
67
68     output_file.write("};\n\n")
69
70     output_file.write("#define NUM_FRAMES %d\n" % w)
71     output_file.write("#define NUM_LEDS %d\n" % h)
72     output_file.write("Animation animation(NUM_FRAMES, animationData, NUM_LEDS);\n")
73     output_file.close()
74
75 if __name__ == "__main__":
76     if len(sys.argv) < 3:
77         usage(sys.argv[0])
78
79     save_animation(sys.argv[1], sys.argv[2])