Долго работает программа, есть ли какой-то способ ускорить работу? - C#
Формулировка задачи:
int[,] bitmap = new int[word.Width, word.Height];
for (int x = 0; x < word.Width; x++)
{
for (int y = 0; y < word.Height; y++)
{
double intensity = 0.3 * word.GetPixel(x, y).R + 0.59 * word.GetPixel(x, y).G + 0.11 * word.GetPixel(x, y).B;
if (intensity > 127)
{
bitmap[x, y] = 1;
}
else
{
bitmap[x, y] = -1;
}
}
}Решение задачи: «Долго работает программа, есть ли какой-то способ ускорить работу?»
textual
Листинг программы
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace CodeExamples.Drawing
{
public struct PixelData
{
public byte blue;
public byte green;
public byte red;
public byte alpha;
public PixelData(byte R, byte G, byte B)
{
blue = B;
green = G;
red = R;
alpha = 255;
}
public PixelData(byte R, byte G, byte B, byte A)
{
blue = B;
green = G;
red = R;
alpha = A;
}
public PixelData(byte A)
{
blue = A;
green = A;
red = A;
alpha = 255;
}
public PixelData(Color color)
{
blue = (byte)color.B;
green = (byte)color.G;
red = (byte)color.R;
alpha = (byte)color.A;
}
public Color ToColor()
{
return Color.FromArgb(alpha, red, green, blue);
}
}
public unsafe class UnsafeBitmap
{
private readonly int PixelDataSize;
private Bitmap Image;
private BitmapData ImageData;
private int Width;
private byte* DataPtr = null;
private UnsafeBitmap()
{
PixelDataSize = sizeof(PixelData);
}
public UnsafeBitmap(Bitmap image) : this()
{
Image = image;
LockBitmap();
}
public void LockBitmap()
{
GraphicsUnit unit = GraphicsUnit.Pixel;
RectangleF boundsF = Image.GetBounds(ref unit);
Rectangle bounds = Rectangle.Round(boundsF);
int width = bounds.Width * PixelDataSize;
if ((width % 4) != 0)
{
width = 4 * (width / 4 + 1);
}
Width = width;
BitmapData data = Image.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
DataPtr = (byte*)data.Scan0.ToPointer();
ImageData = data;
}
public Bitmap UnlockBitmap()
{
Image.UnlockBits(ImageData);
ImageData = null;
DataPtr = null;
return Image;
}
public PixelData GetPixel(int x, int y)
{
PixelData pixel = *PixelAt(x, y);
return pixel;
}
public void SetPixel(int x, int y, PixelData color)
{
PixelData* pixel = PixelAt(x, y);
*pixel = color;
}
public void SetPixel(int x, int y, byte red, byte green, byte blue, byte alpha)
{
PixelData* pixel = PixelAt(x, y);
pixel->red = red;
pixel->green = green;
pixel->blue = blue;
pixel->alpha = alpha;
}
private PixelData* PixelAt(int x, int y)
{
return (PixelData*)(DataPtr + y * Width + x * PixelDataSize);
}
}
public static class UnsafeBitmapExample
{
public static int[,] GetIntensityMap(Bitmap source)
{
int width = source.Width;
int height = source.Height;
int[,] intensityMap = new int[width, height];
UnsafeBitmap bitmap = new UnsafeBitmap(source);
for (int x = 0; x < width; ++x)
{
for (int y = 0; y < height; ++y)
{
PixelData pixel = bitmap.GetPixel(x, y); // оптимизация - метод GetPixel вызывается однократно
double intensity = 0.3 * pixel.red + 0.59 * pixel.green + 0.11 * pixel.blue;
intensityMap[x, y] = (intensity > 127.0) ? 1 : -1;
}
}
bitmap.UnlockBitmap();
return intensityMap;
}
}
}