Processing byte arrays

Discussion in 'C++' started by paul, Jun 28, 2006.

  1. paul

    paul Guest

    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
    second array[1] = results of calculation on first array[ array elements

    - 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]



    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;

    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 );



    pixel+=m_i_bytes_to_jump; // We might not need to
    // start processing
    from the first byte of the next row.
    paul, Jun 28, 2006
    1. Advertisements

  2. 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;

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

    byte *= pixel;

    I'd need more context (and a fine-tooth comb) to tell you any more.
    Frederick Gotham, Jun 28, 2006
    1. Advertisements

  3. paul

    paul Guest

    I meant within the context of this example.

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

    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
    paul, Jun 28, 2006
  4. Paul posted:

    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 ( ), 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, 2006
  5. paul

    paul Guest

    Er, I was perhaps a bit glib when I use "byte" I mean, by "byte *
    pixel", that pixel is
    a pointer to a byte.
    paul, Jun 28, 2006
  6. Pul posted:

    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, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.