freeimage.h

FreeImage_Rotate

1 8 24 32 16UINT16 48RGB16 64RGBA16 32FLOAT 96RGBF 128RGBAF

DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Rotate(FIBITMAP *dib, double angle, const
void *bkcolor FI_DEFAULT(NULL));

Esta función rota una imagen estándar (1-, 8-bit en escala de grises o a 24-, 32-bit en color), una imagen RGB(A)16 o RGB(A)F mediante 3 tijeras. El ángulo de rotación se especifica mediante el parámetro angle en grados. La rotación se produce alrededor del centro del área de la imagen. La imagen rotada retiene el tamaño y la relación de aspecto de la imagen origen (el tamaño de la imagen de destino suele ser más grande), así que esta función debe ser usada cuando se rota una imagen 90°, 180° o 270°.

// this code assumes there is a bitmap loaded and
// present in a variable called ‘dib’
// perform a 90° rotation (CCW rotation)
FIBITMAP *rotated = FreeImage_Rotate(dib, 90);

Para ima´genes de 1-bit, la rotación está limitada a ángulos cuyo valor sea un entero múltiplo de 90° (por ejemplo –90, 90, 180, 270). Se devuelve un valor NULL para otros ángulos.

Cuando el valor del ángulo no sea un entero múltiplo de 90°, el fondo se rellena con el color suministrado en el parámetro bkcolor. Cuando bkcolor es NULL (valor por defecto), el fondo se rellena con el color negro. El tipo de dato de bkcolor depende del tipo de imagen (ver el siguiente ejemplo).

FIBITMAP* testRotateWithBackground(FIBITMAP *src, double angle) {
    FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(src);
    switch(image_type) {
        case FIT_BITMAP:
            switch(FreeImage_GetBPP(src)) {
                case 8:
                    {
                        BYTE color = 128;
                        return FreeImage_Rotate(src, angle, &color);
                    }
                    break;
                case 24: // we could also use 'RGBTRIPLE color' here
                case 32:
                    {
                        RGBQUAD color = { 0, 0, 255, 0 };
                        // for 24-bit images, the first 3 bytes will be read
                        // for 32-bit images, the first 4 bytes will be read
                        return FreeImage_Rotate(src, angle, &color);
                    }
                    break;
            }
            break;
        case FIT_UINT16:
            {
                WORD color = 128;
                return FreeImage_Rotate(src, angle, &color);
            }
            break;
        case FIT_RGB16: // we could also use 'FIRGB16 color' here
        case FIT_RGBA16:
            {
                FIRGBA16 color = { 0, 0, 255, 0 };
                // for RGB16 images, the first 3 WORD will be read
                // for RGBA16 images, the first 4 WORD will be read
                return FreeImage_Rotate(src, angle, &color);
            }
            break;
        case FIT_FLOAT:
            {
                float color = 0.5F;
                return FreeImage_Rotate(src, angle, &color);
            }
            break;
        case FIT_RGBF: // we could also use 'FIRGBF color' here
        case FIT_RGBAF:
            {
                FIRGBAF color = { 0, 0, 1, 0 };
                // for RGBF images, the first 3 float will be read
                // for RGBAF images, the first 4 float will be read
                return FreeImage_Rotate(src, angle, &color);
            }
            break;
    }
    return NULL;
}

Referencias

Paeth A., A Fast Algorithm for General Raster Rotation. Graphics Gems, p. 179, Andrew Glassner editor, Academic Press, 1990.

Yariv E., High quality image rotation (rotate by shear). [Online] http://www.codeproject.com/bitmap/rotatebyshear.asp