Comparing files cdib.cpp and NEW_CDIB.CPP
***** cdib.cpp

BOOL CDib::Draw(CDC* pDC, CPoint origin, CSize size)
{
***** NEW_CDIB.CPP

BOOL CDib::Draw(CDC* pDC, CPoint origin, CSize size, int BltMode)
{
*****

***** cdib.cpp
        }
        pDC->SetStretchBltMode(COLORONCOLOR);
        if (::StretchDIBits(pDC->GetSafeHdc(), origin.x, origin.y, size.cx, size.cy,
***** NEW_CDIB.CPP
        }
        pDC->SetStretchBltMode(BltMode);
        if (::StretchDIBits(pDC->GetSafeHdc(), origin.x, origin.y, size.cx, size.cy,
*****

***** cdib.cpp

BOOL CDib::DrawPart(CDC* pDC, CPoint origin, CSize size, CPoint iorigin, CSize isize)
{
***** NEW_CDIB.CPP

BOOL CDib::DrawPart(CDC* pDC, CPoint origin, CSize size, CPoint iorigin, CSize isize, int BltMode)
{
*****

***** cdib.cpp
        }
        pDC->SetStretchBltMode(COLORONCOLOR);
        if (::StretchDIBits(pDC->GetSafeHdc(), origin.x, origin.y, size.cx, size.cy,
***** NEW_CDIB.CPP
        }
        pDC->SetStretchBltMode(BltMode);
        if (isize.cx + isize.cy < 512) {
                // switch to halftone when images are small...better quality, less pixelation
                SetBrushOrgEx(pDC->GetSafeHdc(), 0, 0, NULL);
        }
        if (::StretchDIBits(pDC->GetSafeHdc(), origin.x, origin.y, size.cx, size.cy,
*****

***** cdib.cpp

BOOL CDib::ReadHeader(CFile* pFile)
{
***** NEW_CDIB.CPP

BOOL CDib::Read(CFile* pFile)
{
*****

***** cdib.cpp
        // 2. read info hdr (to get image size) and color table
        // can't use bfSize in file header
***** NEW_CDIB.CPP
        // 2. read info hdr (to get image size) and color table
        // 3. read image
        // can't use bfSize in file header
*****

***** cdib.cpp
                ComputePaletteSize(m_lpBMIH->biBitCount);
                m_lpImage = NULL;
        }
***** NEW_CDIB.CPP
                ComputePaletteSize(m_lpBMIH->biBitCount);
                MakePalette();
                m_lpImage = (LPBYTE) new char[m_dwSizeImage];
                nCount = pFile->Read(m_lpImage, m_dwSizeImage); // image only
        }
*****

***** cdib.cpp
        catch(CException* pe) {
                AfxMessageBox("Read error while reading header");
                pe->Delete();
***** NEW_CDIB.CPP
        catch(CException* pe) {
                AfxMessageBox("Read error");
                pe->Delete();
*****

***** cdib.cpp

BOOL CDib::Read(CFile* pFile)
{
        // 1. read file header to get size of info hdr + color table
***** NEW_CDIB.CPP

BOOL CDib::ReadSection(CFile* pFile, CDC* pDC /* = NULL */)
{
        // new function reads BMP from disk and creates a DIB section
        //    allows modification of bitmaps from disk
        // 1. read file header to get size of info hdr + color table
*****

***** cdib.cpp
        // 2. read info hdr (to get image size) and color table
        // 3. read image
        // can't use bfSize in file header
        Empty();
***** NEW_CDIB.CPP
        // 2. read info hdr (to get image size) and color table
        // 3. create DIB section based on header parms
        // 4. read image into memory that CreateDibSection allocates
        Empty();
*****

***** cdib.cpp
                m_lpBMIH = (LPBITMAPINFOHEADER) new char[nSize];
                m_nBmihAlloc = m_nImageAlloc = crtAlloc;
                nCount = pFile->Read(m_lpBMIH, nSize); // info hdr & color table
                ComputeMetrics();
***** NEW_CDIB.CPP
                m_lpBMIH = (LPBITMAPINFOHEADER) new char[nSize];
                m_nBmihAlloc = crtAlloc;
                m_nImageAlloc = noAlloc;
                nCount = pFile->Read(m_lpBMIH, nSize); // info hdr & color table
                if(m_lpBMIH->biCompression != BI_RGB) {
                        throw new CException;
                }
                ComputeMetrics();
*****

***** cdib.cpp
                MakePalette();
                m_lpImage = (LPBYTE) new char[m_dwSizeImage];
                nCount = pFile->Read(m_lpImage, m_dwSizeImage); // image only
***** NEW_CDIB.CPP
                MakePalette();
                UsePalette(pDC);
                m_hBitmap = ::CreateDIBSection(pDC->GetSafeHdc(), (LPBITMAPINFO) m_lpBMIH,
                        DIB_RGB_COLORS, (LPVOID*) &m_lpImage, NULL, 0);
                ASSERT(m_lpImage != NULL);
                nCount = pFile->Read(m_lpImage, m_dwSizeImage); // image only
*****

***** cdib.cpp
        catch(CException* pe) {
                AfxMessageBox("Read error");
                pe->Delete();
***** NEW_CDIB.CPP
        catch(CException* pe) {
                AfxMessageBox("ReadSection error");
                pe->Delete();
*****

***** cdib.cpp

BOOL CDib::ReadSection(CFile* pFile, CDC* pDC /* = NULL */)
{
        // new function reads BMP from disk and creates a DIB section
        //    allows modification of bitmaps from disk
        // 1. read file header to get size of info hdr + color table
        // 2. read info hdr (to get image size) and color table
        // 3. create DIB section based on header parms
        // 4. read image into memory that CreateDibSection allocates
        Empty();
        int nCount, nSize;
        BITMAPFILEHEADER bmfh;
        try {
                nCount = pFile->Read((LPVOID) &bmfh, sizeof(BITMAPFILEHEADER));
                if(nCount != sizeof(BITMAPFILEHEADER)) {
                        throw new CException;
                }
                if(bmfh.bfType != 0x4d42) {
                        throw new CException;
                }
                nSize = bmfh.bfOffBits - sizeof(BITMAPFILEHEADER);
                m_lpBMIH = (LPBITMAPINFOHEADER) new char[nSize];
                m_nBmihAlloc = crtAlloc;
                m_nImageAlloc = noAlloc;
                nCount = pFile->Read(m_lpBMIH, nSize); // info hdr & color table
                if(m_lpBMIH->biCompression != BI_RGB) {
                        throw new CException;
                }
                ComputeMetrics();
                ComputePaletteSize(m_lpBMIH->biBitCount);
                MakePalette();
                UsePalette(pDC);
                m_hBitmap = ::CreateDIBSection(pDC->GetSafeHdc(), (LPBITMAPINFO) m_lpBMIH,
                        DIB_RGB_COLORS, (LPVOID*) &m_lpImage, NULL, 0);
                ASSERT(m_lpImage != NULL);
                nCount = pFile->Read(m_lpImage, m_dwSizeImage); // image only
        }
***** NEW_CDIB.CPP

BOOL CDib::Write(CFile* pFile)
{
        BITMAPFILEHEADER bmfh;
        bmfh.bfType = 0x4d42;  // 'BM'
        int nSizeHdr = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_nColorTableEntries;
        bmfh.bfSize = 0;
//      bmfh.bfSize = sizeof(BITMAPFILEHEADER) + nSizeHdr + m_dwSizeImage;
        // meaning of bfSize open to interpretation (bytes, words, dwords?) -- we won't use it
        bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
        bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
                        sizeof(RGBQUAD) * m_nColorTableEntries; 
        try {
                pFile->Write((LPVOID) &bmfh, sizeof(BITMAPFILEHEADER));
                pFile->Write((LPVOID) m_lpBMIH,  nSizeHdr);
                pFile->Write((LPVOID) m_lpImage, m_dwSizeImage);
        }
*****

***** cdib.cpp
        catch(CException* pe) {
                AfxMessageBox("ReadSection error");
                pe->Delete();
                return FALSE;
***** NEW_CDIB.CPP
        catch(CException* pe) {
                pe->Delete();
//              AfxMessageBox("write error");
                return FALSE;
*****

***** cdib.cpp

BOOL CDib::Write(CFile* pFile)
{
        BITMAPFILEHEADER bmfh;
        bmfh.bfType = 0x4d42;  // 'BM'
        int nSizeHdr = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_nColorTableEntries;
        bmfh.bfSize = 0;
//      bmfh.bfSize = sizeof(BITMAPFILEHEADER) + nSizeHdr + m_dwSizeImage;
        // meaning of bfSize open to interpretation (bytes, words, dwords?) -- we won't use it
        bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
        bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
                        sizeof(RGBQUAD) * m_nColorTableEntries; 
        try {
                pFile->Write((LPVOID) &bmfh, sizeof(BITMAPFILEHEADER));
                pFile->Write((LPVOID) m_lpBMIH,  nSizeHdr);
                pFile->Write((LPVOID) m_lpImage, m_dwSizeImage);
        }
        catch(CException* pe) {
                pe->Delete();
//              AfxMessageBox("write error");
                return FALSE;
        }
        return TRUE;
}
***** NEW_CDIB.CPP

void CDib::Serialize(CArchive& ar)
{
        DWORD dwPos;
        dwPos = ar.GetFile()->GetPosition();
//      TRACE("CDib::Serialize -- pos = %d\n", dwPos);
        ar.Flush();
        dwPos = ar.GetFile()->GetPosition();
//      TRACE("CDib::Serialize -- pos = %d\n", dwPos);
        if(ar.IsStoring()) {
                Write(ar.GetFile());
        }
        else {
                Read(ar.GetFile());
        }
}
*****

***** cdib.cpp

void CDib::Serialize(CArchive& ar)
{
        DWORD dwPos;
        dwPos = ar.GetFile()->GetPosition();
//      TRACE("CDib::Serialize -- pos = %d\n", dwPos);
        ar.Flush();
        dwPos = ar.GetFile()->GetPosition();
//      TRACE("CDib::Serialize -- pos = %d\n", dwPos);
        if(ar.IsStoring()) {
                Write(ar.GetFile());
        }
        else {
                Read(ar.GetFile());
        }
}

// helper functions
***** NEW_CDIB.CPP

// helper functions
*****

***** cdib.cpp
        int rrow = row;
        int pixoffset = 0;
        WORD val;
***** NEW_CDIB.CPP
        int rrow = row;
        int pixoffset;
        WORD val;
*****

***** cdib.cpp

void CDib::SetImagePixel(int col, int row, COLORREF color, BYTE alpha)
{
        BYTE r = GetRValue(color);
        BYTE g = GetGValue(color);
        BYTE b = GetBValue(color);
        int rrow = row;
        int pixoffset = 0;
        WORD val = 0;
        BYTE bval;
        
        // check to see that row and column are in image
        if (row < 0 || col < 0 || row >= abs(m_lpBMIH->biHeight) || col >= m_lpBMIH->biWidth)
                return;

        LPRGBQUAD pDibQuad;
        if (m_lpBMIH->biBitCount == 8)
                pDibQuad = (LPRGBQUAD) m_lpvColorTable;

        // if image is stored bottom up, adjust row value
        if (m_lpBMIH->biHeight < 0)
                rrow = (m_lpBMIH->biHeight - 1) - row;

        if (m_lpImage) {
                switch (m_lpBMIH->biBitCount) {
                        case 1:
                                break;
                        case 4:
                                break;
                        case 8:
                                m_lpImage[m_dwBytesPerScanLine * rrow + col] = (BYTE) color;
                                break;
                        case 16:
                                val = (r & 0x7c00) << 10 | (g & 0x03e0) << 5 | b & 0x001f;
                                bval = (BYTE) (val & 0x00ff);
                                m_lpImage[m_dwBytesPerScanLine * rrow + col * 2] = bval;
                                bval = (BYTE) (val & 0xff00 >> 8);
                                m_lpImage[m_dwBytesPerScanLine * rrow + col * 2 + 1] = bval;
                                break;
                        case 24:
                                pixoffset = m_dwBytesPerScanLine * rrow + col * 3;
                                m_lpImage[pixoffset + 0] = b;
                                m_lpImage[pixoffset + 1] = g;
                                m_lpImage[pixoffset + 2] = r;
                                break;
                        case 32:
                                pixoffset = m_dwBytesPerScanLine * rrow + col * 4;
                                m_lpImage[pixoffset + 0] = b;
                                m_lpImage[pixoffset + 1] = g;
                                m_lpImage[pixoffset + 2] = r;
                                m_lpImage[pixoffset + 3] = alpha;
                                break;
                }
        }
}
***** NEW_CDIB.CPP
*****

