* optimized FastPixel some more (no copying of memory anymore)
This commit is contained in:
parent
497fd500d9
commit
6a90788777
83
FastPixel.cs
83
FastPixel.cs
@ -3,7 +3,6 @@ using System.Drawing;
|
|||||||
using System.Drawing.Imaging;
|
using System.Drawing.Imaging;
|
||||||
|
|
||||||
class FastPixel {
|
class FastPixel {
|
||||||
private byte[] rgbValues;
|
|
||||||
private BitmapData bmpData;
|
private BitmapData bmpData;
|
||||||
private bool locked = false;
|
private bool locked = false;
|
||||||
|
|
||||||
@ -38,7 +37,6 @@ class FastPixel {
|
|||||||
this._width = bitmap.Width;
|
this._width = bitmap.Width;
|
||||||
this._height = bitmap.Height;
|
this._height = bitmap.Height;
|
||||||
this.bytesPerPixel = (this._isAlpha)?4:3;
|
this.bytesPerPixel = (this._isAlpha)?4:3;
|
||||||
this.rgbValues = new byte[(this.Width * this.Height) * this.bytesPerPixel];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Lock() {
|
public void Lock() {
|
||||||
@ -47,20 +45,6 @@ class FastPixel {
|
|||||||
|
|
||||||
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
|
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
|
||||||
this.bmpData = this.Bitmap.LockBits(rect, ImageLockMode.ReadWrite, this.Bitmap.PixelFormat);
|
this.bmpData = this.Bitmap.LockBits(rect, ImageLockMode.ReadWrite, this.Bitmap.PixelFormat);
|
||||||
|
|
||||||
unsafe {
|
|
||||||
byte* ptr= (byte*)this.bmpData.Scan0;
|
|
||||||
int offset = this.bmpData.Stride - this.bmpData.Width * this.bytesPerPixel;
|
|
||||||
for(int y = 0; y < this.Height; y++, ptr += offset) {
|
|
||||||
for(int x = 0; x < this.Width; x++, ptr += this.bytesPerPixel) {
|
|
||||||
int index = ((y * this.Width + x) * this.bytesPerPixel);
|
|
||||||
this.rgbValues[index] = ptr[0];
|
|
||||||
this.rgbValues[index+1] = ptr[1];
|
|
||||||
this.rgbValues[index+2] = ptr[2];
|
|
||||||
if (this.bytesPerPixel == 4) this.rgbValues[index+3] = ptr[3];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.locked = true;
|
this.locked = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,37 +52,24 @@ class FastPixel {
|
|||||||
if (!this.locked)
|
if (!this.locked)
|
||||||
throw new Exception("Bitmap not locked.");
|
throw new Exception("Bitmap not locked.");
|
||||||
|
|
||||||
// Copy the RGB values back to the bitmap;
|
|
||||||
if (setPixels) {
|
|
||||||
unsafe {
|
|
||||||
byte* ptr= (byte*)this.bmpData.Scan0;
|
|
||||||
int offset = this.bmpData.Stride - this.bmpData.Width * this.bytesPerPixel;
|
|
||||||
for(int y = 0; y < this.Height; y++, ptr += offset) {
|
|
||||||
for(int x = 0; x < this.Width; x++, ptr += this.bytesPerPixel) {
|
|
||||||
int index = ((y * this.Width + x) * this.bytesPerPixel);
|
|
||||||
ptr[0] = this.rgbValues[index];
|
|
||||||
ptr[1] = this.rgbValues[index+1];
|
|
||||||
ptr[2] = this.rgbValues[index+2];
|
|
||||||
if (this.bytesPerPixel == 4) ptr[3] = this.rgbValues[index+3];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unlock the bits.;
|
// Unlock the bits.;
|
||||||
this.Bitmap.UnlockBits(bmpData);
|
this.Bitmap.UnlockBits(bmpData);
|
||||||
this.locked = false;
|
this.locked = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Clear(Color colour) {
|
public unsafe void Clear(Color color) {
|
||||||
if (!this.locked)
|
if (!this.locked)
|
||||||
throw new Exception("Bitmap not locked.");
|
throw new Exception("Bitmap not locked.");
|
||||||
|
|
||||||
for (int index = 0; index < this.rgbValues.Length; index += this.bytesPerPixel) {
|
byte* ptr = (byte*)this.bmpData.Scan0;
|
||||||
this.rgbValues[index] = colour.B;
|
int offset = this.bmpData.Stride - this.bmpData.Width * this.bytesPerPixel;
|
||||||
this.rgbValues[index + 1] = colour.G;
|
for(int y = 0; y < this.Height; y++, ptr += offset) {
|
||||||
this.rgbValues[index + 2] = colour.R;
|
for(int x = 0; x < this.Width; x++, ptr += this.bytesPerPixel) {
|
||||||
if (this.bytesPerPixel == 4) this.rgbValues[index + 3] = colour.A;
|
ptr[0] = color.B;
|
||||||
|
ptr[1] = color.G;
|
||||||
|
ptr[2] = color.R;
|
||||||
|
if (this.bytesPerPixel == 4) ptr[3] = color.A;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,33 +77,43 @@ class FastPixel {
|
|||||||
this.SetPixel(location.X, location.Y, colour);
|
this.SetPixel(location.X, location.Y, colour);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetPixel(int x, int y, Color colour) {
|
public unsafe void SetPixel(int x, int y, Color color) {
|
||||||
if (!this.locked)
|
if (!this.locked)
|
||||||
throw new Exception("Bitmap not locked.");
|
throw new Exception("Bitmap not locked.");
|
||||||
|
|
||||||
int index = ((y * this.Width + x) * this.bytesPerPixel);
|
byte* ptr = (byte*)this.bmpData.Scan0;
|
||||||
this.rgbValues[index] = colour.B;
|
ptr += getMemoryOffset(x, y);
|
||||||
this.rgbValues[index + 1] = colour.G;
|
|
||||||
this.rgbValues[index + 2] = colour.R;
|
ptr[0] = color.B;
|
||||||
if (this.bytesPerPixel == 4) this.rgbValues[index + 3] = colour.A;
|
ptr[1] = color.G;
|
||||||
|
ptr[2] = color.R;
|
||||||
|
if (this.bytesPerPixel == 4) ptr[3] = color.A;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Color GetPixel(Point location) {
|
public Color GetPixel(Point location) {
|
||||||
return this.GetPixel(location.X, location.Y);
|
return this.GetPixel(location.X, location.Y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Color GetPixel(int x, int y) {
|
public unsafe Color GetPixel(int x, int y) {
|
||||||
if (!this.locked)
|
if (!this.locked)
|
||||||
throw new Exception("Bitmap not locked.");
|
throw new Exception("Bitmap not locked.");
|
||||||
|
|
||||||
int index = ((y * this.Width + x) * this.bytesPerPixel);
|
byte* ptr = (byte*)this.bmpData.Scan0;
|
||||||
int b = this.rgbValues[index];
|
ptr += getMemoryOffset(x, y);
|
||||||
int g = this.rgbValues[index + 1];
|
|
||||||
int r = this.rgbValues[index + 2];
|
int b = ptr[0];
|
||||||
|
int g = ptr[1];
|
||||||
|
int r = ptr[2];
|
||||||
if (this.bytesPerPixel == 4) {
|
if (this.bytesPerPixel == 4) {
|
||||||
int a = this.rgbValues[index + 3];
|
int a = ptr[3];
|
||||||
return Color.FromArgb(a, r, g, b);
|
return Color.FromArgb(a, r, g, b);
|
||||||
}
|
}
|
||||||
return Color.FromArgb(r, g, b);
|
return Color.FromArgb(r, g, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected int getMemoryOffset(int x, int y) {
|
||||||
|
// int offset = this.bmpData.Stride - this.bmpData.Width * this.bytesPerPixel;
|
||||||
|
int result = y*this.bmpData.Stride + x*this.bytesPerPixel;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user