GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Sep-2009, 11:01
Zexion Zexion is offline
New Member
 
Join Date: Sep 2009
Posts: 1
Zexion is on a distinguished road

Help with bitmap conversion/header in C


So, here's the deal. I have a school project, part of which is being able to construct your own bitmaps, including header a all, straight through binary write to a file.

Our first assignment is to read and flatten a 512x512 24-bit Lena into a 1-dimensional char array of appropiate length, in the process converting the RGB coordinates for the colors into 8-bit grayscale.

My group gets through reading the file just fine, into a format that seems fine, judging by watching the array while debugging.

Then we get to writing to a new .bmp-file, and all goes to hell. Yes, it does turn into a readable bitmap format, but it looks pretty darn funky. Just take a look at the attachment. Source code is also included. But after looking at it for almost 3-4 hours, and having lab assistants try to help us, no luck.

So, any of you who could possibly help?
I haven't really been able to isolate the problem, but I imagine it has something to do with the header.

Also, I use NetBeans as IDE, Cygwin as compiler.



Thanks in advance.

EDIT: Darn it, didn't know photobucket compressed the images. Anyways, you can see what the product looks like, to get a general idea of what is wrong, perhaps. If you require the original bmp to see the binary of the header, I will of course provide that.
Attached Files
File Type: zip bmp.zip (2.3 KB, 15 views)
  #2  
Old 09-Sep-2009, 13:53
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,309
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help with bitmap conversion/header in C


Quote:
Originally Posted by Zexion
...

Our first assignment is to read and flatten a 512x512 24-bit...
My group gets through reading the file just fine, into a format that seems fine...

I don't think so. Maybe you are seeing what you expect in the debugger, but that isn't correct. (In other words, I think the program is doing exactly what you are telling it to do, but what you are telling it is incorrect.)

In a 24-bit color bitmap file, there are three bytes per pixel, not four. Maybe you were confused by the fact that the color table for an 8-bit bitmap file has four bytes for each color value (three color values and a padding byte for each color). You got that part right in the bmp_save() function.

Anyhow...

CPP / C++ / C Code:
    for (i = 0; i < bmih.BiWidth * bmih.BiHeight; i++) {
        fread(&rgbtri.rgbBlue, 1, 1, fp);
        fread(&rgbtri.rgbGreen, 1, 1, fp);
        fread(&rgbtri.rgbRed, 1, 1, fp);
        /*fread(&dump, 1, 1, fp); */ /*<--- No! */

That might give you something better to look at. But: See Footnote.

I think that it is always a good idea to make sure that file I/O operations are successful. (Test the return value of fread to make sure the read was successful. You might have discovered that it failed inside the loop since you were actually trying to read too many bytes. You could have seen that as a clue.)

Regards,

Dave

Footnote: Note that the pixel data elements in the file are unsigned chars: 0x00 = darkest, 0xff = brightest.
Last edited by davekw7x : 09-Sep-2009 at 14:46.
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert a bitmap file to an icon file fakepoo .NET Forum 1 19-May-2009 14:21
MFC send Bitmap from callbackfct to OnDraw PoPo93 MS Visual C++ / MFC Forum 0 17-Jan-2008 09:12
Query regarding BITMAP refresh..!! vab MS Visual C++ / MFC Forum 1 10-Jul-2006 03:58
Two Dialog projects using same bitmap file Dominic MS Visual C++ / MFC Forum 3 03-Jul-2006 08:51
How to store and re-use a bitmap Dominic MS Visual C++ / MFC Forum 0 23-Mar-2006 03:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:35.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.