SHARE
TWEET

https://shreevatsa.wordpress.com/2011/04/12/how-does-tuppers

a guest Apr 28th, 2015 227 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. Read a bitmap file and print out N such that Tupper's formula, graphed
  6. in the range N < y < N + 17, produces the bitmap.
  7.  
  8. My little understanding of the BMP format is from skimming through
  9. http://atlc.sourceforge.net/bmp.html (which I found through Wikipedia)
  10. """
  11.  
  12. tupperH = 17
  13. # word, dword, and... 16^2?
  14. w = 2
  15. dw = 4
  16. W = 256
  17.  
  18. def black(r, g, b):
  19.     #Arbitrary choice: (255+255+255)/2
  20.     return r+g+b < 383
  21.  
  22. def eprint(s):
  23.     sys.stderr.write(s+'\n')
  24.  
  25. if __name__=='__main__':
  26.     import sys
  27.     if len(sys.argv)!=2:
  28.         print("Usage: %s name-of-bitmap-file" % sys.argv[0])
  29.         exit
  30.     f = open(sys.argv[1], 'rb')
  31.  
  32.     def read(n):
  33.         s = f.read(n)
  34.         assert len(s)==n, "Unexpected end of file, sorry."
  35.         return s
  36.     def num(s):
  37.         size = 0
  38.         for c in reversed(s): size = size*W + ord(c)
  39.         return size
  40.     def numr(n): return num(read(n))
  41.  
  42.     identifier = read(2)
  43.     eprint("Identifier: %s" % identifier)
  44.  
  45.     size = numr(dw)
  46.     eprint("Size: %d" % size)
  47.  
  48.     read(dw) #Ignore
  49.  
  50.     offset = numr(dw)
  51.     eprint("Offset: %d" % offset)
  52.  
  53.     headersize = numr(dw)
  54.     eprint("Header size: %d" % headersize)
  55.  
  56.     width = numr(dw)
  57.     eprint("Width: %d" % width)
  58.  
  59.     topdown = False
  60.     height = numr(dw)
  61.     if height > 2**31:
  62.         height = 2**32 - height
  63.         topdown = True
  64.     eprint("Height: %d" % height)
  65.     if height>tupperH:
  66.         eprint("Height is larger than %d, so Tupper's formula must be modified." % tupperH)
  67.     eprint("Top-down DIB: %s" % topdown)
  68.  
  69.     planes = numr(w)
  70.     eprint("Number of planes: %d" % planes)
  71.  
  72.     bpp = numr(w)
  73.     eprint("Bits per pixel: %d" % bpp)
  74.  
  75.     compression = numr(dw)
  76.     eprint("Compression: %d" % compression)
  77.     if not (compression==0 or compression==3):
  78.         assert not topdown, "Invalid bitmap file: top-down DIBs cannot have compression"
  79.         eprint("The bitmap file has compression, which the program probably cannot handle. Expect garbage.")
  80.  
  81.     datasize = numr(dw)
  82.     eprint("Bitmap data size: %d" % datasize)
  83.     #assert datasize%4==0
  84.  
  85.     hres = numr(dw)
  86.     eprint("Horizontal resolution: %d" % hres)
  87.  
  88.     vres = numr(dw)
  89.     eprint("Vertical resolution: %d" % vres)
  90.  
  91.     colors = numr(dw)
  92.     eprint("Number of colours: %d" % colors)
  93.  
  94.     impcolors = numr(dw)
  95.     eprint("Number of important colours: %d" % impcolors)
  96.  
  97.     n = colors
  98.     for i in xrange(n):
  99.         b = read(1)
  100.         g = read(1)
  101.         r = read(1)
  102.         eprint("Colour %d: (B, G, R) = (%d, %d, %d)" % (i, num(b), num(g), num(r)))
  103.  
  104.     #TODO: Make it work with other values of bpp.
  105.     assert bpp in [24, 32], "Sorry, this program doesn't handle a BPP of %d; it only handles 24 and 32."%bpp
  106.     img = []
  107.     for h in xrange(height):
  108.         row = []
  109.         for x in xrange(width):
  110.             if bpp in [24, 32]:
  111.                 r = numr(1)
  112.                 g = numr(1)
  113.                 b = numr(1)
  114.                 if bpp == 32:
  115.                   read(1)
  116.                 #print "Pixel (%d,%d) has RGB (%d,%d,%d)." % (x,h,r,g,b)
  117.                 curblack = black(r,g,b)
  118.             if curblack:
  119.                 #sys.stderr.write('*')
  120.                 row.append(1)
  121.             else:
  122.                 #sys.stderr.write(' ')
  123.                 row.append(0)
  124.         #sys.stderr.write('\n')
  125.         img.append(row)
  126.     assert height == len(img)
  127.     if not topdown: img.reverse()
  128.     while len(img)<tupperH:
  129.         img = [[0]*width] + img
  130.     height = len(img)
  131.     # for row in img:
  132.     #     eprint(','.join(str(c) for c in row))
  133.  
  134.     s = f.read()
  135.     eprint("Leftover: %d" % len(s))
  136.  
  137.     N = 0
  138.     for x in xrange(width-1, -1, -1):
  139.         for y in xrange(height):
  140.             c = img[y][x]
  141.             #print c,
  142.             N = N*2 + c
  143.         #print
  144.     print N*height
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top