Processing byte arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • paul@paullee.com

    Processing byte arrays

    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_ju mp ... byte_offset +
    m_i_total_bytes _to_read + m_i_bytes_to_ju mp]
    etc.

    TIA

    Paul



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



    int i_bytes_process ed=0;

    byte * pixel;

    pixel = image->image_data; // image data is defined as byte *
    pixel+=byte_off set; // 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_re ad_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_by tes_processed, calc_result );

    i_bytes_process ed++;

    pixel+=3;
    }

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

  • Frederick Gotham

    #2
    Re: Processing byte arrays

    [color=blue]
    > NB: "int" is synonymous with 32 bit unsigned ints[/color]


    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;


    [color=blue]
    > byte * pixel;[/color]


    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

    Comment

    • paul@paullee.com

      #3
      Re: Processing byte arrays


      Frederick Gotham wrote:[color=blue][color=green]
      > > NB: "int" is synonymous with 32 bit unsigned ints[/color]
      >
      >
      > Not in C++, it ain't.
      >[/color]

      I meant within the context of this example.

      [color=blue]
      >
      > "int" is an abbreviation of "signed int", NOT "unsigned int".
      >
      >
      > If you want an abbreviation, use "unsigned".
      >
      > unsigned i = 0;
      >
      >
      >[color=green]
      > > byte * pixel;[/color]
      >
      >
      > Redudant statement -- does absolutely nothing. Perhaps you meant something
      > like:
      >
      > byte *= pixel;
      >[/color]

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

      [color=blue]
      >
      > I'd need more context (and a fine-tooth comb) to tell you any more.
      >[/color]


      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

      Comment

      • Frederick Gotham

        #4
        Re: Processing byte arrays

        Paul posted:

        [color=blue]
        > Don't you mean byte * pixel = image->image_data ?[/color]


        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

        Comment

        • paul@paullee.com

          #5
          Re: Processing byte arrays


          Frederick Gotham wrote:[color=blue]
          > Paul posted:
          >
          >[color=green]
          > > Don't you mean byte * pixel = image->image_data ?[/color]
          >
          >
          > 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);
          >[/color]

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

          Comment

          • Frederick Gotham

            #6
            Re: Processing byte arrays

            Pul posted:

            [color=blue]
            > Er, I was perhaps a bit glib when I use "byte" I mean, by "byte *
            > pixel", that pixel is
            > a pointer to a byte.[/color]


            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

            Comment

            Working...