Skip to content

Commit 2e93fff

Browse files
committed
Replace PyGame dependency with pillow
PyGame is used to load manipulate and save image data idependently of their file format. The PyGame project is fairly big and has caused various unexpected logging in the tools, so replace it with pillow, which is sufficient for our use case. Also expose OOP and functional API for tiletool, so it can be consumed by custom tools.
1 parent c3d167e commit 2e93fff

9 files changed

Lines changed: 691 additions & 496 deletions

File tree

.github/scripts/build-msys2.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ autoreconf -iv
1111
--target=${MSYSTEM_CHOST} \
1212
--enable-external-toolchain \
1313
--enable-external-emudbg \
14-
--enable-external-gngeo \
15-
--enable-examples=no
14+
--enable-external-gngeo
1615
make
1716
make install

.github/workflows/build-and-publish.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747

4848
- name: Install dependencies for ngdevkit-examples
4949
run: >
50-
sudo apt-get install -y python3-yaml ngdevkit-gngeo python3-pygame imagemagick
50+
sudo apt-get install -y python3-yaml ngdevkit-gngeo python3-pil imagemagick
5151
sox libsox-fmt-mp3 libglew-dev libsdl2-dev
5252
5353
- name: Test by compiling ngdevkit-examples
@@ -86,6 +86,16 @@ jobs:
8686
- name: Build
8787
run: ./.github/scripts/build-msys2.sh
8888

89+
- name: Install dependencies for ngdevkit-examples
90+
run: |
91+
pacman -S --noconfirm pactoys rsync
92+
pacboy -S --noconfirm python:u python-pillow:u toolchain:u zlib:u sox:u imagemagick:u
93+
pip install python-yaml
94+
95+
- name: Test by compiling ngdevkit-examples
96+
run: ./.github/scripts/test.sh
97+
98+
8999
macos:
90100
name: "macOS build"
91101
runs-on: macos-15
@@ -100,8 +110,8 @@ jobs:
100110
run: |
101111
brew tap dciabrin/ngdevkit
102112
brew install python3 --overwrite
113+
brew install pillow
103114
brew install automake ngdevkit-toolchain zip pkg-config sdl2 sdl2_image
104-
$(brew --prefix python)/libexec/bin/pip install pygame --break-system-packages
105115
106116
- name: Build
107117
run: ./.github/scripts/build.sh

.github/workflows/build-tests.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747

4848
- name: Install dependencies for ngdevkit-examples
4949
run: >
50-
sudo apt-get install -y python3-yaml ngdevkit-gngeo python3-pygame imagemagick
50+
sudo apt-get install -y python3-yaml ngdevkit-gngeo python3-pil imagemagick
5151
sox libsox-fmt-mp3 libglew-dev libsdl2-dev
5252
5353
- name: Test by compiling ngdevkit-examples
@@ -86,6 +86,16 @@ jobs:
8686
- name: Build
8787
run: ./.github/scripts/build-msys2.sh
8888

89+
- name: Install dependencies for ngdevkit-examples
90+
run: |
91+
pacman -S --noconfirm pactoys rsync
92+
pacboy -S --noconfirm python:u python-pillow:u toolchain:u zlib:u sox:u imagemagick:u
93+
pip install python-yaml
94+
95+
- name: Test by compiling ngdevkit-examples
96+
run: ./.github/scripts/test.sh
97+
98+
8999
macos:
90100
name: "macOS build"
91101
runs-on: macos-15
@@ -100,8 +110,8 @@ jobs:
100110
run: |
101111
brew tap dciabrin/ngdevkit
102112
brew install python3 --overwrite
113+
brew install pillow
103114
brew install automake ngdevkit-toolchain zip pkg-config sdl2 sdl2_image
104-
$(brew --prefix python)/libexec/bin/pip install pygame --break-system-packages
105115
106116
- name: Build
107117
run: ./.github/scripts/build.sh

README-linux.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,26 @@
55
In order to build the devkit you need autoconf, autoconf-archive and
66
GNU Make 4.x, and various additional dependencies to build the
77
toolchain modules such as GCC and SDCC. The devkit tools uses Python 3
8-
and PyGame 2. The emulator requires SDL 2 and optionally OpenGL
9-
libraries.
8+
and various dependencies (such as pillow and PyYAML).
9+
The emulator requires SDL 2 and optionally OpenGL libraries.
10+
11+
Note that older version of the devkit and its example ROMs depended
12+
on PyGame, which has now been replaced by pillow. So please make
13+
sure to use the latest version of ngdevkit-examples for building ROMs.
1014

1115
The examples require ImageMagick for all the graphics
1216
trickery and sox for audio conversion purpose.
1317

14-
For example, on Ubuntu Focal (20.04), you can install the dependencies with:
18+
For example, on a recent Ubuntu version, you can install the dependencies with:
1519

1620
apt-get install autoconf autoconf-archive automake gcc curl zip unzip
1721
apt-get install libsdl2-dev
18-
apt-get install python3-pygame
22+
apt-get install python3-pil
1923
apt-get install libreadline-dev
2024
GCC_VERSION_PKG=$(apt-cache depends gcc | awk '/Depends.*gcc/ {print $2}')
2125
# make sure you have src packages enabled for dependency information
22-
echo "deb-src http://archive.ubuntu.com/ubuntu/ focal main restricted" > /etc/apt/sources.list.d/ngdevkit.list
23-
echo "deb-src http://archive.ubuntu.com/ubuntu/ focal universe" >> /etc/apt/sources.list.d/ngdevkit.list
26+
echo "deb-src http://archive.ubuntu.com/ubuntu/ noble main restricted" > /etc/apt/sources.list.d/ngdevkit.list
27+
echo "deb-src http://archive.ubuntu.com/ubuntu/ noble universe" >> /etc/apt/sources.list.d/ngdevkit.list
2428
apt-get update
2529
# install build-dependency packages
2630
apt-get build-dep $GCC_VERSION_PKG
@@ -33,7 +37,7 @@ For example, on Ubuntu Focal (20.04), you can install the dependencies with:
3337
Debian users (for example Buster) would install the dependencies above
3438
by updating the src packages URL as follows:
3539

36-
echo "deb-src http://deb.debian.org/debian buster main" > /etc/apt/sources.list.d/ngdevkit.list
40+
echo "deb-src http://deb.debian.org/debian trixie main" > /etc/apt/sources.list.d/ngdevkit.list
3741
# the remaining dependencies are the same
3842

3943
## Building the toolchain

README-macos.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@ later in this documentation.
1717
The devkit uses Python 3, which is installed globally in macOS but
1818
also in brew. To make sure you are installing python dependencies
1919
in the right python distribution, you need to have brew's python
20-
first in your PATH, as explained later in the document.
20+
first in your PATH.
2121

2222

2323
## Pre-requisite
2424

2525
In order to build the devkit you need autoconf, autoconf-archive and
2626
GNU Make 4.x, and various additional dependencies to build the
2727
toolchain modules such as GCC and SDCC. The devkit tools uses Python 3
28-
and PyGame 2. The emulator requires SDL 2 and optionally OpenGL
29-
libraries.
28+
and various dependencies (such as pillow and PyYAML). The emulator
29+
requires SDL 2 and optionally OpenGL libraries.
3030

3131
The examples require ImageMagick for all the graphics
3232
trickery and sox for audio conversion purpose.
3333

3434
Make sure that `brew` is in your PATH and its environment variables are
3535
set up properly. By default, brew is installed in `/usr/local/bin/brew`
3636
on Intel macs, and in `/opt/homebrew/bin/brew` on ARM macs. If `brew`
37-
is not found in your PATH, you must initialize your it with:
37+
is not found in your PATH, you must initialize it with:
3838

3939
eval $(/opt/homebrew/bin/brew shellenv)
4040
# Intel macs would use /usr/local/bin/brew shellenv
@@ -43,25 +43,25 @@ Then, ngdevkit's dependencies are installed as follows:
4343

4444
brew update
4545
brew install gmake
46-
brew install autoconf-archive
47-
brew install glew sdl2 sdl2_image
48-
brew install python3
49-
# make sure you use brew's python3 to install pygame
50-
# if not, update your path like e.g.
51-
# export PATH=$HOMEBREW_PREFIX/opt/python3/bin:$PATH
52-
pip3 install pygame --break-system-packages
46+
brew install python3 pillow
5347
brew deps gcc | xargs brew install
5448
brew deps sdcc | xargs brew install
5549
# dependencies for the example ROMs
50+
brew install autoconf-archive
51+
brew install glew sdl2 sdl2_image
5652
brew install zip imagemagick sox
5753

54+
Note that older version of the devkit and its example ROMs depended
55+
on PyGame, which has now been replaced by pillow. So please make
56+
sure to use the latest version of ngdevkit-examples for building ROMs.
5857

5958
## Building the toolchain
6059

6160
In order to build ngdevkit, the installed brew dependencies must
6261
be available to the compiler, so you must add brew's PATH into
6362
your build flags manually:
6463

64+
HOMEBREW_PREFIX=$(brew --prefix)
6565
export CFLAGS="-I${HOMEBREW_PREFIX}/include${CFLAGS+ ${CFLAGS}}"
6666
export CXXFLAGS="-I${HOMEBREW_PREFIX}/include${CXXFLAGS+ ${CXXFLAGS}}"
6767
export CPPFLAGS="-I${HOMEBREW_PREFIX}/include${CPPFLAGS+ ${CPPFLAGS}}"

README-msys2.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,24 @@ ngdevkit dependencies:
6363
pacboy -S autoconf autoconf-archive automake pkgconf make tar zip unzip
6464
pacboy -S git flex bison expat gettext ncurses-devel zlib-devel
6565
pacboy -S gmp-devel isl-devel mpc-devel mpfr-devel texinfo
66-
pacboy -S python:u python-pygame:u toolchain:u zlib:u SDL2:u glew:u boost:u
66+
pacboy -S python:u python-yaml python-pillow:u toolchain:u zlib:u
6767
# dependencies for the example ROMs
68-
pacboy -S rsync sox:u imagemagick:u
68+
pacboy -S rsync sox:u imagemagick:u SDL2:u glew:u boost:u
69+
70+
Note that older version of the devkit and its example ROMs depended
71+
on PyGame, which has now been replaced by pillow. So please make
72+
sure to use the latest version of ngdevkit-examples for building ROMs.
6973

7074
You can now compile the entire devkit. The latest ngdevkit's autoconf
7175
script should detect the location of all dependencies (python,
72-
pygame...) automatically, as long as you're running it in a UCRT64
76+
pillow, ...) automatically, as long as you're running it in a UCRT64
7377
environment as required.
7478

7579
autoreconf -iv
7680
./configure --prefix=$PWD/local
7781
make
7882
make install
7983

80-
8184
The most tedious part is over! You can now configure your environment
8285
to add the built binaries to your `PATH` and start experimenting
8386
with the devkit and build the example ROMs:
@@ -95,5 +98,4 @@ how to run the examples and the debugger.
9598
[msys2]: https://www.msys2.org
9699
[examples]: https://github.com/dciabrin/ngdevkit-examples
97100
[pywin]: https://www.python.org/downloads/windows
98-
[pygame]: https://www.pygame.org
99101
[subsys]: https://www.msys2.org/docs/environments

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ of dependencies for the examples ROMs:
5656
add-apt-repository -y ppa:dciabrin/ngdevkit
5757
apt update
5858
apt install ngdevkit ngdevkit-gngeo
59-
# the remaining packages are only requred for the examples
59+
# the remaining packages are only required for the examples
6060
apt install pkg-config autoconf zip imagemagick sox libsox-fmt-mp3
6161

6262
If you are running a Fedora distribution, pre-built packages are
@@ -79,9 +79,6 @@ packages, available in the ngdevkit tap:
7979
# install ngdevkit
8080
brew tap dciabrin/ngdevkit
8181
brew install ngdevkit ngdevkit-gngeo
82-
# make sure you use brew's python3 in your shell
83-
export PATH=$HOMEBREW_PREFIX/opt/python3/bin:$PATH
84-
pip3 install pygame pyyaml --break-system-packages
8582
# the remaining packages are only required for the examples
8683
brew install pkg-config autoconf automake zip imagemagick sox
8784

@@ -99,7 +96,6 @@ configure the pacman repository as follows:
9996

10097
MSYSTEM=UCRT64 /usr/bin/bash -l
10198
echo -e "\n[ngdevkit]\nSigLevel = Optional TrustAll\nServer = https://dciabrin.net/msys2-ngdevkit/\$arch" >> /etc/pacman.conf
102-
10399
# install pacboy with `pacman -S pactoys` if necessary
104100
pacboy -Sy
105101
pacboy -S ngdevkit:u ngdevkit-gngeo:u

tools/paltool.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
# Copyright (c) 2018-2019 Damien Ciabrini
2+
# Copyright (c) 2018-2026 Damien Ciabrini
33
# This file is part of ngdevkit
44
#
55
# ngdevkit is free software: you can redistribute it and/or modify
@@ -20,18 +20,10 @@
2020
Extract palette from a 2D image and convert it to C or ASM data.
2121
"""
2222

23-
from __future__ import print_function
24-
import struct
25-
import os
26-
import sys
2723
import argparse
28-
from random import randint
29-
30-
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'
31-
os.environ['SDL_VIDEODRIVER'] = 'dummy'
32-
os.environ['SDL_AUDIODRIVER'] = 'dummy'
33-
import pygame
3424

25+
from PIL import Image
26+
from itertools import batched
3527

3628
def rgb24_to_packed15(col):
3729
r, g, b = [c >> 2 for c in col]
@@ -62,15 +54,18 @@ def main():
6254

6355
arguments = parser.parse_args()
6456

65-
img = pygame.image.load(arguments.FILE)
57+
img = Image.open(arguments.FILE)
6658

67-
# only keep the first 16 colors, even if palette has other
68-
# colors initialized but unused.
69-
pal = img.get_palette()[:16]
59+
# extract the palette as a 24bits RGB
60+
pal_ints = img.getpalette()
61+
pal_rgb = list(batched(pal_ints, 3))
62+
# make sure the palette has 16colors
63+
if len(pal_rgb) > 16:
64+
pal_rgb = pal_rgb[:16]
65+
elif len(pal_rgb) < 16:
66+
pal_rgb.extend([(0, 0, 0)] * (16 - len(pal_rgb)))
7067

71-
# for the time being, do not the 16th bit available in
72-
# the Neo Geo hardware
73-
ngpal = [rgb24_to_packed15(c) for c in pal]
68+
ngpal = [rgb24_to_packed15(c) for c in pal_rgb]
7469

7570
out = open(arguments.output, "w")
7671
output_c_anonymous_palette(ngpal, out)

0 commit comments

Comments
 (0)