2020-02-01 16:39:34 +01:00
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import subprocess
|
|
|
|
|
2020-01-12 14:34:13 +01:00
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
# Palette de couleurs
|
2020-01-28 14:32:39 +01:00
|
|
|
pierre = (0, 0, 0)
|
|
|
|
brique = (255, 0, 0)
|
|
|
|
empty = (255, 255, 255)
|
|
|
|
piece = (203, 255, 0)
|
|
|
|
boite_piece = (255, 153, 0)
|
|
|
|
boite_champi = (204, 0, 255)
|
|
|
|
brique_piece = (101, 127, 0)
|
|
|
|
beton = (84, 84, 84)
|
|
|
|
tuyau_milieu = (0, 255, 102)
|
|
|
|
tuyau_bout = (50, 255, 0)
|
|
|
|
drapeau = (0, 255, 216)
|
|
|
|
|
|
|
|
goomba = (127, 76, 0)
|
|
|
|
koopa_vert = (25, 127, 0)
|
2020-01-28 21:32:58 +01:00
|
|
|
koopa_rouge = (127, 0, 0)
|
2020-01-28 14:32:39 +01:00
|
|
|
|
|
|
|
def color_compare(color1, color2):
|
|
|
|
if color1[0] == color2[0] and color1[1] == color2[1] and color1[2] == color2[2]:
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
return 0
|
2020-01-12 18:04:09 +01:00
|
|
|
|
2020-02-01 16:39:34 +01:00
|
|
|
def write_char(val):
|
|
|
|
return '%c' % int(val%256)
|
2020-01-12 14:34:13 +01:00
|
|
|
|
|
|
|
# Load image
|
|
|
|
filename = input("File name ?\n> ")
|
|
|
|
img = Image.open(filename)
|
2020-01-13 12:12:46 +01:00
|
|
|
print("Converting", filename, "to", "\"" + filename + ".c\",", "size =", img.size)
|
2020-01-12 14:34:13 +01:00
|
|
|
|
2020-02-01 16:39:34 +01:00
|
|
|
#rough binary
|
|
|
|
code = write_char(img.size[0] - img.size[0]%256)
|
|
|
|
code += write_char(img.size[0]%256)
|
|
|
|
code += write_char((img.size[1] - img.size[1]%256)/256)
|
|
|
|
code += write_char(img.size[1]%256)
|
|
|
|
|
|
|
|
|
2020-01-13 12:12:46 +01:00
|
|
|
|
|
|
|
nombre_ennemis=0
|
|
|
|
ennemis = "\nennemi_t ennemies0[]={"
|
|
|
|
|
|
|
|
|
2020-01-12 14:34:13 +01:00
|
|
|
# Balayage des pixels : passe pour dessiner les murs
|
|
|
|
pixels = img.load()
|
|
|
|
for x in range(0,img.size[0]):
|
|
|
|
for i in range(0, img.size[1]):
|
|
|
|
y=img.size[1]-i-1
|
2020-01-12 18:04:09 +01:00
|
|
|
|
2020-02-01 16:39:34 +01:00
|
|
|
if color_compare(pixels[x,y],empty):
|
|
|
|
code += write_char(0)
|
2020-01-12 14:34:13 +01:00
|
|
|
|
2020-02-01 16:39:34 +01:00
|
|
|
elif color_compare(pixels[x,y], pierre):
|
|
|
|
code += write_char(1)
|
2020-01-12 18:04:09 +01:00
|
|
|
|
2020-01-28 14:32:39 +01:00
|
|
|
elif color_compare(pixels[x,y], piece):
|
2020-02-01 16:39:34 +01:00
|
|
|
code += write_char(2)
|
|
|
|
|
2020-01-28 14:32:39 +01:00
|
|
|
elif color_compare(pixels[x,y], brique):
|
2020-02-01 16:39:34 +01:00
|
|
|
code += write_char(3)
|
|
|
|
|
2020-01-28 14:32:39 +01:00
|
|
|
elif color_compare(pixels[x,y],brique_piece):
|
2020-02-01 16:39:34 +01:00
|
|
|
code += write_char(4)
|
|
|
|
|
2020-01-28 14:32:39 +01:00
|
|
|
elif color_compare(pixels[x,y],boite_piece):
|
2020-02-01 16:39:34 +01:00
|
|
|
code += write_char(5)
|
|
|
|
|
2020-01-28 14:32:39 +01:00
|
|
|
elif color_compare(pixels[x,y],boite_champi):
|
2020-02-01 16:39:34 +01:00
|
|
|
code += write_char(6)
|
|
|
|
|
2020-01-28 14:32:39 +01:00
|
|
|
elif color_compare(pixels[x,y],beton):
|
2020-02-01 16:39:34 +01:00
|
|
|
code += write_char(7)
|
2020-01-12 18:04:09 +01:00
|
|
|
|
2020-01-28 14:32:39 +01:00
|
|
|
elif color_compare(pixels[x,y],tuyau_bout):
|
2020-02-01 16:39:34 +01:00
|
|
|
code += write_char(8)
|
2020-01-28 14:32:39 +01:00
|
|
|
elif color_compare(pixels[x,y], tuyau_milieu):
|
2020-01-12 18:04:09 +01:00
|
|
|
left=0
|
|
|
|
right=0
|
|
|
|
up=0 # vide par defaut
|
|
|
|
down=0
|
|
|
|
tx=0
|
|
|
|
ty=0
|
|
|
|
if x:
|
2020-01-28 14:32:39 +01:00
|
|
|
left = (color_compare(pixels[x-1,y], tuyau_bout)) or (color_compare(pixels[x-1,y], tuyau_milieu))
|
2020-01-12 18:04:09 +01:00
|
|
|
if x != img.size[0]-1:
|
2020-01-28 14:32:39 +01:00
|
|
|
right = (color_compare(pixels[x+1,y], tuyau_bout)) or (color_compare(pixels[x+1,y], tuyau_milieu))
|
2020-01-12 18:04:09 +01:00
|
|
|
|
|
|
|
if y:
|
2020-01-28 14:32:39 +01:00
|
|
|
up = (color_compare(pixels[x,y-1], tuyau_bout)) or (color_compare(pixels[x,y-1], tuyau_milieu))
|
2020-01-12 18:04:09 +01:00
|
|
|
if y != img.size[1]-1:
|
2020-01-28 14:32:39 +01:00
|
|
|
down = (color_compare(pixels[x,y+1], tuyau_bout)) or (color_compare(pixels[x,y+1], tuyau_milieu))
|
2020-01-12 18:04:09 +01:00
|
|
|
|
|
|
|
if right and left==0:
|
|
|
|
tx=0
|
|
|
|
ty=3
|
|
|
|
elif left and right==0:
|
|
|
|
tx=1
|
|
|
|
ty=3
|
|
|
|
elif up==0 and down:
|
|
|
|
tx=1
|
|
|
|
ty=0
|
|
|
|
elif up and down==0:
|
|
|
|
tx=1
|
|
|
|
ty=1
|
|
|
|
hexa="0x"+str(tx)+str(ty)
|
|
|
|
code += "{TUYAU,"+hexa+"}, "
|
2020-01-28 14:32:39 +01:00
|
|
|
elif color_compare(pixels[x,y], drapeau):
|
|
|
|
if not color_compare(pixels[x,y-1], drapeau):
|
|
|
|
code += "{END_LEVEL,0x1400}, "
|
|
|
|
elif not color_compare(pixels[x,y-2], drapeau):
|
|
|
|
code+= "{END_LEVEL,0x1401}, "
|
|
|
|
elif not color_compare(pixels[x,y-3], drapeau):
|
|
|
|
code+= "{END_LEVEL,0x1302}, "
|
|
|
|
elif not color_compare(pixels[x,y-4], drapeau):
|
|
|
|
code+= "{END_LEVEL,0x1202}, "
|
|
|
|
elif not color_compare(pixels[x,y-5], drapeau):
|
|
|
|
code+= "{END_LEVEL,0x1202}, "
|
|
|
|
elif not color_compare(pixels[x,y-6], drapeau):
|
|
|
|
code+= "{END_LEVEL,0x1202}, "
|
|
|
|
else:
|
|
|
|
code+= "{END_LEVEL,0x1102},"
|
2020-01-12 14:34:13 +01:00
|
|
|
else:
|
2020-02-01 16:39:34 +01:00
|
|
|
code += write_char(0)
|
2020-01-12 14:34:13 +01:00
|
|
|
#code += str(pixels[x,y])
|
2020-01-13 12:12:46 +01:00
|
|
|
|
2020-01-28 14:32:39 +01:00
|
|
|
if color_compare(pixels[x,y], goomba):
|
2020-01-13 12:12:46 +01:00
|
|
|
nombre_ennemis += 1
|
2020-01-13 20:26:37 +01:00
|
|
|
ennemis += "\n GOOMBA(" + str(8*x) + ", " + str(8*i) + ", -1),"
|
2020-01-28 14:32:39 +01:00
|
|
|
elif color_compare(pixels[x,y], koopa_vert):
|
2020-01-13 20:26:37 +01:00
|
|
|
ennemis += "\n KOOPA_V(" + str(8*x) + ", " + str(8*i) + ", -1),"
|
2020-01-28 21:32:58 +01:00
|
|
|
elif color_compare(pixels[x,y], koopa_rouge):
|
|
|
|
ennemis += "\n KOOPA_R(" + str(8*x) + ", " + str(8*i) + ", -1),"
|
2020-01-13 12:12:46 +01:00
|
|
|
ennemis += "\n};\nennemis_global_size=" + str(nombre_ennemis) + ";\ninit_ennemies(ennemies0);\n"
|
2020-01-12 14:34:13 +01:00
|
|
|
|
2020-02-01 16:39:34 +01:00
|
|
|
#f = open(filename+".packlvl", 'w')
|
|
|
|
#f.write(code)
|
|
|
|
#f.close()
|
|
|
|
|
|
|
|
build(bytes(code, 'utf-8'), filename+".o", "_lvl_"+filename.replace(".png",""))
|
2020-01-13 12:12:46 +01:00
|
|
|
|
|
|
|
print("Converted succesfully !")
|