I'm trying to rewrite this slow chunk of code...
...by using pointers thus...Code:dest.SetPixel(xx, yy, source.GetPixel((int)((x / lenX) * source.Width), (int)creepY));
Where ptrS and ptrD are pointers to the top left pixels of a source bitmap and a destination bitmap (both images have been locked using LockBits).Code:*(ptrD + (yy * bmpDdest.Stride) + xx) = *(ptrS + (((int)creepY * bmpDsource.Stride) + (int)((x / lenX) * bmpDsource.Stride)));
When I run the pointer version, I get a NullReferenceException, which probably means my pointers are running wild and free on the open plains of my RAM.
Here's my function in full...
The code that I am trying to replace works just fine, I can't work out why my pointer math won't work. Is it a bracket in the wrong place or something????Code:public struct RGB24 { public byte Blue; public byte Green; public byte Red; } private unsafe void BlitQuad(ref Bitmap source, ref Bitmap dest, ref Point[] pts) { Trajectory leftEdge = new Trajectory(pts[0], pts[3]); Trajectory rightEdge = new Trajectory(pts[1], pts[2]); Trajectory scan = null; BitmapData bmpDsource = source.LockBits( new Rectangle(new Point(), source.Size), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); BitmapData bmpDdest = dest.LockBits( new Rectangle(new Point(), dest.Size), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); RGB24* ptrS = (RGB24*)bmpDsource.Scan0, ptrD = (RGB24*)bmpDdest.Scan0; uint x, y; float lenY = leftEdge.Length = rightEdge.Length = Math.Max(leftEdge.Length, rightEdge.Length); float lenX; float creepY; int xx, yy; for(y = 0 ; y < lenY ; y++) { creepY = (int)((y / lenY) * source.Height); scan = new Trajectory(leftEdge.GetNext(), rightEdge.GetNext()); lenX = scan.Length; for(x = 0 ; x < lenX ; x++) { scan.GetNext(out xx, out yy); *(ptrD + (yy * bmpDdest.Stride) + xx) = *(ptrS + (((int)creepY * bmpDsource.Stride) + (int)((x / lenX) * bmpDsource.Stride))); //copy the pixel //dest.SetPixel(xx, yy, source.GetPixel((int)((x / lenX) * source.Width), (int)creepY)); } } ptrD = 0; ptrS = 0; source.UnlockBits(bmpDsource); dest.UnlockBits(bmpDdest); } }




Reply With Quote