#!/usr/bin/python """ Copyleft Ben Leslie Released under GPL diddle2gfx - This program will take a palm image database as created by diddle or doodle and produces a set of images in gif, bmp or png format and creates an html file to 'display' the images. """ from Pyrite.Store.File import * import sys, string, os, getopt USAGE = """ diddle2gfx.py [options] [filenames] [filenames] Input filename - defaults to DiddleIDB.pdb Options: -i [filename] Create an index file in filename, defaults to index.html -p [prefix] Prefix value for images with no use for a name, defaults to diddle -f, --fgcolour colour description -b, --bgcolour colour description -t, --type (BMP|GIF|PNG) Format to put output in - defaults to PNG -h, --help """ HTML_HEADER = '' HTML_FOOTER = '
' DEFAULT_PREFIX = 'diddle' DEFAULT_TYPE = 'PNG' DEFAULT_FGCOLOUR = [255, 255, 0] DEFAULT_BGCOLOUR = [32, 97, 32] DEFAULT_INDEX = 'index.html' DEFAULT_DATABASE = 'DiddleIDB.pdb' CONVERT_PATH = "convert" BITMAP_HEADER = [66, 77, 190, 12, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 40, 0, 0, 0, 160, 0, 0, 0, 160, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 128, 12, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0] COLOURS = { 'CYAN' : [255, 255, 000], 'RED' : [000, 000, 255], 'MAGENTA' : [255, 000, 255], 'GREEN' : [000, 255, 000], 'YELLOW' : [000, 255, 255], 'BLUE' : [255, 000, 000], 'WHITE' : [255, 255, 255], 'BLACK' : [000, 000, 000], 'FG' : DEFAULT_FGCOLOUR, 'BG' : DEFAULT_BGCOLOUR } def WriteBitmap(data, fgcolour, bgcolour, filename): """ Write raw bitmap data to filename, placing headers in """ output = open(filename, "w") full_header = BITMAP_HEADER + bgcolour + [0] + fgcolour + [0] # Write header output.write(string.join( (map (lambda k: (chr(k)), full_header)), "")) # Write data output.write(data) output.close() def digitval (ch): if ch in string.digits: return string.atoi(ch) else: return ord(ch) - 55 def hex2num (str): """ Convert a string in hex to an integer """ num = 0 for i in range(len(str)): num = num + digitval(str[len(str) - 1 - i]) * pow(16, i) return num def str2colour (str): """ Convert a HTML colour string into an BGR triplet """ str = string.upper(str) colour = [] if str[0] == '#': #We have an explicit colour defn. colour = [hex2num(str[5:7]), hex2num(str[3:5]), hex2num(str[1:3])] elif str in COLOURS.keys(): colour = COLOURS[str] return colour def reverse(str): new_str = '' for i in range(len(str)): new_str = new_str + str[len(str) - (i+1)] return new_str def PalmFormat2BitmapFormat(data): """ Convert palm format to normal (bad) bitmap format """ new = "" # Reverse the order data = reverse(data) # Now reverse each fscking line new = "" for n in range(len(data) / 20): new = new + reverse(data[n*20:n*20+20]) data = new return data def getHtmlFragment(title, filename, type, comments): return '%s \

%s

%s
' % (filename, string.lower(type), filename, title, comments) def main(files, prefix=DEFAULT_PREFIX, type=DEFAULT_TYPE, fgcolour=DEFAULT_FGCOLOUR, bgcolour=DEFAULT_BGCOLOUR, htmlfilename=DEFAULT_INDEX): """ Open a palm db _file_, and a _header_ and produce a set of bitmaps """ type = string.upper(type) htmlfile = open(htmlfilename, "w") htmlfile.write(HTML_HEADER) for file in files: try: store = FileStore(file) records = store.open(store) except IOError: sys.stderr.write("Could not read %s image database\n" % file) sys.exit() for i in range(len(records)): rawdata = records[i].pack() # If we have a _real_ image if len(rawdata) > 3200: title = rawdata[3200:3225] title = filter(lambda k: k != '\0' , title) if title == "": title = "%s%s" % (header, i) filename = "" for ch in title: if ch not in (string.digits + string.letters): filename = filename + "_" else: filename = filename + ch comments = rawdata[3225:] comments = filter(lambda k: k != '\0' , comments) rawdata = rawdata[:3200] else: filename = "%s%s" % (prefix, i) comments = "" title = filename data = PalmFormat2BitmapFormat(rawdata) print filename htmlfile.write(getHtmlFragment(title, filename, type, comments)) WriteBitmap(data, fgcolour, bgcolour, "%s.bmp" % filename) if type in ('GIF', 'PNG'): os.system('%s "%s.bmp" %s:"%s.%s"; rm "%s.bmp"' % (CONVERT_PATH, filename, string.upper(type), filename, string.lower(type), filename)) htmlfile.write(HTML_FOOTER) htmlfile.close() if __name__ == "__main__": # Parse command line options and call main (arguments, remainder) = getopt.getopt(sys.argv[1:], "i:p:f:b:t:h") index_file = '' prefix = '' fgcolour = [] bgcolour = [] type = '' help = '' for arg in arguments: com, value = arg if com == '-i': if value == '': index_file = DEFAULT_INDEX else: index_file = value elif com == '-p': prefix = value elif com == '-f': fgcolour = str2colour(value) elif com == '-b': bgcolour = str2colour(value) elif com == '-t': type = value elif com == '-h': help = 1 databases = remainder if help: print USAGE sys.exit() if len(databases) == 0: databases = [DEFAULT_DATABASE, ] if prefix == '': prefix = DEFAULT_PREFIX if fgcolour == []: fgcolour = DEFAULT_FGCOLOUR if bgcolour == []: bgcolour = DEFAULT_BGCOLOUR if index_file == '': index_file = DEFAULT_INDEX if type == '': type = DEFAULT_TYPE main(databases, prefix, type, fgcolour, bgcolour, index_file)