ImageGear for C and C++ on Windows v19.1 - Updated
Accessing Pixels of Large Images
User Guide > How to Work with... > Common Operations > Loading and Saving > Load an Image or Document > Working with Large Images > Accessing Pixels of Large Images

ImageGear manages the use of memory mapped files internally. In most cases, the application does not need any additional code for working with memory mapped images, except for setting the global control parameters. However, if an application accesses individual pixels or rasters of a large image, or accesses pixel data directly by the image or raster pointer, ImageGear does not know when to flush the image’s memory mapped file. In this case the application shall flush the memory mapped file explicitly, using IG_DIB_flush.

 
Copy Code
AT_DIMENSION nRasterSize;
AT_INT nRasterCountToFlush;
AT_INT i;
AT_PIXEL* nBuffer;
AT_DIMENSION nImageHeight;
AT_ERRCOUNT nErrCount;
HIGEAR hIGear;

// Load the image
nErrCount = IG_load_file("picture.tif", &hIGear);
if( nErrCount == 0 ) 
{
    IG_image_dimensions_get(hIGear, NULL, &nImageHeight, NULL);
    IG_DIB_raster_size_get(hIGear, IG_PIXEL_UNPACKED, &nRasterSize);
    nRasterCountToFlush = 200 * 1024 * 1024 / nRasterSize;
    nBuffer = new AT_PIXEL[nRasterSize];

    for(i = 0; i < nImageHeight; i ++)
    {
        // Get image raster
        IG_DIB_raster_get(hIGear, i, nBuffer, IG_PIXEL_UNPACKED);

        // Process the raster
        // ...

        if((i + 1) % nRasterCountToFlush == 0)
        {
            // We have accessed about 200 MB of sequential memory. 
            // Flush the memory-mapped file associated with the image.
            IG_DIB_flush(hIGear);
        }
    }
    // Flush the memory-mapped file at the end of pixel access.
    IG_DIB_flush(hIGear);

    delete[] nBuffer;
    IG_image_delete(hIGear);
}

A similar situation occurs when the application accesses image areas. Since areas can be small, even one automatic flush per area access operation can be too much, and can significantly degrade performance. ImageGear does not use automatic flushing in area access operations. Instead, the application shall flush the DIB after accessing one or several areas. If an area to be accessed is very large (hundreds of megabytes or more), we recommend splitting it into smaller areas and processing it sub-area by sub-area.

In order to minimize the amount of flushing, try to prefer row-wise order of processing, as opposed to column-wise. For example, if you need to access an image area consisting of 10 sub-areas vertically and 10 sub-areas horizontally, do it as follows:

  1. Access the first row of sub-areas
  2. Flush the image
  3. Access the second row of sub-areas
  4. Etc.