1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/small3dlib.git synced 2024-11-26 21:30:17 +01:00
small3dlib/tools/img2array.py

156 lines
3.7 KiB
Python
Raw Normal View History

2019-06-01 22:28:12 +02:00
# Python tool to convert an image to C array for small3dlib.
#
# by drummyfish
# released under CC0 1.0.
2019-06-02 20:22:42 +02:00
import sys
2019-06-01 22:28:12 +02:00
from PIL import Image
2019-06-02 20:22:42 +02:00
def printHelp():
print("Convert image to C array for small3dlib.")
print("usage:\n")
2019-06-03 23:03:23 +02:00
print(" python img2array.py [-xW -yH -h -nS -pT -5] file\n")
print(" -xW set width of the output to W pixels")
print(" -yH set height of the output to H pixels")
print(" -h include header guards (for texture per file)")
print(" -nS use the name S for the texture (defaut: \"texture\")")
print(" -pT use palette from file T and indexed colors (otherwise direct colors)")
print(" -5 use 565 format instead of RGB8")
2019-06-02 20:22:42 +02:00
print("");
print("by Miloslav \"drummyfish\" Ciz")
print("released under CC0 1.0")
2019-06-03 23:03:23 +02:00
def rgbTo565(rgb):
return ((rgb[0] >> 3) << 11) | ((rgb[1] >> 2) << 5) | ((rgb[2] >> 3))
2019-06-02 20:22:42 +02:00
if len(sys.argv) < 2:
printHelp()
quit()
FILENAME = ""
2019-06-03 13:08:08 +02:00
PALETTE = ""
USE_PALETTE = False
2019-06-02 20:22:42 +02:00
NAME = "texture"
GUARDS = False
OUT_WIDTH = 64
OUT_HEIGHT = 64
2019-06-03 23:03:23 +02:00
USE_565 = False
2019-06-02 20:22:42 +02:00
for s in sys.argv:
if s [:2] == "-x":
OUT_WIDTH = int(s[2:])
elif s [:2] == "-y":
OUT_HEIGHT = int(s[2:])
elif s == "-h":
GUARDS = True
elif s[:2] == "-n":
NAME = s[2:]
2019-06-03 23:03:23 +02:00
elif s[:2] == "-5":
USE_565 = True
2019-06-03 13:08:08 +02:00
elif s[:2] == "-p":
PALETTE = s[2:]
USE_PALETTE = True
2019-06-02 20:22:42 +02:00
else:
FILENAME = s
2019-06-01 22:28:12 +02:00
2019-06-03 13:08:08 +02:00
imageArray = []
paletteColors = []
paletteArray = []
2019-06-01 22:28:12 +02:00
image = Image.open(FILENAME).convert("RGB")
pixels = image.load()
2019-06-03 23:03:23 +02:00
if USE_PALETTE > 0:
2019-06-03 13:08:08 +02:00
palette = Image.open(PALETTE).convert("RGB")
pixelsPal = palette.load()
for y in range(palette.size[1]):
for x in range(palette.size[0]):
c = pixelsPal[x,y]
paletteColors.append(c)
2019-06-03 23:03:23 +02:00
if USE_565:
paletteArray.append(rgbTo565(c))
else:
paletteArray.append(c[0])
paletteArray.append(c[1])
paletteArray.append(c[2])
2019-06-03 13:08:08 +02:00
2019-06-01 22:28:12 +02:00
image2 = Image.new("RGB",(OUT_WIDTH,OUT_HEIGHT),color="white")
pixels2 = image2.load()
2019-06-02 20:22:42 +02:00
for y in range(OUT_HEIGHT):
for x in range(OUT_WIDTH):
2019-06-01 22:28:12 +02:00
coord = (
int(x / float(OUT_WIDTH) * image.size[0]),
int(y / float(OUT_HEIGHT) * image.size[1]))
pixel = pixels[coord]
2019-06-03 13:08:08 +02:00
if USE_PALETTE:
closestIndex = 0
closestDiff = 1024
2019-06-01 22:28:12 +02:00
2019-06-03 13:08:08 +02:00
# find the index of the closest color:
2019-06-01 22:28:12 +02:00
2019-06-03 13:08:08 +02:00
for i in range(len(paletteColors)):
c = paletteColors[i]
diff = abs(pixel[0] - c[0]) + abs(pixel[1] - c[1]) + abs(pixel[2] - c[2])
2019-06-02 20:22:42 +02:00
2019-06-03 13:08:08 +02:00
if diff < closestDiff:
closestIndex = i
closestDiff = diff
2019-06-02 20:22:42 +02:00
2019-06-03 13:08:08 +02:00
imageArray.append(closestIndex)
pixels2[x,y] = paletteColors[closestIndex]
else:
2019-06-03 23:03:23 +02:00
if USE_565:
imageArray.append(rgbTo565(pixel))
else:
imageArray.append(pixel[0])
imageArray.append(pixel[1])
imageArray.append(pixel[2])
2019-06-03 13:08:08 +02:00
pixels2[x,y] = pixel
2019-06-01 22:28:12 +02:00
2019-06-03 13:08:08 +02:00
#-----------------------
2019-06-28 19:54:18 +02:00
def printArray(array, name, sizeString, dataType="const uint8_t"):
2019-06-25 05:51:14 +02:00
print(dataType + " " + name + "[" + sizeString + "] = {")
2019-06-03 13:08:08 +02:00
arrayString = ""
lineLen = 0
2019-06-01 22:28:12 +02:00
2019-06-03 13:08:08 +02:00
for v in array:
item = str(v) + ","
2019-06-01 22:28:12 +02:00
lineLen += len(item)
if lineLen > 80:
arrayString += "\n"
2019-06-06 23:37:41 +02:00
lineLen = len(item)
2019-06-01 22:28:12 +02:00
arrayString += item
2019-06-03 13:08:08 +02:00
print(arrayString[:-1])
2019-06-03 23:03:23 +02:00
print("}; // " + name)
2019-06-03 13:08:08 +02:00
if GUARDS:
print("#ifndef " + NAME.upper() + "_TEXTURE_H")
print("#define " + NAME.upper() + "_TEXTURE_H\n")
if USE_PALETTE:
2019-06-28 19:54:18 +02:00
printArray(paletteArray,NAME + "Palette",str(len(paletteArray)),"const uint16_t" if USE_565 else "const uint8_t")
2019-06-03 23:03:23 +02:00
print("")
2019-06-03 13:08:08 +02:00
2019-06-03 17:07:40 +02:00
print("#define " + NAME.upper() + "_TEXTURE_WIDTH " + str(OUT_WIDTH))
print("#define " + NAME.upper() + "_TEXTURE_HEIGHT " + str(OUT_HEIGHT))
print("")
2019-06-03 13:08:08 +02:00
2019-06-03 23:03:23 +02:00
printArray(imageArray,NAME + "Texture",str(len(imageArray)))
2019-06-01 22:28:12 +02:00
2019-06-02 20:22:42 +02:00
if GUARDS:
print("\n#endif // guard")
image2.save(NAME + "_preview.png")