CSC111 Why 255?

From dftwiki3
Jump to: navigation, search

--D. Thiebaut (talk) 16:14, 13 March 2014 (EDT)


Sample JES Program


# doNotChangeColor( image ).
# Changes the amount of red, green and blue that is in an image
def makeSquares( image ):
  for x in range(0,getWidth(image)):
    for y in range(0,getHeight(image)):
        pixel = getPixel (image, x, y)
        red   = getRed(pixel)
        green = getGreen(pixel)
        blue  = getBlue(pixel)
        if x < getWidth(image)/2 and y < getHeight(image)/2:
          newColor = makeColor( 255, 0, 0 )
        elif x > getWidth(image)/2 and y < getHeight(image)/2:
          newColor = makeColor( 0, 250, 0 )
        elif x < getWidth( image )/2 and y > getHeight( image)/2:
          newColor = makeColor( 0, 0, 240 )
        else:
          newColor = makeColor( 100, 100, 100 )
          
        # the line below replaces the pixel with its original color.  Change
        # the amount of red, green and blue to see some change in the colors
        setColor( pixel, newColor )
  return image

# ==================================================================  
#                             MAIN PROGRAM
# ==================================================================  
file = pickAFile()
image = makePicture( file ) 
show( image )
  
image = makeSquares( image )
repaint( image )
writePictureTo( image, file )


Sample Image


square.bmp

identify square.bmp
square.bmp BMP 20x20 20x20+0+0 8-bit sRGB 1.25KB 0.000u 0:00.000


ls -l square.bmp 
-rw-r--r--  1 thiebaut  wheel  1254 Mar 13 20:48 square.bmp


Octal Dump


od -t u1 square.bmp
 0000000    66  77 230   4   0   0   0   0   0   0  54   0   0   0  40   0
 0000020     0   0  20   0   0   0  20   0   0   0   1   0  24   0   0   0
 0000040     0   0 176   4   0   0   0   0   0   0   0   0   0   0   0   0
 0000060     0   0   0   0   0   0 240   0   0 240   0   0 240   0   0 240
 0000100     0   0 240   0   0 240   0   0 240   0   0 240   0   0 240   0
 0000120     0 240   0   0 100 100 100 100 100 100 100 100 100 100 100 100
 0000140   100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
 0000160   100 100 240   0   0 240   0   0 240   0   0 240   0   0 240   0
 ...
 0002200     0   0 255   0   0 255   0   0 255   0   0 255 100 100 100   0
 0002220   250   0   0 250   0   0 250   0   0 250   0   0 250   0   0 250
 0002240     0   0 250   0   0 250   0   0 250   0   0   0 255   0   0 255
 0002260     0   0 255   0   0 255   0   0 255   0   0 255   0   0 255   0
 0002300     0 255   0   0 255   0   0 255 100 100 100   0 250   0   0 250
 0002320     0   0 250   0   0 250   0   0 250   0   0 250   0   0 250   0
 0002340     0 250   0   0 250   0


Bits


def toBin( n, noBits ):
    s = bin( n )
    s = "0" * noBits + s[2:]
    return s[-noBits:]

noBits = int( input( "Number of bits? " ) )

noCombinations = 2**noBits
print( "Number of combinations =", noCombinations )
for i in range( noCombinations ):
    print( "%3d %s" % ( i, toBin( i, noBits ) ) )