Go Down

Topic: translating shifts from Java (Processing) to C++ (arduino) (Read 2 times) previous topic - next topic

robvoi

Jan 07, 2013, 02:50 pm Last Edit: Jan 07, 2013, 03:07 pm by robvoi Reason: 1
Hi,

I'm trying to translate/reuse some processing code which uses shifts. (for translating 2 byte RGB to 1byte grayscale)  - I am stuck.
I didn't yet understand the difference between the shift operations.

Maybe someone could enlight me and help with the translation.

Processing code:
Code: [Select]
//loc is defined and (I think) doesn't matter too much for the translation
int msb = packedbuf[2*loc] & 0xff;
int lsb = packedbuf[2*loc+1] & 0xff;
int r = (lsb >>> 3);
int g = ((lsb & 0x7) << 3);
g |= (msb >>> 5);
int b = (msb & 0x1f);

r = (r << 3)|(r >>> 2);
g = (g << 2)|(g >>> 4);
b = (b << 3)|(b >>> 2);


My arduino code, which is wrong as I just removed everything what didn't seemed to be c++ bit shift related:
Code: [Select]
//l is defined and is used instead of loc from the processing code, variables are defined earlier in the code
msb = 2*l;
lsb = 2*l+1;
           
r = (lsb >> 3);
g = (lsb << 3);
g |= (msb >> 5);
b = (msb);

r = (r << 3)|(r >> 2);
g = (g << 2)|(g >> 4);
b = (b << 3)|(b >> 2);


I am going back to wikipedia and google trying to understand the java shift operations ...

Thanks
Robert


robvoi

#1
Jan 07, 2013, 04:49 pm Last Edit: Jan 07, 2013, 04:59 pm by robvoi Reason: 1
Maybe there some explanation on what has to be achived by the code helps:

I have two bytes as RGB value: RRRRRGGG GGGBBBBB. At least that's what I expect to get from the camera. (If I would fully understand what the processing code does, I would be sure :-) )

I want to seperate them into three integers.
int 1= RRRRR
int 2= GGGGG
int 3= BBBBB

I thought it would maybe possible like this:

Code: [Select]
//buf contains the the byte stream
for (unsigned int l= 0; l < TWI_BUFFER_LENGTH/2; ++l){
r = (buf[l*2] >> 16) & 0xff ;     //bitwise shifting
g = (buf[l*2] >> 8) & 0xff ;
b = buf[l*2] & 0xff ;
//here calculate grayscale and write to grayscale img buffer
}


But it only gives me a black picture.

Robert

PeterH


Maybe there some explanation on what has to be achived by the code helps:

I have two bytes as RGB value: RRRRRGGG GGGBBBBB. At least that's what I expect to get from the camera. (If I would fully understand what the processing code does, I would be sure :-) )


I want to seperate them into three integers.
int 1= RRRRR
int 2= GGGGG
int 3= BBBBB


Yes, that is what the Processing code seems to do. The >>> operator is the Java unsigned bit shift operator - when you do an unsigned bit shift, the bits shifted into the most significant position are zero. The code seems to extract three 5-bit values from the 16 bits. The code to extract the g (green) channel is a bit more complicated that the other channels because it has to take three bits from one byte and two bits from the other.
I only provide help via the forum - please do not contact me for private consultancy.

robtillaart


the  A >>> B  pattern translates to a  (A >> B) & C  pattern  where C is a mask with the lower (8-B) bits set to 1

leaves:
Code: [Select]

int msb = packedbuf[2*loc] & 0xff;
int lsb = packedbuf[2*loc+1] & 0xff;

int r = (lsb >> 3) & 0x1F;
int g = ((lsb & 0x07) << 3) | ((msb >> 5) & 0x07) ;
int b = (msb & 0x1f);
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robvoi

This worked. And thanks a lot for the explanation.
As you now finaly also wrote the convertion function for me I now get a grayscale picture from the cam.

I have to tune it as it is too dark, even with the most correct translation function you provided (have to figure out the bit shift part, but also without the calculaiton for the whole picture only takes 0.2 seconds).

I learned a lot from the information you provided. In retrospect all you explained (beside the bit shifting part) is clear and I should have been able to do it myself. But that how it is when you learn something.

Thanks
Robert

Go Up