Posts Tagged ‘pgm’
* Cybook t4b Format Specification
Posted on January 13th, 2010 by John. Filed under hardware.
The new epub thumbnail files (.epub.thn) are what Bookeen calls t4b files. They are very similar to the older t2b thumbnail files they were using in earlier versions of the Cybook firmware. As the name suggests instead of using 2 bits to represent color values 4 bits are now used. This increases the number of colors from 4 to 16. In addition to the increased color range the t4b files now require a header of “t4bp” without the quotes.
The image’s dimensions are 96×144. The bits representing 0, 1, 2, 3… are written directly to the file. it is very similar to a pgm file in this regard. Each 4 bit sequence represents a pixel color. Only black, white and shades of gray are supported.
Every t4b file will have 13,824 pixels. The file size will always be 6,916 bytes. The formula to determine this is: (height x width x 2 bits per pixel) / 8 bits per byte. ((96 * 144 * 4) / 8 ) + 4 = 6,916. The + 4 is the header.
Following are two python scripts for converting an image to a t4b file and for converting a t4b file into a pgm image.
image2t4b.py
#!/usr/bin/env python
import sys, Image
REDUCE_MARKS = [16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240]
def reduce_color(c):
val = 0
for mark in REDUCE_MARKS:
if c > mark:
val += 1
else:
break
return val
def main():
if len(sys.argv) != 3:
raise Exception('Must have 2 arguments. %s input.image output.epub.thn' % sys.argv[0])
outf = open(sys.argv[2], 'wb')
im = Image.open(sys.argv[1]).convert("L")
im.thumbnail((96, 144))
newim = Image.new('L', (96, 144), 'white')
x,y = im.size
newim.paste(im, ((96-x)/2, (144-y)/2))
outf.write('t4bp')
px = []
for p in newim.getdata():
px.append(p)
if len(px) == 2:
byte_val = bin(reduce_color(px[0]))[2:].zfill(4) + bin(reduce_color(px[1]))[2:].zfill(4)
outf.write(chr(int(byte_val, 2)))
px = []
elif len(px) > 2:
raise Exception('Fatal error px length increased past 2.')
outf.close()
if __name__ == '__main__':
main()
t4b2pgm.py
#!/usr/bin/env python
import sys, os
def get_greys(b):
if not b:
return 0, 0
b = bin(int(ord(b)))
b = b[2:].zfill(8)
w = str(int(b[0:4], 2))
x = str(int(b[4:8], 2))
return w, x
def main():
if len(sys.argv) != 3:
raise Exception('Must have 2 arguments. %s input.epub.thm output.pgm' % sys.argv[0])
t4bfile = open(sys.argv[1], 'rb')
pgmfile = open(sys.argv[2], 'w')
pgmfile.write('P2\n96 144\n15\n')
# Read past the t4b header
t4bfile.read(4)
for i in range(144):
for j in range(48):
b = t4bfile.read(1)
vals = get_greys(b)
pgmfile.write('%s %s ' % (vals[0], vals[1]))
pgmfile.write('\n')
pgmfile.close()
t4bfile.close()
if __name__ == '__main__':
main()
Tags
Archives
- March 2010 (1)
- January 2010 (8)
- December 2009 (5)
- November 2009 (6)
- October 2009 (4)
- September 2009 (2)
- August 2009 (6)
- July 2009 (6)
- June 2009 (4)
- May 2009 (6)
- April 2009 (4)
- March 2009 (2)
- February 2009 (4)
- January 2009 (4)
- December 2008 (7)
- November 2008 (2)