Open GL Super Bible

Previous Table of Contents Next


Copying Pixmaps

OpenGL also provides a function to copy an area on the screen to another location—as needed, for instance, in scrolling or “magnifying glass” views:

int mousex, mousey;

glReadBuffer(GL_FRONT);
glDrawBuffer(GL_FRONT);
glPixelZoom(2.0, 2.0);
glRasterPos2i(0, 0);
glCopyPixels(mousex - 8, mousey - 8, 16, 16, GL_COLOR);

Here the glCopyPixels function copies pixels from the given location to the current raster position:

void glCopyPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum
type)

The x and y parameters specify the lower-left corner of the area to be copied. Width and height specify the size of the image to be copied. Pixels are copied from the specified (x,y) location to the current raster position. The type argument specifies which values are to be copied. For most applications, the pixel type is GL_COLOR to copy color indices or RGB values.

Pixel zoom is applied to the output pixels but not to the input pixels. In the example just above, a 16 x 16-pixel image will be copied to the lower-left corner of the window and scaled to 32 x 32 pixels. Offsets and sizes specified with calls to glPixelStore do not affect glCopyPixels. Changes made with glPixelTransfer and glPixelMap do, however.

A Bitmap File Viewer

Now that we’ve covered all the bitmap-related functions that are available, let’s write a Windows .BMP file-viewing program using OpenGL. Our goals for this program are fairly straightforward:

  Load any Windows .BMP file
  Scale the image to the current window size
  Provide simple controls to change the image brightness and gamma correction
  Show a magnified view of the image underneath the mouse pointer
  Save the displayed image to disk
  Print the displayed image

The final code for this program can be found in CH11\OGLVIEW.C.

About Windows Bitmap Files

Before we write the code, let’s review the ubiquitous Windows bitmap format. Despite their limitations, Windows .BMP files are probably the most common and widely supported files used by PCs capable of from 2 to 16.7 million colors. With only a few exceptions, .BMP files do not utilize data compression schemes, so it’s easy to read and use these files in your OpenGL programs.

A .BMP file is organized into three or four sections, depending on the type of colors used (see Figure 11-3). All .BMP files start with a BITMAPFILEHEADER structure containing an identification string (“BM”) the total size of the file, and an offset to the actual image data. Here is that structure:

typedef struct
{
  WORD    bfType;             /* “BM” */
  DWORD   bfSize;             /* Size of file in bytes */
  WORD    bfReserved1;        /* Reserved, always 0 */
  WORD    bfReserved2;        /* Reserved, always 0 */
  DWORD   bfOffBits;          /* Offset to image in bytes */
} BITMAPFILEHEADER;


Figure 11-3  Organization of a .BMP file

Following the file header is a BITMAPINFOHEADER structure that describes the contents of the image, as follows:

typedef struct
{
  DWORD      biSize;          /* Size of BITMAPINFOHEADER in bytes */
  LONG       biWidth;         /* Width of image in pixels */
  LONG       biHeight;        /* Height of image in pixels */
  WORD       biPlanes;        /* # of color planes (always 1) */
  WORD       biBitCount;      /* # of color bits */
  DWORD      biCompression;   /* Type of compression used */
  DWORD      biSizeImage;     /* Size of the image in bytes */
  LONG       biXPelsPerMeter; /* Horizontal pixels per meter */
  LONG       biYPelsPerMeter; /* Vertical pixels per meter */
  DWORD      biClrUsed;       /* Number of color used */
  DWORD      biClrImportant;  /* Number of 'important’ colors */
} BITMAPINFOHEADER;

For color index (palette) images, a color palette follows the BITMAPINFOHEADER structure for every color in the image. Image data follows immediately after.


Previous Table of Contents Next