Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. imaging code; looking for optimization

imaging code; looking for optimization

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpgraphicsalgorithmsdata-structuresperformance
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sarabjs
    wrote on last edited by
    #1

    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

    D D 2 Replies Last reply
    0
    • S sarabjs

      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

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • D Dave Kreskowiak

        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

        S Offline
        S Offline
        sarabjs
        wrote on last edited by
        #3

        hmm... not so confident in managed c++.. can't remove the if statement either! thanks anyways...

        1 Reply Last reply
        0
        • S sarabjs

          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

          D Offline
          D Offline
          Daniel Grunwald
          wrote on last edited by
          #4

          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; }

          S 1 Reply Last reply
          0
          • D Daniel Grunwald

            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; }

            S Offline
            S Offline
            sarabjs
            wrote on last edited by
            #5

            and using the foreach loop worked wonders! The execution time's safely within the acceptable range now :) Thanks !!!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups