Next: Color Quantization, Up: Utilities [Contents][Index]

#### 37.8.1 Memory Allocation

To create a matrix to be used in several of the functions listed above use either `fl_get_matrix()` described above or

    void *fl_make_matrix(int nrow, int ncol, unsigned int esize,
                          void *inMem);

where `nrow` and `ncol` are the number of rows and columns of the matrix respectively. `esize` is the size (in bytes) of each matrix element.

Both functions return a two-dimensional array of entities of size `esize`. The first function initializes all elements to zero. The second function does not allocate nor initialize memory for the matrix itself. Instead it uses the memory with address `inMem` that is supplied by the caller, which should be a one-dimensional array of length `nrow * ncol * esize`.

You can use the returned pointer as a regular two-dimensional array (`matrix[r][c]`) or as a single array of length `nrow *ncol`, starting from at `matrix[0]`:

    short **matrix = fl_get_matrix(nrow, ncol, sizeof **matrix);

    /* access the matrix as a 2-d array */
    matrix[3][4] = 5;

    /* or access it as 1D array */
    *(matrix[0] + 3 * ncol + 4) = 5;

    /* most useful in image processing to use it as 1D array */

    memcpy(saved, matrix, nrow * ncol * sizeof **matrix);

To free a matrix allocated using one the above functions, use

    void fl_free_matrix(void *matrix);

The function frees all memory allocated. After the function returns the matrix cab not be de-referenced anymore. In the case where the matrix was created by `fl_make_matrix()` the function will only free the memory that’s allocated to hold the matrix indices but not the memory supplied by the caller. It is the caller’s responsibility to free that part of the memory.

There are also some useful functions that manipulate images directly. The following is a brief summary of them.

    FL_IMAGE *flimage_dup(FL_IMAGE *im);

This function duplicates an image `im` and returns the duplicated image. At the moment, only the first image is duplicated even if the input image has multiple frames. Furthermore, markers and annotations are not duplicated.

    Pixmap flimage_to_pixmap(FL_IMAGE *im, FL_WINDOW win);
    int flimage_from_pixmap(FL_IMAGE *im, Pixmap pixmap);

The first function converts an image into a Pixmap (a server side resource) that can be used in the pixmap object (see pixmap-class???).

The second function does the reverse. `im` must be a properly allocated image.

Next: Color Quantization, Up: Utilities [Contents][Index]