Image generation?
This commit is contained in:
@@ -2,6 +2,10 @@ import os
|
||||
from args import args
|
||||
from PIL import Image
|
||||
import struct
|
||||
import sys
|
||||
from assethelpers import getAssetRelativePath
|
||||
|
||||
PALETTES = []
|
||||
|
||||
def extractPaletteFromImage(image):
|
||||
# goes through and finds all unique colors in the image
|
||||
@@ -10,6 +14,9 @@ def extractPaletteFromImage(image):
|
||||
pixels = list(image.getdata())
|
||||
uniqueColors = []
|
||||
for color in pixels:
|
||||
# We treat alpha 0 as rgba(0,0,0,0) for palette purposes
|
||||
if color[3] == 0:
|
||||
color = (0, 0, 0, 0)
|
||||
if color not in uniqueColors:
|
||||
uniqueColors.append(color)
|
||||
return uniqueColors
|
||||
@@ -40,21 +47,70 @@ def processPalette(assetPath):
|
||||
|
||||
outputRelative = os.path.relpath(outputFilePath, args.output_assets)
|
||||
|
||||
return {
|
||||
palette = {
|
||||
"outputFile": outputRelative,
|
||||
"paletteColors": len(pixels),
|
||||
"files": [ outputFilePath ]
|
||||
"files": [ outputFilePath ],
|
||||
"pixels": pixels
|
||||
}
|
||||
|
||||
PALETTES.append(palette)
|
||||
|
||||
return palette
|
||||
|
||||
def processImage(assetPath):
|
||||
print(f"Processing image: {assetPath}")
|
||||
|
||||
# Load the image
|
||||
image = Image.open(assetPath)
|
||||
pixels = extractPaletteFromImage(image)
|
||||
|
||||
# Get the image's palette because we are going to try and find a matching
|
||||
# palette from the already processed palettes...
|
||||
imagePalette = extractPaletteFromImage(image)
|
||||
|
||||
# Now find which palette has every single color in this image's palette
|
||||
paletteIndex = -1
|
||||
for i, palette in enumerate(PALETTES):
|
||||
paletteColors = palette["pixels"]
|
||||
if all(color in paletteColors for color in imagePalette):
|
||||
paletteIndex = i
|
||||
break
|
||||
|
||||
# Did we manage to find a matching palette?
|
||||
if paletteIndex == -1:
|
||||
print(f"Error: No matching palette found for image {assetPath}. Please process a suitable palette first.")
|
||||
sys.exit(1)
|
||||
|
||||
# We found the palette, so now we can convert the image to use that palette
|
||||
palette = PALETTES[paletteIndex]
|
||||
indexes = []
|
||||
for color in imagePalette:
|
||||
if color in palette["pixels"]:
|
||||
index = palette["pixels"].index(color)
|
||||
indexes.append(index)
|
||||
else:
|
||||
print(f"Error: Color {color} in image {assetPath} not found in palette.")
|
||||
sys.exit(1)
|
||||
|
||||
relative = getAssetRelativePath(assetPath)
|
||||
fileNameWithoutExt = os.path.splitext(os.path.basename(assetPath))[0]
|
||||
outputFileRelative = os.path.join(os.path.dirname(relative), f"{fileNameWithoutExt}.dpi")
|
||||
outputFilePath = os.path.join(args.output_assets, outputFileRelative)
|
||||
os.makedirs(os.path.dirname(outputFilePath), exist_ok=True)
|
||||
with open(outputFilePath, "wb") as f:
|
||||
# Write header
|
||||
f.write(b"DPI") # Dusk Palettized Image
|
||||
f.write(struct.pack("<i", image.width)) # Width
|
||||
f.write(struct.pack("<i", image.height)) # Height
|
||||
f.write(struct.pack("<i", paletteIndex)) # Palette index
|
||||
|
||||
# Write uint8_t pixel index.
|
||||
for index in indexes:
|
||||
f.write(struct.pack("B", index))
|
||||
|
||||
return {
|
||||
# "outputFile": os.path.relpath(assetPath, args.input_assets),
|
||||
# "paletteColors": len(pixels),
|
||||
"files": [ assetPath ]
|
||||
"paletteIndex": paletteIndex,
|
||||
"palette": PALETTES[paletteIndex],
|
||||
"outputFile": outputFileRelative,
|
||||
"files": [ assetPath, outputFilePath ]
|
||||
}
|
@@ -47,32 +47,28 @@ def processTileset(assetPath):
|
||||
relativeFile = getAssetRelativePath(assetPath)
|
||||
relativeDir = os.path.dirname(relativeFile)
|
||||
|
||||
data = "DTF" # Header for Dusk Tileset Format
|
||||
# Write width (int32_t)
|
||||
data += tilewidth.to_bytes(4, byteorder='little').decode('latin1')
|
||||
# Write height (int32_t)
|
||||
data += tileheight.to_bytes(4, byteorder='little').decode('latin1')
|
||||
# Write tilecount (int32_t)
|
||||
data += tilecount.to_bytes(4, byteorder='little').decode('latin1')
|
||||
# Write column count (int32_t)
|
||||
data += columns.to_bytes(4, byteorder='little').decode('latin1')
|
||||
# Write row count (int32_t)
|
||||
rows = (tilecount + columns - 1) // columns
|
||||
data += rows.to_bytes(4, byteorder='little').decode('latin1')
|
||||
|
||||
buf = bytearray(b"DTF")
|
||||
buf += tilewidth.to_bytes(4, byteorder='little')
|
||||
buf += tileheight.to_bytes(4, byteorder='little')
|
||||
buf += tilecount.to_bytes(4, byteorder='little')
|
||||
buf += columns.to_bytes(4, byteorder='little')
|
||||
buf += rows.to_bytes(4, byteorder='little')
|
||||
|
||||
# Write image source file name, padd to ASSET_FILE_NAME_MAX_LENGTH
|
||||
imageSourceBytes = image["outputFile"].encode('utf-8')
|
||||
data += len(imageSourceBytes).to_bytes(4, byteorder='little').decode('latin1')
|
||||
data += imageSourceBytes.decode('latin1')
|
||||
buf += len(imageSourceBytes).to_bytes(4, byteorder='little')
|
||||
buf += imageSourceBytes
|
||||
paddingLength = max(0, ASSET_FILE_NAME_MAX_LENGTH - len(imageSourceBytes))
|
||||
data += '\x00' * paddingLength
|
||||
buf += b'\x00' * paddingLength
|
||||
|
||||
# Write to output file
|
||||
fileNameWithoutExt = os.path.splitext(os.path.basename(assetPath))[0]
|
||||
outputFilePath = os.path.join(args.output_assets, relativeDir, f"{fileNameWithoutExt}.dtf")
|
||||
os.makedirs(os.path.dirname(outputFilePath), exist_ok=True)
|
||||
with open(outputFilePath, 'wb') as f:
|
||||
f.write(data.encode('latin1'))
|
||||
f.write(buf)
|
||||
|
||||
return {
|
||||
"outputFile": os.path.relpath(outputFilePath, args.output_assets),
|
||||
@@ -81,5 +77,6 @@ def processTileset(assetPath):
|
||||
"tileCount": tilecount,
|
||||
"columns": columns,
|
||||
"rows": rows,
|
||||
"image": image
|
||||
"image": image,
|
||||
"files": [ outputFilePath ] + image["files"]
|
||||
}
|
Reference in New Issue
Block a user