Image Rotation Algorithm

Source: Internet
Author: User

Image Rotation Algorithm

Cndg

Image rotation is to convert the New and Old Coordinates and linear interpolation between pixels. Because the coordinate values are converted using multiplication, this article uses addition to replace multiplication to solve the speed problem.

The Code is as follows:

// Function: Image Rotation

// Parameter description: isrc, indicating the original image; idst, indicating the target image; angle, indicating the rotation angle; ncolortype, indicating the fill color of the area outside the boundary

// Return value: None
Void imagerotate2 (cximage * isrc, cximage * idst, double angle, int ncolortype)
{
Long nwidth, nheight, neffectwidth, nbpp, nxdpi, nydpi, nneweffectwidth, imageheight, imagewidth;
Nwidth = isrc-> getwidth ();
Nheight = isrc-> getheight ();
Neffectwidth = isrc-> geteffwidth ();
Nbpp = isrc-> getbpp ();
Nxdpi = isrc-> getxdpi ();
Nydpi = isrc-> getydpi ();

Byte * s = NULL, value;
S = isrc-> getbits ();
Double VCOs, vsin;
Double CX, Cy, VX, Vy, cntx, cnty;
Long off, off1, off2, noffset;
Long I, j, M, N;
Double an, svx, svy;
Double dx0, dy0, dx1, dy1, ZZ;

An = angle * PI/180.0;
VCOs = cos (an); vsin = sin ();

// Coordinates of the four corners of the source image (using the image center as the coordinate system origin)
Float fsrcx1, fsrcy1, fsrcx2, fsrcy2, fsrcx3, fsrcy3, fsrcx4, fsrcy4;
// Coordinates of the four angles after rotation (using the image center as the coordinate system origin)
Float fdstx1, fdsty1, fdstx2, fdsty2, fdstx3, fdsty3, fdstx4, fdsty4;
// Calculate the coordinates of the four corners of the source image (using the image center as the coordinate system origin)
Fsrcx1 = (float) (-(nwidth-1)/2 );
Fsrcy1 = (float) (nheight-1)/2 );
Fsrcx2 = (float) (nwidth-1)/2 );
Fsrcy2 = (float) (nheight-1)/2 );
Fsrcx3 = (float) (-(nwidth-1)/2 );
Fsrcy3 = (float) (-(nheight-1)/2 );
Fsrcx4 = (float) (nwidth-1)/2 );
Fsrcy4 = (float) (-(nheight-1)/2 );
 
// Calculate the coordinates of the four corners of the new image (using the image center as the coordinate system origin)
Fdstx1 = VCOs * fsrcx1 + vsin * fsrcy1;
Fdsty1 =-vsin * fsrcx1 + VCOs * fsrcy1;
Fdstx2 = VCOs * fsrcx2 + vsin * fsrcy2;
Fdsty2 =-vsin * fsrcx2 + VCOs * fsrcy2;
Fdstx3 = VCOs * fsrcx3 + vsin * fsrcy3;
Fdsty3 =-vsin * fsrcx3 + VCOs * fsrcy3;
Fdstx4 = VCOs * fsrcx4 + vsin * fsrcy4;
Fdsty4 =-vsin * fsrcx4 + VCOs * fsrcy4;
// Calculate the actual width of the rotated image
Imagewidth = (long) (max (FABS (fdstx4-fdstx1), FABS (fdstx3-fdstx2) + 0.5 );
// Calculate the height of the rotated image
Imageheight = (long) (max (FABS (fdsty4-fdsty1), FABS (fdsty3-fdsty2) + 0.5 );

Double F1, F2;
F1 = (double) (-0.5 * (imagewidth-1) * VCOs-0.5 * (imageheight-1) * vsin + 0.5*(nwidth-1 ));
F2 = (double) (0.5 * (imagewidth-1) * vsin-0.5 * (imageheight-1) * VCOs + 0.5*(nheight-1 ));

//////////////////////////////////////// ////////////////////////////////////////
// Allocate space
Nneweffectwidth = (imagewidth * nbpp + 31)/32*4;
Byte * D = new byte [nneweffectwidth * imageheight];
If (ncolortype = 1) // black
{
Memset (D, 0, sizeof (byte) * (nneweffectwidth * imageheight ));
}
Else
{
Memset (D, 255, sizeof (byte) * (nneweffectwidth * imageheight ));
}
//////////////////////////////////////// /////////////////////////////////////////
Cntx = F1 + 0.5;
Cnty = f2+ 0.5;
Cx = 0; Cy = 0;
Svx = Cx * VCOs + cy * vsin + cntx;
Svy =-cx * vsin + cy * VCOs + cnty;
Off = 0, off1 = 0;
Long I0, J0;
For (I = 0; I <imageheight; I ++)
{
VX = svx;
Vy = svy;
Off = I * nneweffectwidth + 0;
For (j = 0; j <imagewidth; j ++)
{
// Calculate the coordinates of the pixel in S
// Vy =-J * vsin + I * VCOs + F2 + 0.5;
// Vx = J * VCOs + I * vsin + F1 + 0.5;
M = (long) VX;
N = (long) Vy;
If (M <1) | (M> nWidth-2) | (n <1) | (n> nHeight-2 ))
{
ZZ = 0;
If (nbpp = 24)
{
If (ncolortype = 1) // black
{
ZZ = 0;
}
Else
{
ZZ = 255;
}
D [off] = (INT) ZZ;
D [off + 1] = (INT) ZZ;
D [off + 2] = (INT) ZZ;
}
Else
{
If (ncolortype = 1) // black
{
ZZ = 0;
}
Else
{
ZZ = 255;
}
D [off] = (INT) ZZ;
}

}
Else
{
If (nbpp = 24)
{
Dx0 = VX-m; dy0 = Vy-N;
Dx1 = 1-dx0; dy1 = 1-dy0;
Off1 = N * neffectwidth + 3 * m;
ZZ = 0;
ZZ = s [off1] * dx1 * dy1;
Off2 = off1 + 3;
ZZ + = s [off2] * dx0 * dy1;
Off2 = off1 + neffectwidth;
ZZ + = s [off2] * dx1 * dy0;
Off2 = off1 + neffectwidth + 3;
ZZ + = s [off2] * dx0 * dy0;
If (zz> 255)
ZZ = 255;
If (zz <0)
ZZ = FABS (zz );
D [off] = (INT) ZZ;

Off1 = N * neffectwidth + 3 * m + 1;
ZZ = 0;
ZZ = s [off1] * dx1 * dy1;
Off2 = off1 + 3;
ZZ + = s [off2] * dx0 * dy1;
Off2 = off1 + neffectwidth;
ZZ + = s [off2] * dx1 * dy0;
Off2 = off1 + neffectwidth + 3;
ZZ + = s [off2] * dx0 * dy0;
If (zz> 255)
ZZ = 255;
If (zz <0)
ZZ = FABS (zz );
D [off + 1] = (INT) ZZ;

Off1 = N * neffectwidth + 3 * m + 2;
ZZ = 0;
ZZ = s [off1] * dx1 * dy1;
Off2 = off1 + 3;
ZZ + = s [off2] * dx0 * dy1;
Off2 = off1 + neffectwidth;
ZZ + = s [off2] * dx1 * dy0;
Off2 = off1 + neffectwidth + 3;
ZZ + = s [off2] * dx0 * dy0;
If (zz> 255)
ZZ = 255;
If (zz <0)
ZZ = FABS (zz );
D [off + 2] = (INT) ZZ;
}
Else
{
Dx0 = VX-m; dy0 = Vy-N;
Dx1 = 1-dx0; dy1 = 1-dy0;
Off1 = N * neffectwidth + m;
ZZ = 0;
ZZ = s [off1] * dx1 * dy1;
Off2 = off1 + 1;
ZZ + = s [off2] * dx0 * dy1;
Off2 = off1 + neffectwidth;
ZZ + = s [off2] * dx1 * dy0;
Off2 = off1 + neffectwidth + 1;
ZZ + = s [off2] * dx0 * dy0;
If (zz> 255)
ZZ = 255;
If (zz <0)
ZZ = FABS (zz );
D [off] = (INT) ZZ;
}
}
If (nbpp = 24)
{
Off + = 3;
}
Else
{
Off ++;
}
VX = VX + VCOs;
Vy = Vy-vsin;
}
Svx = svx + vsin;
Svy = svy + VCOs;
}

Cximage TMP;
TMP. Copy (* isrc );
TMP. Create (imagewidth, imageheight, nbpp, isrc-> GetType ());
TMP. setpalette (isrc-> getpalette ());
// TMP. setxdpi (nxdpi );
// TMP. setydpi (nydpi );
Rgbquad color;
For (I = 0; I <imageheight; I ++)
{
For (j = 0; j <imagewidth; j ++)
{
Noffset = I * nneweffectwidth + (nbpp/8) * J;
If (nbpp = 24)
{
// Color = RGB (d [noffset], d [noffset + 1], d [noffset + 2]);
Color. rgbred = d [noffset + 2];
Color. rgbgreen = d [noffset + 1];
Color. rgbblue = d [noffset];
Color. rgbreserved = 0;
TMP. setpixelcolor (J, I, color );
}
Else
{
TMP. setpixelindex (J, I, d [noffset]);
}}
}

If (idst)
Idst-> copy (TMP );
Else
Isrc-> copy (TMP );
If (d)
{
Delete [] D;
D = NULL;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.