Next: Scaling, Previous: Image Flipping, Up: Simple Image Processing [Contents][Index]

#### 37.7.5 Cropping

There are two functions available to crop an image

    int flimage_autocrop(FL_IMAGE *im, unsigned int background);
    int flimage_crop(FL_IMAGE *im, int xl, int yt, int xr, int yb);

The first function, as its name suggests, automatically crops an image using the background as the color to crop. The function works by searching the image from all four sides and removing all contiguous regions of the uniform background from the sides. The image is modified in place. If cropping is successful, a non-negative integer is returned, otherwise -1. If `background` is specified as the constant `FLIMAGE_AUTOCOLOR`, the background is chosen as the first pixel of the image.

The second function uses the parameters supplied by the user to crop the image. `xl` and `xr` are the offsets into the image from the left and the right sides, respectively, i.e., if both `xl` and `xr` are 1, the cropping removes the first column and the last column from the image. Parameters `yt` and `yb` specify the offsets into the image from the top and bottom of the image respectively.

Note the offsets do not have to be positive. When they are negative, they indicate enlargement of the image. The additional regions are filled with the uniform color specified by `image->fill_color`, a packed RGB color. This can be quite useful to add a couple of pixels of border to an image. For example, the following adds a 1 pixel wide yellow border to an image

    image->fill_color = FL_PACK(255,255,0);
    flimage_crop(image, -1, -1, -1, -1);

Another function is available that can be used to obtain the auto-cropping offsets

    int flimage_get_autocrop(FL_IMAGE *im, unsigned background,
                             int *xl, int *yt, int *xl, int *yb);

This function works the same way as `flimage_autocrop()`, except that no actual cropping is performed. Upon function return the parameters `xl`, `yt`, `xl` and `yb` are set to the offsets found by the function. The application can then make adjustment to these offsets and call `flimage_crop()`.

Next: Scaling, Previous: Image Flipping, Up: Simple Image Processing [Contents][Index]