Click here to Skip to main content
15,898,987 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Reading data of a monochrome BMP file Pin
Member 211673611-Feb-09 4:33
Member 211673611-Feb-09 4:33 
AnswerRe: Reading data of a monochrome BMP file Pin
CPallini10-Feb-09 22:04
mveCPallini10-Feb-09 22:04 
GeneralRe: Reading data of a monochrome BMP file Pin
Member 211673610-Feb-09 22:23
Member 211673610-Feb-09 22:23 
GeneralRe: Reading data of a monochrome BMP file Pin
CPallini10-Feb-09 22:59
mveCPallini10-Feb-09 22:59 
GeneralRe: Reading data of a monochrome BMP file Pin
Member 211673611-Feb-09 4:35
Member 211673611-Feb-09 4:35 
GeneralRe: Reading data of a monochrome BMP file Pin
CPallini11-Feb-09 7:51
mveCPallini11-Feb-09 7:51 
GeneralRe: Reading data of a monochrome BMP file Pin
Member 211673611-Feb-09 14:57
Member 211673611-Feb-09 14:57 
GeneralRe: Reading data of a monochrome BMP file Pin
CPallini11-Feb-09 22:06
mveCPallini11-Feb-09 22:06 
Here again you're doing a mistake on computing the memory required.
For a monochrome bitmap, the size required is bit more elaborate than
(Width * Height), since the file holds 8 pixels in each byte, and the horizontal line is padded (if I remember well) to have a 32-bit multiple length. Hence
int bytesPerLine = Width / 8; // eight pixels per byte
if (Width % 8) bytesPerLine++; // take into account the reminder

// pad the line to make it to a 32-bit multiple
if (bytesPerLine % 4 ) bytesPerLine += 4 - (bytesPerLine % 4);

// finally you get the size
int iSize = bytesPerLine * Height;


I hope the above is correct (I haven't tested it). You may verify it comparing the result with the actual size of the file minus the bfOffBits offset.

Two final notes:

  • If you allocate memory of a given size don't trust any other counter (you're using EOF) while filling it: check always you're not exceeding the allocated size (in your code try to read until the allocated buffer is filled and not until the end of the file: if the file contains more bytes your application crashes).
  • If you need to read a block, the read it directly, don't read a byte at time.
  • Smile | :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
    -- Alfonso the Wise, 13th Century King of Castile.

    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
    -- Iain Clarke

    [My articles]

GeneralRe: Reading data of a monochrome BMP file Pin
Member 211673611-Feb-09 22:13
Member 211673611-Feb-09 22:13 
QuestionUsing CDialog::OnOK() Pin
Babu@codeproject10-Feb-09 18:51
Babu@codeproject10-Feb-09 18:51 
AnswerRe: Using CDialog::OnOK() Pin
Eytukan10-Feb-09 19:10
Eytukan10-Feb-09 19:10 
AnswerRe: Using CDialog::OnOK() Pin
SandipG 10-Feb-09 19:42
SandipG 10-Feb-09 19:42 
Question[Message Deleted] Pin
Purish Dwivedi10-Feb-09 17:39
Purish Dwivedi10-Feb-09 17:39 
AnswerRe: Sample multilingual application with full steps using vc++ in visual studio 2005 Pin
Sarath C10-Feb-09 18:51
Sarath C10-Feb-09 18:51 
AnswerREPOST Pin
_AnsHUMAN_ 10-Feb-09 19:32
_AnsHUMAN_ 10-Feb-09 19:32 
Answer2nd Repost.. Please Ignore. Pin
SandipG 10-Feb-09 19:49
SandipG 10-Feb-09 19:49 
General[Message Deleted] Pin
Purish Dwivedi10-Feb-09 21:54
Purish Dwivedi10-Feb-09 21:54 
RantPlease don't be rude Pin
CPallini10-Feb-09 22:26
mveCPallini10-Feb-09 22:26 
General[Message Deleted] Pin
Purish Dwivedi10-Feb-09 22:58
Purish Dwivedi10-Feb-09 22:58 
GeneralRe: Please don't be rude Pin
CPallini10-Feb-09 23:05
mveCPallini10-Feb-09 23:05 
Questioncreating app buttons Pin
uzziah010-Feb-09 16:09
uzziah010-Feb-09 16:09 
Questiondisable end program notification in console application Pin
Arif Liminto10-Feb-09 14:53
professionalArif Liminto10-Feb-09 14:53 
AnswerRe: disable end program notification in console application Pin
Eytukan10-Feb-09 16:18
Eytukan10-Feb-09 16:18 
GeneralRe: disable end program notification in console application Pin
Arif Liminto10-Feb-09 16:27
professionalArif Liminto10-Feb-09 16:27 
GeneralRe: disable end program notification in console application Pin
Eytukan10-Feb-09 16:30
Eytukan10-Feb-09 16:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.