Skip to content

Instantly share code, notes, and snippets.

@dov
Created January 8, 2017 19:58
Show Gist options
  • Save dov/b009bdf5b642815247c23288228ad966 to your computer and use it in GitHub Desktop.
Save dov/b009bdf5b642815247c23288228ad966 to your computer and use it in GitHub Desktop.
A parser of the Android Gesture file format that outputs giv files for each character
#!/usr/bin/python
######################################################################
# A python parser for the file format in the Android Gesture builder.
# Each character is output in a giv (http://giv.sourceforge.net/giv/)
# file format.
#
# MIT License
#
# Copyright (c) 2017 Dov Grobgeld
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Dov Grobgeld
######################################################################
import sys,struct
def readShort(fh):
return struct.unpack('>h',fh.read(2))[0]
def readUShort(fh):
return struct.unpack('>H',fh.read(2))[0]
def readInt(fh):
return struct.unpack('>i',fh.read(4))[0]
def readFloat(fh):
return struct.unpack('>f',fh.read(4))[0]
def readLong(fh):
hl = struct.unpack('>2I',fh.read(8))
return (hl[0]<<32) + hl[1]
# From stack overflow
# build a table mapping lead byte to expected follow-byte count
# bytes 00-BF have 0 follow bytes, F5-FF is not legal UTF8
# C0-DF: 1, E0-EF: 2 and F0-F4: 3 follow bytes.
# leave F5-FF set to 0 to minimize reading broken data.
_lead_byte_to_count = []
for i in range(256):
_lead_byte_to_count.append(
1 + (i >= 0xe0) + (i >= 0xf0) if 0xbf < i < 0xf5 else 0)
def readUTF8(f, count):
"""Read `count` UTF-8 bytes from file `f`, return as unicode"""
# Assumes UTF-8 data is valid; leaves it up to the `.decode()` call to validate
res = []
while count:
count -= 1
lead = f.read(1)
res.append(lead)
readcount = _lead_byte_to_count[ord(lead)]
if readcount:
res.append(f.read(readcount))
return (''.join(res)).decode('utf8')
def readString(fh):
stringLen = readUShort(fh)
return readUTF8(fh, stringLen)
class Gesture:
def __init__(self,
name,
points):
self.name = name
self.points = points
def ReadGesturesFile(filename):
fh = open(filename, 'rb')
# Header
# 2 bytes short File format version number
# 4 bytes int Number of entries
version = readShort(fh)
numEntries = readInt(fh)
entries = []
# assume unistroke
for entryId in range(numEntries):
# Entry
# X bytes UTF String Entry name
# 4 bytes int Number of gestures
name = readString(fh)
numGestures = readInt(fh)
for gestureId in range(numGestures):
# Gesture
# 8 bytes long Gesture ID
# 4 bytes int Number of strokes
id = readLong(fh)
numStrokes = readInt(fh)
giv = open('gesture-'+name+'-%d.giv'%gestureId,'w')
giv.write('$marks fcircle\n')
for strokeId in range(numStrokes):
# Stroke
# 4 bytes int Number of points
numPoints = readInt(fh)
points = []
for pointId in range(numPoints):
# Point
# 4 bytes float X coordinate of the point
# 4 bytes float Y coordinate of the point
# 8 bytes long Time stamp
x,y = readFloat(fh),readFloat(fh)
points += [(x,y)]
timeStamp = readLong(fh)
giv.write('%f %f\n'%(x,y))
giv.close()
entries += [Gesture(name, points)]
return entries
if __name__=='__main__':
if len(sys.argv)<1:
print 'Need name of gesture file!'
exit(-1)
Filename = sys.argv[1]
ReadGesturesFile(Filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment