![]() |
Leptonica
1.77.0
Image processing and image analysis suite
|
#include <string.h>#include <math.h>#include "allheaders.h"Go to the source code of this file.
Functions | |
| static void | rotateAMColorLow (l_uint32 *datad, l_int32 w, l_int32 h, l_int32 wpld, l_uint32 *datas, l_int32 wpls, l_float32 angle, l_uint32 colorval) |
| static void | rotateAMGrayLow (l_uint32 *datad, l_int32 w, l_int32 h, l_int32 wpld, l_uint32 *datas, l_int32 wpls, l_float32 angle, l_uint8 grayval) |
| static void | rotateAMColorCornerLow (l_uint32 *datad, l_int32 w, l_int32 h, l_int32 wpld, l_uint32 *datas, l_int32 wpls, l_float32 angle, l_uint32 colorval) |
| static void | rotateAMGrayCornerLow (l_uint32 *datad, l_int32 w, l_int32 h, l_int32 wpld, l_uint32 *datas, l_int32 wpls, l_float32 angle, l_uint8 grayval) |
| static void | rotateAMColorFastLow (l_uint32 *datad, l_int32 w, l_int32 h, l_int32 wpld, l_uint32 *datas, l_int32 wpls, l_float32 angle, l_uint32 colorval) |
| PIX * | pixRotateAM (PIX *pixs, l_float32 angle, l_int32 incolor) |
| PIX * | pixRotateAMColor (PIX *pixs, l_float32 angle, l_uint32 colorval) |
| PIX * | pixRotateAMGray (PIX *pixs, l_float32 angle, l_uint8 grayval) |
| PIX * | pixRotateAMCorner (PIX *pixs, l_float32 angle, l_int32 incolor) |
| PIX * | pixRotateAMColorCorner (PIX *pixs, l_float32 angle, l_uint32 fillval) |
| PIX * | pixRotateAMGrayCorner (PIX *pixs, l_float32 angle, l_uint8 grayval) |
| PIX * | pixRotateAMColorFast (PIX *pixs, l_float32 angle, l_uint32 colorval) |
Variables | |
| static const l_float32 | MIN_ANGLE_TO_ROTATE = 0.001 |
Grayscale and color rotation for area mapping (== interpolation)
Rotation about the image center
PIX *pixRotateAM()
PIX *pixRotateAMColor()
PIX *pixRotateAMGray()
static void rotateAMColorLow()
static void rotateAMGrayLow()
Rotation about the UL corner of the image
PIX *pixRotateAMCorner()
PIX *pixRotateAMColorCorner()
PIX *pixRotateAMGrayCorner()
static void rotateAMColorCornerLow()
static void rotateAMGrayCornerLow()
Faster color rotation about the image center
PIX *pixRotateAMColorFast()
static void rotateAMColorFastLow()
Rotations are measured in radians; clockwise is positive.
The basic area mapping grayscale rotation works on 8 bpp images.
For color, the same method is applied to each color separately.
This can be done in two ways: (1) as here, computing each dest
rgb pixel from the appropriate four src rgb pixels, or (2) separating
the color image into three 8 bpp images, rotate each of these,
and then combine the result. Method (1) is about 2.5x faster.
We have also implemented a fast approximation for color area-mapping
rotation (pixRotateAMColorFast()), which is about 25% faster
than the standard color rotator. If you need the extra speed,
use it.
Area mapping works as follows. For each dest
pixel you find the 4 source pixels that it partially
covers. You then compute the dest pixel value as
the area-weighted average of those 4 source pixels.
We make two simplifying approximations:
~ For simplicity, compute the areas as if the dest
pixel were translated but not rotated.
~ Compute area overlaps on a discrete sub-pixel grid.
Because we are using 8 bpp images with 256 levels,
it is convenient to break each pixel into a
16x16 sub-pixel grid, and count the number of
overlapped sub-pixels.
It is interesting to note that the digital filter that
implements the area mapping algorithm for rotation
is identical to the digital filter used for linear
interpolation when arbitrarily scaling grayscale images.
The advantage of area mapping over pixel sampling
in grayscale rotation is that the former naturally
blurs sharp edges ("anti-aliasing"), so that stair-step
artifacts are not introduced. The disadvantage is that
it is significantly slower.
But it is still pretty fast. With standard 3 GHz hardware,
the anti-aliased (area-mapped) color rotation speed is
about 15 million pixels/sec.
The function pixRotateAMColorFast() is about 10-20% faster
than pixRotateAMColor(). The quality is slightly worse,
and if you make many successive small rotations, with a
total angle of 360 degrees, it has been noted that the
center wanders -- it seems to be doing a 1 pixel translation
in addition to the rotation.
Definition in file rotateam.c.
| [in] | pixs | 2, 4, 8 bpp gray or colormapped, or 32 bpp RGB |
| [in] | angle | radians; clockwise is positive |
| [in] | incolor | L_BRING_IN_WHITE, L_BRING_IN_BLACK |
Notes:
(1) Rotates about image center.
(2) A positive angle gives a clockwise rotation.
(3) Brings in either black or white pixels from the boundary.
Definition at line 149 of file rotateam.c.
| [in] | pixs | 32 bpp |
| [in] | angle | radians; clockwise is positive |
| [in] | colorval | e.g., 0 to bring in BLACK, 0xffffff00 for WHITE |
Notes:
(1) Rotates about image center.
(2) A positive angle gives a clockwise rotation.
(3) Specify the color to be brought in from outside the image.
Definition at line 212 of file rotateam.c.
| [in] | pixs | |
| [in] | angle | radians; clockwise is positive |
| [in] | fillval | e.g., 0 to bring in BLACK, 0xffffff00 for WHITE |
Notes:
(1) Rotates the image about the UL corner.
(2) A positive angle gives a clockwise rotation.
(3) Specify the color to be brought in from outside the image.
Definition at line 508 of file rotateam.c.
| [in] | pixs | |
| [in] | angle | radians; clockwise is positive |
| [in] | colorval | e.g., 0 to bring in BLACK, 0xffffff00 for WHITE |
Notes:
(1) This rotates a color image about the image center.
(2) A positive angle gives a clockwise rotation.
(3) It uses area mapping, dividing each pixel into
16 subpixels.
(4) It is about 10% to 20% faster than the more accurate linear
interpolation function pixRotateAMColor(),
which uses 256 subpixels.
(5) For some reason it shifts the image center.
No attempt is made to rotate the alpha component.
Definition at line 741 of file rotateam.c.
| [in] | pixs | 1, 2, 4, 8 bpp gray or colormapped, or 32 bpp RGB |
| [in] | angle | radians; clockwise is positive |
| [in] | incolor | L_BRING_IN_WHITE, L_BRING_IN_BLACK |
Notes:
(1) Rotates about the UL corner of the image.
(2) A positive angle gives a clockwise rotation.
(3) Brings in either black or white pixels from the boundary.
Definition at line 447 of file rotateam.c.
| [in] | pixs | 8 bpp |
| [in] | angle | radians; clockwise is positive |
| [in] | grayval | 0 to bring in BLACK, 255 for WHITE |
Notes:
(1) Rotates about image center.
(2) A positive angle gives a clockwise rotation.
(3) Specify the grayvalue to be brought in from outside the image.
Definition at line 266 of file rotateam.c.
| [in] | pixs | |
| [in] | angle | radians; clockwise is positive |
| [in] | grayval | 0 to bring in BLACK, 255 for WHITE |
Notes:
(1) Rotates the image about the UL corner.
(2) A positive angle gives a clockwise rotation.
(3) Specify the grayvalue to be brought in from outside the image.
Definition at line 562 of file rotateam.c.
|
static |
This is a special simplification of area mapping with division of each pixel into 16 sub-pixels. The exact coefficients that should be used are the same as for the 4x linear interpolation scaling case, and are given there. I tried to approximate these as weighted coefficients with a maximum sum of 4, which allows us to do the arithmetic in parallel for the R, G and B components in a 32 bit pixel. However, there are three reasons for not doing that: (1) the loss of accuracy in the parallel implementation is visually significant (2) the parallel implementation (described below) is slower (3) the parallel implementation requires allocation of a temporary color image
There are 16 cases for the choice of the subpixel, and for each, the mapping to the relevant source pixels is as follows:
subpixel src pixel weights
0 sp1 1 (3 * sp1 + sp2) / 4 2 (sp1 + sp2) / 2 3 (sp1 + 3 * sp2) / 4 4 (3 * sp1 + sp3) / 4 5 (9 * sp1 + 3 * sp2 + 3 * sp3 + sp4) / 16 6 (3 * sp1 + 3 * sp2 + sp3 + sp4) / 8 7 (3 * sp1 + 9 * sp2 + sp3 + 3 * sp4) / 16 8 (sp1 + sp3) / 2 9 (3 * sp1 + sp2 + 3 * sp3 + sp4) / 8 10 (sp1 + sp2 + sp3 + sp4) / 4 11 (sp1 + 3 * sp2 + sp3 + 3 * sp4) / 8 12 (sp1 + 3 * sp3) / 4 13 (3 * sp1 + sp2 + 9 * sp3 + 3 * sp4) / 16 14 (sp1 + sp2 + 3 * sp3 + 3 * sp4) / 8 15 (sp1 + 3 * sp2 + 3 * sp3 + 9 * sp4) / 16
Another way to visualize this is to consider the area mapping (or linear interpolation) coefficients for the pixel sp1. Expressed in fourths, they can be written as asymmetric matrix:
4 3 2 1 3 2.25 1.5 0.75 2 1.5 1 0.5 1 0.75 0.5 0.25
The coefficients for the three neighboring pixels can be similarly written.
This is implemented here, where, for each color component, we inline its extraction from each participating word, construct the linear combination, and combine the results into the destination 32 bit RGB pixel, using the appropriate shifts.
It is interesting to note that an alternative method, where we do the arithmetic on the 32 bit pixels directly (after shifting the components so they won't overflow into each other) is significantly inferior. Because we have only 8 bits for internal overflows, which can be distributed as 2, 3, 3, it is impossible to add these with the correct linear interpolation coefficients, which require a sum of up to 16. Rounding off to a sum of 4 causes appreciable visual artifacts in the rotated image. The code for the inferior method can be found in prog/rotatefastalt.c, for reference.
Definition at line 840 of file rotateam.c.