⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 colconv.cpp

📁 WDK 自带的xpsdrv filter之 color
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    ERR_ON_HR(hr);
    return hr;
}

/*++

Routine Name:

    CColorRefConverter::TransformColor

Routine Description:

    Method which applies a color transform to a set of color data

Arguments:

    pSrcData - Pointer to the source color channel data
    pDstData - Pointer to the destination color channel data

Return Value:

    HRESULT
    S_OK - On success
    E_*  - On error

--*/
HRESULT
CColorRefConverter::TransformColor(
    __in    CColorChannelData* pSrcData,
    __inout CColorChannelData* pDstData
    )
{
    HRESULT hr = S_OK;

    HTRANSFORM hColorTrans = NULL;

    BOOL bUseWCS = FALSE;

    if (SUCCEEDED(hr = CHECK_POINTER(pSrcData, E_POINTER)) &&
        SUCCEEDED(hr = CHECK_POINTER(pDstData, E_POINTER)) &&
        SUCCEEDED(hr = m_pProfManager->GetColorTransform(&hColorTrans, &bUseWCS)))
    {
        if (bUseWCS)
        {
            DWORD cSrcChan = 0;
            DWORD cDstChan = 0;

            COLORDATATYPE srcDataType = COLOR_FLOAT;
            COLORDATATYPE dstDataType = COLOR_FLOAT;

            DWORD cbSrcChan = 0;
            DWORD cbDstChan = 0;

            PBYTE pSrcBuff = NULL;
            PBYTE pDstBuff = NULL;

            if (SUCCEEDED(hr = CHECK_POINTER(hColorTrans, E_HANDLE)) &&
                SUCCEEDED(hr = pSrcData->GetChannelCountNoAlpha(&cSrcChan)) &&
                SUCCEEDED(hr = pSrcData->GetChannelType(&srcDataType)) &&
                SUCCEEDED(hr = pSrcData->GetChannelDataNoAlpha(&cbSrcChan, reinterpret_cast<PVOID*>(&pSrcBuff))) &&
                SUCCEEDED(hr = pDstData->GetChannelCountNoAlpha(&cDstChan)) &&
                SUCCEEDED(hr = pDstData->GetChannelType(&dstDataType)) &&
                SUCCEEDED(hr = pDstData->GetChannelDataNoAlpha(&cbDstChan, reinterpret_cast<PVOID*>(&pDstBuff))))
            {
                if (cbDstChan > 0)
                {
                    if (!WcsTranslateColorsXD(hColorTrans,
                                              1,
                                              cSrcChan,
                                              srcDataType,
                                              cbSrcChan,
                                              pSrcBuff,
                                              cDstChan,
                                              dstDataType,
                                              cbDstChan,
                                              pDstBuff))
                    {
                        hr = GetLastErrorAsHResult();
                    }
                }
                else
                {
                    hr = E_FAIL;
                }
            }
        }
        else
        {
            COLOR srcColor;
            COLOR dstColor;

            COLORTYPE srcType;
            COLORTYPE dstType;

            if (SUCCEEDED(hr = pSrcData->GetColor(&srcColor, &srcType)) &&
                SUCCEEDED(hr = pDstData->GetColor(&dstColor, &dstType)))
            {
                if (TranslateColors(hColorTrans,
                                    &srcColor,
                                    1,
                                    srcType,
                                    &dstColor,
                                    dstType))
                {
                    hr = pDstData->SetColor(&dstColor, dstType);
                }
                else
                {
                    hr = GetLastErrorAsHResult();
                }
            }
        }
    }

    ERR_ON_HR(hr);
    return hr;
}

/*++

Routine Name:

    CColorRefConverter::CreateColorString

Routine Description:

    Method which creates a XML formatted string descripting a transformed set of colors

Arguments:

    pDstData      - Pointer to destination color channel data
    pbstrColorRef - Pointer to a string to contain the XML formatted color data

Return Value:

    HRESULT
    S_OK - On success
    E_*  - On error

--*/
HRESULT
CColorRefConverter::CreateColorString(
    __in    CColorChannelData* pDstData,
    __inout BSTR*              pbstrColorRef
    )
{
    CComBSTR bstrTmpHolder;

    HRESULT hr = S_OK;

    EColorDataType colDataType = sRGB;
    if (SUCCEEDED(hr = CHECK_POINTER(pDstData,  E_POINTER)) &&
        SUCCEEDED(hr = CHECK_POINTER(pbstrColorRef, E_POINTER)) &&
        SUCCEEDED(hr = pDstData->GetColorDataType(&colDataType)))
    {
        try
        {
            CStringXDW cstrChannelData;

            if (colDataType == scRGB)
            {
                //
                // scRGB
                //
                cstrChannelData += L"sc#";
                hr = SetFloatChannelData(pDstData, &cstrChannelData);
            }
            else if (colDataType == nChannel)
            {
                //
                // CMYK
                //
                cstrChannelData += L"ContextColor ";

                //
                // Add the ICC profile and retrieve the URI
                //
                CComBSTR bstrKey;
                CComBSTR bstrICCURI;
                if (SUCCEEDED(hr = m_pResCache->WriteResource<IPartColorProfile>(m_pXpsConsumer, m_pFixedPage, m_pProfManager)) &&
                    SUCCEEDED(hr = m_pProfManager->GetKeyName(&bstrKey)) &&
                    SUCCEEDED(hr = m_pResCache->GetURI(bstrKey, &bstrICCURI)))
                {
                    //
                    // Append the URI and a space
                    //
                    cstrChannelData += bstrICCURI;
                    cstrChannelData += L" ";

                    hr = SetFloatChannelData(pDstData, &cstrChannelData);
                }
            }
            else if (colDataType == sRGB)
            {
                //
                // sRGB
                //
                cstrChannelData += L"#";

                //
                // Over all channels, write out hex byte values
                //
                DWORD cbData = 0;
                PBYTE pData = NULL;
                DWORD cChanCount = 0;
                if (SUCCEEDED(hr = pDstData->GetChannelData(&cbData, reinterpret_cast<PVOID*>(&pData))) &&
                    SUCCEEDED(hr = pDstData->GetChannelCount(&cChanCount)))
                {
                    if (cbData >= cChanCount * sizeof(BYTE))
                    {
                        for (DWORD cChan = 0; cChan < cChanCount; cChan++)
                        {
                            CStringXDW cstrChannel;
                            cstrChannel.Format(L"%02x", pData[cChan]);
                            cstrChannelData.Append(cstrChannel);
                        }
                    }
                    else
                    {
                        hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
                    }
                }
            }
            else
            {
                RIP("Invalid color type\n");
                hr = E_FAIL;
            }

            if (SUCCEEDED(hr))
            {
                SysFreeString(*pbstrColorRef);
                *pbstrColorRef = cstrChannelData.AllocSysString();
            }
        }
        catch (CXDException& e)
        {
            hr = e;
        }
    }

    ERR_ON_HR(hr);
    return hr;
}

/*++

Routine Name:

    CResourceDictionaryConverter::CResourceDictionaryConverter

Routine Description:

    CResourceDictionaryConverter constructir

Arguments:

    pXpsConsumer - Pointer to the XPS consumer interface
    pFixedPage   - Pointer to the fixed page interface
    pResCache    - Pointer to the resource cache
    pProfManager - Pointer to the color profile manager
    pResDel      - Pointer to the list of resources to delete from the page
    pBmpConv     - Pointer to the bitmap conversion class
    pRefConv     - Pointer to the color reference conversion class

Return Value:

    None
    Throws an exception on error.

--*/
CResourceDictionaryConverter::CResourceDictionaryConverter(
    __in IXpsDocumentConsumer*  pXpsConsumer,
    __in IFixedPage*            pFixedPage,
    __in CFileResourceCache*    pResCache,
    __in CProfileManager*       pProfManager,
    __in ResDeleteMap*          pResDel,
    __in CBitmapColorConverter* pBmpConv,
    __in CColorRefConverter*    pRefConv
    ) :
    CColorConverter(pXpsConsumer, pFixedPage, pResCache, pProfManager, pResDel),
    m_pBmpConv(pBmpConv),
    m_pRefConv(pRefConv)
{
    if (m_pBmpConv == NULL ||
        m_pRefConv == NULL)
    {
        throw CXDException(E_POINTER);
    }
}

/*++

Routine Name:

    CResourceDictionaryConverter::~CResourceDictionaryConverter

Routine Description:

    CResourceDictionaryConverter destructor

Arguments:

    None

Return Value:

    None

--*/
CResourceDictionaryConverter::~CResourceDictionaryConverter()
{
}

/*++

Routine Name:

    CResourceDictionaryConverter::Convert

Routine Description:

    Applies color conversion on a remote resource dictionary. This is achieved
    by creating a color SAX parser passing a stream based off the resource

Arguments:

    pbstrDictionaryURI - The remote dictionary URI

Return Value:

    HRESULT
    S_OK - On success
    E_*  - On error

--*/
HRESULT
CResourceDictionaryConverter::Convert(
    __inout BSTR* pbstrDictionaryURI
    )
{
    HRESULT hr = S_OK;

    if (SUCCEEDED(hr = CHECK_POINTER(pbstrDictionaryURI, E_POINTER)))
    {
        try
        {
            CStringXDW cstrOriginalURI(*pbstrDictionaryURI);

            //
            // Create a color managed dictionary object and pass to the cache manager
            //
            CRemoteDictionary newDictionary(m_pFixedPage, m_pBmpConv, m_pRefConv, *pbstrDictionaryURI);

            CComBSTR bstrKey;
            if (SUCCEEDED(hr = m_pResCache->WriteResource<IPartResourceDictionary>(m_pXpsConsumer, m_pFixedPage, &newDictionary)) &&
                SUCCEEDED(hr = newDictionary.GetKeyName(&bstrKey)) &&
                SUCCEEDED(hr = m_pResCache->GetURI(bstrKey, pbstrDictionaryURI)))
            {
                (*m_pResDel)[cstrOriginalURI] = TRUE;
            }
        }
        catch (CXDException& e)
        {
            hr = e;
        }
        catch (exception& DBG_ONLY(e))
        {
            ERR(e.what());
            hr = E_FAIL;
        }
    }

    return hr;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -