473,385 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to extract RGB as unsigned char * from unsigned char * of 24 bit BMP Color images

Dear All,


Now, I am stuck above question so long and I would like to get Red, Green and Blue as unsigned char * each from unsigned char * of 24 bit BMP Color images.

e.g
unsigned char * image = new unsigned char [ width * height];
unsigned char * red = new unsigned char [ width * height];
unsigned char * blue= new unsigned char [ width * height];
unsigned char * green = new unsigned char [ width * height];

the coding are as follows

bool MainDlg::Test()
{

char *colorFileLocation = "C\\Images\\white.bmp";
colorSize = Load_Colorfile_to_memory(colorFileLocation, &cColorContent);

FILE *colorF;
colorF = fopen(colorFileLocation, "rb");

BITMAPFILEHEADER colorBmfh;
fread(&colorBmfh,sizeof(BITMAPFILEHEADER),1,colorF );

BITMAPINFOHEADER colorBmih;
fread(&colorBmih,sizeof(BITMAPINFOHEADER),1,colorF );

int colorBmWidth = colorBmih.biWidth;
int colorBmHeight = colorBmih.biHeight;
int colorPitch = colorBmWidth;

unsigned char *colorImage = new unsigned char[colorBmWidth * colorBmHeight * 3];
colorImage = cColorContent;
unsigned char *Red = new unsigned char[colorBmWidth * colorBmHeight ];
unsigned char *Green = new unsigned char[colorBmWidth * colorBmHeight ];
unsigned char *Blue = new unsigned char[colorBmWidth * colorBmHeight ];


/*for (int k =0;k<colorBmHeight;k++)
{
for (int i =0;i<colorBmWidth;i++)
{
Red[k*colorBmHeight + i] = colorImage[k*colorBmHeight+ i*3];
Green[k*colorBmHeight + i] = colorImage[k*colorBmHeight+ i*3+1];
Blue[k*colorBmHeight + i] = colorImage[k*colorBmHeight+ i*3+2];
}
}*/


unsigned char *cImage = GetColorImages(colorBmWidth, colorBmHeight, Red, colorPitch);

Please advise me and looking forward to hearing from you soon.

Thanks in advance
Feb 24 '10 #1
8 7545
jkmyoung
2,057 Expert 2GB
Either:
1. You need to set colorImage to bytes instead of char

or

1. You need to convert into 24 bit sequences. Each row in a bmp is a multiple of (4 bytes).
2. You need to offset correctly.

The total width of each row in bytes will be (colorBmWidth / 4 * 3 + (colorBmWidth % 4))*4;

http://en.wikipedia.org/wiki/BMP_fil...at#Bitmap_data
Feb 24 '10 #2
Hi,


Thanks for your prompt reply. As for me, I would like to extract out red, green, blue, unused as each unsigned char * since my dll based on native C or C++ only. As well, the other SDK only accept as I mentioned R,G,B differently then combine as one color byte. Please advise me and looking forward to hearing from you soon.



Best regards
Feb 25 '10 #3
jkmyoung
2,057 Expert 2GB
Are you planning on running this on any system but your own?

Find out the sizeof(char) on your system. If it works out to be 1 then you don't have to do anything extra.

To clarify the last point, the pixel's data has 000's at the end of a row. Eg a 2X2 row data would look something like (in hex)

pixel (0,0):
FF 11 11
pixel (0,1):
22 FF 22
end of row: (6 bytes used. fill to 8)
00 00

pixel (1,0):
33 33 FF
pixel (1,1):
44 44 44
end of row: (6 bytes used. fill to 8)
00 00

So the pixel data looks something like:
FF 11 11 22 FF 22 00 00
33 33 FF 44 44 44 00 00

You just need to add the extra bits per row.
you have:
[k*colorBmHeight+ i*3]
(height? should be width logically)

Calculate the number of bytes in a row. Something like:
((colorBmWidth * 3 / 4) + colorBmWidth % 4) * 4
Feb 25 '10 #4
Hi,


Thank you your prompt reply and

I am planning on running in native C++ using VS 2008 and integrate with

The sizeof(char) on your system is 1 byte and I couldn't well where I put these lines in my coding.

[k*colorBmHeight+ i*3]
The image size is let say 1976 x 1472

Calculate the number of bytes in a row. Something like:
((colorBmWidth * 3 / 4) + colorBmWidth % 4) * 4

I am looking forward to hearing from you.


Best regards
Feb 26 '10 #5
jkmyoung
2,057 Expert 2GB
What you have now is:
[k*colorBmHeight+ i*3];

colorBmHeight should instead be the byte size of a row.

int sizeOfRow = ((colorBmWidth * 3 / 4) + colorBmWidth % 4) * 4 ;

So it becomes:
[k*sizeOfRow+ i*3];
Feb 26 '10 #6
Hi,


Now access violation writing location in after k = 491
// For Color Images
unsigned char *colorImage = new unsigned char[colorBmWidth * colorBmHeight * 3];
colorImage = cColorContent;
unsigned char *red = new unsigned char[colorBmWidth * colorBmHeight ];
unsigned char *green = new unsigned char[colorBmWidth * colorBmHeight ];
unsigned char *blue = new unsigned char[colorBmWidth * colorBmHeight ];
unsigned char *unused = new unsigned char[colorBmWidth * colorBmHeight ];

// Convert from unsigned char * to unsigned long *
int sizeOfRow = ((colorBmWidth * 3 / 4) + colorBmWidth % 4) * 4 ;

for (int k =0;k<colorBmHeight;k++)
{
for (int i =0;i<colorBmWidth;i++)
{
//red[(k*colorBmWidth) + i] = colorImage[(k*colorBmWidth) + (i*3)];
red[(k*sizeOfRow) + i] = colorImage[(k*sizeOfRow) + (i*3)];
green[(k*sizeOfRow) + i] = colorImage[(k*sizeOfRow) + (i*3)+ 1];
blue[(k*sizeOfRow) + i] = colorImage[(k*sizeOfRow) + (i*3)+ 2];
unused[(k*sizeOfRow) + i] = colorImage[(k*sizeOfRow) + (i*3) + 3];
//unused[(k*sizeOfRow) + i] = 0;
}
}
Mar 1 '10 #7
Hi,


Now error came out after red buffer used sizeOfRow instead of colorBmWidth.

// For Color Images
unsigned char *colorImage = new unsigned char[colorBmWidth * colorBmHeight * 3];
colorImage = cColorContent;
unsigned char *red = new unsigned char[colorBmWidth * colorBmHeight ];
unsigned char *green = new unsigned char[colorBmWidth * colorBmHeight ];
unsigned char *blue = new unsigned char[colorBmWidth * colorBmHeight ];
unsigned char *unused = new unsigned char[colorBmWidth * colorBmHeight ];

// Convert from unsigned char * to unsigned long *
int sizeOfRow = ((colorBmWidth * 3 / 4) + colorBmWidth % 4) * 4 ;

for (int k =0;k<colorBmHeight;k++)
{
for (int i =0;i<colorBmWidth;i++)
{
//red[(k*colorBmWidth) + i] = colorImage[(k*colorBmWidth) + (i*3)];
red[(k*sizeOfRow) + i] = colorImage[(k*sizeOfRow) + (i*3)]; // access violation
writing location in after k = 491

green[(k*sizeOfRow) + i] = colorImage[(k*sizeOfRow) + (i*3)+ 1];
blue[(k*sizeOfRow) + i] = colorImage[(k*sizeOfRow) + (i*3)+ 2];
unused[(k*sizeOfRow) + i] = colorImage[(k*sizeOfRow) + (i*3) + 3];
//unused[(k*sizeOfRow) + i] = 0;
}
}

could it be possible to resue the 24 bits color images into R, G and B of 8 bits then again as 8 bits of R, G and B into 24 bits of color images for verification test before putting into third party SDKs.


Thanks and best regards
Mar 1 '10 #8
jkmyoung
2,057 Expert 2GB
Sorry, formula was wrong:

int sizeOfRow = colorBmWidth * 3 + colorBmWidth % 4;

overcomplicated it.
Mar 1 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: MiniDisc_2k2 | last post by:
Okay, here's a question about the standard. What does it say about unsigned/signed mismatches in a comparison statement: char a = 3; unsigned char b = 255; if (a<b) Now what's the real...
10
by: Danny Anderson | last post by:
Hola! I have an array of shorts that represents a stream of data. This data is packed in tight, so certain elements may actually contain data for more than one variable. I am to decipher this...
7
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For...
44
by: RB | last post by:
How to extract bytes from long, starting from the last byte? For example, I have a long number: 0x12345678 I need to represent it as the following bytes list: 0x78, 0x56, 0x34, 0x12 Thanks in...
45
by: Curt Geske | last post by:
I'm suprised no one suggested a union! #include <stdio.h> union _x { long lng; char byt; } X; void main( void )
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
3
by: QQ | last post by:
Hello, Here is my simple program int main() { unsigned char a =0x81; char b = 0x81; printf("unsigned char = 0x%x(%d), char = 0x%x(%d)\n",a,a,b,b); printf("cast char to unsigned...
7
by: Guch Wu | last post by:
Array<unsigned char,2> A(10,10); unsigned char* rawdata; rawdata = new (unsinged char); Can I extract data in A to rawdata without using for loop?
3
by: roopa.v1 | last post by:
Hi, How to assign long to character array and later extract it
7
by: JoeC | last post by:
I am trying to create a windows program that reads binary graphics as a resource. This has nothing to do with win32 but conversion of data with memcpy. graphic::graphic(UINT uiResID, HINSTANCE...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.