첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ BitmapHelper.cs

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace TestProject
{
    /// <summary>
    /// 비트맵 헬퍼
    /// </summary>
    public static class BitmapHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 비트맵 로드하기 - LoadBitmap(filePath)

        /// <summary>
        /// 비트맵 로드하기
        /// </summary>
        /// <param name="filePath">파일 경로</param>
        /// <returns>비트맵</returns>
        public static Bitmap LoadBitmap(string filePath)
        {
            using(Bitmap bitmap = new Bitmap(filePath))
            {
                return new Bitmap(bitmap);
            }
        }

        #endregion
        #region 오일 페인트 필터 적용하기 - ApplyOilPaintFilter(sourceBitmap, level, filterSize)

        /// <summary>
        /// 오일 페인트 필터 적용하기
        /// </summary>
        /// <param name="sourceBitmap">소스 비트맵</param>
        /// <param name="level">레벨</param>
        /// <param name="filterSize">필터 크기</param>
        /// <returns>비트맵</returns>
        public static Bitmap ApplyOilPaintFilter(Bitmap sourceBitmap, int level, int filterSize)
        {
            BitmapData sourceBitmapData = sourceBitmap.LockBits
            (
                new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height),
                ImageLockMode.ReadOnly,
                PixelFormat.Format32bppArgb
            );

            byte[] sourceByteArray = new byte[sourceBitmapData.Stride * sourceBitmapData.Height];
            byte[] targetByteArray = new byte[sourceBitmapData.Stride * sourceBitmapData.Height];

            Marshal.Copy(sourceBitmapData.Scan0, sourceByteArray, 0, sourceByteArray.Length);

            sourceBitmap.UnlockBits(sourceBitmapData);

            int[] intensityArray = new int [level];
            int[] blueArray      = new int [level];
            int[] greenArray     = new int [level];
            int[] redArray       = new int [level];

            level = level - 1;

            int filterOffset     = (filterSize - 1) / 2;
            int targetOffset     = 0;
            int sourceOffset     = 0;
            int currentIntensity = 0;
            int maximumIntensity = 0;
            int maximumIndex     = 0;

            double blue  = 0;
            double green = 0;
            double red   = 0;

            for(int offsetY = filterOffset; offsetY < sourceBitmap.Height - filterOffset; offsetY++)
            {
                for(int offsetX = filterOffset; offsetX < sourceBitmap.Width - filterOffset; offsetX++)
                {
                    blue = green = red = 0;

                    currentIntensity = maximumIntensity = maximumIndex = 0;

                    intensityArray = new int[level + 1];
                    blueArray      = new int[level + 1];
                    greenArray     = new int[level + 1];
                    redArray       = new int[level + 1];

                    targetOffset = offsetY * sourceBitmapData.Stride + offsetX * 4;

                    for(int filterY = -filterOffset; filterY <= filterOffset; filterY++)
                    {
                        for(int filterX = -filterOffset; filterX <= filterOffset; filterX++)
                        {
                            sourceOffset = targetOffset + (filterX * 4) + (filterY * sourceBitmapData.Stride);

                            currentIntensity = (int )Math.Round
                            (
                                ((double)(sourceByteArray[sourceOffset    ] +
                                          sourceByteArray[sourceOffset + 1] +
                                          sourceByteArray[sourceOffset + 2]) / 3.0 * (level)) / 255.0
                            );

                            intensityArray[currentIntensity] += 1;

                            blueArray [currentIntensity] += sourceByteArray[sourceOffset    ];
                            greenArray[currentIntensity] += sourceByteArray[sourceOffset + 1];
                            redArray  [currentIntensity] += sourceByteArray[sourceOffset + 2];

                            if(intensityArray[currentIntensity] > maximumIntensity)
                            {
                                maximumIntensity = intensityArray[currentIntensity];

                                maximumIndex = currentIntensity;
                            }
                        }
                    }

                    blue  = blueArray [maximumIndex] / maximumIntensity;
                    green = greenArray[maximumIndex] / maximumIntensity;
                    red   = redArray  [maximumIndex] / maximumIntensity;

                    targetByteArray[targetOffset    ] = ClipByte(blue);
                    targetByteArray[targetOffset + 1] = ClipByte(green);
                    targetByteArray[targetOffset + 2] = ClipByte(red);
                    targetByteArray[targetOffset + 3] = 255;
                }
            }

            Bitmap targetBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height);

            BitmapData targetBitmapData = targetBitmap.LockBits
            (
                new Rectangle(0, 0, targetBitmap.Width, targetBitmap.Height),
                ImageLockMode.WriteOnly,
                PixelFormat.Format32bppArgb
            );

            Marshal.Copy(targetByteArray, 0, targetBitmapData.Scan0, targetByteArray.Length);

            targetBitmap.UnlockBits(targetBitmapData);

            return targetBitmap;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Private

        #region 바이트 자르기 - ClipByte(value)

        /// <summary>
        /// 바이트 자르기
        /// </summary>
        /// <param name="value">값</param>
        /// <returns>바이트</returns>
        private static byte ClipByte(double value)
        {
            return (byte)(value > 255 ? 255 : (value < 0 ? 0 : value));
        }

        #endregion
    }
}

 

728x90

 

▶ MainForm.cs

using System.Drawing;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            Bitmap sourceBitmap = BitmapHelper.LoadBitmap("IMAGE\\sample.jpg");
            Bitmap targetBitmap = BitmapHelper.ApplyOilPaintFilter(sourceBitmap, 15, 13);

            this.pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
            this.pictureBox.Image    = targetBitmap;
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요