Files
Black-Netherite-Resource-Pack
Smooth-Block-Sounds-Resource-Pack
Code
8 515
Black-Netherite-Resource-Pack/generate_textures.py
import struct
import zlib
import zipfile
from pathlib import Path
def png_chunk(chunk_type, data):
return (
struct.pack(">I", len(data))
+ chunk_type
+ data
+ struct.pack(">I", zlib.crc32(chunk_type + data) & 0xFFFFFFFF)
)
def decode_png(png_data):
if png_data[:8] != b"\x89PNG\r\n\x1a\n":
raise ValueError("The vanilla texture is not a PNG file")
position = 8
idat = bytearray()
width = height = color_type = bit_depth = None
palette = None
transparency = None
while position < len(png_data):
length = struct.unpack(">I", png_data[position:position + 4])[0]
chunk_type = png_data[position + 4:position + 8]
data = png_data[position + 8:position + 8 + length]
position += 12 + length
if chunk_type == b"IHDR":
width, height, bit_depth, color_type, compression, filtering, interlace = struct.unpack(
">IIBBBBB", data
)
if (
color_type not in (0, 2, 3, 4, 6)
or bit_depth not in ((1, 2, 4, 8) if color_type in (0, 3) else (8,))
or compression
or filtering
or interlace
):
raise ValueError(
"Unsupported vanilla PNG format "
f"(bit depth {bit_depth}, color type {color_type})"
)
elif chunk_type == b"PLTE":
palette = [tuple(data[index:index + 3]) for index in range(0, len(data), 3)]
elif chunk_type == b"tRNS":
transparency = data
elif chunk_type == b"IDAT":
idat.extend(data)
elif chunk_type == b"IEND":
break
if width is None:
raise ValueError("The vanilla texture has no PNG header")
if color_type == 3 and palette is None:
raise ValueError("The vanilla indexed PNG has no color palette")
decoded = zlib.decompress(bytes(idat))
channel_count = {0: 1, 2: 3, 3: 1, 4: 2, 6: 4}[color_type]
bits_per_pixel = bit_depth * channel_count
bytes_per_pixel = max(1, (bits_per_pixel + 7) // 8)
stride = (width * bits_per_pixel + 7) // 8
rows = []
position = 0
def paeth(left, above, upper_left):
estimate = left + above - upper_left
distance_left = abs(estimate - left)
distance_above = abs(estimate - above)
distance_upper_left = abs(estimate - upper_left)
if distance_left <= distance_above and distance_left <= distance_upper_left:
return left
if distance_above <= distance_upper_left:
return above
return upper_left
previous = bytearray(stride)
for _ in range(height):
filter_type = decoded[position]
position += 1
current = bytearray(decoded[position:position + stride])
position += stride
for index in range(stride):
left = current[index - bytes_per_pixel] if index >= bytes_per_pixel else 0
above = previous[index]
upper_left = previous[index - bytes_per_pixel] if index >= bytes_per_pixel else 0
if filter_type == 1:
current[index] = (current[index] + left) & 255
elif filter_type == 2:
current[index] = (current[index] + above) & 255
elif filter_type == 3:
current[index] = (current[index] + ((left + above) // 2)) & 255
elif filter_type == 4:
current[index] = (current[index] + paeth(left, above, upper_left)) & 255
elif filter_type != 0:
raise ValueError("Unsupported PNG filter")
rgba = bytearray()
for pixel in range(width):
if color_type == 3:
bit_position = pixel * bit_depth
byte = current[bit_position // 8]
shift = 8 - bit_depth - (bit_position % 8)
palette_index = (byte >> shift) & ((1 << bit_depth) - 1)
red, green, blue = palette[palette_index]
alpha = transparency[palette_index] if transparency and palette_index < len(transparency) else 255
rgba.extend((red, green, blue, alpha))
continue
index = pixel * bytes_per_pixel
if color_type == 0:
gray = current[index]
alpha = 0 if transparency and bytes((gray,)) == transparency else 255
rgba.extend((gray, gray, gray, alpha))
elif color_type == 2:
red, green, blue = current[index:index + 3]
alpha = 255
if transparency and len(transparency) >= 6:
transparent_rgb = struct.unpack(">HHH", transparency[:6])
alpha = 0 if (red, green, blue) == tuple(value >> 8 for value in transparent_rgb) else 255
rgba.extend((red, green, blue, alpha))
elif color_type == 4:
gray, alpha = current[index:index + 2]
rgba.extend((gray, gray, gray, alpha))
else:
rgba.extend(current[index:index + 4])
rows.append(rgba)
previous = current
return width, height, rows
def encode_recolored_png(source_data, pixel_transform=None):
width, height, rows = decode_png(source_data)
recolored_rows = []
for y, row in enumerate(rows):
recolored = bytearray(row)
for index in range(0, len(recolored), 4):
red, green, blue = recolored[index:index + 3]
brightness = (red * 299 + green * 587 + blue * 114) // 1000
shade = max(2, min(28, brightness // 6))
recolored[index:index + 3] = bytes((shade, shade, shade))
if pixel_transform is not None:
pixel_transform(recolored, y, width)
recolored_rows.append(b"\x00" + bytes(recolored))
raw_data = b"".join(recolored_rows)
png_data = b"\x89PNG\r\n\x1a\n"
png_data += png_chunk(
b"IHDR",
struct.pack(">IIBBBBB", width, height, 8, 6, 0, 0, 0)
)
png_data += png_chunk(b"IDAT", zlib.compress(raw_data, 9))
png_data += png_chunk(b"IEND", b"")
return png_data
def shorten_sword_blade(pixels, y, width):
if y < 3:
for index in range(0, width * 4, 4):
pixels[index:index + 4] = b"\x00\x00\x00\x00"
def find_vanilla_texture(relative_path):
minecraft_path = Path.home() / "AppData/Roaming/.minecraft"
search_bases = [minecraft_path, *pack_path.parents]
version_directories = set()
for base in search_bases:
version_directories.add(base / "versions")
version_directories.add(base / ".minecraft/versions")
candidates = []
for versions_directory in version_directories:
if versions_directory.is_dir():
candidates.extend(versions_directory.glob("**/*.jar"))
candidates = sorted(
set(candidates),
key=lambda path: path.stat().st_mtime,
reverse=True,
)
for jar_path in candidates:
with zipfile.ZipFile(jar_path) as jar:
if relative_path in jar.namelist():
return jar.read(relative_path), jar_path
raise FileNotFoundError(
"Could not find the vanilla texture in the Minecraft versions folder: "
+ relative_path
)
def write_recolored_texture(source_path, output_paths, pixel_transform=None):
source_data, jar_path = find_vanilla_texture(source_path)
recolored_data = encode_recolored_png(source_data, pixel_transform)
for output_path in output_paths:
output_path.parent.mkdir(parents=True, exist_ok=True)
output_path.write_bytes(recolored_data)
return jar_path