How to pass 3 bytes to buffer in one command?

How to pass 3 bytes to buffer in one command?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// image_buffer is type unsigned char* of RGB image

// This seems to be wrong:
image_buffer[0]=(unsigned char)(H/255)*100;
image_buffer[1]=(unsigned char)(S*100);
image_buffer[3]=(unsigned char) V;
// 
// result: (unsigned char *) 0x6f0020 ""
// Nothing was added

// Normally I would do something like:
// memcpy(image_buffer, rows_buffer[0], raw_size);
/* But because of convertion from RGB to HSV I need to join the 3 components to 3 bytes string and add it to image_buffer. */


The commands will be inside loop of 1200 cycles. I found that if I run any function inside such loop it will make the reading+conversion time 25% slower.
So what is wrong on my try to copy value? I know image_buffer i pointer but does image_buffer[0]= really assign value? Also does (unsigned char *) really do the conversion correct? Passing of (unsigned char)(H/255)*100 into Watch will give value 0.
Last edited on
1
2
3
image_buffer[0]
image_buffer[1]
image_buffer[3] //←Really 3? 

Passing of (unsigned char)(H/255)*100 into Watch will give value 0.
What is the type of H? If it is integral type, you are using integer division with all implications of that.
I suggest you to reorder multiplication and dicision as H*100/255.
does image_buffer[0]= really assign value?
Yes, as any C++ tutorial will tell you.

What exactly is your problem? What do you intend to do?
The type of H,S,V are floats. H value at this moment is 88.421051, but maximum value can be 360

You can study this function:
http://paste.ofcode.org/a4N5QCrzwDjw2HFy2ESACs
it reads jpeg file in RGB and during reading line by line it should convert to HSV and save to image_buffer. It was a purpose not to run function RGB2HSV directly because it slows down program by 25%.

Is now clear what I am trying to do? The function code should explain it.
But I don't ask you to write complete function for me, I need to solve just this part small part: save the H,S,V as 3 bytes string into buffer. I don't ask for complete solution, so respect it.
Last edited on
(unsigned char)(H/255) will transform number into integer. If it is less than 1, it will be 0. I think there should be another pair of parentheses around whole expression.
This is testing part:
1
2
3
image_buffer[0]=(unsigned char)((H*100)/255);
image_buffer[1]=(unsigned char)(S*100);
image_buffer[2]=(unsigned char)V;

And result now is (unsigned char *) 0x6f0020 "\"\032G"

It should be OK but here I am not sure why I see so many characters. It is actually escaped backslash \032 and G. So it is OK. Thanks a lot.
Once more I have similar problem, this time I convert from HSV to RGB.

I found that if I try assign 0 or 0.0 so the result is that the buffer is reset to "":
1
2
3
data->row_pointers[y][x]=(unsigned char) 0;
data->row_pointers[y][x+1]=(unsigned char) 2;
data->row_pointers[y][x+2]=(unsigned char) 3;

so data are lost.

But if I type any number so it is OK, the buffer is replaced by value:
1
2
3
data->row_pointers[y][x]=(unsigned char) 1;
data->row_pointers[y][x+1]=(unsigned char) 2;
data->row_pointers[y][x+2]=(unsigned char) 3;

Can you explain why this happens and how to assign the integer 0?

Notice: I have exactly same thing in the function rgb2hsv and it works (image converted). But in the second function in the same file I have hsv2rgb and here it fails. For the caste that you are interested about complete code:
functions: http://paste.ofcode.org/jK6sd5YhXXYWhqCUXWxhSU
data type: http://paste.ofcode.org/d9avtrzdYp6tmdWY4Fhh93
Last edited on
the buffer is reset to "":
Are you printing it as c-string instead of printing as binary data? 0 means "end of string" for c-strings.
I am watching it in Watch panel while debuging. I see I must to switch it. I set to see it as hexadecimal array do see the content.
Topic archived. No new replies allowed.