Listing 5. Source code for program named ImageNegate01.
//34567890123456789012345678901234567890123456789012345678
/*Project ImageNegate01

This program assumes that the programmer knows the width
and height dimensions of the image.

The purpose of this program is to show how to load an
image file into memory, how to create a negative of the
image, and how to display both the original and the
negated image in an onscreen window.

A PCX image file with dimensions of 324x330 is loaded into
a bitmap in memory.

An onscreen window is created that is of sufficient size
to contain two copies of the image, one above the other.

The original image is copied into the upper half of the
onscreen window.

Then all of the pixels in the bitmap are negated and the
resulting modified bitmap is copied into the onscreen
window immediately below the original image.

Pressing any key causes the program to terminate.
*/
#include <allegro.h>

int main(){
  int pixel = 0;//temporary storage for a pixel
  int red = 0;//temporary storage for red, green, and blue
  int green = 0;
  int blue = 0;

  //Typical Allegro setup.
  allegro_init();
  install_keyboard();
  set_color_depth(32);
  //Create an onscreen window. The window width must be a
  // multiple of 4 and should be greater than or equal to
  // the width of the image. The height of the window
  // should be equal to or greater than twice the height
  // of the image. If the latter two conditions aren't
  // met, the window won't be large enough to contain two
  //copies of the image.
  set_gfx_mode(GFX_AUTODETECT_WINDOWED,324,660,0,0);

  //Declare a pointer variable capable of pointing to a
  // bitmap.
  BITMAP *picA = NULL;
  //Load an image file from the current directory.
  picA = load_bitmap("starfish.pcx", NULL);

  //Copy the image to the upper-left corner of the
  // onscreen window.
  blit(picA, screen, 0,0,0,0,324,330);


  //Cycle through the bitmap negating each pixel.
  for(int row = 0;row < 330;row++){
    for(int column = 0;column < 324;column++){
      pixel = getpixel(picA,column,row);
      red = getr(pixel);
      green = getg(pixel);
      blue = getb(pixel);
      putpixel(picA,column,row,makecol(
                             255-red,255-green,255-blue));
    }//end loop row
  }//end loop on column

  //Copy the modified bitmap to the onscreen window
  // immediately below the original image.
  blit(picA, screen, 0,0,0,330,324,330);

  //Block and wait until the user presses a key.
  readkey();
  
  //Destroy bitmaps to avoid memory leaks.
  destroy_bitmap(picA);

  return 0;//Return 0 to indicate a successful run.
}//end main function
END_OF_MAIN()