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

📄 colconv.cpp

📁 WDK 自带的xpsdrv filter之 color
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*++

Copyright (c) 2005 Microsoft Corporation

All rights reserved.

THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

File Name:

   colconv.cpp

Abstract:

   Color conversion manager implementation. The CColorConverter class is responsible
   for coordinating the handling of the color conversion process.

   Note regarding transform caching: The color filter provides caching for the last
   transform. This aids performance as quite some time can be consumed creating a
   transform. The filter may however suffer if source content color profiles are rapidly
   switched (e.g. alternating sRGB and scRGB mark-up). This could be mitigated by increasing
   the number cached transforms beyond one.

--*/

#include "precomp.h"
#include "debug.h"
#include "globals.h"
#include "xdstring.h"
#include "xdexcept.h"
#include "colconv.h"
#include "cmimg.h"
#include "dictionary.h"

using XDPrintSchema::JobOptimalDestinationColorProfile::EProfileOption;
using XDPrintSchema::JobOptimalDestinationColorProfile::RGB;
using XDPrintSchema::JobOptimalDestinationColorProfile::CMYK;

using XDPrintSchema::PageICMRenderingIntent::AbsoluteColorimetric;
using XDPrintSchema::PageICMRenderingIntent::RelativeColorimetric;
using XDPrintSchema::PageICMRenderingIntent::Photographs;
using XDPrintSchema::PageICMRenderingIntent::BusinessGraphics;

COLORTYPE g_nChannelMap[] = {
    COLOR_3_CHANNEL,
    COLOR_CMYK,
    COLOR_5_CHANNEL,
    COLOR_6_CHANNEL,
    COLOR_7_CHANNEL,
    COLOR_8_CHANNEL
};

/*++

Routine Name:

    CColorConverter::CColorConverter

Routine Description:

    Constructor for the base CColorConverter class. Provides common functionality
    between the bitmap and color ref converter classes

Arguments:

    pXpsConsumer - Pointer to the XPS consumer interface. Used to write out resources
    pFixedPage   - Pointer to the FixedPage interface. Resource cache uses this when writing
    pResCache    - Pointer to the resource cache.
    pProfManager - Pointer to a profile manager for supplying a suitable color profile

Return Value:

    None
    Throws CXDException(HRESULT) on an error

--*/
CColorConverter::CColorConverter(
    __in IXpsDocumentConsumer* pXpsConsumer,
    __in IFixedPage*           pFixedPage,
    __in CFileResourceCache*   pResCache,
    __in CProfileManager*      pProfManager,
    __in ResDeleteMap*         pResDel
    ) :
    m_pXpsConsumer(pXpsConsumer),
    m_pFixedPage(pFixedPage),
    m_pProfManager(pProfManager),
    m_pResCache(pResCache),
    m_pResDel(pResDel)
{
    HRESULT hr = S_OK;

    if (FAILED(hr = CHECK_POINTER(m_pXpsConsumer, E_POINTER)) ||
        FAILED(hr = CHECK_POINTER(m_pFixedPage, E_POINTER)) ||
        FAILED(hr = CHECK_POINTER(m_pProfManager, E_POINTER)) ||
        FAILED(hr = CHECK_POINTER(m_pResCache, E_POINTER)) ||
        FAILED(hr = CHECK_POINTER(m_pResDel, E_POINTER)))
    {
        throw CXDException(hr);
    }
}

/*++

Routine Name:

    CColorConverter::~CColorConverter

Routine Description:

    Default destructor for the CColorConverter class

Arguments:

    None

Return Value:

    None

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

/*++

Routine Name:

    CBitmapColorConverter::CBitmapColorConverter

Routine Description:

    Constructor for the CBitmapColorConverter class

Arguments:

    pXpsConsumer - Pointer to the XPS consumer interface. Used to write out resources
    pFixedPage   - Pointer to the FixedPage interface. Resource cache uses this when writing
    pResCache    - Pointer to the resource cache.
    pProfManager - Pointer to a profile manager for supplying a suitable color profile

Return Value:

    None
    Throws CXDException(HRESULT) on an error

--*/
CBitmapColorConverter::CBitmapColorConverter(
    __in IXpsDocumentConsumer* pXpsConsumer,
    __in IFixedPage*           pFixedPage,
    __in CFileResourceCache*   pResCache,
    __in CProfileManager*      pProfManager,
    __in ResDeleteMap*         pResDel
    ) :
    CColorConverter(pXpsConsumer, pFixedPage, pResCache, pProfManager, pResDel)
{
}

/*++

Routine Name:

    CBitmapColorConverter::~CBitmapColorConverter

Routine Description:

    Destructor for the CBitmapColorConverter class

Arguments:

    None

Return Value:

    None

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

/*++

Routine Name:

    CColorConverter::ConvertBitmap

Routine Description:

    Method which performs the color conversion process to a bitmap resource and sets
    the resource URI to contain the URI of the converted bitmap resource

Arguments:

    pbstrBmpURI - Pointer to a string containing the bitmap resource path and name

Return Value:

    HRESULT
    S_OK - On success
    E_*  - On error

--*/
HRESULT
CBitmapColorConverter::Convert(
    __inout BSTR* pbstrBmpURI
    )
{
    HRESULT hr = S_OK;

    if (SUCCEEDED(hr = CHECK_POINTER(pbstrBmpURI, E_POINTER)))
    {
        try
        {
            //
            // Create a color managed image object
            //
            CColorManagedImage bmpColManaged(*pbstrBmpURI, m_pProfManager, m_pFixedPage, m_pResDel);

            //
            // Write out the cached bitmap and get a keyname for the written bitmap
            //
            CComBSTR bstrKey;
            if (SUCCEEDED(hr = m_pResCache->WriteResource<IPartImage>(m_pXpsConsumer, m_pFixedPage, &bmpColManaged)) &&
                SUCCEEDED(hr = bmpColManaged.GetKeyName(&bstrKey)))
            {
                hr = m_pResCache->GetURI(bstrKey, pbstrBmpURI);

                ASSERTMSG(SUCCEEDED(hr), "Failed to process image");
            }
        }
        catch (CXDException& e)
        {
            hr = e;
        }
    }
    else
    {
        hr = E_POINTER;
    }

    ERR_ON_HR(hr);
    return hr;
}

/*++

Routine Name:

    CColorRefConverter::CColorRefConverter

Routine Description:

    Constructor for the base CColorRefConverter class which handles parsing and
    conversion of a color string within XPS mark-up

Arguments:

    pXpsConsumer - Pointer to the XPS consumer interface. Used to write out resources
    pFixedPage   - Pointer to the FixedPage interface. Resource cache uses this when writing
    pResCache    - Pointer to the resource cache.
    pProfManager - Pointer to a profile manager for supplying a suitable color profile

Return Value:

    None
    Throws CXDException(HRESULT) on an error

--*/
CColorRefConverter::CColorRefConverter(
    __in IXpsDocumentConsumer* pXpsConsumer,
    __in IFixedPage*           pFixedPage,
    __in CFileResourceCache*   pResCache,
    __in CProfileManager*      pProfManager,
    __in ResDeleteMap*         pResDel
    ) :
    CColorConverter(pXpsConsumer, pFixedPage, pResCache, pProfManager, pResDel)
{
}

/*++

Routine Name:

    CColorRefConverter::~CColorRefConverter

Routine Description:

    Destructor for the CColorRefConverter class

Arguments:

    None

Return Value:

    None

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

/*++

Routine Name:

    CColorRefConverter::ConvertColor

Routine Description:

    Method which performs the color conversion process to an XPS color ref element

Arguments:

    pbstrColorRef - Pointer to a string containing the color data to be converted

Return Value:

    HRESULT
    S_OK - On success
    E_*  - On error

--*/
HRESULT
CColorRefConverter::Convert(
    __inout BSTR* pbstrColorRef
    )
{
    HRESULT hr = S_OK;

    BOOL bIsResourceReference = FALSE;
    if (SUCCEEDED(hr = CHECK_POINTER(pbstrColorRef, E_POINTER)) &&
        SUCCEEDED(hr = ParseColorString(*pbstrColorRef, &m_srcData, &bIsResourceReference)) &&
        !bIsResourceReference)
    {
        //
        // If the source and destination types are the same (e.g. scRGB in and scRGB out)
        // we need go no further - simply return the existing string. This does not apply
        // to nChannel as ContextColors specify their own profile and we cannot prove that
        // this matches our output. The color space for sRGB and scRGB are implicit however.
        //
        EColorDataType srcType = sRGB;
        EColorDataType dstType = sRGB;
        if (SUCCEEDED(hr = InitDstChannels(&m_srcData, &m_dstData)) &&
            SUCCEEDED(hr = m_srcData.GetColorDataType(&srcType)) &&
            SUCCEEDED(hr = m_dstData.GetColorDataType(&dstType)))
        {
            if (dstType == nChannel ||
                dstType != srcType)
            {
                if (SUCCEEDED(hr = TransformColor(&m_srcData, &m_dstData)))
                {
                    hr = CreateColorString(&m_dstData, pbstrColorRef);
                }
            }
        }
    }

    ERR_ON_HR(hr);
    return hr;
}

/*++

Routine Name:

    CColorRefConverter::InitDstChannels

Routine Description:

    Initialise the destination color channel data based on the destination color
    profile

Arguments:

    pChannelDataDst - Pointer to a color channel data object to be intialised

⌨️ 快捷键说明

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