Skip to content

Instantly share code, notes, and snippets.

@whs
Created July 11, 2011 14:39
Show Gist options
  • Select an option

  • Save whs/1075984 to your computer and use it in GitHub Desktop.

Select an option

Save whs/1075984 to your computer and use it in GitHub Desktop.
Hackathon R1
import math, struct
from PIL import Image
fName = "file1039"
D = open(fName, "rb").read()
width = int(math.sqrt(len(D)/8))
#print width
# split image to two parts
Img = D[:width*width*4]
Img2 = D[width*width*4:]
#print len(Img)
#print len(Img2)
if len(Img) != len(Img2):
raise Exception, "Cannot split to 2 exactly-sized images!"
ImgD = Image.fromstring("RGBA", (width,)*2, Img)
ImgD.save("out.png")
# split to 32 bit blocks
opCode = []
D = open(fName, "rb")
D.seek(width*width*4)
#opcode reader
try:
byte = D.read(4)
while byte != "":
buf = ""
for i in byte:
buf += '{0:08b}'.format(ord(i))
opCode.append(buf)
byte = D.read(4)
finally:
D.close()
#print opCode
#rows = [[]]*width
#cols = [[]]*width
cnt = 0
index = {}
for x in opCode:
myRow = int(math.floor(cnt/width))
myCol = cnt-(myRow*width)
if x[0:3] not in ("000", "001", "010", "011", "100"):
if x[3:] == "0"*29:
raise Exception, "BUG! "+x
if x[3:] != "0"*29:
if x[3:] in index:
index[x[3:]].append((myRow, myCol))
if len(index[x[3:]]) > 2:
raise Exception, "Duplicate hash "+x[3:]
else:
index[x[3:]] = [(myRow, myCol)]
#cols[myCol].append(x[3:])
#rows[myRow].append(x[3:])
cnt += 1
import pprint
pprint.pprint(index)
cnt = 0
noProcessRow = []
noProcessCol = []
for c in opCode:
myRow = int(math.floor(cnt/width))
if myRow in noProcessRow:
cnt += 1
continue
myCol = cnt-(myRow*width)
if myCol in noProcessCol:
cnt += 1
continue
myHash = c[3:]
if not c.startswith("000"):
print c
if c.startswith("100"):
ImgD = ImgD.transpose(Image.ROTATE_90)
elif c.startswith("101"):
target = filter(lambda x: x[0] != myRow and x[1] != myCol, index[myHash])[0]
print target
# swap it.
srcCode = ImgD.getpixel((myRow, myCol))
targetCode = ImgD.getpixel(target)
ImgD.putpixel((myRow, myCol), targetCode)
ImgD.putpixel(target, srcCode)
elif c.startswith("110"):
try:
target = filter(lambda x: x[0] != myRow, index[myHash])[0]
except IndexError:
cnt += 1
continue
print (myRow, myCol), target
#swap the entire row
curX = 0
while curX < width:
srcCode = ImgD.getpixel((curX, myCol))
targetCode = ImgD.getpixel((curX, target[1]))
# swap it now
ImgD.putpixel((curX, myCol), targetCode)
ImgD.putpixel((curX, target[1]), srcCode)
curX += 1
noProcessRow.append(target[0])
noProcessRow.append(myRow)
elif c.startswith("111"):
try:
target = filter(lambda x: x[1] != myCol, index[myHash])[0]
except IndexError:
cnt += 1
continue
print (myRow, myCol), target
#swap the entire col
curY = 0
while curY < width:
srcCode = ImgD.getpixel((myRow, curY))
targetCode = ImgD.getpixel((target[0], curY))
# swap it now
ImgD.putpixel((myRow, curY), targetCode)
ImgD.putpixel((target[0], curY), srcCode)
curY += 1
noProcessCol.append(target[1])
noProcessCol.append(myCol)
elif c.startswith("000"):
cnt += 1
continue
else:
raise NotImplementedError, c[:3]+" is not implemented"
cnt += 1
print "Done!"
ImgD.save("output.png")
open("output.bin", "w").write(ImgD.tostring())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment