473,388 Members | 1,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. int index = -1;
  2.  
  3. for (int j = 0; j < h; ++j)
  4.     for (int i = 0; i < w; ++i)
  5.     {
  6.         data1D[++index] = data3D[j, i, 0];
  7.         data1D[++index] = data3D[j, i, 1];
  8.         data1D[++index] = data3D[j, i, 2];
  9.         data1D[++index] = 255;
  10.     }

I need something faster.
I tried with Marshal.Copy() and unsafe, fixed pointers but without success.
Mar 17 '10 #1
4 3372
tlhintoq
3,525 Expert 2GB
There is no multiplication going on in your example.
Isn't the end result supposed to be the three values multiplied?
Mar 17 '10 #2
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...
Mar 17 '10 #3
tlhintoq
3,525 Expert 2GB
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?
Mar 17 '10 #4
GaryTexmo
1,501 Expert 1GB
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.
Mar 17 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: DraguVaso | last post by:
Hi, I need to find the FASTEST way to get a string in a Loop, that goes from "a" to "ZZZZZZZZZZZZZZZZZ". So it has to go like this: a b .... z
5
by: Daniel | last post by:
in C# fastest way to convert a string into a MemoryStream
5
by: johannblake | last post by:
I need to copy all the data from a one dimensional byte array to a specific column in a two dimensional int array. For example: byte byteArray = {4, 5, 6}; int intArray = new int; After...
5
by: Tales Normando | last post by:
The title says it all. Anyone?
7
by: kebalex | last post by:
Hi, I have an app (written in .NET 2.0) which updates a picturebox according to some user input (a slider control). when the user makes a change i loop through all of the pixels, do a...
24
by: ThunderMusic | last post by:
Hi, The subject says it all... I want to use a byte and use it as byte* so I can increment the pointer to iterate through it. What is the fastest way of doing so in C#? Thanks ThunderMusic
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.