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