imaging code; looking for optimization
-
Hi folks, The following piece of C#.NET code creates an A4 size binary image by reading values from a two-dimensional byte array iPage[][]. Is there anyway I can further optimize the nested for loop inside the unsafe block? It is taking ~2.2s, and I'd love to bring it down further. Sarab. // page dimensions iwd=iPage.Length; iht=iPage[0].Length; // create new bitmap bMap=new Bitmap(iwd, iht, PixelFormat.Format24bppRgb); // create bitmap data bData=bMap.LockBits(new Rectangle (0,0,iwd,iht),ImageLockMode.WriteOnly,PixelFormat.Format24bppRgb); // get bitmap position Scan0 = bData.Scan0; iStride=bData.Stride; unsafe { // initialize pointer byte *p = (byte*)(void*)Scan0; int nOffset = iStride-iwd*3; for(int y=0;y
-
Hi folks, The following piece of C#.NET code creates an A4 size binary image by reading values from a two-dimensional byte array iPage[][]. Is there anyway I can further optimize the nested for loop inside the unsafe block? It is taking ~2.2s, and I'd love to bring it down further. Sarab. // page dimensions iwd=iPage.Length; iht=iPage[0].Length; // create new bitmap bMap=new Bitmap(iwd, iht, PixelFormat.Format24bppRgb); // create bitmap data bData=bMap.LockBits(new Rectangle (0,0,iwd,iht),ImageLockMode.WriteOnly,PixelFormat.Format24bppRgb); // get bitmap position Scan0 = bData.Scan0; iStride=bData.Stride; unsafe { // initialize pointer byte *p = (byte*)(void*)Scan0; int nOffset = iStride-iwd*3; for(int y=0;y
It looks like that about as fast as your going to get it without resorting to writing the thing in Managed C++. Whoops! Accidently clicked Submit... If you can come up with an algorithm to get rid of that
if
statement, you'll probably cut the time in half. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -- modified at 18:01 Tuesday 27th September, 2005 -
It looks like that about as fast as your going to get it without resorting to writing the thing in Managed C++. Whoops! Accidently clicked Submit... If you can come up with an algorithm to get rid of that
if
statement, you'll probably cut the time in half. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -- modified at 18:01 Tuesday 27th September, 2005 -
Hi folks, The following piece of C#.NET code creates an A4 size binary image by reading values from a two-dimensional byte array iPage[][]. Is there anyway I can further optimize the nested for loop inside the unsafe block? It is taking ~2.2s, and I'd love to bring it down further. Sarab. // page dimensions iwd=iPage.Length; iht=iPage[0].Length; // create new bitmap bMap=new Bitmap(iwd, iht, PixelFormat.Format24bppRgb); // create bitmap data bData=bMap.LockBits(new Rectangle (0,0,iwd,iht),ImageLockMode.WriteOnly,PixelFormat.Format24bppRgb); // get bitmap position Scan0 = bData.Scan0; iStride=bData.Stride; unsafe { // initialize pointer byte *p = (byte*)(void*)Scan0; int nOffset = iStride-iwd*3; for(int y=0;y
What values does the byte array contain? Do you only swap 0 <-> 255 or does the byte array contain different values? When it contains only 0 or 255, you can use p[0]=p[1]=p[2]=~iPage[x][y]; How do you create the iPage array? If you could swap it to be iPage[y][x], you could evaluate iPage[y] outside the inner loop. (byte[] thisRow = iPage[y] and then continue to use the inner loop). Also, you might want to eliminate bounds checking on iPage array access by using a foreach loop (it looks slower, but the C# compiler generates special code (no bounds checking) when foreach is used on an array). So propably this could be faster: (provided you swap x and y in your array creation and use only the values 0 and 255): foreach (byte[] row in iPage) { foreach (byte val in row) [ p[0] = p[1] = p[2] = ~val; p += 3; } p += nOffset; }
-
What values does the byte array contain? Do you only swap 0 <-> 255 or does the byte array contain different values? When it contains only 0 or 255, you can use p[0]=p[1]=p[2]=~iPage[x][y]; How do you create the iPage array? If you could swap it to be iPage[y][x], you could evaluate iPage[y] outside the inner loop. (byte[] thisRow = iPage[y] and then continue to use the inner loop). Also, you might want to eliminate bounds checking on iPage array access by using a foreach loop (it looks slower, but the C# compiler generates special code (no bounds checking) when foreach is used on an array). So propably this could be faster: (provided you swap x and y in your array creation and use only the values 0 and 255): foreach (byte[] row in iPage) { foreach (byte val in row) [ p[0] = p[1] = p[2] = ~val; p += 3; } p += nOffset; }