Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6096985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:59:27+00:00 2026-05-23T12:59:27+00:00

I have a 3D array of bytes in c# which I have read from

  • 0

I have a 3D array of bytes in c# which I have read from a bitmap:

byte[w, h, 3]

What is the easiest and more performance-friendly way of reshaping this array into 2D (linear) form?

byte[w*h, 3]

In other words I want to keep number of channels (features) but in a linear shape (rather than a square shape)

Let me try to illustrate input and desired output:

input:

|(r1,g1,b1)    (r2,g2,b2)    (r3,g3,b3)|
|(r4,g4,b4)    (r5,g5,b5)    (r6,g6,b6)|
|(r7,g7,b7)    (r8,g8,b8)    (r9,g9,b9)|

note that arr[0, 0, 0] = r1, arr[0, 0, 1] = g1, arr[0, 0, 2] = b1, etc..

and output:

|(r1,g1,b1)    (r2,g2,b2)    (r3,g3,b3)    (r4,g4,b4)    (r5,g5,b5)    (r6,g6,b6) ...|
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:59:27+00:00Added an answer on May 23, 2026 at 12:59 pm

    This seems to work fine, because the array is already in the right shape in memory:

    var a = new byte[2,  2, 2] { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };
    var b = new byte[2 * 2, 2];
    
    //sizeof(byte) is obviously 1 here, but I put it there for documentation
    Buffer.BlockCopy(a, 0, b, 0, a.Length * sizeof(byte));
    

    For those interested: As for what to do if you really want to transpose a 2D array into 1D:

    byte[,] a = {
        {1, 2},
        {3, 4},
        {5, 6},
    };
    var b = new byte[a.GetLength(1) * a.GetLength(0)]; //Transpose
    
    const int R_STRIDE1 = 8; //Tune this for your CPU
    const int C_STRIDE1 = 8; //Tune this for your CPU
    
    //You should hoist the calls to GetLength() out of the loop unlike what I do here
    for (int r1 = 0; r1 < a.GetLength(0); r1 += R_STRIDE1)
    for (int c1 = 0; c1 < a.GetLength(1); c1 += C_STRIDE1)
        for (int r2 = 0; r2 < R_STRIDE1; r2++)
        for (int c2 = 0; c2 < C_STRIDE1; c2++)
        {
            var r = r1 + r2;
            var c = c1 + c2;
            if (r < a.GetLength(0) && c < a.GetLength(1))
                b[c * a.GetLength(0) + r] = a[r, c];
        }
    

    This should take advantage of caching in the CPU. I have only performed limited testing on this — it could still be slow. Try tweaking it if it is.
    You can (somewhat non-trivially) extend this to a 3D array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have dynamic array filled with bytes, which are read from .raw file with
I have a byte array of around 10,000 bytes which is basically a blob
I have a multi-byte primitive type called s32 which I want to read from
I have dll which accepts Pointer to array of bytes from C++ and try
I have a byte[] array, the contents of which represent a TIFF file (as
I'm using ASP.NET MVC and have a model which has an image (byte array)
Let's say I have array of bytes: byte[] arr = new byte[] { 0,
I have an array of bytes that I receive from an external entity. It
I have an array of bytes, in memory. What's the fastest way to see
I'm trying to read a null terminated string from a byte array; the parameter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.