private static Bitmap HistogramEqualizationFillIn(Bitmap bitmap, int total, int[,] statistics) { int width = bitmap.Width; int height = bitmap.Height; int R = 0, G = 1, B = 2; System.IntPtr srcScan, dstScan; BitmapData srcBmData, dstBmData; Bitmap dstBitmap = ImageExtract.InitPonitMethod(bitmap, width, height, out srcScan, out dstScan, out srcBmData, out dstBmData); unsafe //啟動不安全代碼 { byte *srcP = (byte *)srcScan; byte *dstP = (byte *)dstScan; int srcOffset = srcBmData.Stride - width * 3; int dstOffset = dstBmData.Stride - width * 3; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++, srcP += 3, dstP += 3) { dstP[2] = (byte)(((double)statistics[R, srcP[2]] / total) * (COLOR_SIZE_RANGE - 1)); dstP[1] = (byte)(((double)statistics[G, srcP[1]] / total) * (COLOR_SIZE_RANGE - 1)); dstP[0] = (byte)(((double)statistics[B, srcP[0]] / total) * (COLOR_SIZE_RANGE - 1)); } srcP += srcOffset; dstP += dstOffset; } } bitmap.UnlockBits(srcBmData); dstBitmap.UnlockBits(dstBmData); return(dstBitmap); }
protected Bitmap process(Bitmap srcBitmap) { int width = srcBitmap.Width; int height = srcBitmap.Height; System.IntPtr srcScan, dstScan; BitmapData srcBmData, dstBmData; Bitmap dstBitmap = ImageExtract.InitPonitMethod(srcBitmap, width, height, out srcScan, out dstScan, out srcBmData, out dstBmData); unsafe //啟動不安全代碼 { byte *srcP = (byte *)srcScan; byte *dstP = (byte *)dstScan; int srcOffset = srcBmData.Stride - width * 3; int dstOffset = dstBmData.Stride - width * 3; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++, srcP += 3, dstP += 3) { *(dstP + ImageExtract.COLOR_R) = processColorR(srcP[ImageExtract.COLOR_R], srcP[ImageExtract.COLOR_G], srcP[ImageExtract.COLOR_B]); *(dstP + ImageExtract.COLOR_G) = processColorG(srcP[ImageExtract.COLOR_R], srcP[ImageExtract.COLOR_G], srcP[ImageExtract.COLOR_B]); *(dstP + ImageExtract.COLOR_B) = processColorB(srcP[ImageExtract.COLOR_R], srcP[ImageExtract.COLOR_G], srcP[ImageExtract.COLOR_B]); } srcP += srcOffset; dstP += dstOffset; } } srcBitmap.UnlockBits(srcBmData); dstBitmap.UnlockBits(dstBmData); return(dstBitmap); }
private Bitmap makeBitmapFromPixels() { System.IntPtr srcScan, dstScan; BitmapData srcBmData, dstBmData; Bitmap dstBitmap = ImageExtract.InitPonitMethod(_SourceImage, _ImageWidth, _ImageHeight, out srcScan, out dstScan, out srcBmData, out dstBmData); Console.Write(_MaxVal); unsafe //啟動不安全代碼 { byte *srcP = (byte *)srcScan; byte *dstP = (byte *)dstScan; int srcOffset = srcBmData.Stride - _ImageWidth * 3; int dstOffset = dstBmData.Stride - _ImageWidth * 3; for (int y = 0; y < _ImageHeight; y++) { for (int x = 0; x < _ImageWidth; x++, srcP += 3, dstP += 3) { double val = Math.Abs(_output[x, y].Magnitude()); //*(dstP) = *(dstP + 1) = *(dstP + 2) = (byte)(Math.Log(val, 2) * _LOG_C); double outVal = (val / _MaxVal) * 255; *(dstP) = *(dstP + 1) = *(dstP + 2) = (byte)outVal; } srcP += srcOffset; dstP += dstOffset; } } _SourceImage.UnlockBits(srcBmData); dstBitmap.UnlockBits(dstBmData); return(dstBitmap); }
private void getPixelsFromImage() { System.IntPtr srcScan; BitmapData srcBmData; ImageExtract.InitPonitMethod(_SourceImage, _ImageWidth, _ImageHeight, out srcScan, out srcBmData); unsafe //啟動不安全代碼 { byte *srcP = (byte *)srcScan; int srcOffset = srcBmData.Stride - _ImageWidth * 3; for (int y = 0; y < _ImageHeight; y++) { for (int x = 0; x < _ImageWidth; x++, srcP += 3) { byte gray = calGray(srcP[2], srcP[1], srcP[0]); double val = gray; val *= ((x + y) % 2 == 0) ? -1 : 1; _pixels[x, y] = new Complex(val); } srcP += srcOffset; } } _SourceImage.UnlockBits(srcBmData); }
private static Bitmap binarization(Bitmap bitmap, int value) { int width = bitmap.Width; int height = bitmap.Height; System.IntPtr srcScan, dstScan; BitmapData srcBmData, dstBmData; Bitmap dstBitmap = ImageExtract.InitPonitMethod(bitmap, width, height, out srcScan, out dstScan, out srcBmData, out dstBmData); unsafe //啟動不安全代碼 { byte *srcP = (byte *)srcScan; byte *dstP = (byte *)dstScan; int srcOffset = srcBmData.Stride - width * 3; int dstOffset = dstBmData.Stride - width * 3; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++, srcP += 3, dstP += 3) { *dstP = (srcP[0] > value) ? MAX : MIN; //blue *(dstP + 1) = (srcP[1] > value) ? MAX : MIN; //green *(dstP + 2) = (srcP[2] > value) ? MAX : MIN; //red } srcP += srcOffset; dstP += dstOffset; } } bitmap.UnlockBits(srcBmData); dstBitmap.UnlockBits(dstBmData); return(dstBitmap); }
private static void HistogramEqualizationStatistics(Bitmap bitmap, ref int[,] statistics) { int width = bitmap.Width; int height = bitmap.Height; int R = 0, G = 1, B = 2; System.IntPtr srcScan; BitmapData srcBmData; ImageExtract.InitPonitMethod(bitmap, width, height, out srcScan, out srcBmData); unsafe //啟動不安全代碼 { byte *srcP = (byte *)srcScan; int srcOffset = srcBmData.Stride - width * 3; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++, srcP += 3) { statistics[R, srcP[2]] += 1; statistics[G, srcP[1]] += 1; statistics[B, srcP[0]] += 1; } srcP += srcOffset; } } bitmap.UnlockBits(srcBmData); }
public static Bitmap extract(Bitmap bitmap, out byte[,] pix, out byte[,] resPix) { int width = bitmap.Width, height = bitmap.Height; Bitmap dstBitmap = new Bitmap(bitmap); pix = ImageExtract.getimageArray(bitmap); resPix = new byte[3, width *height]; return(dstBitmap); }
private static int TrainingByThresholding(Bitmap bitmap, int value, int color) { if (color > 2 || color < 0) { return(0); } int width = bitmap.Width; int height = bitmap.Height; System.IntPtr srcScan, dstScan; BitmapData srcBmData, dstBmData; ImageExtract.InitPonitMethod(bitmap, width, height, out srcScan, out dstScan, out srcBmData, out dstBmData); int minCount = 1, minTotal = 0; int maxCount = 1, maxTotal = 0; unsafe //啟動不安全代碼 { byte *srcP = (byte *)srcScan; byte *dstP = (byte *)dstScan; int srcOffset = srcBmData.Stride - width * 3; for (int x = 1; x < width - 1; x++) { for (int y = 1; y < height - 1; y++) { byte current = (byte)(srcP[color]); //blue if (current > value) { minCount++; minTotal += current; } else { maxCount++; maxTotal += current; } } } } bitmap.UnlockBits(srcBmData); int res = ((minTotal / minCount) + (maxTotal / maxCount)) / 2; return(res); }
private Bitmap mosaic(Bitmap bitmap, int effect) { int width = bitmap.Width, height = bitmap.Height, count = effect * effect, offset = (effect / 2) + (effect % 2); Bitmap dstBitmap = new Bitmap(bitmap); byte[,] pix = ImageExtract.getimageArray(bitmap); byte[,] resPix = new byte[3, width *height]; for (int y = offset; y < (height - offset); y += effect) { for (int x = offset; x < (width - offset); x += effect) { //mask int current = x + y * width; int[] sum = { 0, 0, 0 }; for (int my = 0; my < effect; my++) { for (int mx = 0; mx < effect; mx++) { int pos = current + (mx - 1) + ((my - 1) * width); sum[0] += pix[0, pos]; sum[1] += pix[1, pos]; sum[2] += pix[2, pos]; } } sum[0] = (byte)(sum[0] / count); sum[1] = (byte)(sum[1] / count); sum[2] = (byte)(sum[2] / count); for (int my = 0; my < effect; my++) { for (int mx = 0; mx < effect; mx++) { int pos = current + (mx - 1) + ((my - 1) * width); resPix[0, pos] = (byte)sum[0]; resPix[1, pos] = (byte)sum[1]; resPix[2, pos] = (byte)sum[2]; } } } } ImageExtract.writeImageByArray(resPix, dstBitmap); return(dstBitmap); }
private Bitmap horizontal(Bitmap srcBitmap) { Bitmap dstBitmap = new Bitmap(srcBitmap); int width = srcBitmap.Width, height = srcBitmap.Height; byte[,] pixels = ImageExtract.getimageArray(srcBitmap); byte[,] result = new byte[3, width *height]; int x, y, offset, halfOffset, halfHeightOffset, halfHeight = height / 2; int r, g, b; for (y = 0; y < height - (height % 2); y += 2) { for (x = 0; x < width - (width % 2); x++) { offset = y * width; halfOffset = (y / 2) * width; halfHeightOffset = halfHeight * width; r = pixels[ImageExtract.COLOR_R, x + offset + width] + pixels[ImageExtract.COLOR_R, x + offset + width]; result[ImageExtract.COLOR_R, x + halfOffset] = (byte)((r > 255) ? 255 : (r < 0) ? 0 : r); g = pixels[ImageExtract.COLOR_G, x + offset + width] + pixels[ImageExtract.COLOR_G, x + offset + width]; result[ImageExtract.COLOR_G, x + halfOffset] = (byte)((g > 255) ? 255 : (g < 0) ? 0 : g); b = pixels[ImageExtract.COLOR_B, x + offset + width] + pixels[ImageExtract.COLOR_B, x + offset + width]; result[ImageExtract.COLOR_B, x + halfOffset] = (byte)((b > 255) ? 255 : (b < 0) ? 0 : b); r = pixels[ImageExtract.COLOR_R, x + offset] - pixels[ImageExtract.COLOR_R, x + offset + width]; result[ImageExtract.COLOR_R, x + halfOffset + halfHeightOffset] = (byte)((r > 255) ? 255 : (r < 0) ? 0 : r); g = pixels[ImageExtract.COLOR_G, x + offset] - pixels[ImageExtract.COLOR_G, x + offset + width]; result[ImageExtract.COLOR_G, x + halfOffset + halfHeightOffset] = (byte)((g > 255) ? 255 : (g < 0) ? 0 : g); b = pixels[ImageExtract.COLOR_B, x + offset] - pixels[ImageExtract.COLOR_B, x + offset + width]; result[ImageExtract.COLOR_B, x + halfOffset + halfHeightOffset] = (byte)((b > 255) ? 255 : (b < 0) ? 0 : b); } } ImageExtract.writeImageByArray(result, dstBitmap); return(dstBitmap); }
private static Bitmap colouring(Bitmap source, Bitmap Colour) { //Bitmap grayImage = new Grayscale(source).Process(); // source image to gray Bitmap grayImage = source; int[,] statisticsColour = Statistics(Colour); int width = grayImage.Width; int height = grayImage.Height; System.IntPtr srcScan, dstScan; BitmapData srcBmData, dstBmData; Bitmap dstBitmap = ImageExtract.InitPonitMethod(grayImage, width, height, out srcScan, out dstScan, out srcBmData, out dstBmData); unsafe //啟動不安全代碼 { byte *srcP = (byte *)srcScan; byte *dstP = (byte *)dstScan; int srcOffset = srcBmData.Stride - width * 3; int dstOffset = dstBmData.Stride - width * 3; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++, srcP += 3, dstP += 3) { int index = srcP[0] % COLOR_SIZE_RANGE; * dstP = (byte)statisticsColour[ImageExtract.COLOR_B, index]; //blue *(dstP + 1) = (byte)statisticsColour[ImageExtract.COLOR_G, index]; //green *(dstP + 2) = (byte)statisticsColour[ImageExtract.COLOR_R, index]; //red } srcP += srcOffset; dstP += dstOffset; } } grayImage.UnlockBits(srcBmData); dstBitmap.UnlockBits(dstBmData); return(dstBitmap); }
private Bitmap superimposedNoise(int[,] noise) { int width = _ImageSource.Width; int height = _ImageSource.Height; System.IntPtr srcScan, dstScan; BitmapData srcBmData, dstBmData; Bitmap dstBitmap = ImageExtract.InitPonitMethod(_ImageSource, width, height, out srcScan, out dstScan, out srcBmData, out dstBmData); unsafe //啟動不安全代碼 { byte *srcP = (byte *)srcScan; byte *dstP = (byte *)dstScan; int srcOffset = srcBmData.Stride - width * 3; int dstOffset = dstBmData.Stride - width * 3; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++, srcP += 3, dstP += 3) { int r = srcP[0] + noise[x, y]; int b = srcP[1] + noise[x, y]; int g = srcP[2] + noise[x, y]; *(dstP) = (byte)((r > 255) ? 255 : (r < 0) ? 0 : r); *(dstP + 1) = (byte)((b > 255) ? 255 : (b < 0) ? 0 : b); *(dstP + 2) = (byte)((g > 255) ? 255 : (g < 0) ? 0 : g); } srcP += srcOffset; dstP += dstOffset; } } _ImageSource.UnlockBits(srcBmData); dstBitmap.UnlockBits(dstBmData); return(dstBitmap); }
private static Bitmap sobel(Bitmap bitmap) { int width = bitmap.Width, height = bitmap.Height; int w = 3, h = 3; Bitmap dstBitmap = new Bitmap(bitmap); byte[,] pix = ImageExtract.getimageArray(bitmap); byte[,] resPix = new byte[3, width *height]; for (int y = 1; y < (height - 1); y++) { for (int x = 1; x < (width - 1); x++) { //b,g,r int current = x + (y * width); for (int c = 0; c < 3; c++) { //mask byte[] mask = new byte[w * h]; for (int my = 0; my < h; my++) { for (int mx = 0; mx < w; mx++) { int pos = current + (mx - 1) + ((my - 1) * width); mask[mx + my * w] = pix[c, pos]; } } resPix[c, current] = sobelMask33(mask); //resPix[c, current] = pix[c, current]; } } } ImageExtract.writeImageByArray(resPix, dstBitmap); return(dstBitmap); }
private static Bitmap colorFunction(Bitmap srcBitmap) { int width = srcBitmap.Width; int height = srcBitmap.Height; System.IntPtr srcScan, dstScan; BitmapData srcBmData, dstBmData; Bitmap dstBitmap = ImageExtract.InitPonitMethod(srcBitmap, width, height, out srcScan, out dstScan, out srcBmData, out dstBmData); Random random = new Random(); //亂數種子 unsafe //啟動不安全代碼 { byte *srcP = (byte *)srcScan; byte *dstP = (byte *)dstScan; int srcOffset = srcBmData.Stride - width * 3; int dstOffset = dstBmData.Stride - width * 3; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++, srcP += 3, dstP += 3) { int color = srcP[0]; *dstP = (byte)(color / 0.3 % COLOR_SIZE_RANGE); *(dstP + 1) = (byte)(color / 0.59 % COLOR_SIZE_RANGE); *(dstP + 2) = (byte)(color / 0.11 % COLOR_SIZE_RANGE); } srcP += srcOffset; dstP += dstOffset; } } srcBitmap.UnlockBits(srcBmData); dstBitmap.UnlockBits(dstBmData); return(dstBitmap); }
private Bitmap vertical(Bitmap srcBitmap) { Bitmap dstBitmap = new Bitmap(srcBitmap); int width = srcBitmap.Width, height = srcBitmap.Height; byte[,] pixels = ImageExtract.getimageArray(srcBitmap); byte[,] result = new byte[3, width *height]; int x, y, offset; int r, g, b, halfWidth = width / 2; for (y = 0; y < height - (height % 2); y++) { for (x = 0; x < width - (width % 2); x += 2) { offset = y * width; r = pixels[ImageExtract.COLOR_R, x + offset] + pixels[ImageExtract.COLOR_R, x + 1 + offset]; result[ImageExtract.COLOR_R, x / 2 + offset] = (byte)((r > 255) ? 255 : (r < 0) ? 0 : r); g = pixels[ImageExtract.COLOR_G, x + offset] + pixels[ImageExtract.COLOR_G, x + 1 + offset]; result[ImageExtract.COLOR_G, x / 2 + offset] = (byte)((g > 255) ? 255 : (g < 0) ? 0 : g); b = pixels[ImageExtract.COLOR_B, x + offset] + pixels[ImageExtract.COLOR_B, x + 1 + offset]; result[ImageExtract.COLOR_B, x / 2 + offset] = (byte)((b > 255) ? 255 : (b < 0) ? 0 : b); r = pixels[ImageExtract.COLOR_R, x + offset] - pixels[ImageExtract.COLOR_R, x + 1 + offset]; result[ImageExtract.COLOR_R, x / 2 + halfWidth + offset] = (byte)((r > 255) ? 255 : (r < 0) ? 0 : r); g = pixels[ImageExtract.COLOR_G, x + offset] - pixels[ImageExtract.COLOR_G, x + 1 + offset]; result[ImageExtract.COLOR_G, x / 2 + halfWidth + offset] = (byte)((g > 255) ? 255 : (g < 0) ? 0 : g); b = pixels[ImageExtract.COLOR_B, x + offset] - pixels[ImageExtract.COLOR_B, x + 1 + offset]; result[ImageExtract.COLOR_B, x / 2 + halfWidth + offset] = (byte)((b > 255) ? 255 : (b < 0) ? 0 : b); } } ImageExtract.writeImageByArray(result, dstBitmap); return(dstBitmap); }
private Bitmap _filter(Bitmap bitmap, int maskWidth, int maskHeight) { byte[,] pix, resPix; int width = bitmap.Width, height = bitmap.Height, pos, count = maskWidth * maskHeight, current; Bitmap dstBitmap = ImageExtract.extract(bitmap, out pix, out resPix); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { pos = x + y * width; if (!ImageProcess.IsFilterOnSide(ref pix, ref resPix, width, height, 1, 1, x, y, pos)) { current = x + y * width; int[] sum = { 0, 0, 0 }; for (int my = 0; my < maskHeight; my++) { for (int mx = 0; mx < maskWidth; mx++) { pos = current + (mx - 1) + ((my - 1) * width); sum[0] += pix[0, pos]; sum[1] += pix[1, pos]; sum[2] += pix[2, pos]; } } resPix[0, current] = (byte)(sum[0] / count); resPix[1, current] = (byte)(sum[1] / count); resPix[2, current] = (byte)(sum[2] / count); } } } ImageExtract.writeImageByArray(resPix, dstBitmap); return(dstBitmap); }
protected Bitmap convolute(Bitmap srcBitmap, int maskWidth, int maskHeight) { int width = srcBitmap.Width; int height = srcBitmap.Height; System.IntPtr srcScan, dstScan; BitmapData srcBmData, dstBmData; Bitmap dstBitmap = ImageExtract.InitPonitMethod(srcBitmap, width, height, out srcScan, out dstScan, out srcBmData, out dstBmData); int index = 0; byte[] mask1 = new byte[maskHeight * maskWidth]; byte[] mask2 = new byte[maskHeight * maskWidth]; byte[] mask3 = new byte[maskHeight * maskWidth]; int offset_width = (maskWidth / 2); int offset_height = (maskHeight / 2); unsafe //啟動不安全代碼 { byte *srcP = (byte *)srcScan; byte *dstP = (byte *)dstScan; int srcOffset = srcBmData.Stride - width * 3; int dstOffset = dstBmData.Stride - width * 3; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++, srcP += 3, dstP += 3) { //*(dstP + ImageExtract.COLOR_R) = processColorR(srcP[ImageExtract.COLOR_R], srcP[ImageExtract.COLOR_G], srcP[ImageExtract.COLOR_B]); if ((x < offset_width) || (x == (width - offset_width)) || (y < offset_height) || (y >= (height - offset_height))) { continue; } index = 0; for (int my = 0; my < maskHeight; my++) { for (int mx = 0; mx < maskWidth; mx++) { int cu_y = (my - offset_height); int cu_x = (mx - offset_width) * 3; mask1[index] = *(srcP + (cu_x) + (cu_y * (width * 3 + srcOffset)) + ImageExtract.COLOR_B); mask2[index] = *(srcP + (cu_x) + (cu_y * (width * 3 + srcOffset)) + ImageExtract.COLOR_G); mask3[index] = *(srcP + (cu_x) + (cu_y * (width * 3 + srcOffset)) + ImageExtract.COLOR_R); ++index; } } *(dstP + ImageExtract.COLOR_B) = maskFilter(mask1); *(dstP + ImageExtract.COLOR_G) = maskFilter(mask2); *(dstP + ImageExtract.COLOR_R) = maskFilter(mask3); } srcP += srcOffset; dstP += dstOffset; } } srcBitmap.UnlockBits(srcBmData); dstBitmap.UnlockBits(dstBmData); return(dstBitmap); }
public Bitmap Process() { byte[, ,] image = ImageExtract.getimageMartix(_ImageSource); int[,] noiseMap = getNoiseMap(); return(superimposedNoise(noiseMap)); }
private void _process(Bitmap srcBitmap) { int width = srcBitmap.Width; int height = srcBitmap.Height; _ImageOfR = new Bitmap(srcBitmap); _ImageOfG = new Bitmap(srcBitmap); _ImageOfB = new Bitmap(srcBitmap); System.IntPtr srcScan, scan_R, scan_G, scan_B, scan_Gray; BitmapData srcBmData, BmData_R, BmData_G, BmData_B, BmData_Gray; _ImageGray = ImageExtract.InitPonitMethod(srcBitmap, width, height, out srcScan, out scan_Gray, out srcBmData, out BmData_Gray); ImageExtract.InitPonitMethod(_ImageOfR, width, height, out scan_R, out BmData_R); ImageExtract.InitPonitMethod(_ImageOfG, width, height, out scan_G, out BmData_G); ImageExtract.InitPonitMethod(_ImageOfB, width, height, out scan_B, out BmData_B); unsafe //啟動不安全代碼 { byte *srcP = (byte *)srcScan; byte *dst_R = (byte *)scan_R; byte *dst_G = (byte *)scan_G; byte *dst_B = (byte *)scan_B; byte *dst_Gray = (byte *)scan_Gray; int srcOffset = srcBmData.Stride - width * 3; int dstOffset = BmData_Gray.Stride - width * 3; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++, srcP += 3, dst_R += 3, dst_G += 3, dst_B += 3, dst_Gray += 3) { byte gray = (byte)(.299 * srcP[2] + .587 * srcP[1] + .114 * srcP[0]); dst_Gray[0] = dst_Gray[1] = dst_Gray[2] = gray; dst_R[0] = dst_R[1] = dst_R[2] = srcP[2]; //dst_R[2] = srcP[2]; //dst_R[1] = dst_R[0] = 0; dst_G[0] = dst_G[1] = dst_G[2] = srcP[1]; //dst_G[1] = srcP[1]; //dst_G[0] = dst_G[2] = 0; dst_B[0] = dst_B[1] = dst_B[2] = srcP[0]; //dst_B[0] = srcP[0]; //dst_B[1] = dst_B[2] = 0; } srcP += srcOffset; dst_R += dstOffset; dst_G += dstOffset; dst_B += dstOffset; dst_Gray += dstOffset; } } srcBitmap.UnlockBits(srcBmData); _ImageGray.UnlockBits(BmData_Gray); _ImageOfR.UnlockBits(BmData_R); _ImageOfG.UnlockBits(BmData_G); _ImageOfB.UnlockBits(BmData_B); }
private Bitmap test(int effect) { int width = _SourceImage.Width; int height = _SourceImage.Height; System.IntPtr srcScan, dstScan; BitmapData srcBmData, dstBmData; Bitmap dstBitmap = ImageExtract.InitPonitMethod(_SourceImage, width, height, out srcScan, out dstScan, out srcBmData, out dstBmData); int offset_width = effect; int offset_height = effect; int mask_size = effect * effect; unsafe //啟動不安全代碼 { byte *srcP = (byte *)srcScan; byte *dstP = (byte *)dstScan; int srcOffset = srcBmData.Stride - width * 3; int dstOffset = dstBmData.Stride - width * 3; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { //*(dstP + ImageExtract.COLOR_R) = processColorR(srcP[ImageExtract.COLOR_R], srcP[ImageExtract.COLOR_G], srcP[ImageExtract.COLOR_B]); if ((x < offset_width) || (x == (width - offset_width)) || (y < offset_height) || (y >= (height - offset_height))) { continue; } int sum_r = 0, sum_g = 0, sum_b = 0; for (int my = 0; my < effect; my++) { for (int mx = 0; mx < effect; mx++) { int cu_y = (my - offset_height); int cu_x = (mx - offset_width) * 3; sum_r += *(srcP + (cu_x) + (cu_y * (width * 3 + srcOffset)) + ImageExtract.COLOR_B); sum_g += *(srcP + (cu_x) + (cu_y * (width * 3 + srcOffset)) + ImageExtract.COLOR_G); sum_b += *(srcP + (cu_x) + (cu_y * (width * 3 + srcOffset)) + ImageExtract.COLOR_R); } } for (int my = 0; my < effect; my++) { for (int mx = 0; mx < effect; mx++) { int cu_y = (my - offset_height); int cu_x = (mx - offset_width) * 3; *(dstP + ImageExtract.COLOR_B) = (byte)(sum_r / mask_size); *(dstP + ImageExtract.COLOR_B) = (byte)(sum_g / mask_size); *(dstP + ImageExtract.COLOR_B) = (byte)(sum_b / mask_size); } } srcP += (effect * 3); dstP += (effect * 3); } srcP += srcOffset; dstP += dstOffset; } } _SourceImage.UnlockBits(srcBmData); dstBitmap.UnlockBits(dstBmData); return(dstBitmap); }