mirror of
https://gitlab.com/drummyfish/anarch.git
synced 2024-12-04 06:32:19 -05:00
Start image compression
This commit is contained in:
parent
74358d3aa5
commit
dbef3b3d57
@ -16,6 +16,7 @@ def printHelp():
|
|||||||
print(" -nS use the name S for the texture (defaut: \"texture\")")
|
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(" -pT use palette from file T and indexed colors (otherwise direct colors)")
|
||||||
print(" -5 use 565 format instead of RGB8")
|
print(" -5 use 565 format instead of RGB8")
|
||||||
|
print(" -c compress (4 bpp, 16 color palette), only with -pT")
|
||||||
print(" -t transpose (store by columns)")
|
print(" -t transpose (store by columns)")
|
||||||
print("");
|
print("");
|
||||||
print("by Miloslav \"drummyfish\" Ciz")
|
print("by Miloslav \"drummyfish\" Ciz")
|
||||||
@ -37,6 +38,7 @@ OUT_WIDTH = 64
|
|||||||
OUT_HEIGHT = 64
|
OUT_HEIGHT = 64
|
||||||
USE_565 = False
|
USE_565 = False
|
||||||
TRANSPOSE = False
|
TRANSPOSE = False
|
||||||
|
COMPRESS = False
|
||||||
|
|
||||||
for s in sys.argv:
|
for s in sys.argv:
|
||||||
if s [:2] == "-x":
|
if s [:2] == "-x":
|
||||||
@ -54,9 +56,14 @@ for s in sys.argv:
|
|||||||
USE_PALETTE = True
|
USE_PALETTE = True
|
||||||
elif s[:2] == "-t":
|
elif s[:2] == "-t":
|
||||||
TRANSPOSE = True
|
TRANSPOSE = True
|
||||||
|
elif s[:2] == "-c":
|
||||||
|
COMPRESS = True
|
||||||
else:
|
else:
|
||||||
FILENAME = s
|
FILENAME = s
|
||||||
|
|
||||||
|
if not USE_PALETTE:
|
||||||
|
COMPRESS = False
|
||||||
|
|
||||||
imageArray = []
|
imageArray = []
|
||||||
paletteColors = []
|
paletteColors = []
|
||||||
paletteArray = []
|
paletteArray = []
|
||||||
@ -83,18 +90,7 @@ if USE_PALETTE > 0:
|
|||||||
image2 = Image.new("RGB",(OUT_WIDTH,OUT_HEIGHT),color="white")
|
image2 = Image.new("RGB",(OUT_WIDTH,OUT_HEIGHT),color="white")
|
||||||
pixels2 = image2.load()
|
pixels2 = image2.load()
|
||||||
|
|
||||||
for y in range(OUT_HEIGHT):
|
def findClosestColor(pixel,paletteColors):
|
||||||
for x in range(OUT_WIDTH):
|
|
||||||
x2 = y if TRANSPOSE else x
|
|
||||||
y2 = x if TRANSPOSE else y
|
|
||||||
|
|
||||||
coord = (
|
|
||||||
int(x2 / float(OUT_WIDTH) * image.size[0]),
|
|
||||||
int(y2 / float(OUT_HEIGHT) * image.size[1]))
|
|
||||||
|
|
||||||
pixel = pixels[coord]
|
|
||||||
|
|
||||||
if USE_PALETTE:
|
|
||||||
closestIndex = 0
|
closestIndex = 0
|
||||||
closestDiff = 1024
|
closestDiff = 1024
|
||||||
|
|
||||||
@ -108,6 +104,21 @@ for y in range(OUT_HEIGHT):
|
|||||||
closestIndex = i
|
closestIndex = i
|
||||||
closestDiff = diff
|
closestDiff = diff
|
||||||
|
|
||||||
|
return closestIndex
|
||||||
|
|
||||||
|
for y in range(OUT_HEIGHT):
|
||||||
|
for x in range(OUT_WIDTH):
|
||||||
|
x2 = y if TRANSPOSE else x
|
||||||
|
y2 = x if TRANSPOSE else y
|
||||||
|
|
||||||
|
coord = (
|
||||||
|
int(x2 / float(OUT_WIDTH) * image.size[0]),
|
||||||
|
int(y2 / float(OUT_HEIGHT) * image.size[1]))
|
||||||
|
|
||||||
|
pixel = pixels[coord]
|
||||||
|
|
||||||
|
if USE_PALETTE:
|
||||||
|
closestIndex = findClosestColor(pixel,paletteColors)
|
||||||
imageArray.append(closestIndex)
|
imageArray.append(closestIndex)
|
||||||
pixels2[x,y] = paletteColors[closestIndex]
|
pixels2[x,y] = paletteColors[closestIndex]
|
||||||
else:
|
else:
|
||||||
@ -122,6 +133,46 @@ for y in range(OUT_HEIGHT):
|
|||||||
|
|
||||||
#-----------------------
|
#-----------------------
|
||||||
|
|
||||||
|
def compressImageArray(a):
|
||||||
|
result = []
|
||||||
|
|
||||||
|
reducedPalette = []
|
||||||
|
|
||||||
|
histogram = [0 for i in range(256)]
|
||||||
|
|
||||||
|
for c in a:
|
||||||
|
histogram[c] += 1
|
||||||
|
|
||||||
|
for i in range(16):
|
||||||
|
maxValue = 0
|
||||||
|
maxIndex = 0
|
||||||
|
|
||||||
|
for j in range(256):
|
||||||
|
if histogram[j] > maxValue:
|
||||||
|
maxValue = histogram[j]
|
||||||
|
maxIndex = j
|
||||||
|
|
||||||
|
reducedPalette.append(maxIndex)
|
||||||
|
histogram[maxIndex] = 0
|
||||||
|
|
||||||
|
paletteMap = [findClosestColor(paletteColors[i],[paletteColors[j] for j in reducedPalette]) for i in range(256)]
|
||||||
|
|
||||||
|
oneByte = 0
|
||||||
|
byteCount = 0
|
||||||
|
|
||||||
|
for c in a:
|
||||||
|
if byteCount % 2 == 0:
|
||||||
|
oneByte = paletteMap[c]
|
||||||
|
else:
|
||||||
|
oneByte = (oneByte << 4) | paletteMap[c]
|
||||||
|
result.append(oneByte)
|
||||||
|
|
||||||
|
byteCount += 1
|
||||||
|
|
||||||
|
result = reducedPalette + result
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
def printArray(array, name, sizeString, dataType="const uint8_t"):
|
def printArray(array, name, sizeString, dataType="const uint8_t"):
|
||||||
print(dataType + " " + name + "[" + sizeString + "] = {")
|
print(dataType + " " + name + "[" + sizeString + "] = {")
|
||||||
arrayString = ""
|
arrayString = ""
|
||||||
@ -154,6 +205,9 @@ print("#define " + NAME.upper() + "_TEXTURE_WIDTH " + str(OUT_WIDTH))
|
|||||||
print("#define " + NAME.upper() + "_TEXTURE_HEIGHT " + str(OUT_HEIGHT))
|
print("#define " + NAME.upper() + "_TEXTURE_HEIGHT " + str(OUT_HEIGHT))
|
||||||
print("")
|
print("")
|
||||||
|
|
||||||
|
if COMPRESS:
|
||||||
|
imageArray = compressImageArray(imageArray)
|
||||||
|
|
||||||
printArray(imageArray,NAME + "Texture",str(len(imageArray)))
|
printArray(imageArray,NAME + "Texture",str(len(imageArray)))
|
||||||
|
|
||||||
if GUARDS:
|
if GUARDS:
|
||||||
|
BIN
assets/wall_texture0.png
Normal file
BIN
assets/wall_texture0.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
Loading…
Reference in New Issue
Block a user