aboutsummaryrefslogtreecommitdiffstats
path: root/tools/palfix.py
diff options
context:
space:
mode:
authorRangi42 <remy.oukaour+rangi42@gmail.com>2024-10-07 16:48:18 -0400
committerRangi42 <remy.oukaour+rangi42@gmail.com>2024-10-07 16:48:18 -0400
commitb52b44f8834fd64d7f279804585e0a554cf61d5b (patch)
tree7081a0f36d4bcb82786cb97361da03f48bb1e0e6 /tools/palfix.py
parentComment wave5 garbage data (#474) (diff)
downloadpokeyellow-b52b44f8834fd64d7f279804585e0a554cf61d5b.tar.gz
pokeyellow-b52b44f8834fd64d7f279804585e0a554cf61d5b.tar.xz
pokeyellow-b52b44f8834fd64d7f279804585e0a554cf61d5b.zip
Remove independent tools and link to pokemon-asm-tools
Diffstat (limited to 'tools/palfix.py')
-rwxr-xr-xtools/palfix.py63
1 files changed, 0 insertions, 63 deletions
diff --git a/tools/palfix.py b/tools/palfix.py
deleted file mode 100755
index 3a997d54..00000000
--- a/tools/palfix.py
+++ /dev/null
@@ -1,63 +0,0 @@
-#!/usr/bin/env python3
-# -*- coding: utf-8 -*-
-
-"""
-Usage: python palfix.py image.png
-
-Fix the palette format of the input image to two-bit grayscale.
-Colored images will have their palette sorted {white, light
-color, dark color, black}.
-"""
-
-import sys
-
-import png
-
-def rgb8_to_rgb5(c):
- r, g, b = c
- return (r // 8, g // 8, b // 8)
-
-def invert(c):
- r, g, b = c
- return (31 - r, 31 - g, 31 - b)
-
-def luminance(c):
- r, g, b = c
- return 0.299 * r**2 + 0.587 * g**2 + 0.114 * b**2
-
-def rgb5_pixels(row):
- yield from (rgb8_to_rgb5(row[x:x+3]) for x in range(0, len(row), 4))
-
-def fix_pal(filename):
- with open(filename, 'rb') as file:
- width, height, rows = png.Reader(file).asRGBA8()[:3]
- rows = list(rows)
- b_and_w = {(0, 0, 0), (31, 31, 31)}
- colors = {c for row in rows for c in rgb5_pixels(row)} - b_and_w
- if not colors:
- colors = {(21, 21, 21), (10, 10, 10)}
- elif len(colors) == 1:
- c = colors.pop()
- colors = {c, invert(c)}
- elif len(colors) != 2:
- return False
- palette = tuple(sorted(colors | b_and_w, key=luminance, reverse=True))
- assert len(palette) == 4
- rows = [[3 - palette.index(c) for c in rgb5_pixels(row)] for row in rows]
- writer = png.Writer(width, height, greyscale=True, bitdepth=2, compression=9)
- with open(filename, 'wb') as file:
- writer.write(file, rows)
- return True
-
-def main():
- if len(sys.argv) < 2:
- print(f'Usage: {sys.argv[0]} pic.png', file=sys.stderr)
- sys.exit(1)
- for filename in sys.argv[1:]:
- if not filename.lower().endswith('.png'):
- print(f'{filename} is not a .png file!', file=sys.stderr)
- elif not fix_pal(filename):
- print(f'{filename} has too many colors!', file=sys.stderr)
-
-if __name__ == '__main__':
- main()