1 #!/usr/bin/env python2 2 3 """ 4 Convert raw RGB888 images exported by The GIMP to RGB565 images. 5 6 Copyright (C) 2023 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT ANY 14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 15 PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License along 18 with this program. If not, see <http://www.gnu.org/licenses/>. 19 """ 20 21 import sys 22 23 # Permit big endian for 8-byte transfers. 24 25 big_endian = "--big-endian" in sys.argv[3:] 26 27 f = open(sys.argv[1]) 28 s = f.read() 29 f.close() 30 31 f = open(sys.argv[2], "w") 32 33 for i in xrange(0, len(s), 3): 34 r, g, b = map(ord, s[i:i+3]) 35 rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3) 36 msb = chr(rgb16 >> 8) 37 lsb = chr(rgb16 & 0xff) 38 39 if big_endian: 40 f.write(msb + lsb) 41 else: 42 f.write(lsb + msb) 43 44 f.close() 45 46 # vim: tabstop=4 expandtab shiftwidth=4