C# - APP: Image Processing - Copying channels between two bitmaps

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AramAz
    New Member
    • Nov 2007
    • 9

    #1

    C# - APP: Image Processing - Copying channels between two bitmaps

    I'm trying to use unsafe mode to do some operations on two different bitmaps.

    -I loaded 2 images
    -Called my Change method.
    - Locked their Bitmap data as bmDataLeft and bmDataRight.
    - Doing some operations on BGR channel bits.

    The problem is that no operation between them is affected.
    First look at my code:

    Code:
            public static Bitmap Change(Bitmap LeftBitamp, Bitmap RightBitamp)
            {
                Bitmap LeftB = LeftBitamp;
                Bitmap RightB = RightBitamp;
                // the return format is BGR.
                // This is where I lock the data so I can declare a pointer to them.
                // bmDataLeft for the LeftBitmap, bmDataRight for the RightBitmap, 
                BitmapData bmDataLeft = LeftB.LockBits(new Rectangle(0, 0, LeftB.Width, LeftB.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                BitmapData bmDataRight = RightB.LockBits(new Rectangle(0, 0, RightB.Width, RightB.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
    
                // The normal procedure for pointer to an image variables
                int strideL = bmDataLeft.Stride;
                IntPtr Scan0L = bmDataLeft.Scan0;
    
                int strideR = bmDataRight.Stride;
                IntPtr Scan0R = bmDataRight.Scan0;
    
                unsafe
                {
                    #region 3 bytes
                    // These are my actual data I will do operations on.
                    byte* pL = (byte*) (void*) Scan0L;
                    byte* pR = (byte*) (void*) Scan0R;
                    // These are for the correct indexing.
                    // There shouldn't be a problem here.
                    int nOffsetLeft = strideL - LeftB.Width*3;
                    int nOffsetRight = strideR - RightB.Width*3;
                    int nWidthLeft = LeftB.Width*3;
                    int nWidthRight = RightB.Width*3;
    
                    // Looping through all pixels.
                    for (int y = 0; y < LeftB.Height; ++y)
                    {
                        for (int x = 0; x < nWidthLeft; ++x)
                        {
                            // This is the line that is not functioning properly
                            // Notice I'm trying to copy bytes from bmDataRight to bm DataLeft:
                            pL[0] = (byte)(pR[0]);
                            
                            // But strangely this one does work! :
                            // pL[0] = (byte) (pL[0]+50);
                            // Notice that the above line is using pL[] to put something on pL[] . What I want is to use pR[] and populate pL[].
    
    
                            ++pL;
                            ++pR;
                        }
                        pL += nOffsetLeft;
                        pR += nOffsetRight;
                    }
    
                    #endregion
    
                    
                }
    
                LeftB.UnlockBits(bmDataLeft);
                RightB.UnlockBits(bmDataRight);
                LeftB.Save("LeftOne.jpg");
                RightB.Save("RightOne.jpg");
    
                return LeftB;
            }
    As you can see at the commented lines, When I try to use a data from a bitmap data on itself it works OK, but when I try to use both BitmapData datas I get no result!

    I will appreciate all your solutions or if someone could tell me my mistake. Or may be in one Statement line there is only one bitmapData accesible in memory?

    Thank you,

    BestRegards,
    Aram Azhari
  • AramAz
    New Member
    • Nov 2007
    • 9

    #2
    Bump.
    Anyone please ?

    Comment

    • AramAz
      New Member
      • Nov 2007
      • 9

      #3
      Guys I found my problem. I was traversing through the data byte by byte. What I neeeded to do was to traverse pixel by pixel, meaning that I have increment the pointers by 3 and Change the loop to a one-third (before it was LeftB.Width*3 , now it is just LeftB.Width).

      [CODE=java]

      for (int y = 0; y < LeftB.Height; ++y)
      {
      for (int x = 0; x < LeftB.Width; ++x)
      {
      // This is the line that is not functioning properly
      // Notice I'm trying to copy bytes from bmDataRight to bm DataLeft:
      pL[0] = (byte)(pR[0]);

      // But strangely this one does work! :
      // pL[0] = (byte) (pL[0]+50);
      // Notice that the above line is using pL[] to put something on pL[] . What I want is to use pR[] and populate pL[].


      pL+=3;
      pR+=3;
      }
      pL += nOffsetLeft;
      pR += nOffsetRight;
      }

      }

      [/CODE]


      I hope this helps somebody else as well.

      Regards,
      Aram Azhari

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Was this something you could not do with the Bitmap class and it's GetPixel()/SetPixel() functions?

        Comment

        • AramAz
          New Member
          • Nov 2007
          • 9

          #5
          Originally posted by Plater
          Was this something you could not do with the Bitmap class and it's GetPixel()/SetPixel() functions?
          I could, But that would take forever to complete the operation.
          C# internal methods of GetPixel() SetPixel() are too slow to be used for image processing. It is more used for getting color of a point or painting which uses a very limited number of pixels.
          What we're doing here is a lot of points and a lot of operations on them. For ex, if I have 2 pictures each size of 1024x768, and if I want to swap their blue channel it would take 1024*768*3 operations to do such a job. That is 2,359,296 operations.
          GetPixel() SetPixel() is a slow process and it would takes even 5 minutes! This even gets worse when you want to do effects using convolution Matrixes (operation between 9 pixels)
          but using pointers (pointing to direct memory addresses which hold the bitmap pixel datas) it may be up to 3 seconds!

          I hope my explanation was helpful to you.

          Regards,
          Aram Azhari

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Could you use a MemoryStream object populated from the Raw Bitmap data and manipulate the bytes through there?
            It looked like you were doing the stride and number of bytes per pixel calculations already so you would know which bytes to effect?

            Although if you found the answer already it doesn't much matter I suppose. Plus, I've never been very good with API calls.

            Comment

            • AramAz
              New Member
              • Nov 2007
              • 9

              #7
              Originally posted by Plater
              Could you use a MemoryStream object populated from the Raw Bitmap data and manipulate the bytes through there?
              It looked like you were doing the stride and number of bytes per pixel calculations already so you would know which bytes to effect?

              Although if you found the answer already it doesn't much matter I suppose. Plus, I've never been very good with API calls.
              Thank you for your reply Plater,

              Using a MemoryStream is as fast as using pointers, and it has an advantage:
              - It is NOT unsafe code!

              As you said, I already found my mistake but I will try to do it in the Memory Stream way and post a similar solution using such.

              Regards,
              Aram Azhari

              Comment

              Working...