Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

breaking up a character array

Status
Not open for further replies.

gorgor

Programmer
Aug 15, 2002
164
US
I have a 1-D character array of over 64,000 bytes. I want to break this array into a 3-D character array. Each element would be 32bits(4bytes). What would be the easiest way to do this?

The original character array is the character representation of a 32-bit per pixel bitmap image. The information for each 32-bit pixel is just appended linearly to the 32-bit representation of the previous pixel. I want to split it up into a mulitidimensional array so I can easily manipulate specific pixels in the bitmap later. The new array would be something like: unsigned char pixelInfo[row][column][4];

Any ideas? Thanks in advance!
 
Make the 3D array. Read the data from the 1D array into the 3D one (most likely with nested for loops).
 
Since the data is combined linearly the
3D array model is flawed. Since col !=
row + 1 in a 3D array, etc..Unless I'm missing something the reorganization
would require an extensive recombination
of your data to accomodate the col concept.

Why not just write a function that emulates the 3D array?

Here's an example proggy with integer arrays:
#include <stdio.h>
#include <stdlib.h>

int *arrCreate(int);
void arrPopulate(int *, int);
void translateArray(int *,int);


int main(void) {
int *mainarr;

mainarr = arrCreate(64001);
if (mainarr != NULL) {
arrPopulate(mainarr,64001);
translateArray(mainarr,12003);
}
return 0;
}

int *arrCreate(int x) {
int *array = malloc(x * sizeof(int));
if (array) {
return array;
}
return NULL;
}

void arrPopulate(int *arr, int x) {
int p;

for (p=0 ; p <= x ; p++) {
arr[p] = (1 + rand() % 12);
}
}

void translateArray(int *arr, int y) {
int x,p = 1;

for (x=0 ; x <= y ; x++) {
++p;
if (p % 3 == 0) {
printf(&quot;Row %d, Index %d, Value %d\n&quot;,p,p + 1,
arr[x]);
}
}
}
 
I know the width and height of the bitmap image. I can get this info at the beginning of the program. I can then take the first (32 * imageWidth) bits of the 1-D character array and store them from pixelInfo[row=0][column=x] to pixelInfo[row=0][column=x+imageWidth] in the 3-D array. Somehow I would remove 'row1' from the original 1-D array and move on to the next row. I can do the same for the remaining rows. Being an engineer, not a programmer, my programming experience is limited and I don't know the syntax for doing what I'm trying to do.

marsd, thank you for the sample code, I can't really tell what it is doing.

chipper, that's exactly what I want to do, but I don't know the syntax for doing it.

Thanks again...more help would be greatly appreciated.
 
Example:

[tt]
char array1D[ 27 ]; /* original array */
char array3D[ 3 ][ 3 ][ 3 ]; /* destination array */
int i, j, k, x = 0;

for ( i = 0; i < 3; ++i )
{
for ( j = 0; j < 3; ++j )
{
for ( int k = 0; k < 3; ++k )
{
array3D[j][k] = array1D[ x++ ];
}
}
}
[/tt]

The array declarations just create the arrays. Presumably you'd put data into the 1D array at some point.

Those ints are loop counters. i, j, and k keep track of what index you're on in each dimension at any given point through your traversal of the 3D array. x just keeps track of which item you're on in the 1D array.

The for loops essentially loop through each dimension. Each time through, you reach a new element of the 3D array, which you load with the next element of the 1D array.

Now, your array is bigger and has different dimensions, and you might have to switch some indices around to get your array &quot;pointing&quot; the right way, but that's the basic idea.
 
Oof. I finally italicized my post. It was bound to happen sooner or later.

Here's a repost of that code, now with 50% more indices:

[tt]
char array1D[ 27 ]; /* original array */
char array3D[ 3 ][ 3 ][ 3 ]; /* destination array */
int i, j, k, x = 0;

for ( i = 0; i < 3; ++i )
{
for ( j = 0; j < 3; ++j )
{
for ( int k = 0; k < 3; ++k )
{
array3D[j][k] = array1D[ x++ ];
}
}
}
[/tt]
 
chipper, thanks for the code example. I beleive, since a char is 2 bytes, you are populating a 3D array with each element containing 2 bytes (since both arrays are defined as 'char'). I need to populate the 3D array with each element containing 4 bytes (32 bits). Will a simple 'for' loop still work?
 
A char is actually only one byte in C, but in this case, it really doesn't matter. Since we're indexing into the arrays, when we say &quot;array1D[ 4 ]&quot;, we're getting the 4th element of the array. If it's an array of 4-byte integers, we're going 16 bytes past the beginning and getting the integer there (the 4th one in the array). If it's an array of 8-byte doubles, we're going 32 bytes past the beginning of the array and getting the double there (the 4th one in the array). The indexing operator takes care of all that pointer arithmetic for you.

You do have to remember to declare your arrays with the right types. If you want them to hold 4-byte elements, remember to declare them as arrays of ints (assuming you have 4-byte ints).

Now, if you mean you'll have a 1D array of characters (1 byte each) and a 3D array of ints (4 bytes each), it'll be a little more complicated, but not much.
 
the original array is an unsigned char array. It contains information about roughly 13,500 pixels of a bitmap image. Each pixel is described by 4 bytes (it has 32-bit color depth). The pixels are stored in the 1-d array one-after-the other, so the array is roughly 432k.

I want a 2-d array (i said 3-d before, i meant 2-d) with this pixel information so it can be managable programatically. I want to convert it to an array &quot;pixelArray[rows][columns]&quot;. Each element would contain 4 bytes.

The original array is unsigned char's so that's why I wanted to use unsigned chars for the 2-d array. I'm not sure what type to use so each element would be treated as 4 bytes.

Did this clarify my question? Thanks for any further help you can provide.

P.S. I tried the sample code (array3D[j][k] = array1D[ x++ ];) and the compiler is giving me &quot;Array assignments are illegal...Lvalue required.&quot; errors for that line of code. I'm using LabWindows for a compiler.
 
Code:
unsigned char array1D[ BITMAPSIZE ];
int array2D[ XDIM ][ YDIM ];
int i, j;
int x = 0;

for ( i = 0; i < XDIM; ++i )
{
    for ( j = 0; j < YDIM; ++j )
    {
        array2D[ i ][ j ] = *(int*)(array1D + x );
        x += 4;
    }
}

The above assumes int is 4 bytes on your machine (there's a good bet it is, but you might have to use a long instead; test it with sizeof(int), or just use int32_t if your library supports it).

I've never heard of LabWindows, and it shouldn't have given that error for that code. If it still gives you problems for this, let me know and I'll look for a workaround (which may amount to using a different compiler).

There's already some code written to deal with bitmaps. If you need to do a lot of work with bitmaps, finding a library for it might be easier, faster, and less error-prone than writing the code yourself.
 
I have some built in functions for dealing with bitmaps in LabWindows (LabWindows is a National Instruments programming environment that is designed for test automation of instruments). I need to stick with it because that is what we use for our code development. It makes slight attempt to be kind of like Visual Studio, but not really. We will be moving to .net eventually. I'll check out your sample code and let you know how it goes.

If you know of any good C library resources, please pass them along...Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top