Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- Read a bitmap file and print out N such that Tupper's formula, graphed
- in the range N < y < N + 17, produces the bitmap.
- My little understanding of the BMP format is from skimming through
- http://atlc.sourceforge.net/bmp.html (which I found through Wikipedia)
- """
- tupperH = 17
- # word, dword, and... 16^2?
- w = 2
- dw = 4
- W = 256
- def black(r, g, b):
- #Arbitrary choice: (255+255+255)/2
- return r+g+b < 383
- def eprint(s):
- sys.stderr.write(s+'\n')
- if __name__=='__main__':
- import sys
- if len(sys.argv)!=2:
- print("Usage: %s name-of-bitmap-file" % sys.argv[0])
- exit
- f = open(sys.argv[1], 'rb')
- def read(n):
- s = f.read(n)
- assert len(s)==n, "Unexpected end of file, sorry."
- return s
- def num(s):
- size = 0
- for c in reversed(s): size = size*W + ord(c)
- return size
- def numr(n): return num(read(n))
- identifier = read(2)
- eprint("Identifier: %s" % identifier)
- size = numr(dw)
- eprint("Size: %d" % size)
- read(dw) #Ignore
- offset = numr(dw)
- eprint("Offset: %d" % offset)
- headersize = numr(dw)
- eprint("Header size: %d" % headersize)
- width = numr(dw)
- eprint("Width: %d" % width)
- topdown = False
- height = numr(dw)
- if height > 2**31:
- height = 2**32 - height
- topdown = True
- eprint("Height: %d" % height)
- if height>tupperH:
- eprint("Height is larger than %d, so Tupper's formula must be modified." % tupperH)
- eprint("Top-down DIB: %s" % topdown)
- planes = numr(w)
- eprint("Number of planes: %d" % planes)
- bpp = numr(w)
- eprint("Bits per pixel: %d" % bpp)
- compression = numr(dw)
- eprint("Compression: %d" % compression)
- if not (compression==0 or compression==3):
- assert not topdown, "Invalid bitmap file: top-down DIBs cannot have compression"
- eprint("The bitmap file has compression, which the program probably cannot handle. Expect garbage.")
- datasize = numr(dw)
- eprint("Bitmap data size: %d" % datasize)
- #assert datasize%4==0
- hres = numr(dw)
- eprint("Horizontal resolution: %d" % hres)
- vres = numr(dw)
- eprint("Vertical resolution: %d" % vres)
- colors = numr(dw)
- eprint("Number of colours: %d" % colors)
- impcolors = numr(dw)
- eprint("Number of important colours: %d" % impcolors)
- n = colors
- for i in xrange(n):
- b = read(1)
- g = read(1)
- r = read(1)
- eprint("Colour %d: (B, G, R) = (%d, %d, %d)" % (i, num(b), num(g), num(r)))
- #TODO: Make it work with other values of bpp.
- assert bpp in [24, 32], "Sorry, this program doesn't handle a BPP of %d; it only handles 24 and 32."%bpp
- img = []
- for h in xrange(height):
- row = []
- for x in xrange(width):
- if bpp in [24, 32]:
- r = numr(1)
- g = numr(1)
- b = numr(1)
- if bpp == 32:
- read(1)
- #print "Pixel (%d,%d) has RGB (%d,%d,%d)." % (x,h,r,g,b)
- curblack = black(r,g,b)
- if curblack:
- #sys.stderr.write('*')
- row.append(1)
- else:
- #sys.stderr.write(' ')
- row.append(0)
- #sys.stderr.write('\n')
- img.append(row)
- assert height == len(img)
- if not topdown: img.reverse()
- while len(img)<tupperH:
- img = [[0]*width] + img
- height = len(img)
- # for row in img:
- # eprint(','.join(str(c) for c in row))
- s = f.read()
- eprint("Leftover: %d" % len(s))
- N = 0
- for x in xrange(width-1, -1, -1):
- for y in xrange(height):
- c = img[y][x]
- #print c,
- N = N*2 + c
- #print
- print N*height
Add Comment
Please, Sign In to add comment