Re: Pack bitmap bytes into native-endian 32bit int



On Jan 14, 11:02 am, Nobody <nob...@xxxxxxxxxxx> wrote:
On Tue, 11 Jan 2011 19:21:54 -0800, luser- -droog wrote:
This should be:

                for (k = 0; k < 4; k++) {

Otherwise, you're iterating over the columns twice.

I tried that, but my sample data only has three bytes per row.

So? However much data you have, there are always 4 8-bit bytes in a
32-bit word.

Yebbut...

As I said before:



But you also need to allow for:

1. any padding at the end of each row (i.e. if w < stride*4).
...
For 1, either:

a) use e.g. "if (j*4+k < stride)" around the code which reads the bytes,
or
b) iterate over the bytes rather than the words, then push an extra 0-3
zero bytes into the final word at the end of each row.

Either way, you need to "create" 0-3 extra zero bytes at the end of each
row. If you iterate over the words, the innermost level needs a check to
use the created zeros rather than trying to read non-existent source
bytes. If you iterate over the bytes, you need to "complete" the last word
after the loop.

I think I've got that covered.
I zero the word first and then mask and shift the
bytes into it. The padding is alredy there when I
copy the word into word-packed mask.

uint32_t u;

...

for (j = 0; j < stride/4; j++) {
u = 0;
/* each 8bit byte in 32bit int */
for (k = 0; k < w/8; k++) {
uint8_t b;
if (endian == big) b = samp[i*(w/8) + k];
else b = samp[(i+1)*(w/8) - (k+1)];
u <<= 8;
if (endian == big) u |= b;
else u |= invertbits(b);
//printf("%X\n", u);
}
//printf("%08X\n", u);
*((uint32_t *)(data + i*stride + j*4)) = u;
}
}

.



Relevant Pages

  • Re: Pack bitmap bytes into native-endian 32bit int
    ... iterate over the bytes rather than the words, then push an extra 0-3 ... zero bytes into the final word at the end of each row. ... If you iterate over the words, the innermost level needs a check to ... copy the word into word-packed mask. ...
    (comp.lang.c)
  • Re: Newtons method
    ... var list and the calc figures out the rest, then you can iterate as ... When I do this on the 50, all I get is infinity for the ... it comes up with zero for its derivative. ...
    (comp.sys.hp48)
  • Re: Newtons method
    ... var list and the calc figures out the rest, then you can iterate as ... When I do this on the 50, all I get is infinity for the ... it comes up with zero for its derivative. ...
    (comp.sys.hp48)
  • Re: Unix scripting question
    ... First I know zero about Unix and ... shells. ... want to process (iterate to) the next job in the list. ...
    (comp.unix.shell)
  • Re: Pack bitmap bytes into native-endian 32bit int
    ... but my sample data only has three bytes per row. ... you need to "create" 0-3 extra zero bytes at the end of each ... If you iterate over the words, the innermost level needs a check to ...
    (comp.lang.c)