Thread: BITMAPINFOHEADER, bfType

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    BITMAPINFOHEADER, bfType

    Hey everybody! Just a quick question: I was looking up the struct BITMAPINFOHEADER on msdn, and it said that the member variable bfType "specifies the file type", and that "it must be BM".

    Also, in a sample prog from a book, it checked if bfType was equal to "0x4D42" (actually, it did a #define and used that instead), to "make sure that it is a bitmap file". I searched for the number on MSDN, and in the first result, the place where it has "0x4D42" is also checking whether or not it is a bitmap file; and on the right, it has a comment that says "BM".

    So my question is, what exactly is "BM"? And am I supposed to just memorize the number 0x4D42, or is there a #define for it?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    ..er..

    0x42 is 'B' and 0x4D is 'M' as in ANSI codes for these letters.

    ie char s='B';

    and

    char sh=0x42; //0x42=66 (decimal)

    are the same.

    How you define them is up to you: #define, const char or just use the id in place.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Oh That's somewhat simpler. So... could I do this:

    char s = ('B' << 1) + ('M');
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    No.

    #define ID_BMP 0x4D42

    or

    #define ID_BMP "BM"

    or

    const char ID_BMP[]="BM";

    or

    In the BITMAPINFOHEADER bfType, just use "BM"

    Any of these work just fine.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, gotcha But why couldn't I use the shifty way?..
    'B', then left-shift one byte over, and add 'M' (to the right). Wouldn't that do the same thing?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>Hunter2: char s = ('B' << 1) + ('M');<<

    ..try putting it into msvc and step through the code and see what garbage is produced...(you may also wish to try the examples I have given and watch the results too).

    In any event 'char s'? All that is doing is declaring 1 byte which you are then trying to stuff with information which has nothing to do with what you are after.

    "BM" is two bytes long...

    edit: Just checked it's BITMAPFILEHEADER structure, the bfType parameter is of type WORD so:

    const WORD wType=0x4D42; // "BM"

    is probably best.
    Last edited by Ken Fitlike; 09-07-2002 at 08:11 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    lol oops This got me confused: "ie char s='B';" But, uhh, I guess I'll just take your word for it and give up on the shift thing. Too lazy, really, to try it out. BTW, I tried "if((...).bfType != "BM")" and it didn't work... I had to go (int)"BM" instead. Is there anything wrong with that?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    See the 'edit' in my last post.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well... so I use a WORD, or just #define something. But my question was, is there anything wrong with "if((...).bfType == (int)"BM")"?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Ken Fitlike
    No.

    #define ID_BMP 0x4D42

    or

    #define ID_BMP "BM"

    or

    const char ID_BMP[]="BM";

    or

    In the BITMAPINFOHEADER bfType, just use "BM"

    Any of these work just fine.
    the first of these will cause problems on little endian systems.
    hello, internet!

  11. #11
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>the first of these will cause problems on little endian systems.<<

    Why?

    #define ID_BMP 0x4D42

    4D is 'M' and 42 is 'B' - or have I missed something?

    Anyway, as I later clarified:

    const WORD wType=0x4D42; // "BM"

    is probably best.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok whatever, I guess I'll just ditch the comparison to "BM"

    Oh wait...
    #define ID_BMP 0x4D42

    4D is 'M' and 42 is 'B' - or have I missed something?
    Wouldnt 4D42 be MB then?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>Wouldnt 4D42 be MB then?<<

    Try it and see for yourself. Also take a look at your original question and the example you were referring to.

    Also do a search on 'little endian' on google and also see for yourself.

    here's an older but surprisingly relevant discussion.

    >>"if((...).bfType == (int)"BM")"?<<

    There might be issues with UNICODE with this but i'm not really sure about that.

    Best to just use: const WORD wType=0x4D42; // "BM"

    and do your comparison with that.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  14. #14
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Ken Fitlike

    There might be issues with UNICODE with this but i'm not really sure about that.

    Best to just use: const WORD wType=0x4D42; // "BM"

    and do your comparison with that.
    Nah, I dont see any unicode problem.......Your just defining a WORD value from 2 BYTES....whether the BYTES are in charactor form or hexadecimal it means nothing when the compiler has changed it.....(which it will as the chars will not appear in the object code)

    If you did try to put the "BM" in a _T() macro then the conversion would be 2 WORDS = 1 DWORD.......not good for a WORD comparison in that case

  15. #15
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>Nah<<

    I realised this too late but thanks for clarifying, Fordy. Appreciated.

Popular pages Recent additions subscribe to a feed