Creating Gray-scale Images

This code example illustrates the creation of a gray-scale image. In gray-scale images, each pixel is characterized by a single intensity value.

To run the example from the command line, type this command:
>create -t3 -w256 -h40
(-w and -h specify the image width and height).

The source:

void create_manual_gray( int width = 128, int height = 100 ) {
    IplImage* img = iplCreateImageHeader(
        1,                    // 1 channel
        0,                    // no alpha channel
        IPL_DEPTH_16U,        // unsigned short data
        "GRAY",               // color model
        "GRAY",               // channel sequence
        IPL_DATA_ORDER_PIXEL, // channel arrangement
        IPL_ORIGIN_TL,        // top left orientation
        IPL_ALIGN_DWORD,      // 4 bytes align
        width, height,        // image width and height
        NULL, NULL,           // no ROI, no mask ROI
        NULL, NULL);          // ID, image not tiled

    /// allocate image data without filling
    iplAllocateImage( img, 0, 0 );

    /// draw wedge if image was created
    if( img->imageData ) {
        /// factor to get full range of pixel values 
        int const muler = 65600 / (width + height);
        char* p = img->imageData;
        /// set every pixel value
        for( int y=0; y<height; ++y ) {
            for( int x=0; x<width; ++x )
                /// pixel is of 16u depth
                ((unsigned short*)p)[x] = muler * (x + y);
            /// go to next line of the image
            p = p + img->widthStep;
        }
    }
    /// display the image
    ipView( img, " Manual Gray", is_modal );
    /// free memory
    iplDeallocate( img, IPL_IMAGE_ALL );
}

* Legal Information © 1998-2000, Intel Corporation