Converting Integers to Bitmap Color

Please Help...!
Hi, I am trying to replace color in bmp image. I tried making unsigned char variable directly equal to some value...
like
unsigned char oldr=255, oldg=255, oldb=0
This works pretty fine.

But now I want to take that color as input from the user.
When I use cin stream, it takes 255 as three characters, so directly getting input to unsigned char is not working.
I tried to take input to integer and then typecast to char...
But that is not working. I tried to print the characters and that characters, sometimes, are fine with respect to ASCII table. and sometimes only show spaces. But don't work in bitmap.
Kindly Help me...!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

unsigned int inewr, inewg, inewb, ioldr, ioldg, ioldb // Creating Integers;
cout << "Enter R - G - B of Color to Replace: ";
cin  >> ioldr >> ioldg >> ioldb; // Taking Integers of Old Color From User
cout << "Enter R - G - B of New: ";
cin >> inewr >> inewg >> inewb;  // Taking Integers of New Color From User
// Type casting to char
unsigned char newr=inewr, newg=inewg, newb=inewb, oldr=ioldr, oldg=ioldr, oldb=ioldb;
cout << newr << " " << newg << " " << newb << endl; // Printing out characters
cout << oldr << " " << oldg << " " << oldb << endl;

// Checking color and writing in file...

for (int j=0; j<height; j++) {
	for (int i=0; i<width*3; i+=3) {
			b = mainarray[i][j];
			g = mainarray[i+1][j]; 
			r = mainarray[i+2][j];
			if ( r==oldr && g==oldg && b==oldb) {
				fout.write((char*) &newb, 1);
				fout.write((char*) &newg, 1);
				fout.write((char*) &newr, 1);
			} else {
				fout.write((char*) &b, 1);
				fout.write((char*) &g, 1);
				fout.write((char*) &r, 1);
			}
		}
	}


Note: Arrays and all other things are ok in the code that I didn't show you...
Just there is problem in type casting. As I said putting oldr=255 works very fine...
Also I am not using any library. It is for 24-bit bitmap images
Last edited on
I think you are looking for this

http://www.cplusplus.com/reference/cstdlib/atoi/
closed account (48T7M4Gy)
It strikes me that the OP is confused about color systems. Each color is made up of 24 bits i.e. 3- 8 bit bytes with each byte capable of having a value between 0-255. There is no apparent reason to store the bytes as characters. If you do then the integer value conversion from char to int for char xyz is xyz- '0' but that seems to me to be a waste of time/effort if I understand the intent correctly.

The convention for 24 bit color and setup is explained at http://www.rapidtables.com/web/color/RGB_Color.htm
Topic archived. No new replies allowed.