GDBByteChanIO -- Generic Byte Image Transfer

GDBByteChanIO() (Image Window Byte Image data I/O) allows access to the imagery data held on a generic database file. There are two functions: reading of a rectangular window of imagery from the file into a byte (pixel) buffer; and writing of data from a byte (pixel) buffer into a rectangular window of imagery on the file.

GDBByteChanIO() automatically performs shrinking/zooming between windows of different sizes, mapping between buffer and generic database channels, and data type conversions.

See Also: GDBIntChanIO(), GDBRealChanIO()

CALL SEQUENCE

void GDBByteChanIO(fpGDB, nFunc, nXOff, nYOff, nXSize, nYSize,
                  pabyImgBuf, nBufXSize, nBufYSize,
                  nBufChanCount, panChanMap);
FILE    *fpGDB;
           File pointer of generic database file to access.
int     nFunc;
           Function to perform.  This should be GDB_READ, to read
           from the file into pabyImgBuf, or GDB_WRITE to write from
           pabyImgBuf into the file.
int     nXOff;
           X (pixel) offset to imagery window in file.
int     nYOff;
           Y (line)  offset to imagery window in file.
int     nXSize;
           X (pixel) size of imagery window in file.
int     nYSize;
           Y (line)  size of imagery window in file.
uchar   *pabyImgBuf;
           Buffer of byte image data to read/write.

           On a read, this buffer receives  the  imagery data  read
           from  the file.  On a write,  this buffer  contains  the
           imagery  data to write into the file.  The image held in
           pabyImgBuf has dimensions nBufYSize lines, each line made
           up of nBufXSize pixels, each pixel having nBufChanCount
           channels.
int     nBufXSize;
           X (pixel) size of imagery window in buffer.
int     nBufYSize;
           Y (line)  size of imagery window in buffer.
int     nBufChanCount;
           Number of channel values in imgbuf[].
int     *panChanMap;
           Channel mapping buffer.  The list of channels to read,
           or write.  This array should contain nBufChanCount entries.

Data Organization

GDBByteChanIO() expects buffer pabyImgBuf to contain nBufXSize * nBufYSize * nBufChanCount elements arranged as follows: there are nBufYSize lines where each line is made up of nBufXSize pixels where each pixel is made up of nBufChanCount channels. This is referred to as pixel interleaving.

For example: An image window with dimensions two lines by three pixels with two channels can conceptually be arranged as follows:

                a11 a12 a13        b11 b12 b13
                a21 a22 a23        b21 b22 b23
When packed into pabyImgBuf, data would be arranged as follows:

        a11 b11 a12 b12 a13 b13 a21 b21 a22 b22 a23 b23

Channel Mapping

Channel mapping determines how the channels held in pabyImgBuf are mapped onto the channels in the database file. This is necessary since pabyImgBuf often holds only a subset of the channels that are actually on the database.

Channel mapping is determined by the panChanMap buffer where panChanMap[n] holds the channel on disk which is associated with the n'th channel in buffer pabyImgBuf. The panChanMap array has length nBufChanCount.

For example: Suppose the database file has 12 channels, and pabyImgBuf has 3 channels. The first channel in pabyImgBuf is mapped to channel 7 on disk, the second to 11, and the third to 2. panChanMap would be:

                panChanMap[0] =  7
                panChanMap[1] = 11
                panChanMap[2] =  2

Windows

A rectangular window of imagery is defined on the file using offset and size by the parameters nXOff, nYOff, nXSize, and nYSize.

The passed buffer pabyImgBuf is assumed to contain an image of the size defined by parameters nBufXSize by nBufYSize.

If the window size on the file and the image size in pabyImgBuf do not match then GDBByteChanIO() will automatically shrink or zoom the image data. Shrinking is done using decimation while zooming is done using replication. In most applications however, data is used at 'full' (1 to 1) resolution where nXSize=nBufXSize and nYSize=nBufYSize.

Data Conversion

Each channel of imagery data on a file can be one of the following four types: 8 bit unsigned integer (also called byte); 16 bit signed integer; 16 bit unsigned integer; and 32 bit real. A GeoGateway file may contain a mixture of these data types.

The caller of GDBByteChanIO() does not need know the type of data each channel on the file contains since GDBByteChanIO() automatically converts between byte and other data types. This gives the appearance that all channels on the file are 8 bit.

As a general rule, when one data type is converted to another, input values which are less than or greater than the minimum or maximum allowed output values are set to the minimum or maximum output value. Real values are truncated to integer values, where necessary. Scaling and rounding is NOT performed.

The following table shows how a pixel value N in the database is converted to and from a byte value in the buffer, for each of the four data types.

 +---------------+-----------------------+-----------------------+
 | GDBByteChanIO |         READ          |        WRITE          |
 +---------------+---------------+-------+---------------+-------+
 | data          |   file        | buffer|     buffer    | file  |
 | type          |   input       | output|     input     | output|
 +---------------+---------------+-------+---------------+-------+
 | 8-bit         |               |       |               |       |
 | unsigned      |   0<=N<=255   |   N   |     0<=N<=255 |   N   |
 | integer       |               |       |               |       |
 +---------------+---------------+-------+---------------+-------+
 | 16-bit        |   0> N        |   0   |               |       |
 | signed        |   0<=N<=255   |   N   |     0<=N<=255 |   N   |
 | integer       |      N >255   |  255  |               |       |
 +---------------+---------------+-------+---------------+-------+
 | 16-bit        |   0> N        |   0   |               |       |
 | unsigned      |   0<=N<=255   |   N   |   0<=N<=255   |   N   |
 | integer       |     N >255    |  255  |               |       |
 +---------------+---------------+-------+---------------+-------+
 | 32-bit        |   0> N        |   0   |               |       |
 | real          |   0<=N<=255   | int(N)|   0<=N<=255   |real(N)|
 |               |      N >255   |  255  |               |       |
 +---------------+---------------+-------+---------------+-------+

EXAMPLE1

Image data from channel 3 on a file is read, multiplied by 2 and written back to channel 4 of the same file. This is done on a line by line basis for the entire image.

             . . .
    FILE        *fp;
    int         line,pixs,lins,chns,i;
    unsigned int  pix;
    unsigned char imagry[10000];
    char        dbname[64];
    int         inchan,outchan;

             . . .

    fp = GDBOpen (dbname,"r+");         | Open the PCIDSK file
    GDBSizeInfo(fp,&pixs,&lins,&chns);  | Get database size

    inchan = 3;
    outchan = 4;
    for (line = 1; line <= lins; line++)
                | Loop through each line:  read, process and write
    {
        GDBByteChanIO (fp,GDB_READ,0,line-1,pixs,1,
                       imagry,pixs,1,1,&inchan);
        for (i = 0; i < pixs; i++)
        {
            pix = imagry[i];
            pix *= 2;
            if (pix > 255) pix = 255;
            imagry[i] = pix;
        }
        GDBByteChanIO (idb_fp,GDB_WRITE,0,line-1,pixs,1,
                       imagry,pixs,1,1,&outchan);
    }

EXAMPLE2

A window of data, offset (123 pixels, 111 lines), size (128 pixels, 128 lines), is to be read off a PCIDSK file and shrunk to a size of 32 pixels by 64 lines. In addition only the three channels 7, 3 and 5 are required off disk.

             . . .
    FILE          *fp;
    int           chnmap[3],window[4];
    char          dbname[64];
    unsigned char bimg[6144];            | 6144=32*64*3
             . . .
    fp = GDBOpen(dbname,"r+");           | Open the PCIDSK file 
    window[0] = 123;                     | X offset
    window[1] = 111;                     | Y offset
    window[2] = 128;                     | X size
    window[3] = 128;                     | Y size
    chnmap[0] = 7;
    chnmap[1] = 3;
    chnmap[2] = 5;
    CheckChan (fp,"CHNMAP",CHNMAP,3);
                |  Check that window is valid and channels exist
    CheckWin (fp,"WINDOW",window,chnmap);

    GDBByteChanIO(fp,GDB_READ,window[0],window[1],window[2],
                  window[3],bimg,32,64,3,chnmap);
                            | Get the window off the database
             . . .

About PCI Help Gateway