Fastest byte[,,] to byte[] copy

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jamesSharp007
    New Member
    • Mar 2010
    • 2

    Fastest byte[,,] to byte[] copy

    Hi,

    I'm doing some video processing and need to copy 3d array of bytes to 1d array of bytes where:

    3d array is [width, height, layers] and
    1d array is [width * height * layers]

    Now i have:
    Code:
    int index = -1;
    
    for (int j = 0; j < h; ++j)
        for (int i = 0; i < w; ++i)
        {
            data1D[++index] = data3D[j, i, 0];
            data1D[++index] = data3D[j, i, 1];
            data1D[++index] = data3D[j, i, 2];
            data1D[++index] = 255;
        }

    I need something faster.
    I tried with Marshal.Copy() and unsafe, fixed pointers but without success.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    There is no multiplication going on in your example.
    Isn't the end result supposed to be the three values multiplied?

    Comment

    • jamesSharp007
      New Member
      • Mar 2010
      • 2

      #3
      No, there is no multiplication.

      Arrays looks like:

      data3D[0, 0, 0] = 0
      data3D[0, 0, 1] = 128
      data3D[0, 0, 2] = 255
      data3D[0, 0, 3] = 255

      data3D[0, 1, 0] = 0
      data3D[0, 1, 1] = 32
      data3D[0, 1, 2] = 64
      data3D[0, 1, 3] = 255
      ...

      and

      data1D[0] = 0
      data1D[1] = 128
      data1D[2] = 255
      data1D[3] = 255
      data1D[4] = 0
      data1D[5] = 32
      data1D[6] = 64
      data1D[7] = 255
      ...

      Simple copying VGA array with 4 layers takes about 13ms when effects like blurring with + and * operations take only 4ms...

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        And you think that a 1d array is faster than a 3d array? It's not. Reading data3d[0, 1, 0] and reading data1d[5] is exactly the same thing, because an array is just a pointer to a memory address anyway.

        Simple copying VGA array with 4 layers takes about 13ms when effects like blurring with + and * operations take only 4ms...
        I'm not sure what comparing the blur effect time to the array copy time has to do with anything. I have a strawberry that is more red than a lemon. So how can I make a lime more orange? Yeah, they're all fruit but they don't have any direct relationship or impact on each other.

        I guess I'm lost. You say you need to copy from 3d to 1d? Why? What is the expected benefit?

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          I used to do stuff like this way back... for what I did, the advantage of a 1D array was that all the data was in a contiguous block of memory. For my application, the array data was a byte colour index for my screen mode, so I could actually write to my screen using integers (16-bit) to write 2 bytes at a time, which halved my loop iterations :D Ahhh fun times.

          Anyway, if you're bothering to do this conversion I'm going to go ahead and assume it's because you have to, for input reasons... is that correct? My question is, why are you getting a 3D array? Is there any way you can change your program to make that array only one dimension from whatever outputs it?

          If you can't, you might be stuck... you might be able to copy one dimension at a time using a C# equivalent of memcopy if one exists (memset? Hmm, been a while) but really, your best bet is to do your best to not be in this situation.

          Comment

          Working...