If you erase lower two bits in the red value, you can clear space for hiding values 0–4.
```
for p in getPixels(picture):
# Clear out the red 2xLSB
r = getRed(p)
setRed(p,r-(r%4))
```
If you erase the lower two bits from red, green, and blue, you can save six bits. Six bits can encode 64 values. That’s enough to encode all 27 letters, both upper and lower case.
(a) Write a function to input a picture and a string. Save each character in the string in a pixel of the picture, by saving it across the least significant two bits in each of red, green, and blue. Can you tell the difference between the original picture and the picture with the
encoded text message?
No, it’s highly unlikely someone could tell a difference between the original and the modified picture.
Note: The below method special cases for the space character, as otherwise messages cannot include spaces, this is possible to do as six bits allows for additional space beyond the 54 needed for upper and lower case letters.
(b) Now write a function to decode the original text.
(a) ```
def encodeMessage(picture, message):
#Go through all pixels and erase the lower two bits
for p in getPixels(picture):
# Clear out the red 2xLSB
r = getRed(p)
setRed(p,r-(r%4))
# Clear out the green 2xLSB
g = getGreen(p)
setGreen(p,g-(g%4))
# Clear out the blue 2xLSB
b = getBlue(p)
setBlue(p, b-(b%4))
pixels = getPixels(picture)
for charIndex in range(0, len(message)):
characterVal = (ord(message[charIndex])-65)
#Special case space symbol
if ord(message[charIndex])==32:
characterVal = 55
#Convert the number into a binary representation
binaryValues = [0,0,0,0,0,0]
index = 5
while characterVal>0:
binaryValues[index] = ((characterVal%2))
characterVal = characterVal/2
index= index-1
#Encode the values into the picture
redVal = binaryValues[1]
redVal = redVal+ 2*binaryValues[0]
greenVal = binaryValues[3]
greenVal = greenVal+ 2*binaryValues[2]
blueVal = binaryValues[5]
blueVal = blueVal+ 2*binaryValues[4]
r = getRed(pixels[charIndex])
setRed(pixels[charIndex],r+redVal)
g = getGreen(pixels[charIndex])
setGreen(pixels[charIndex],g+greenVal)
b = getBlue(pixels[charIndex])
setBlue(pixels[charIndex],b+blueVal)
return picture
```
(b) ```
def decodeMessage(picture):
secretMessage = ""
charIndex = 0
for p in getPixels(picture):
r = getRed(p)%4
g = getGreen(p)%4
b = getBlue(p)%4
binaryValues = [0,0,0,0,0,0]
#Translate the values into binary
if not r==0:
if r==1:
binaryValues[1] = 1
elif r==2:
binaryValues[0] = 1
elif r==3:
binaryValues[0]=1
binaryValues[1]=1
if not g == 0:
if g==1:
binaryValues[3] = 1
elif g==2:
binaryValues[2] = 1
elif g==3:
binaryValues[2]=1
binaryValues[3]=1
if not b == 0:
if b==1:
binaryValues[5] = 1
elif b==2:
binaryValues[4] = 1
elif b==3:
binaryValues[4]=1
binaryValues[5]=1
binaryValues.reverse()
characterValue = 0
multiple = 1
for index in range(0, 6):
characterValue = characterValue+(multiple)*binaryValues[index]
multiple = multiple*2
#Special Case Space value
if not characterValue==55:
secretMessage = secretMessage+ (chr(characterValue+65))
else:
secretMessage = secretMessage + " "
return secretMessage
```
Note: Unless you pass along a message length, the end of the message will have a great number of extra capital A values.
You might also like to view...
The Compare button is located on the View tab.
Answer the following statement true (T) or false (F)
What will display when the following code is executed?
``` imagePanel = new JPanel(); imageLabel = new JLabel(); imagePanel.Add(imageLabel); ImageIcon sunnyFaceImage = new ImageIcon("SunnyFace.gif"); imageLabel.setIcon(sunnyFaceImage); pack(); ``` A) The JFrame that encloses the window will resize itself to accommodate the SunnyFace image. B) imagePanel will resize itself to accommodate the SunnyFace image. C) The SunnyFace image will resize itself to fit on imageLabel. D) The SunnyFace image will resize itself to fit on imagePanel.
What is not true about a report?
A) It can be previewed before it is printed. B) Its record sources include one of more tables or a query. C) It is designed to display only one record at a time. D) It can be sorted independently of its data source.
Which of the following options is appropriate to show the numbers 9779851089510 in a cell? Select one:
a. Enclose the number is brackets b. Place the character T before the number c. Apply the Text format in the cell and type the numbers d. Place the character TX before the number