mirror of
https://gitlab.com/drummyfish/anarch.git
synced 2024-12-03 06:02:15 -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(" -pT use palette from file T and indexed colors (otherwise direct colors)")
|
||||
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("");
|
||||
print("by Miloslav \"drummyfish\" Ciz")
|
||||
@ -37,6 +38,7 @@ OUT_WIDTH = 64
|
||||
OUT_HEIGHT = 64
|
||||
USE_565 = False
|
||||
TRANSPOSE = False
|
||||
COMPRESS = False
|
||||
|
||||
for s in sys.argv:
|
||||
if s [:2] == "-x":
|
||||
@ -54,9 +56,14 @@ for s in sys.argv:
|
||||
USE_PALETTE = True
|
||||
elif s[:2] == "-t":
|
||||
TRANSPOSE = True
|
||||
elif s[:2] == "-c":
|
||||
COMPRESS = True
|
||||
else:
|
||||
FILENAME = s
|
||||
|
||||
if not USE_PALETTE:
|
||||
COMPRESS = False
|
||||
|
||||
imageArray = []
|
||||
paletteColors = []
|
||||
paletteArray = []
|
||||
@ -83,6 +90,22 @@ if USE_PALETTE > 0:
|
||||
image2 = Image.new("RGB",(OUT_WIDTH,OUT_HEIGHT),color="white")
|
||||
pixels2 = image2.load()
|
||||
|
||||
def findClosestColor(pixel,paletteColors):
|
||||
closestIndex = 0
|
||||
closestDiff = 1024
|
||||
|
||||
# find the index of the closest color:
|
||||
|
||||
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])
|
||||
|
||||
if diff < closestDiff:
|
||||
closestIndex = i
|
||||
closestDiff = diff
|
||||
|
||||
return closestIndex
|
||||
|
||||
for y in range(OUT_HEIGHT):
|
||||
for x in range(OUT_WIDTH):
|
||||
x2 = y if TRANSPOSE else x
|
||||
@ -95,19 +118,7 @@ for y in range(OUT_HEIGHT):
|
||||
pixel = pixels[coord]
|
||||
|
||||
if USE_PALETTE:
|
||||
closestIndex = 0
|
||||
closestDiff = 1024
|
||||
|
||||
# find the index of the closest color:
|
||||
|
||||
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])
|
||||
|
||||
if diff < closestDiff:
|
||||
closestIndex = i
|
||||
closestDiff = diff
|
||||
|
||||
closestIndex = findClosestColor(pixel,paletteColors)
|
||||
imageArray.append(closestIndex)
|
||||
pixels2[x,y] = paletteColors[closestIndex]
|
||||
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"):
|
||||
print(dataType + " " + name + "[" + sizeString + "] = {")
|
||||
arrayString = ""
|
||||
@ -153,6 +204,9 @@ if USE_PALETTE:
|
||||
print("#define " + NAME.upper() + "_TEXTURE_WIDTH " + str(OUT_WIDTH))
|
||||
print("#define " + NAME.upper() + "_TEXTURE_HEIGHT " + str(OUT_HEIGHT))
|
||||
print("")
|
||||
|
||||
if COMPRESS:
|
||||
imageArray = compressImageArray(imageArray)
|
||||
|
||||
printArray(imageArray,NAME + "Texture",str(len(imageArray)))
|
||||
|
||||
|
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