converting from rgbdata to uchar array

I have a class RGBColor with float values r, g and b. These range between 0.0 and 1.0. In my program I have a 2d array of RGBColor instances, and I need to convert this into a uchar array and multiply them by 255 so they can act as actual rgbvalues (0.0 = 0, 1.0 = 255). My code for doing this is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int size = world->vp.v_res * world->vp.h_res * 3;
uchar data[size];
for(int x = 0; x < world->vp.v_res; x++) {
	for(int y = 0; y < world->vp.h_res; y++) {
		for(int z = 0; z < 3; z++) {
			switch(z) {
				case 0:
					data[x + y + z] = (uchar)world->pixel_data[x][y].r*255;
					break;
				case 1:
					data[x + y + z] = (uchar)world->pixel_data[x][y].g*255;
					break;
				case 2:
					data[x + y + z] = (uchar)world->pixel_data[x][y].b*255;
					break;
			}
		}
	}
}


The array I'm inputting to this (the pixel_data array) I know contains only 0.0 and 1.0, and nothing between those values, so ideally the resulting char-array would only ontain the values 0 and 255, but it doesnt. It contains seemingly random values between them. So does anyone know what I'm doing wrong here?

Peas,
Fafner

You could make this a lot more readible if you seperated the conversion from the rgb values to an array of 3 unsigned char values from the loop (for example, you could create a member function for RGBColor that returns a struct of 3 uchars). Also, it seems to me like you use your RGBColor class as a struct with public data members, is that correct? That's not really a good idea if you expect the data members of RGBColor to have a range from .0f to 1.0f.

Anyways, if there are random values in between that prolly means you leave some of your RGBColor members uninitialized.
That's a good idea, creating such a member function! Yes, the values are public, so you're probably right about that as well.

I also thought about the values not being initialized, but the thing is that I'm writing the values out to console before and after the conversion, and they're all fine in the before-version (being either 0.0 or 1.0).
Can you please show me the constructor code of RGBColor, as well as the code where you read the values?
RGBColor constructor:
1
2
RGBColor::RGBColor(const float a, const float b, const float c) : r(a), g(b), b(c) {
}


1
2
3
4
5
for(int x = 0; x < world->vp.v_res; x++) {
	for(int y = 0; y < world->vp.h_res; y++) {
		log_out->append(QString::number(world->pixel_data[x][y].r*255));
	}
}


It only logs out the r-value, I know, but it should be enough.
Apologies, the pre-conversion output is between 0 and 255, not 0 and 1 as I said 2 posts back.
Uhhhhhh why are your constructor parameters constant? Doesn't make too much sense cause you aren't passing any references or anything. Oh well it doesn't matter, it's technically not an error.

Ok, questions:

What is log_out?
what is world?
Do you actually read the values there? Looks to me like you are printing them :O
Ok, I found a way in which I could just save the pixel data as uchar array, without going through a 2d array of rgbcolors. It seems to work now. Thanks for you help anyway!
Topic archived. No new replies allowed.