Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bitmap matrix from Texture2D

Discussion in 'General Graphics' started by karmatha, Jul 27, 2019.

  1. karmatha

    karmatha

    Joined:
    Aug 25, 2012
    Posts:
    50
    Hi I'm trying to convert c# code that works with System.Drawing.Bitmap into Unity. What I'm trying to do is swap out all references to Bitmap with Texture2D.

    I have the following method:
    Code (CSharp):
    1.  
    2. /// <summary>
    3.         ///
    4.         /// Produces a binary Matrix with Dimensions
    5.         /// For the threshold, we take the sum of  weighted R,g,b value. The sum of weights must be 1.
    6.         /// The result fills the field bm;
    7.         /// </summary>
    8.         /// <param name="bitmap"> A Bitmap, which will be transformed to a binary Matrix</param>
    9.         /// <returns>Returns a binaray boolean Matrix </returns>
    10.         static Bitmap_p ConvertBitmap(Bitmap bitmap)
    11.         {
    12.  
    13.             byte[] Result = new byte[bitmap.Width * bitmap.Height];
    14.             BitmapData SourceData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    15.             unsafe
    16.             {
    17.                 byte* SourcePtr = (byte*)(void*)SourceData.Scan0;
    18.  
    19.                 int l = Result.Length;
    20.                 Debug.Log(l);
    21.                 for (int i = 0; i < l; i++)
    22.                 {
    23.                     //  if ((0.2126 * (double)SourcePtr[4 * i + 2] + 0.7153 * (double)SourcePtr[4 * i + 1] + 0.0721 * (double)SourcePtr[4 * i]) < Treshold*255)
    24.                     if (((double)SourcePtr[4 * i + 2] + (double)SourcePtr[4 * i + 1] + (double)SourcePtr[4 * i]) < Treshold * 255 * 3)
    25.                         Result[i] = 1;
    26.                     else
    27.                         Result[i] = 0;
    28.                 }
    29.             }
    30.  
    31.             bitmap.UnlockBits(SourceData);
    32.             bm = new Bitmap_p(bitmap.Width, bitmap.Height);
    33.             bm.data = Result;
    34.  
    35.             return bm;
    36.         }
    37.         }
    I tried to do this:

    Code (CSharp):
    1.        
    2.         static Bitmap_p ConvertBitmap(Texture2D texture)
    3.         {
    4.  
    5.             byte[] original = texture.EncodeToPNG();
    6.             byte[] result = new byte[texture.width * texture.height];
    7.  
    8.             for (int i = 0; i < original.Length; i++)
    9.             {
    10.                 if (((double)original[4 * i + 2] + (double)original[4 * i + 1] + (double)original[4 * i]) < Treshold * 255 * 3)
    11.                     result[i] = 1;
    12.                 else
    13.                     result[i] = 0;
    14.             }
    15.  
    16.             bm = new Bitmap_p(texture.width, texture.height);
    17.             bm.data = result;
    18.  
    19.             return bm;
    20.         }
    but I'm getting array index out of bounds errors. I'm completely not used to working with pointers so I'm confused as heck. Also, perhaps `PixelFormat.Format32bppArgb` is a different format than EncodeToPNG? Can anyone help me along?
     
    ina and varigeri92 like this.
  2. karmatha

    karmatha

    Joined:
    Aug 25, 2012
    Posts:
    50
    Got it.


    Code (CSharp):
    1.       static Bitmap_p ConvertTexture(Texture2D texture)
    2.         {
    3.             var original = texture.GetPixels32();
    4.             byte[] result = new byte[texture.width * texture.height];
    5.  
    6.             for (int i = 0; i < original.Length; i++)
    7.             {
    8.                 if(original[i].r + original[i].g + original[i].b < Treshold * 255 * 3)
    9.                 {
    10.                     result[i] = 1;
    11.                 }
    12.                 else
    13.                 {
    14.                     result[i] = 0;
    15.                 }
    16.             }
    17.  
    18.             bm = new Bitmap_p(texture.width, texture.height){
    19.                 data = result
    20.             };
    21.  
    22.             return bm;
    23.         }
    Leaving this thread as it might be useful to others.