ImageX分块处理和保存

Posted 秋月的私语

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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"));

 

以上是关于ImageX分块处理和保存的主要内容,如果未能解决你的问题,请参考以下文章

浅谈数列分块问题

分块-区间求和

数据结构——分块

分块学习总结

matlab高光谱影像读取保存分块

分块入门9 --- 区间众数