Click here to Skip to main content
15,912,400 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: While loop not working on C. Pin
Richard MacCutchan8-Oct-17 1:45
mveRichard MacCutchan8-Oct-17 1:45 
GeneralRe: While loop not working on C. Pin
Tarun Jha8-Oct-17 3:36
Tarun Jha8-Oct-17 3:36 
GeneralRe: While loop not working on C. Pin
Richard MacCutchan8-Oct-17 6:15
mveRichard MacCutchan8-Oct-17 6:15 
GeneralRe: While loop not working on C. Pin
Tarun Jha8-Oct-17 6:24
Tarun Jha8-Oct-17 6:24 
GeneralRe: While loop not working on C. Pin
geodoom12-Oct-17 12:12
geodoom12-Oct-17 12:12 
GeneralRe: While loop not working on C. Pin
Tarun Jha13-Oct-17 5:40
Tarun Jha13-Oct-17 5:40 
AnswerRe: While loop not working on C. Pin
«_Superman_»8-Oct-17 17:59
professional«_Superman_»8-Oct-17 17:59 
GeneralRe: While loop not working on C. Pin
Tarun Jha10-Oct-17 5:24
Tarun Jha10-Oct-17 5:24 
AnswerRe: While loop not working on C. Pin
Tarun Jha11-Oct-17 7:15
Tarun Jha11-Oct-17 7:15 
SuggestionRe: While loop not working on C. Pin
David Crow11-Oct-17 7:46
David Crow11-Oct-17 7:46 
GeneralRe: While loop not working on C. Pin
geodoom13-Oct-17 7:24
geodoom13-Oct-17 7:24 
GeneralRe: While loop not working on C. Pin
Tarun Jha13-Oct-17 7:26
Tarun Jha13-Oct-17 7:26 
AnswerRe: While loop not working on C. Pin
Manish K. Agarwal12-Oct-17 4:33
Manish K. Agarwal12-Oct-17 4:33 
GeneralRe: While loop not working on C. Pin
Tarun Jha13-Oct-17 5:46
Tarun Jha13-Oct-17 5:46 
GeneralRe: While loop not working on C. Pin
David Crow13-Oct-17 16:13
David Crow13-Oct-17 16:13 
AnswerRe: While loop not working on C. Pin
asa7612-Oct-17 6:33
asa7612-Oct-17 6:33 
QuestionRe: While loop not working on C. Pin
David Crow13-Oct-17 2:40
David Crow13-Oct-17 2:40 
QuestionCTreeObject class Pin
_Flaviu3-Oct-17 22:59
_Flaviu3-Oct-17 22:59 
AnswerRe: CTreeObject class Pin
«_Superman_»3-Oct-17 23:07
professional«_Superman_»3-Oct-17 23:07 
GeneralRe: CTreeObject class Pin
_Flaviu5-Oct-17 21:38
_Flaviu5-Oct-17 21:38 
QuestionCImage processing Pin
rbrunton3-Oct-17 2:03
rbrunton3-Oct-17 2:03 
GeneralRe: CImage processing Pin
Richard MacCutchan3-Oct-17 7:53
mveRichard MacCutchan3-Oct-17 7:53 
Questionraytracer Pin
bluatigro29-Sep-17 3:36
bluatigro29-Sep-17 3:36 
QuestionRe: raytracer Pin
David Crow29-Sep-17 4:21
David Crow29-Sep-17 4:21 
AnswerRe: raytracer Pin
Jochen Arndt29-Sep-17 4:24
professionalJochen Arndt29-Sep-17 4:24 
The first thing to know is that the bytes per pixel line must be a 32-bit multiple. That means, that you have to allocate additional space for the fill bytes if necessary when preparing a full buffer or write data line by line. So you have to calculate the bytes per line first (here for 24 bits per pixel):
unsigned nWidthBytes = ((screenx * 24 + 31) & ~31) / 8;
// EDIT: Corrected
//unsigned nExtra = 8 * ((screenx * 24) % 32);
unsigned nExtra = nWidthBytes - 3 * screenx;

This can the be used to calculate the total size of pixel data and the number of extra bytes pewr line:
unsigned nPixelSize = nWidthBytes * screeny;
Note also that you should re-organise your pixel data for this (exchange x and y):
PIXEL pixel[ screeny ][ screenx ] ;


For the other fields use these (untested):
// File header
FileHdr.bfType = 0x4D42; // "BM"
FileHdr.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD) + nPixelSize;
FileHdr.bfReserved1 = pBmHdr.bfReserved2 = 0;
// EDIT: Corrected
//FileHdr.bfOffBits = sizeof(BITMAPFILEHEADER);
FileHdr.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD);

// Info header for 24-bit RGB
InfoHdr.biSize = sizeof(BITMAPINFOHEADER);
InfoHdr.biWidth = screenx;
InfoHdr.biHeight = screeny;
InfoHdr.biPlanes = 1;
InfoHdr.biBitCount = 24;
InfoHdr.biCompression = BI_BITFIELDS;
InfoHdr.biSizeImage = nPixelSize;
InfoHdr.biXPelsPerMeter = 0;
InfoHdr.biYPelsPerMeter = 0;
InfoHdr.biClrUsed = 0;
InfoHdr.biClrImportant = 0;

// Color table (bit masks for the three colours) with BI_BITFIELDS
// NOTE: I'm actually not sure if these must be reversed!
DWORD clrTable[3] = { 0xff, 0xff00, 0xff0000 };

fwrite(&FileHdr, 1, sizeof(BITMATFILEHEADER), file);
fwrite(&InfoHdr, 1, sizeof(BITMATINFOHEADER), file);
fwrite(clrTable, 1, sizeof(clrTable), file);
for (int y = 0; y < screeny; y++)
{
    // EDIT: Corrected
    //fwrite(pixel[y], 3, nWidthBytes, file);
    fwrite(pixel[y], 3, screenx, file);
    if (nExtra)
    {
        DWORD dummy = 0;
        fwrite(&dummy, 1, nExtra, file);
    }
}


modified 1-Oct-17 4:13am.

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.