From 9558ac18302221591eab7fa754463e26ba4c13e2 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Mon, 28 Mar 2016 18:30:53 +0200 Subject: [PATCH] introduce TOI grayscale 4-bit format --- assets/lock.png | Bin 0 -> 710 bytes assets/lock.toi | Bin 0 -> 323 bytes tools/png2toi | 42 ++++++++++++++++++++++++++++++++---------- 3 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 assets/lock.png create mode 100644 assets/lock.toi diff --git a/assets/lock.png b/assets/lock.png new file mode 100644 index 0000000000000000000000000000000000000000..35d0b9080ea061cc2bc6dfadbd938d06fb4f16f9 GIT binary patch literal 710 zcmV;%0y+JOP)$DLuPi8CLnm}Tz22gkJbzbPzjJLkiy~VjVvxc?%%RaLjTd`fp==Ms?kman|jl24ar{b#< zP5Ru72@V1P_DoksKj8oX^oZ~~D*z~$_`fj70Khsgz#|L*9BCf@Xa@kxgaDrc01OHL zm%0D|QNvR77*c5fz&TOn5dc8munahv0RS8oVIKgXz{9yB0KhN8RscXT&%wrEB5X{M z4Blom1oPL*Sz2CO&k_M(%+=y+w{zZDxJN5A(#rlars#2NtbFS7i&p~MInaGQSs((@ zt@<2(Or)SNTOa%$P^#n^sTB1~$7I`DRF#29E0${LeApS-Z)1wf*<@FhQZcu=nI$c^ z{3Ekyf;|tq?ah*8m`?p!jfrqapH1qi+l@}s*-+?TAEX_42mXJsqBa00e^y0)nc$VS zlhZl#eb#bsPqavGcRap#=;DFa%6z__c=Y5u*PP_fS6HE|FH>8UDt_ZkC3u-gfC2BZ z(N9E_8jG~?9_jFan5S%=w}%}jbw>4~ITdkRYis8447@5=*roAUusYf`IGN^}lib4m z^Vgb$^!A5Chs>?o#?n}ai1tqnjD*ylh literal 0 HcmV?d00001 diff --git a/assets/lock.toi b/assets/lock.toi new file mode 100644 index 0000000000000000000000000000000000000000..e3d557f6f5821b54e4d299bf2fd032d2c67468f4 GIT binary patch literal 323 zcmV-J0lfZHPf2G0EC4|J{~tg2_a0O1Pn)x8#(6ZspN0%TAiN$$@UH>`7%-!lvVZ{! zxRC^ZFu=ilgy;l>C>O$h5SOKUhXII&oBRpLVFkI^0m$%2GJp~6rk_Aj9+;>A+&G{- zGgR<5P(?n3`4z}Kk6ThWWbp*J=s^YsKBNGl zrYQg4)2o1B&wGUEMFtcA7X6#ZfCkjxgWZh|fI$_2DauAAQ6|&$@{XCSR?a9dPcedt zGVVo6F>g0OMVV0&*N0Kj0Tg9?k1YBC7MslL5rUf$G0&I-RsOaRB|Vt$2ML~VKuaaU zx&Qw+3t*-l%>bmd^UDA|L5YG)``03XEXEWGb+!33jFd~_8o#@P2C V75p@0U{pW= 3 and sys.argv[2] == 'g': + gray = True + if not ifn.endswith('.png'): print('Must provide PNG file') sys.exit(2) @@ -18,22 +22,40 @@ im = Image.open(ifn) w, h = im.size print('Opened %s ... %d x %d @ %s' % (ifn, w, h, im.mode)) -if not im.mode == 'RGB': - print('PNG file must use RGB mode') - sys.exit(3) +if not gray: + if not im.mode == 'RGB': + print('PNG file must use RGB mode') + sys.exit(3) +else: + if not im.mode == 'L': + print('PNG file must use grayscale mode') + sys.exit(3) + if w % 2 > 0: + print('PNG file must have width divisible by 2') + sys.exit(4) pix = im.load() ofn = '%s.toi' % ifn[:-4] with open(ofn, 'wb') as f: - f.write(bytes('TOIa', 'ascii')) + if not gray: + f.write(bytes('TOIa', 'ascii')) + else: + f.write(bytes('TOIg', 'ascii')) f.write(struct.pack('>HH', w, h)) data = bytes() - for j in range(h): - for i in range(w): - r, g, b = pix[i, j] - c = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3) - data += struct.pack('>H', c) + if not gray: + for j in range(h): + for i in range(w): + r, g, b = pix[i, j] + c = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3) + data += struct.pack('>H', c) + else: + for j in range(h): + for i in range(w // 2): + l1, l2 = pix[i * 2, j], pix[i * 2 + 1, j] + c = (l1 & 0xF0) | (l2 >> 4) + data += struct.pack('>B', c) z = zlib.compressobj(level=9, wbits=10) zdata = z.compress(data) + z.flush() zdata = zdata[2:-4] # strip header and checksum