Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.5KB

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