Pix

Photo-editing software applies "filters" to pictures.  These are mathematical transformations of the data.  Many of these involve individual pixels.  In Java, we use bitwise operators to manipulate individual pixels.

        

Before using this program, copy the original picture on the left (face.bmp).

import java.io.*;

public class Negative
{  public static void main(String[] args)
   {  new Negative(); }
   
   public Negative()
   {
      try
      {
         RandomAccessFile data = new RandomAccessFile("face.bmp","rw");
         long size = data.length();
         
         for (int x=54; x < size; x++)
         {
            data.seek(x);
            byte b = data.readByte();
            b = (byte)(b ^ 255);
            data.seek(x);
            data.writeByte(b);
         }
         data.close();
      }
      catch (IOException ex)
      { }
   }
}

/****

Before starting this activity you might wish to read the following
about BITWISE OPERATORS:  http://docs.rinet.ru/ZhPP/ch5.htm#TheBitwiseOperators

Before running this program you need to make a COPY of the picture,
so you can always look at the original if you need to.

(1) This program changes a .bmp picture.  It will only work with a .bmp file.
    It changes each BYTE in the picture.  Figure out what it does to the numbers,
    and explain why the picture looks the way it does.

Change the command:  (b ^ 255)  to each of the following commands.
After each change you should:
    (a) run the program to change the picture
    (b) VIEW the picture to see the effect
    (c) use a Hex-Editor to compare the new numbers to the originals.
    (d) Explain what has happened at the bit level,
        and why it has that visual effect on the picture.
    (e) Copy the original picture again, before making the next change.
    
(2)  (b & 127)

(3)  (b & 224)

(4)  (b | 128}

(5)  (b | 1)

(6)  (b ^ 1)

One of these ideas will be useful for Steganography.
Read the article about Steganography and decide which command is useful.

Steganography:    http://www.jjtc.com/pub/r2026.pdf  
or this: http://www.garykessler.net/library/steganography.html
****/