int center = 16;
int split = 4;
int x0 = rect.Left;
int y0 = rect.Top;
int w = rect.Width;
int h = rect.Height;
Rectangle rect = new Rectangle(0, 0, w, h);
BitmapData info = new BitmapData();
image.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed, info);
try {
// copy original data
byte[] g = new byte[w * h];
int ofs = 0;
int gap = info.Stride - w; // gap between lines
byte* p = (byte*)info.Scan0;
for (int y = h; y > 0; y--) {
// read pixels
for (int x = w; x > 0; x--) {
g[ofs] = *(p++);
}
// move to next line
p += gap;
}
// sharpen
p = (byte*)info.Scan0;
ofs = 0;
// first line - just copy
for (int x = w; x > 0; x--) {
*(p++) = g[ofs++];
}
// move to next line
p += gap;
// middle lines
for (int y = h - 2; y > 0; y--) {
// first pixel - just copy
*(p++) = g[ofs++];
// middle lixels
for (int x = w - 2; x > 0; x--) {
int gray = (center * g[ofs] - g[ofs + w + 1] - 2 * g[ofs - w] - g[ofs - w + 1] - 2 * g[ofs - 1] - 2 * g[ofs + 1] - g[ofs + w - 1] - 2 * g[ofs + w] - g[ofs - w - 1]) / split;
*(p++) = (byte)Math.Max(Math.Min(gray, 255), 0);
ofs++;
}
// last pixel - just copy
*(p++) = g[ofs++];
// move to next line
p += gap;
}
// last line - just copy
for (int x = w; x > 0; x--) {
*(p++) = g[ofs++];
}
} finally {
image.UnlockBits(info);
}
|