bmpconv.cpp

来自「WDK 自带的xpsdrv filter之 color」· C++ 代码 · 共 1,188 行 · 第 1/2 页

CPP
1,188 行
字号

    ERR_ON_HR(hr);
    return hr;
}

/*++

Routine Name:

    CBmpConverter::LockSurface

Routine Description:

    Locks the requested rectangle for reading and writing of the pixel data directly

Arguments:

    prcLock   - The rectangle area of the bitmap to lock
    bReadOnly - Is this a read only lock
    pcbStride - Pointer to variable that recieves the stride between scanlines
    pcWidth   - Pointer to variable that recieves the bitmap pixel width
    pcHeight  - Pointer to variable that recieves the bitmap pixel height
    pcbData   - Pointer to variable that recieves the size of the pixel data buffer
    ppbData   - Pointer to a BYTE pointer that recieves the data pointer

Return Value:

    HRESULT
    S_OK - On success
    E_*  - On error

--*/
HRESULT
CBmpConverter::LockSurface(
    __in  WICRect*    prcLock,
    __in  CONST BOOL& bReadOnly,
    __out UINT*       pcbStride,
    __out UINT*       pcWidth,
    __out UINT*       pcHeight,
    __out UINT*       pcbData,
    __out PBYTE*      ppbData
    )
{
    HRESULT hr = S_OK;

    if (SUCCEEDED(hr = CHECK_POINTER(m_pBitmap, E_PENDING)) &&
        SUCCEEDED(hr = CHECK_POINTER(m_pImagingFactory, E_PENDING)) &&
        SUCCEEDED(hr = CHECK_POINTER(prcLock, E_POINTER)) &&
        SUCCEEDED(hr = CHECK_POINTER(pcbStride, E_POINTER)) &&
        SUCCEEDED(hr = CHECK_POINTER(pcWidth, E_POINTER)) &&
        SUCCEEDED(hr = CHECK_POINTER(pcHeight, E_POINTER)) &&
        SUCCEEDED(hr = CHECK_POINTER(pcbData, E_POINTER)) &&
        SUCCEEDED(hr = CHECK_POINTER(ppbData, E_POINTER)))
    {
        *pcbStride = 0;
        *pcWidth = 0;
        *pcHeight = 0;
        *pcbData = 0;
        *ppbData = NULL;

        //
        // Free any current locks and acquire a new one. Get the stride, size and data from the lock.
        //
        m_pCurrentLock = NULL;
        if (SUCCEEDED(hr) &&
            SUCCEEDED(hr = m_pBitmap->Lock(prcLock, bReadOnly ? WICBitmapLockRead : WICBitmapLockWrite, &m_pCurrentLock)) &&
            SUCCEEDED(hr = CHECK_POINTER(m_pCurrentLock, E_FAIL)) &&
            SUCCEEDED(hr = m_pCurrentLock->GetStride(pcbStride)) &&
            SUCCEEDED(hr = m_pCurrentLock->GetSize(pcWidth, pcHeight)))
        {
            hr = m_pCurrentLock->GetDataPointer(pcbData, ppbData);
        }
    }

    ERR_ON_HR(hr);
    return hr;
}

/*++

Routine Name:

    CBmpConverter::UnlockSurface

Routine Description:

    Unlocks any current lock on the bitmap surface

Arguments:

    None

Return Value:

    HRESULT
    S_OK - On success
    E_*  - On error

--*/
HRESULT
CBmpConverter::UnlockSurface(
    VOID
    )
{
    m_pCurrentLock = NULL;

    return S_OK;
}

/*++

Routine Name:

    CBmpConverter::GetColorContext

Routine Description:

    Retrieves any color context associated with the bitmap

Arguments:

    ppColorContext - Pointer to a color context interface pointer to recieve the context

Return Value:

    HRESULT
    S_OK                - On success
    E_ELEMENT_NOT_FOUND - On no context present
    E_*                 - On error

--*/
HRESULT
CBmpConverter::GetColorContext(
    __deref_out IWICColorContext** ppColorContext
    )
{
    HRESULT hr = S_OK;

    if (SUCCEEDED(hr = CHECK_POINTER(ppColorContext, E_POINTER)))
    {
        *ppColorContext = NULL;

        if (HasColorContext())
        {
            *ppColorContext = m_pColorContext;
        }
        else
        {
            hr = E_ELEMENT_NOT_FOUND;
        }
    }

    ERR_ON_HR_EXC(hr, E_ELEMENT_NOT_FOUND);
    return hr;
}

/*++

Routine Name:

    CBmpConverter::HasAlphaChannel

Routine Description:

    Indicates if the bitmap has an alpha channel

Arguments:

    None

Return Value:

    TRUE  - The bitmap has an alpha channel
    FALSE - There is no alpha channel

--*/
BOOL
CBmpConverter::HasAlphaChannel(
    VOID
    ) CONST
{
    BOOL bHasAlpha = FALSE;

    switch (m_ePixelFormat)
    {
        case kWICPixelFormat32bppBGRA:
        case kWICPixelFormat32bppPBGRA:
        case kWICPixelFormat64bppRGBA:
        case kWICPixelFormat64bppPRGBA:
        case kWICPixelFormat128bppRGBAFloat:
        case kWICPixelFormat128bppPRGBAFloat:
        case kWICPixelFormat64bppRGBAFixedPoint:
        case kWICPixelFormat128bppRGBAFixedPoint:
        case kWICPixelFormat64bppRGBAHalf:
        case kWICPixelFormat40bppCMYKAlpha:
        case kWICPixelFormat80bppCMYKAlpha:
        case kWICPixelFormat32bpp3ChannelsAlpha:
        case kWICPixelFormat40bpp4ChannelsAlpha:
        case kWICPixelFormat48bpp5ChannelsAlpha:
        case kWICPixelFormat56bpp6ChannelsAlpha:
        case kWICPixelFormat64bpp7ChannelsAlpha:
        case kWICPixelFormat72bpp8ChannelsAlpha:
        case kWICPixelFormat64bpp3ChannelsAlpha:
        case kWICPixelFormat80bpp4ChannelsAlpha:
        case kWICPixelFormat96bpp5ChannelsAlpha:
        case kWICPixelFormat112bpp6ChannelsAlpha:
        case kWICPixelFormat128bpp7ChannelsAlpha:
        case kWICPixelFormat144bpp8ChannelsAlpha:
        {
            bHasAlpha = TRUE;
        }
        break;

        default:
        {
        }
        break;
    }

    return bHasAlpha;
}

/*++

Routine Name:

    CBmpConverter::HasColorContext

Routine Description:

    Indicates whether the bitmap has a color context

Arguments:

    None

Return Value:

    TRUE  - The bitmap has a color context
    FALSE - There is no color context associated with the bitmap

--*/
BOOL
CBmpConverter::HasColorContext(
    VOID
    ) CONST
{
    return m_pColorContext != NULL;
}

/*++

Routine Name:

    CBmpConverter::HasColorProfile

Routine Description:

    Indicates whether the color context (if present) contains a color profile

Arguments:

    None

Return Value:

    TRUE  - There is a color context and it contains a color profile
    FALSE - There is either no color context or the context does not contain a color profile

--*/
BOOL
CBmpConverter::HasColorProfile(
    VOID
    ) CONST
{
    BOOL bHasProfile = FALSE;

    if (HasColorContext())
    {
        WICColorContextType wicContextType = WICColorContextUninitialized;
        if (m_pColorContext != NULL &&
            SUCCEEDED(m_pColorContext->GetType(&wicContextType)))
        {
            bHasProfile = wicContextType == WICColorContextProfile;
        }
    }

    return bHasProfile;
}

/*++

Routine Name:

    CBmpConverter::GetPixelFormat

Routine Description:

    Retrieves the underlying pixel format

Arguments:

    None

Return Value:

    Enumerated value identifying the current pixel format of the bitmap

--*/
EWICPixelFormat
CBmpConverter::GetPixelFormat(
    VOID
    )
{
    return m_ePixelFormat;
}

/*++

Routine Name:

    CBmpConverter::GetSize

Routine Description:

    Retrieves the dimensions of the bitmap

Arguments:

    pcWidth  - Pointer to a  variable that recieves the pixel width
    pcHeight - Pointer to a  variable that recieves the pixel height

Return Value:

    HRESULT
    S_OK - On success
    E_*  - On error

--*/
HRESULT
CBmpConverter::GetSize(
    __out UINT* pcWidth,
    __out UINT* pcHeight
    )
{
    HRESULT hr = S_OK;

    if (SUCCEEDED(hr = CHECK_POINTER(m_pBitmap, E_PENDING)) &&
        SUCCEEDED(hr = CHECK_POINTER(m_pImagingFactory, E_PENDING)) &&
        SUCCEEDED(hr = CHECK_POINTER(pcWidth, E_POINTER)) &&
        SUCCEEDED(hr = CHECK_POINTER(pcHeight, E_POINTER)))
    {
        *pcWidth = 0;
        *pcHeight = 0;

        hr = m_pBitmap->GetSize(pcWidth, pcHeight);
    }

    ERR_ON_HR(hr);
    return hr;
}

/*++

Routine Name:

    CBmpConverter::GetResolution

Routine Description:

    Retrieves the resolution of the underlying bitmap

Arguments:

    pDpiX - Pointer to a variable that recieves the horizontal resolution
    pDpiY - Pointer to a variable that recieves the vertical resolution

Return Value:

    HRESULT
    S_OK - On success
    E_*  - On error

--*/
HRESULT
CBmpConverter::GetResolution(
    __out DOUBLE* pDpiX,
    __out DOUBLE* pDpiY
    )
{
    HRESULT hr = S_OK;

    if (SUCCEEDED(hr = CHECK_POINTER(m_pBitmap, E_PENDING)) &&
        SUCCEEDED(hr = CHECK_POINTER(m_pImagingFactory, E_PENDING)) &&
        SUCCEEDED(hr = CHECK_POINTER(pDpiX, E_POINTER)) &&
        SUCCEEDED(hr = CHECK_POINTER(pDpiY, E_POINTER)))
    {
        *pDpiX = 0.0;
        *pDpiY = 0.0;

        hr = m_pBitmap->GetResolution(pDpiX, pDpiY);
    }

    ERR_ON_HR(hr);
    return hr;
}

/*++

Routine Name:

    CBmpConverter::SetProfile

Routine Description:

    This method sets creates a color context based of the supplied profile filename and
    associates that cotext with the bitmap

Arguments:

    szProfile - The profile filename

Return Value:

    HRESULT
    S_OK - On success
    E_*  - On error

--*/
HRESULT
CBmpConverter::SetProfile(
    __in_z LPWSTR szProfile
    )
{
    HRESULT hr = S_OK;

    if (SUCCEEDED(hr = CHECK_POINTER(szProfile, E_POINTER)) &&
        SUCCEEDED(hr = CHECK_POINTER(m_pImagingFactory, E_PENDING)))
    {
        m_pColorContext = NULL;
        if (SUCCEEDED(hr = m_pImagingFactory->CreateColorContext(&m_pColorContext)))
        {
            hr = m_pColorContext->InitializeFromFilename(szProfile);
        }
    }

    ERR_ON_HR(hr);
    return hr;
}

/*++

Routine Name:

    CBmpConverter::operator=

Routine Description:

    Assignment operator

Arguments:

    converter - Instance of a CBmpConverter to be assigned to

Return Value:

    Reference to the newly assigned CBmpConverter instance

--*/
CBmpConverter&
CBmpConverter::operator=(
    __in CONST CBmpConverter& converter
    )
{
    //
    // Assign all the member pointers. The COM pointer takes care of reference counting.
    //
    m_pImagingFactory = converter.m_pImagingFactory;
    m_pBitmap = converter.m_pBitmap;
    m_pColorContext = converter.m_pColorContext;
    m_pCurrentLock = converter.m_pCurrentLock;
    m_ePixelFormat = converter.m_ePixelFormat;

    ASSERTMSG(m_pCurrentLock == NULL, "Copying locked bitmap can lead to a deadlock.\n");

    return *this;
}

/*++

Routine Name:

    CBmpConverter::CreateImagingFactory

Routine Description:

    Creates the WIC imaging factory

Arguments:

    None

Return Value:

    HRESULT
    S_OK - On success
    E_*  - On error

--*/
HRESULT
CBmpConverter::CreateImagingFactory(
    VOID
    )
{
    m_pImagingFactory = NULL;
    HRESULT hr = m_pImagingFactory.CoCreateInstance(CLSID_WICImagingFactory);
    if (SUCCEEDED(hr))
    {
        hr = CHECK_POINTER(m_pImagingFactory, E_FAIL);
    }

    ERR_ON_HR(hr);
    return hr;
}

/*++

Routine Name:

    CBmpConverter::PixelFormatFromGUID

Routine Description:

    Converts the WIC pixel format GUID to the matching pixel format enumeration

Arguments:

    pixelFormat - GUID defining the pixel format
    pPixFormat  - Pointer to the pixel format to be set

Return Value:

    HRESULT
    S_OK - On success
    E_*  - On error

--*/
HRESULT
CBmpConverter::PixelFormatFromGUID(
    __in  REFGUID          pixelFormat,
    __out EWICPixelFormat* pPixFormat
    )
{
    HRESULT hr = S_OK;

    if (SUCCEEDED(hr = CHECK_POINTER(pPixFormat, E_POINTER)))
    {
        *pPixFormat = kWICPixelFormatDontCare;
        BOOL bFound = 0;

        //
        // Loop all the potential formats
        //
        for (UINT cFormat = 0;
             cFormat < kWICPixelFormatMax;
             cFormat++)
        {
            //
            // Try to match the guid
            //
            if (pixelFormat == g_lutPixFrmtGuid[cFormat])
            {
                //
                // The index provides the corresponding pixel format enumerated value
                //
                bFound = TRUE;
                *pPixFormat = static_cast<EWICPixelFormat>(cFormat);
                break;
            }
        }

        if (!bFound)
        {
            RIP("Could not match format GUID\n");
            hr = E_FAIL;
        }
    }

    ERR_ON_HR(hr);
    return hr;
}


⌨️ 快捷键说明

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