1.1 --- a/tools/makeimage.py Thu May 18 01:58:26 2017 +0200
1.2 +++ b/tools/makeimage.py Thu May 18 23:50:22 2017 +0200
1.3 @@ -2,7 +2,7 @@
1.4
1.5 """
1.6 Convert images for display in an Acorn Electron MODE 2 variant with a pixel
1.7 -layout of R0RGGGBB, giving 128 colours instead of the usual 8 colours.
1.8 +layout of I0RRGGBB, giving 128 colours instead of the usual 8 colours.
1.9
1.10 Copyright (C) 2015, 2017 Paul Boddie <paul@boddie.org.uk>
1.11
1.12 @@ -17,12 +17,6 @@
1.13
1.14 You should have received a copy of the GNU General Public License along
1.15 with this program. If not, see <http://www.gnu.org/licenses/>.
1.16 -
1.17 -----
1.18 -
1.19 -ImageMagick can be used to dither images to a 232 palette before conversion:
1.20 -
1.21 -convert in.png -ordered-dither threshold,4,8,4 out.png
1.22 """
1.23
1.24 from os.path import split, splitext
1.25 @@ -76,15 +70,19 @@
1.26
1.27 else:
1.28 r, g, b = data.next()
1.29 + rm, gm, bm, i = get_values(r, g, b)
1.30 +
1.31 + # Encode the byte value: I0RRGGBB.
1.32 +
1.33 word.insert(0,
1.34 - # R<7> -> D<7>
1.35 - (r & 0x80) |
1.36 - # R<6> -> D<5>
1.37 - ((r & 0x40) >> 1) |
1.38 - # G<7:5> -> D<4:2>
1.39 - ((g >> 5) << 2) |
1.40 + # I -> D<7>
1.41 + (i << 7) |
1.42 + # R<7:6> -> D<5:4>
1.43 + (rm >> 2) |
1.44 + # G<7:6> -> D<3:2>
1.45 + (gm >> 4) |
1.46 # B<7:6> -> D<1:0>
1.47 - (b >> 6))
1.48 + (bm >> 6))
1.49
1.50 flush_word(f, word)
1.51 x += 1
1.52 @@ -96,11 +94,28 @@
1.53 finally:
1.54 f.close()
1.55
1.56 +def get_values(r, g, b):
1.57 +
1.58 + "Return modified values for 'r', 'g' and 'b', plus an intensity bit."
1.59 +
1.60 + rm = r & 0xc0
1.61 + gm = g & 0xc0
1.62 + bm = b & 0xc0
1.63 + rd = r - rm
1.64 + gd = g - gm
1.65 + bd = b - bm
1.66 + i = ((rd ** 2 + gd ** 2 + bd ** 2) ** 0.5) >= 32 and 1 or 0
1.67 + return rm, gm, bm, i
1.68 +
1.69 def make_preview(im):
1.70 imp = PIL.Image.new("RGB", im.size)
1.71 data = []
1.72 for r, g, b in im.getdata():
1.73 - data.append((((r >> 6) << 6), ((g >> 5) << 5), (b >> 6) << 6))
1.74 + rm, gm, bm, i = get_values(r, g, b)
1.75 + r = rm + (i * 32)
1.76 + g = gm + (i * 32)
1.77 + b = bm + (i * 32)
1.78 + data.append((r, g, b))
1.79 imp.putdata(data)
1.80 return imp
1.81