Reputation: 6283
What kind of variable should I initialize and how do I do it?
For example
byte[][][] data = ?????;
The function is from LibJpeg.NET library. The function definition is
Int32 jpeg_read_raw_data (
Byte[][][] data,
Int32 max_lines
)
There is no good documentation for this function, so I don't know what to send it, but I want to try to send something and see what the function will return.
The problem is that I don't know how to call it.
Upvotes: 1
Views: 281
Reputation: 6685
byte[][][] data is a 3 dimensional array, allocate it accordingly
= {{{0,1,2}{3,4,5}{6,7,8}}
{{0,1,2}{3,4,5}{6,7,8}}
{{0,1,2}{3,4,5}{6,7,8}}}
NOTE! THIS WILL NOT WORK! This is a jagged array, so unless you define data as byte[3,3,3] then you are going to go nowhere.
It occurred to me: I have no idea why you are using this library. Is there a reason the built in .NET JPEG classes aren't sufficient? If so, I imagine there would be a way to use the LibJpeg.NET library to get a byte stream from a file and arrange it into a 3D array. This seems like a very "hacky" way to do this, and it makes me wonder if it's just a port of a C library with no updates to fit into a framework like .NET or even OOP.
Upvotes: 2
Reputation: 15829
Just to correct firoso's answer, you can allocate arrays like that, he just has not specified the types as required in C# (His code would work in Java, AFAIK)
private byte[][][] lol =
{
new[]
{
new byte[] {0, 1, 2},
new byte[] {3, 4, 5},
new byte[] {6, 7, 8}
},
new[]
{
new byte[] {0, 1, 2},
new byte[] {3, 4, 5},
new byte[] {6, 7, 8}
},
new[]
{
new byte[] {0, 1, 2},
new byte[] {3, 4, 5},
new byte[] {6, 7, 8}
}
};
The new[]
is actually a shortcut for new byte[][]
and the new byte[]
cannot be shortened to new[]
as then the compiler infers the numbers types as int
.
I would not recommend using this method if you can use Heinzi's iterative method instead; I am only pointing out it is possible to allocate multidimensional arrays like this (it is good for a lot of specific data).
Upvotes: 1
Reputation: 176169
After a short look into the sources of LibJpeg.NET it looks like you should initialize the first dimension of the data
array with the number of scan lines:
byte[][][] data = new byte[max_lines][][];
The rest of the array is filled when reading the raw data.
I'm not too familiar with that API so I don't know how you would get the number of scan lines though.
Upvotes: 1
Reputation: 172280
byte[][][]
is an array of (arrays of (arrays of bytes)), also called a jagged array. It's not the same as a three-dimensional array (e.g. byte[,,] x = new byte[10,5,3];
).
Here is an example for allocating and initializing such a structure:
byte[][][] a = new byte[10][][];
for (int i = 0; i < 10; i++)
{
a[i] = new byte[5][];
for (int ii = 0; i < 5; i++)
{
a[i][ii] = new byte[3];
a[i][ii][0] = 42;
a[i][ii][1] = 52;
a[i][ii][2] = 62;
}
}
Oh, and by the way, here's a related question: C# 3 dimensional array definition issue.
And here's the MSDN entry on jagged arrays: http://msdn.microsoft.com/en-us/library/2s05feca.aspx.
Upvotes: 2