Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-08-2004, 06:29 AM   PM User | #1
ahLi
New to the CF scene

 
Join Date: Feb 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
ahLi is an unknown quantity at this point
writing a bitmap file

hi. i tried to output a bitmap file but it seems to be invalid and hence, i can't open it.

I've assigned values to the BITMAPINFOHEADER and colour table. Using WriteFile method, I wrote the above BITMAPINFO into a bitmap file.

The raw pixel image is in a char array:
unsigned char ByteImage [200*152];

Do you know how to write the data in this ByteImage into the bitmap data part of the bitmap file?

I tried something like:
WriteFile(hFile, ByteImage, sizeof(ByteImage), &dwBytesWritten, NULL);

the bitmap file is created but it can't be opened for viewing, saying that the file is not a valid bitmap format. anybody knows what went wrong?

thanks alot
ahLi is offline   Reply With Quote
Old 03-09-2004, 12:17 AM   PM User | #2
sad69
Senior Coder

 
Join Date: Feb 2004
Posts: 1,206
Thanks: 0
Thanked 0 Times in 0 Posts
sad69 is an unknown quantity at this point
I don't know what you're talking about!

You'll have to explain what language, platform, etc.. that you're working with.

Sadiq.
sad69 is offline   Reply With Quote
Old 03-09-2004, 02:15 AM   PM User | #3
ahLi
New to the CF scene

 
Join Date: Feb 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
ahLi is an unknown quantity at this point
Lanuage and platform

I'm using C++ language and working on Windows NT platform.

I'm trying to create a bitmap file. I've already initialised the properties needed in the BITMAPINFO structure.

For example,
m_DIB->bmiHeader.biWidth = 152;
m_DIB->bmiHeader.biHeight = 200;
etc...

Next, I need to write this BITMAPINFO structure into a .bmp file.

WriteFile(hFile, m_DIB, sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD), &dwBytesWritten, NULL);

Now, I need to read the bitmap data into the .bmp file.

unsigned char ByteImage [200*152];
memcpy(ByteImage,birData->BiometricData+24,152*200);
dwBytesToWrite = sizeof(ByteImage);
WriteFile(hFile, ByteImage, dwBytesToWrite, &dwBytesWritten, NULL);

After doing this, the .bmp file is created. However, when I want to open and view it, this error message appears:
"Paint cannot read this file. This is not a valid bitmap file, or its format is not currently supported"

Do you have any idea what went wrong?
ahLi is offline   Reply With Quote
Old 03-10-2004, 08:12 AM   PM User | #4
Mhtml
Senior Coder

 
Mhtml's Avatar
 
Join Date: Jun 2002
Location: Sydney, Australia
Posts: 3,531
Thanks: 0
Thanked 1 Time in 1 Post
Mhtml is an unknown quantity at this point
Why are you using a char array?? You should be using a BYTE array. I don't know if this will make a difference but it might.

I think .bmp should also be written as BGR not RGB so you'll have to swap every 3rd byte with byte[n-2] ...

It's a little hard to see the whole problem without seeing a little more code though.
__________________
Omnis mico antequam dominus Spookster!
Mhtml is offline   Reply With Quote
Old 03-10-2004, 08:19 AM   PM User | #5
Mhtml
Senior Coder

 
Mhtml's Avatar
 
Join Date: Jun 2002
Location: Sydney, Australia
Posts: 3,531
Thanks: 0
Thanked 1 Time in 1 Post
Mhtml is an unknown quantity at this point
And the size of the array should be width*height*depth.
__________________
Omnis mico antequam dominus Spookster!
Mhtml is offline   Reply With Quote
Old 03-12-2004, 02:44 AM   PM User | #6
ahLi
New to the CF scene

 
Join Date: Feb 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
ahLi is an unknown quantity at this point
I've managed to output a valid bitmap file now but its the image is distorted...

What do you mean by swaping every 3rd byte with [n-2]byte?
How do I actually go about doing that?
ahLi is offline   Reply With Quote
Old 03-13-2004, 11:15 AM   PM User | #7
Mhtml
Senior Coder

 
Mhtml's Avatar
 
Join Date: Jun 2002
Location: Sydney, Australia
Posts: 3,531
Thanks: 0
Thanked 1 Time in 1 Post
Mhtml is an unknown quantity at this point
Well, you need to do a loop to swap every 3rd byte with the one 2 bytes before it. Which will convert it from RGB to BGR.

Like --

Code:
BYTE tmp;
for ( int i = 3; i < array_size; i+=3 ) {
  
  tmp = bmp[i-2];
  bmp[i-2] = bmp[i];
  bmp[i] = tmp;

}
Or you can use memmove()...

I'm really sick right now so my thinking may be a little off, hopefully that code is actually right ..


[edit:] Assuming I'm actually right ( are the colors wrong on the bitmap? Inverted? ) ...

Each pixel has a color index right, each color is an RGB value.

Bitmaps should be in the BGR format I think, at least I remember when I wrote a bitmap loader for opengl textures that it had to be transformed from BGR to RGB to use it. Which is why I believe it needs to be save in BGR...

Anyways, the BYTE array is really just and array of colors. .. So for example -

BYTE[0] = Red value
BYTE[1] = Green value
BYTE[2] = Blue value

RGB!

But, assuming my memory serves me correctly and you need to swap these bytes every set of 3 in the array would be in the format BGR

BYTE[0] = Blue value
BYTE[1] = Green value
BYTE[2] = Red value

And it just repeats like that, with BYTE[3] being blue value and BYTE[5] being red value and so on..

I hope I haven't confused you. This better be right or I'll look like a fool..
__________________
Omnis mico antequam dominus Spookster!

Last edited by Mhtml; 03-13-2004 at 11:26 AM..
Mhtml is offline   Reply With Quote
Old 03-13-2004, 11:27 AM   PM User | #8
Mhtml
Senior Coder

 
Mhtml's Avatar
 
Join Date: Jun 2002
Location: Sydney, Australia
Posts: 3,531
Thanks: 0
Thanked 1 Time in 1 Post
Mhtml is an unknown quantity at this point
Post your code if you still have some problems.
__________________
Omnis mico antequam dominus Spookster!
Mhtml is offline   Reply With Quote
Old 03-16-2004, 05:38 AM   PM User | #9
ahLi
New to the CF scene

 
Join Date: Feb 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
ahLi is an unknown quantity at this point
I'm trying to create a 24bit uncompressed bitmap file. the following codes gave me distorted bitmap image and appears to have 4 distinct sections.


// Put image data in ByteImage
memcpy(ByteImage,birData-> BiometricData + 24, 152*200);

// Bitmap file header
BITMAPFILEHEADER *m_header = (BITMAPFILEHEADER *) malloc(sizeof(BITMAPFILEHEADER));
m_header->bfType = 'MB';
m_header->bfSize = 0;
m_header->bfReserved1 = 0;
m_header->bfReserved2 = 0;
m_header->bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

// Bitmap infoheader
BITMAPINFOHEADER *m_DIB = (BITMAPINFOHEADER *) malloc(sizeof(BITMAPINFOHEADER));
m_DIB->biSize = sizeof (BITMAPINFOHEADER);
m_DIB->biWidth = 152;
m_DIB->biHeight = 200;
m_DIB->biPlanes = 1;
m_DIB->biBitCount = 24;
m_DIB->biCompression = BI_RGB; m_DIB->biSizeImage = 152*200*3;
m_DIB->biXPelsPerMeter = 0;
m_DIB->biYPelsPerMeter = 0;
m_DIB->biClrUsed = 0;
m_DIB->biClrImportant = 0;

unsigned char tmp;
for (int j = 3; j < 152*200*3; j+=3)
{
tmp = ByteImage[j-2];
ByteImage[j-2] = ByteImage[j];
ByteImage[j] = tmp;
}

// Open the file
wsprintf(szFileName, TEXT("c:\\Templates\\%s.bmp"), lptszUserName);
hFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return(0);

// Write BITMAPFILEHEADER
WriteFile(hFile, m_header, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);

// Write BITMAPINFOHEADER
WriteFile(hFile, m_DIB, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);

// Write bitmap data
WriteFile(hFile, ByteImage, sizeof(ByteImage), &dwBytesWritten, NULL);

// Close the file
CloseHandle(hFile);


I've read that behind each scan line there's some "junk bytes" appended to it. Could it be that these "junk bytes" that caused the distorted image?
ahLi is offline   Reply With Quote
Old 01-02-2007, 09:49 AM   PM User | #10
s.basu9
New to the CF scene

 
Join Date: Jan 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
s.basu9 is an unknown quantity at this point
Smile junk bytes in 24-bit bitmap

Yes the distortion is just bcoz of junk bytes. You have to take care of junk bytes while reading or writting a 24-bit bitmap image. Since in each scan line the number of bytes should b multiple of 4, there may be some extra bytes. At the time of reading u must jump over those bytes in each scan line. And while creating a 24-bit bitmap u've to write the junk bytes into the output file.
To find out junk bytes in each scan line u can try the following line of code in c :--
int junkBytes=(fileSize - (imageSize*3 + 54)/width) / height;
where fileSize = total disk size of the file
imageSize = width * height;
another thing u must notice --- in ur code u've written "MB" as BMP identifier, but it should be "BM".

Last edited by s.basu9; 01-02-2007 at 10:04 AM.. Reason: bmp identifier
s.basu9 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:09 PM.


Advertisement
Log in to turn off these ads.