Storing resources into a single file

This is a discussion on Storing resources into a single file within the Game Programming forums, part of the General Programming Boards category; I think one typically uses DIB_RGB_COLORS for a 24, 16 or 32bpp image. Have you tried it? Does your file ...

  1. #16
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I think one typically uses DIB_RGB_COLORS for a 24, 16 or 32bpp image. Have you tried it?

    Does your file mapping meet this requirement?
    Quote Originally Posted by MSDN
    If hSection is not NULL, it must be a handle to a file-mapping object created by calling the CreateFileMapping function with the PAGE_READWRITE or PAGE_WRITECOPY flag. Read-only DIB sections are not supported. Handles created by other means will cause CreateDIBSection to fail.
    [edit]Who says that you can pass NULL for ppvBits?[/edit]
    [edit2]Got to take into account BITMAPFILEHEADER which is before BITMAPINFO in file.[/edit]
    Last edited by anonytmouse; 08-14-2004 at 11:12 PM.

  2. #17
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    I read that on MSDN as well and did that. I'm calling CreateFile() with GENERIC_READ | GENERIC_WRITE, CreateFileMapping() with PAGE_READWRITE, and MapViewOfFile() with FILE_MAP_READ | FILE_MAP_WRITE.

    >>[edit]Who says that you can pass NULL for ppvBits?[/edit]
    I just realized that MSDN said nothing about it, so I passed an arg to it.

    >>[edit2]Got to take into account BITMAPFILEHEADER which is before BITMAPINFO in file.[/edit]
    I'm doing this now:
    Code:
        LPBITMAPFILEHEADER pBFH = (LPBITMAPFILEHEADER)m_pMappedFile;
        LPBITMAPINFOHEADER lpBMIH = LPBITMAPINFOHEADER(LPBYTE(m_pMappedFile) + sizeof(BITMAPFILEHEADER));
        DWORD dwOffset = pBFH->bfOffBits;
        LPVOID pBits;
    
        HDC dc = CreateCompatibleDC(NULL);
        m_hBmp = CreateDIBSection(dc, (LPBITMAPINFO)lpBMIH, DIB_RGB_COLORS, &pBits, m_hFileMapping, dwOffset);
    But it is still returning NULL but with GetLastError() returning success... What am I doing wrong?

    I do appreciate all the help. This one thorn in my eye is a killer. I can't continue with my project until I resolve this issue.

    [edit]
    >> You want DIB_PAL_COLORS ? I'll assume yes.
    I've been going back and forth just trying to see if anything would change. It hasn't. It seems to me that I'm doing things properly, but apparently I'm not. What can cause GetLastError() to return success upon failure?
    [/edit]
    Last edited by LuckY; 08-14-2004 at 11:32 PM.

  3. #18
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Do you check with a debugger that the values pointed to by pBFH and lpBMIH are correct? You should really do that.

    Also make sure dc is valid.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #19
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    OK, you want the good news or the bad news?

    The good news:
    Change:
    Code:
        m_hBmp = CreateDIBSection(dc, (LPBITMAPINFO)lpBMIH, DIB_RGB_COLORS, &pBits, m_hFileMapping, dwOffset);
    to:
    Code:
        m_hBmp = CreateDIBSection(dc, (LPBITMAPINFO)lpBMIH, DIB_RGB_COLORS, &pBits, m_hFileMapping, 52);
    and it will "work". This is to do with DWORD alignment. I'll post back in a minute.

  5. #20
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    And now the bad news...

    The problem is that the standard bitmap header, without extra color information, is 54 bytes. However:
    Quote Originally Posted by MSDN
    The bitmap bit values are aligned on doubleword boundaries, so dwOffset must be a multiple of the size of a DWORD.
    Unfortunately, the value 54 is not DWORD aligned (a multiple of 4). Therefore, the CreateDibSection and file mapping solution does not work with most bitmap images!

    You can find more information on this issue:
    http://groups.google.com/groups?q=CreateDibSection%2054
    http://www.eggheadcafe.com/ng/micros...post309964.asp

    There doesn't seem to be a simple elegant solution but some ideas:

    - Use an incorrect offset like I did in the previous post. Your image will be a couple of bytes off, but the user might just think that their monitor is shoddy!

    - Patch the input bitmaps before distributing with your program. This is not a bad solution, especially since you are already working with the input bitmap when you add it to your "resource file". You could change the offset to 56.

    - Ditch the file mapping with CreateDibSection. Just create a normal dib section and copy the bits into it from the file mapping. This is a little less efficient but requires no change to the input bitmap.

    Out of curiosity, let me know what solution you go with.

  6. #21
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Holy cow. You are awesome anontymouse. I just started thinking about the DWORD alignment after rereading MSDN for the nth time. That is a beautiful thing. Now that it's actually loading...

    I changed the assignment of dwOffset to:
    Code:
    DWORD dwOffset = pBFH->bfOffBits - (pBFH->bfOffBits % sizeof(DWORD));
    Son of a gun, who'da thunk it would be that simple? Thanks a lot anontymouse and MrWizard. Finally the aspirin for my headache...

    [edit]
    As for the approach I think I'll be taking, I think padding the bitmap header info is what I'll go with since (as you mentioned) the resources in my file will be made for a specific purpose. Again, thanks.
    [/edit]

    [edit2]
    By the way, I love that first solution "the user might just think that their monitor is shoddy!" Heheh.
    [/edit2]
    Last edited by LuckY; 08-15-2004 at 01:59 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 03:29 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21