1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tools/rgb24to16.py Sun Nov 12 17:14:10 2023 +0100
1.3 @@ -0,0 +1,46 @@
1.4 +#!/usr/bin/env python2
1.5 +
1.6 +"""
1.7 +Convert raw RGB888 images exported by The GIMP to RGB565 images.
1.8 +
1.9 +Copyright (C) 2023 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT ANY
1.17 +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
1.18 +PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.19 +
1.20 +You should have received a copy of the GNU General Public License along
1.21 +with this program. If not, see <http://www.gnu.org/licenses/>.
1.22 +"""
1.23 +
1.24 +import sys
1.25 +
1.26 +# Permit big endian for 8-byte transfers.
1.27 +
1.28 +big_endian = "--big-endian" in sys.argv[3:]
1.29 +
1.30 +f = open(sys.argv[1])
1.31 +s = f.read()
1.32 +f.close()
1.33 +
1.34 +f = open(sys.argv[2], "w")
1.35 +
1.36 +for i in xrange(0, len(s), 3):
1.37 + r, g, b = map(ord, s[i:i+3])
1.38 + rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3)
1.39 + msb = chr(rgb16 >> 8)
1.40 + lsb = chr(rgb16 & 0xff)
1.41 +
1.42 + if big_endian:
1.43 + f.write(msb + lsb)
1.44 + else:
1.45 + f.write(lsb + msb)
1.46 +
1.47 +f.close()
1.48 +
1.49 +# vim: tabstop=4 expandtab shiftwidth=4