Use wine paths by default even if non-existent
[carla.git] / data / png2rgba.py
bloba0d7fc9ad26623d599cea937a433c4ca9000f0cf
1 #!/usr/bin/env python
3 import os, numpy, sys
4 import Image
6 # -----------------------------------------------------
8 def png2rgba(namespace, filenames):
10 fdH = open("%s.hpp" % namespace, "w")
11 fdH.write("/* (Auto-generated binary data file). */\n")
12 fdH.write("\n")
13 fdH.write("#ifndef BINARY_%s_HPP\n" % namespace.upper())
14 fdH.write("#define BINARY_%s_HPP\n" % namespace.upper())
15 fdH.write("\n")
16 fdH.write("namespace %s\n" % namespace)
17 fdH.write("{\n")
19 fdC = open("%s.cpp" % namespace, "w")
20 fdC.write("/* (Auto-generated binary data file). */\n")
21 fdC.write("\n")
22 fdC.write("#include \"%s.hpp\"\n" % namespace)
23 fdC.write("\n")
25 tempIndex = 1
27 for filename in filenames:
28 shortFilename = filename.rsplit(os.sep, 1)[-1].split(".", 1)[0]
29 shortFilename = shortFilename.replace("-", "_")
31 png = Image.open(filename)
32 pngNumpy = numpy.array(png)
33 pngData = pngNumpy.tolist()
34 pngData.reverse()
36 height = len(pngData)
37 for dataBlock in pngData:
38 width = len(dataBlock)
39 channels = len(dataBlock[0])
40 break
41 else:
42 print("Invalid image found, cannot continue!")
43 quit()
45 if channels not in (3, 4):
46 print("Invalid image channel count, cannot continue!")
47 quit()
49 print("Generating data for \"%s\" using '%s' type" % (filename, "GL_BGR" if channels == 3 else "GL_BGRA"))
50 #print(" Width: %i" % width)
51 #print(" Height: %i" % height)
52 #print(" DataSize: %i" % (width * height * channels))
54 fdH.write(" extern const char* %sData;\n" % shortFilename)
55 fdH.write(" const unsigned int %sDataSize = %i;\n" % (shortFilename, width * height * channels))
56 fdH.write(" const unsigned int %sWidth = %i;\n" % (shortFilename, width))
57 fdH.write(" const unsigned int %sHeight = %i;\n" % (shortFilename, height))
59 if tempIndex != len(filenames):
60 fdH.write("\n")
62 fdC.write("static const unsigned char temp%i[] = {\n" % tempIndex)
64 curColumn = 1
65 fdC.write(" ")
67 for dataBlock in pngData:
68 if curColumn == 0:
69 fdC.write(" ")
71 for data in dataBlock:
72 if channels == 3:
73 r, g, b = data
74 fdC.write(" %3u, %3u, %3u," % (b, g, r))
75 else:
76 r, g, b, a = data
78 if filename in ("artwork/claw1.png",
79 "artwork/claw2.png",
80 "artwork/run1.png",
81 "artwork/run2.png",
82 "artwork/run3.png",
83 "artwork/run4.png",
84 "artwork/scratch1.png",
85 "artwork/scratch2.png",
86 "artwork/sit.png",
87 "artwork/tail.png"):
88 if r == 255:
89 a -= 38
90 if a < 0: a = 0
91 #a = 0
92 #else:
93 #r = g = b = 255
95 fdC.write(" %3u, %3u, %3u, %3u," % (b, g, r, a))
97 if curColumn > 20:
98 fdC.write("\n ")
99 curColumn = 1
100 else:
101 curColumn += 1
103 fdC.write("};\n")
104 fdC.write("const char* %s::%sData = (const char*)temp%i;\n" % (namespace, shortFilename, tempIndex))
106 if tempIndex != len(filenames):
107 fdC.write("\n")
109 tempIndex += 1
111 fdH.write("}\n")
112 fdH.write("\n")
113 fdH.write("#endif // BINARY_%s_HPP\n" % namespace.upper())
114 fdH.write("\n")
115 fdH.close()
117 fdC.write("\n")
118 fdC.close()
120 # -----------------------------------------------------
122 if __name__ == '__main__':
123 if len(sys.argv) != 3:
124 print("Usage: %s <namespace> <artwork-folder>" % sys.argv[0])
125 quit()
127 namespace = sys.argv[1]
128 artFolder = sys.argv[2]
130 if not os.path.exists(artFolder):
131 print("Folder '%s' does not exist" % artFolder)
132 quit()
134 # find png files
135 pngFiles = []
137 for root, dirs, files in os.walk(artFolder):
138 for name in [name for name in files if name.lower().endswith(".png")]:
139 pngFiles.append(os.path.join(root, name))
141 pngFiles.sort()
143 # create code now
144 png2rgba(namespace, pngFiles)