6 # -----------------------------------------------------
8 def png2rgba(namespace
, filenames
):
10 fdH
= open("%s.hpp" % namespace
, "w")
11 fdH
.write("/* (Auto-generated binary data file). */\n")
13 fdH
.write("#ifndef BINARY_%s_HPP\n" % namespace
.upper())
14 fdH
.write("#define BINARY_%s_HPP\n" % namespace
.upper())
16 fdH
.write("namespace %s\n" % namespace
)
19 fdC
= open("%s.cpp" % namespace
, "w")
20 fdC
.write("/* (Auto-generated binary data file). */\n")
22 fdC
.write("#include \"%s.hpp\"\n" % namespace
)
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()
37 for dataBlock
in pngData
:
38 width
= len(dataBlock
)
39 channels
= len(dataBlock
[0])
42 print("Invalid image found, cannot continue!")
45 if channels
not in (3, 4):
46 print("Invalid image channel count, cannot continue!")
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
):
62 fdC
.write("static const unsigned char temp%i[] = {\n" % tempIndex
)
67 for dataBlock
in pngData
:
71 for data
in dataBlock
:
74 fdC
.write(" %3u, %3u, %3u," % (b
, g
, r
))
78 if filename
in ("artwork/claw1.png",
84 "artwork/scratch1.png",
85 "artwork/scratch2.png",
95 fdC
.write(" %3u, %3u, %3u, %3u," % (b
, g
, r
, a
))
104 fdC
.write("const char* %s::%sData = (const char*)temp%i;\n" % (namespace
, shortFilename
, tempIndex
))
106 if tempIndex
!= len(filenames
):
113 fdH
.write("#endif // BINARY_%s_HPP\n" % namespace
.upper())
120 # -----------------------------------------------------
122 if __name__
== '__main__':
123 if len(sys
.argv
) != 3:
124 print("Usage: %s <namespace> <artwork-folder>" % sys
.argv
[0])
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
)
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
))
144 png2rgba(namespace
, pngFiles
)