Image Filter art --- Swirl filter, image filter --- swirl

Source: Internet
Author: User
Tags image filter

Image Filter art --- Swirl filter, image filter --- swirl

The Swirl FilterSwirl filter turns an image around the center point (cenX, cenY) as follows:

Source image

The Code is as follows:

//

///

/// Swirl Filter

///

/// Source image.

/// The X position of Swirl.

/// The Y position of Swirl.

/// The degree of swirl, 0-360.

/// The result image.

Private Bitmap SwirlFilterProcess (Bitmap srcBitmap, int cenX, int cenY, int swilDegree)

{

Bitmap a = new Bitmap (srcBitmap );

Int w = a. Width;

Int h = a. Height;

Int radius = 0;

SwilDegree = Math. Max (0, Math. Min (360, swilDegree ));

Double k = swilDegree/3600.0;

Bitmap dst = new Bitmap (w, h );

System. drawing. imaging. bitmapData srcData =. lockBits (new Rectangle (0, 0, w, h), System. drawing. imaging. imageLockMode. readWrite, System. drawing. imaging. pixelFormat. format32bppArgb );

System. drawing. imaging. bitmapData dstData = dst. lockBits (new Rectangle (0, 0, w, h), System. drawing. imaging. imageLockMode. readWrite, System. drawing. imaging. pixelFormat. format32bppArgb );

Unsafe

{

Byte * pIn = (byte *) srcData. Scan0.ToPointer ();

Byte * pOut = (byte *) dstData. Scan0.ToPointer ();

Byte * p = null;

Int stride = srcData. Stride-w * 4;

Int offsetX = 0, offsetY = 0;

Int newX = 0, newY = 0;

Double radian = 0;

For (int y = 0; y

{

For (int x = 0; x <w; x ++)

{

OffsetX = x-cenX;

OffsetY = y-cenY;

Radian = Math. Atan2 (offsetY, offsetX );

Radius = (int) (Math. Sqrt (offsetX * offsetX + offsetY * offsetY ));

NewX = (int) (radius * Math. Cos (radian + k * radius) + cenX;

NewY = (int) (radius * Math. Sin (radian + k * radius) + cenY;

NewX = Math. Min (w-1, Math. Max (0, newX ));

NewY = Math. Min (h-1, Math. Max (0, newY ));

P = pIn + newY * srcData. Stride + newX * 4;

POut [0] = (byte) p [0];

POut [1] = (byte) p [1];

POut [2] = (byte) p [2];

POut [3] = (byte) 255;

POut + = 4;

}

POut + = stride;

}

A. UnlockBits (srcData );

Dst. UnlockBits (dstData );

}

Return dst;


}

Finally, put a complete C # program Demo: Success!

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.