public static FastDirectImage Create(int width, int height, PixelFormat pixelFormat) { var newImg = new FastDirectImage(); newImg.PixelFormat = pixelFormat; newImg.BitPerPixel = Image.GetPixelFormatSize(pixelFormat); newImg.PixelFormatSize = newImg.BitPerPixel / 8; // Gör inte såhär för att räkna ut Stride: newImg.Stride = width * newImg.PixelFormatSize; // Detta då stride för bitmappar måste alignbara med 32 bitar (4 byte). (Antagligen för att förenkla kopiering av rader) // http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.drawing/2006-09/msg00057.html //In your loop, you copy the pixels one scanline at a time and take into //consideration the amount of padding that occurs due to memory alignment. newImg.Stride = ((width * newImg.BitPerPixel + 31) & ~31) >> 3; newImg.Padding = newImg.Stride - (((width * newImg.BitPerPixel) + 7) / 8); newImg.bits = new byte[newImg.Stride * height]; newImg.handle = GCHandle.Alloc(newImg.bits, GCHandleType.Pinned); IntPtr pointer = Marshal.UnsafeAddrOfPinnedArrayElement(newImg.bits, 0); newImg.NativeBitmap = new Bitmap(width, height, newImg.Stride, pixelFormat, pointer); // Kan kanske vara något för //var b = newImg.NativeBitmap.LockBits(new Rectangle(0, 0, newImg.NativeBitmap.Width, newImg.NativeBitmap.Height), ImageLockMode.ReadWrite, pixelFormat); return newImg; }
public double[] CreateVerticalHistogram(FastDirectImage img) { Stopwatch totalTime = Stopwatch.StartNew(); int width = img.Width; int height = img.Height; double[] vhist = new double[height]; for (int y = 0; y < height; y++) { double sum = 0; for (int x = 0; x < width; x++) sum += img.GetPixel(x, y).GetBrightness(); vhist[y] = sum / width; } Debug.WriteLine("HistogramCreator:CreateVerticalHistogram Total time: " + totalTime.Elapsed.TotalSeconds.ToString()); return vhist; }
public static FastGrayScaleImage FromImage(FastDirectImage img) { int width = img.Width; int height = img.Height; var result = new FastGrayScaleImage(width, height); int oIndex = -img.Padding; int dIndex = -result.image.Padding; int dIndexLineLenght = result.image.Stride - result.image.Padding; int dIndexEoi = (height * result.image.Stride) - result.image.Padding; int dIndexNextLine; int r; int g; int b; int imgPixelFormatSize = img.PixelFormatSize; int resultPixelFormatSize = result.image.PixelFormatSize; byte[] imgBytes = img.Bits; byte[] resultBytes = result.image.Bits; while (dIndex < dIndexEoi) { oIndex += img.Padding; dIndex += result.image.Padding; dIndexNextLine = dIndex + dIndexLineLenght; while(dIndex < dIndexNextLine) { r = imgBytes[oIndex]; g = imgBytes[oIndex + 1]; b = imgBytes[oIndex + 2]; resultBytes[dIndex] = (byte)((b * 0.11) + (g * 0.59) + (r * 0.3)); oIndex += imgPixelFormatSize; dIndex += resultPixelFormatSize; } } return result; }
public FastGrayScaleImage(int width, int height) { image = FastDirectImage.Create(width, height, PixelFormat.Format8bppIndexed); CreateGrayScalePalette(image.NativeBitmap); }
private void UpdateVerticalHistogram(FastDirectImage image) { var h = new HistogramCreator(); double[] vhist = h.CreateVerticalHistogram(image); Bitmap b = new Bitmap(100, image.Height, PixelFormat.Format16bppRgb555); using (Graphics g = Graphics.FromImage(b)) { g.FillRectangle(Brushes.Black, 0, 0, b.Width, b.Height); for (int i = 0; i < vhist.Length; i++) g.DrawLine(Pens.White, 0, i, (float)vhist[i] * (float)100, i); g.Flush(); } verticalHistPictureBox.Image = b; }