By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,236 Members | 1,839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,236 IT Pros & Developers. It's quick & easy.

Processing byte arrays

P: n/a
Hi all,
Could some kind soul peruse the following code and see if there is
anything wrong with it?
Its producing output, but its only occupying the first third of the
output array; to give an example, think of a TV screen where only the
top third shows anything, and what is does show is repeated 3 times.

The code should skip through an array of bytes, taking them in groups
of three, and then writing the results of a calculation on the three
bytes to a third array, so that

second array[0] = results of calculation on first array[ array elements
0,1,2]
second array[1] = results of calculation on first array[ array elements
3,4,5]

- in essence converting 24 bit colour to 8 bit greyscale.

There are elements in the code to allow jumping over redundant elements
in the first array;
for instance, you might only want to scan through the array elements
array[byte_offset ... byte_offset + m_i_total_bytes_to_read]
and then array[byte_offset + m_i_bytes_to_jump ... byte_offset +
m_i_total_bytes_to_read + m_i_bytes_to_jump]
etc.

TIA

Paul

NB: "int" is synonymous with 32 bit unsigned ints

int i_bytes_processed=0;

byte * pixel;

pixel = image->image_data; // image data is defined as byte *
pixel+=byte_offset; // We might not have to start at
(0,0) in the image

// total bytes to read is 3*original
data width x height - 24 bit colour,
// ditto for "bytes_to_read_per_row"
for( int bytes_read = 0; bytes_read < m_i_total_bytes_to_read;
bytes_read+=m_i_bytes_to_read_per_row)
{

for(int i=0; i< m_i_bytes_to_read_per_row; i+=3)
{
byte red = pixel[2]; // last 8 bits is red data
byte green = pixel[1]; // middle 8 bits is green data
byte blue = pixel[0]; // first 8 bits is blue data

// Do some
processing on the values (omitted)

// Write out to the array -
this is a linked list
//
containing bytes elements

m_image_average_b_w.SetAt(i_bytes_processed, calc_result );

i_bytes_processed++;

pixel+=3;
}

pixel+=m_i_bytes_to_jump; // We might not need to
// start processing
from the first byte of the next row.
}

Jun 28 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
NB: "int" is synonymous with 32 bit unsigned ints

Not in C++, it ain't.
"int" is an abbreviation of "signed int", NOT "unsigned int".
If you want an abbreviation, use "unsigned".

unsigned i = 0;
byte * pixel;

Redudant statement -- does absolutely nothing. Perhaps you meant something
like:

byte *= pixel;
I'd need more context (and a fine-tooth comb) to tell you any more.
--

Frederick Gotham
Jun 28 '06 #2

P: n/a

Frederick Gotham wrote:
NB: "int" is synonymous with 32 bit unsigned ints

Not in C++, it ain't.


I meant within the context of this example.


"int" is an abbreviation of "signed int", NOT "unsigned int".
If you want an abbreviation, use "unsigned".

unsigned i = 0;
byte * pixel;

Redudant statement -- does absolutely nothing. Perhaps you meant something
like:

byte *= pixel;


Don't you mean byte * pixel = image->image_data ?


I'd need more context (and a fine-tooth comb) to tell you any more.

What it does, simply put is
second_array[0] = f( first_array[0,1,2])
second_array[1]=f( first_array[3,4,5])
second_array[2]=f( first_array[6,7,8])
etc., where the function f(...) is just some junk I do on the 3 byte
array elements of the first array

Jun 28 '06 #3

P: n/a
Paul posted:

Don't you mean byte * pixel = image->image_data ?

Think I might have found your error.

Here's a verbatim copy-paste from your original post:

byte * pixel;

pixel = image->image_data; // image data is defined as byte *
You've two problems here.
First of all, let's change it to what you intended:

byte * pixel = image->image_data;
Take out your favourite C++ operator precedence table (
http://www.difranco.net/cop2334/cpp_op_prec.htm ), and you'll see that
multiplication is performed before assignment -- so you're trying to
assign something to an R-value.

And even if you supply the necessary parentheses:

bytes * (pixel = image->image_data);

You're still left with a partly redundant statement, because the result
of the multiplication is discarded.

I'm still not quite sure what you're trying to do.
--

Frederick Gotham
Jun 28 '06 #4

P: n/a

Frederick Gotham wrote:
Paul posted:

Don't you mean byte * pixel = image->image_data ?

Think I might have found your error.

Here's a verbatim copy-paste from your original post:

byte * pixel;

pixel = image->image_data; // image data is defined as byte *
You've two problems here.
First of all, let's change it to what you intended:

byte * pixel = image->image_data;
Take out your favourite C++ operator precedence table (
http://www.difranco.net/cop2334/cpp_op_prec.htm ), and you'll see that
multiplication is performed before assignment -- so you're trying to
assign something to an R-value.

And even if you supply the necessary parentheses:

bytes * (pixel = image->image_data);


Er, I was perhaps a bit glib when I use "byte" I mean, by "byte *
pixel", that pixel is
a pointer to a byte.

Jun 28 '06 #5

P: n/a
Pul posted:

Er, I was perhaps a bit glib when I use "byte" I mean, by "byte *
pixel", that pixel is
a pointer to a byte.

Wups, it's me that's confused! I'm used to having a capital letter at the
start of a type's name, e.g.

Byte *pixel;
When I saw:

byte * pixel;
I (mistakenly) presumed that they were both objects, and that you were
performing a redundant multiplication.
--

Frederick Gotham
Jun 28 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.