ImageX分块处理和保存

    COneTif* pOt = new COneTif();
    pOt->m_strTifPath = m_pOt->m_strTifPath;
    if (!pOt->Initilize())
    {
        return;
    }
    const int nBlock = 1024;
    const int& nImgWidth = pOt->m_nImgWidth;
    const int& nImgHeight = pOt->m_nImgHeight;
    const int& nBandNum = pOt->m_nBandNum;
    const int& nBPB = pOt->m_nBPB;
    BYTE* pBlockBufferRead = new BYTE[nImgWidth * nBandNum * nBPB * nBlock];
    BYTE* pBlockBufferWrite = new BYTE[nImgWidth * nBandNum * nBPB * nBlock];
    memset(pBlockBufferRead, 0, sizeof(BYTE)* nImgWidth * nBandNum * nBPB * nBlock);
    memset(pBlockBufferWrite, 0, sizeof(BYTE)* nImgWidth * nBandNum * nBPB * nBlock);
    int nBPP = nBPB * nBandNum;

    IImageX* pImage = nullptr;
    HRESULT hRes = CoCreateInstance(CLSID_ImageDriverX, NULL, CLSCTX_ALL, IID_IImageX, (void**)&pImage);
    if (FAILED(hRes))
    {
        return;
    }

    hRes = pImage->CreateImg(szFileName.GetBuffer(), modeCreate, nImgWidth, nImgHeight, 0, nBandNum, BIP, 0, 0, 1);
    if (hRes == S_FALSE)
    {
        return;
    }

    int nRowBlockNum = (nImgHeight + nBlock - 1) / nBlock;
    for (int j = 0; j < nRowBlockNum; ++j)
    {
        memset(pBlockBufferRead, 0, sizeof(BYTE)* nImgWidth * nBandNum * nBPB * nBlock);
        memset(pBlockBufferWrite, 0, sizeof(BYTE)* nImgWidth * nBandNum * nBPB * nBlock);
        pOt->m_pImage->ReadImg(0, j * nBlock, nImgWidth, j * nBlock + nBlock, pBlockBufferRead, nImgWidth, nBlock, nBandNum, 0, 0, nImgWidth, nBlock, -1, 0);
        for (int m = 0; m < nBlock; m++)
        {
            BYTE *pBufferWriteIndex = pBlockBufferWrite + m * nImgWidth * nBPP;
            BYTE *pBufferBlockIndex = pBlockBufferRead + m * nImgWidth * nBPP;
            for (int n = 0; n < nImgWidth; n++)
            {
                BYTE *pSubBufferWriteIndex = pBufferWriteIndex + n * nBPP;
                BYTE *pSubBufferBlockIndex = pBufferBlockIndex + n * nBPP;

                //对每一个像素进行计算
                if (nBandNum == 3)
                {
                    //此时rgba实际上为rgb,bmp格式存放的是bgr
                    const BYTE* rgba = pSubBufferBlockIndex;
                    double rgb[3];
                    rgb[0] = m_arrWeight[0] * rgba[0] + m_arrWeight[1] * rgba[1] + m_arrWeight[2] * rgba[2];
                    rgb[1] = m_arrWeight[4] * rgba[0] + m_arrWeight[5] * rgba[1] + m_arrWeight[6] * rgba[2];
                    rgb[2] = m_arrWeight[8] * rgba[0] + m_arrWeight[9] * rgba[1] + m_arrWeight[10] * rgba[2];
                    for (int k = 0; k < nBandNum; ++k)
                    {
                        rgb[k] = rgb[k] > 255.0 ? 255.0 : rgb[k];
                        pSubBufferWriteIndex[k] = (BYTE)rgb[k];
                    }
                }
                else if (nBandNum == 4)
                {
                    //加入的调色代码
                    const BYTE* rgba = pSubBufferBlockIndex;
                    double rgb[4];
                    rgb[0] = m_arrWeight[0] * rgba[0] + m_arrWeight[1] * rgba[1] + m_arrWeight[2] * rgba[2] + m_arrWeight[3] * rgba[3];
                    rgb[1] = m_arrWeight[4] * rgba[0] + m_arrWeight[5] * rgba[1] + m_arrWeight[6] * rgba[2] + m_arrWeight[7] * rgba[3];
                    rgb[2] = m_arrWeight[8] * rgba[0] + m_arrWeight[9] * rgba[1] + m_arrWeight[10] * rgba[2] + m_arrWeight[11] * rgba[3];
                    rgb[3] = m_arrWeight[12] * rgba[0] + m_arrWeight[13] * rgba[1] + m_arrWeight[14] * rgba[2] + m_arrWeight[15] * rgba[3];

#ifdef _MATRIX_
                    double ivv[3];
                    RGBtoIHS(rgb[0], rgb[1], rgb[2], ivv);
                    ivv[2] = ivv[2] / 4;
                    IHStoRGB(ivv, rgb);
                    rgb[0] = 16 * sqrt(rgb[0]);
                    rgb[1] = 16 * sqrt(rgb[1]);
                    rgb[2] = 16 * sqrt(rgb[2]);
#endif // _MATRIX_

                    for (int k = 0; k < nBandNum; ++k)
                    {
                        rgb[k] = rgb[k] > 255.0 ? 255.0 : rgb[k];
                        pSubBufferWriteIndex[k] = (BYTE)rgb[k];
                    }
                }
            }
        }
        pImage->WriteImg(0, j * nBlock, nImgWidth, j * nBlock + nBlock, pBlockBufferWrite, nImgWidth, nBlock, nBandNum, 0, 0, nImgWidth, nBlock, -1, 0);
    }

    if (pBlockBufferRead)
    {
        delete[] pBlockBufferRead;
        pBlockBufferRead = nullptr;
    }
    if (pBlockBufferWrite)
    {
        delete[] pBlockBufferWrite;
        pBlockBufferWrite = nullptr;
    }

    delete pOt;
    pOt = nullptr;

    if (pImage != NULL)
    {
        pImage->Close();
        pImage->Release();
        pImage = NULL;
    }

    AfxMessageBox(_T("OK"));
原文地址:https://www.cnblogs.com/autumoonchina/p/8005146.html