473,669 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

30 New Member
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 *colorFileLocat ion = "C\\Images\\whi te.bmp";
colorSize = Load_Colorfile_ to_memory(color FileLocation, &cColorContent) ;

FILE *colorF;
colorF = fopen(colorFile Location, "rb");

BITMAPFILEHEADE R colorBmfh;
fread(&colorBmf h,sizeof(BITMAP FILEHEADER),1,c olorF);

BITMAPINFOHEADE R colorBmih;
fread(&colorBmi h,sizeof(BITMAP INFOHEADER),1,c olorF);

int colorBmWidth = colorBmih.biWid th;
int colorBmHeight = colorBmih.biHei ght;
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<colorBmHei ght;k++)
{
for (int i =0;i<colorBmWid th;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 7585
jkmyoung
2,057 Recognized Expert Top Contributor
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
KYAW KYAW OO
30 New Member
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 Recognized Expert Top Contributor
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
KYAW KYAW OO
30 New Member
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 Recognized Expert Top Contributor
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
KYAW KYAW OO
30 New Member
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<colorBmHei ght;k++)
{
for (int i =0;i<colorBmWid th;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
KYAW KYAW OO
30 New Member
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<colorBmHei ght;k++)
{
for (int i =0;i<colorBmWid th;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 Recognized Expert Top Contributor
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
6467
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 answer here? If a is converted to unsigned, then b>a. But, if b is converted to signed,then a>b. What's the correct coversion (what is the compiler supposed to do?)
10
2780
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 stream. The first variable packed into this array happens to be a short, meaning I get away with a simple assignment: myFirstVariable=myArray;
7
3597
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 example, consider this four-bit number: 1100 from this number I want to extract two numbers: 1000 and 100 had the four-bit number been 0101 I would want to extract 100 and 1. How should I do this? I wish I had some code to post but I don't...
44
12707
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 advance, Rita
45
3714
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
15647
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 standard doesn't even care what a normal char is (because signed and unsigned have equal behavior). For example if someone does this: unsigned char a = -2; /* or = 254 */
3
41928
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 0x%x\n",(unsigned char)b); }
7
2843
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
17407
by: roopa.v1 | last post by:
Hi, How to assign long to character array and later extract it
7
2107
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 hinstance){ size = 32; bitData.clear(); void * p = NULL; // point to the data int end; BYTE data;
0
8465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8383
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8894
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8803
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8587
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2029
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.