From 4a3be94d79cc6682bfaf1fd9d2c2e9a05f598f56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sun, 2 Jun 2019 20:22:42 +0200 Subject: [PATCH] Continue image tool --- img2array.py | 48 ++++++++++++++++++++++++++++++++++++++++-------- obj2array.py | 6 +++--- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/img2array.py b/img2array.py index b3e685d..7bab2a0 100644 --- a/img2array.py +++ b/img2array.py @@ -3,16 +3,39 @@ # by drummyfish # released under CC0 1.0. +import sys from PIL import Image +def printHelp(): + print("Convert image to C array for small3dlib.") + print("usage:\n") + print(" python img2array.py [TODO] file\n") + print(" TODO\n") + print(""); + print("by Miloslav \"drummyfish\" Ciz") + print("released under CC0 1.0") +if len(sys.argv) < 2: + printHelp() + quit() +FILENAME = "" +NAME = "texture" +GUARDS = False +OUT_WIDTH = 64 +OUT_HEIGHT = 64 -FILENAME = "house.png" -NAME = "house" - -OUT_WIDTH = 256 -OUT_HEIGHT = 256 +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:] + else: + FILENAME = s image = Image.open(FILENAME).convert("RGB") pixels = image.load() @@ -22,8 +45,8 @@ pixels2 = image2.load() imageArray = [] -for y in range(OUT_WIDTH): - for x in range(OUT_HEIGHT): +for y in range(OUT_HEIGHT): + for x in range(OUT_WIDTH): coord = ( int(x / float(OUT_WIDTH) * image.size[0]), int(y / float(OUT_HEIGHT) * image.size[1])) @@ -34,6 +57,12 @@ for y in range(OUT_WIDTH): pixels2[x,y] = pixel +#----------------------- + +if GUARDS: + print("#ifndef " + NAME.upper() + "_TEXTURE_H") + print("#define " + NAME.upper() + "_TEXTURE_H\n") + print("#define " + NAME.upper() + "_WIDTH " + str(OUT_WIDTH)) print("#define " + NAME.upper() + "_HEIGHT " + str(OUT_HEIGHT)) print("") @@ -57,4 +86,7 @@ for v in imageArray: print(arrayString[:-1]) print("}; // " + NAME + "Texture") -image2.save("out.png") +if GUARDS: + print("\n#endif // guard") + +image2.save(NAME + "_preview.png") diff --git a/obj2array.py b/obj2array.py index 7fdb3cb..3d4c04c 100644 --- a/obj2array.py +++ b/obj2array.py @@ -150,8 +150,8 @@ def arrayString(name, array, components, scales, align, short, dataType): result = "" if GUARDS: - print("#ifndef " + NAME.upper() + "_H") - print("#define " + NAME.upper() + "_H\n") + print("#ifndef " + NAME.upper() + "_MODEL_H") + print("#define " + NAME.upper() + "_MODEL_H\n") print(arrayString(NAME + "Vertices",vertices,3,[VERTEX_SCALE],5,False,"S3L_Unit")) print(arrayString(NAME + "TriangleIndices",triangles,3,[1],5,True,"S3L_Index")) @@ -172,7 +172,7 @@ else: print(arrayString(NAME + "TriangleUVs",uvs2,6,[U_SCALE,V_SCALE],5,False,"S3L_Unit")) -print("S3L_Model3D " + NAME + " = ") +print("S3L_Model3D " + NAME + "Model = ") if COMPACT: print("{.vertices=" +