2

我一直在为这个问题苦苦挣扎很多天,找不到任何解释!

背景:

我正在使用 VC9+MFC 在 Windows 上创建一个色彩管理的照片编辑应用程序,并使用WCS(Windows 颜色系统)API 将像素从照片嵌入的颜色配置文件转换为显示器的配置文件。

我的显示器已使用“Windows 显示校准”进行校准,并创建了一个名为“CalibratedDisplayProfile-x.icc”的配置文件。

问题:

当我将像素从“ProPhoto RGB”转换为显示器的配置文件时,黑暗区域的颜色发生变化,色调变为绿色。如果目标配置文件是 sRGB,则在中间调/高光中不会发生这种情况。这是屏幕截图。

正确和错误图像

测试:

为了简化问题,我写了一些测试代码来翻译一个单一的颜色,但测试结果真的让我很困惑。源颜色 "c0" 是RGB(0,0,65535),但输出颜色 "c1" 是RGB(0,0,0)!!并且功能“CheckColor”失败并出现错误“Invalid Arguments”......

这怎么可能发生?难道我做错了什么?

您可以在此处下载这两个配置文件:颜色配置文件

非常感谢!

CString strProfilePath = _T("C:\\Windows\\System32\\spool\\drivers\\color\\");
CString strSrcProfile  = strProfilePath + _T("ProPhoto.icm");
CString strDstProfile  = strProfilePath + _T("CalibratedDisplayProfile-2.icc");
PROFILE pf = {0};
pf.dwType = PROFILE_FILENAME;
pf.pProfileData = (PVOID)strSrcProfile.GetBuffer();
pf.cbDataSize = (strSrcProfile.GetLength() + 1) * sizeof(TCHAR);
HPROFILE hSrcProfile = ::OpenColorProfile( &pf, PROFILE_READ, FILE_SHARE_READ, OPEN_EXISTING );
pf.pProfileData = (PVOID)strDstProfile.GetBuffer();
pf.cbDataSize = (strDstProfile.GetLength() + 1) * sizeof(TCHAR);
HPROFILE hDstProfile = ::OpenColorProfile( &pf, PROFILE_READ, FILE_SHARE_READ, OPEN_EXISTING );

HPROFILE hProfiles[2];
hProfiles[0] = hSrcProfile;
hProfiles[1] = hDstProfile;
DWORD dwIndents[2] = { INTENT_RELATIVE_COLORIMETRIC, INTENT_RELATIVE_COLORIMETRIC };
HTRANSFORM hTransform = ::CreateMultiProfileTransform( hProfiles, 2, dwIndents, 2, BEST_MODE, INDEX_DONT_CARE );

COLOR c0, c1;
c0.rgb.red = 0;
c0.rgb.green = 0;
c0.rgb.blue = 0xffff;
::TranslateColors( hTransform, &c0, 1, COLOR_RGB, &c1, COLOR_RGB );

BYTE btResult = 0;
::CheckColors( hTransform, &c0, 1, COLOR_RGB, &btResult );

::DeleteColorTransform( hTransform );
::CloseColorProfile( hSrcProfile );
::CloseColorProfile( hDstProfile );
4

1 回答 1

0

我在 Windows 7 SP1 x64 上遇到了与您完全相同的问题。似乎该TranslateColors功能要么被设计破坏,要么不应该以这种方式使用。我认为这是微软的错,因为他们可以在 MSDN 上编写更多的 WCS 示例。

但我设法使用TranslateBitmapBits函数解决了这个问题。这是一个示例:

bool translateColors(BYTE* srcRgbColors, BYTE* dstRgbColors, DWORD nBytes)
{
    BOOL bResult = FALSE;

    HPROFILE   hSrcProfile       = nullptr;
    HPROFILE   hDstProfile       = nullptr;
    HTRANSFORM hColorTransform   = nullptr;

    /* open source sRGB profile */
    wchar_t* srcProfilePath = L"sRGB Color Space Profile.icm";

    tagPROFILE targetProfile;
    targetProfile.dwType = PROFILE_FILENAME;
    targetProfile.pProfileData = srcProfilePath;
    targetProfile.cbDataSize = sizeof(wchar_t) * (wcslen(srcProfilePath) + 1);

    hSrcProfile = OpenColorProfile(&targetProfile, PROFILE_READ, FILE_SHARE_READ, OPEN_EXISTING);
    if (nullptr == hSrcProfile) goto EXIT;

    /* open destination monitor profile */
    wchar_t* dstProfilePath = L"ActiveMonitorProfile.icm";

    tagPROFILE destinationProfile;
    destinationProfile.dwType = PROFILE_FILENAME;
    destinationProfile.pProfileData = dstProfilePath;
    destinationProfile.cbDataSize = sizeof(wchar_t) * (wcslen(dstProfilePath) + 1);

    hDstProfile = OpenColorProfile(&destinationProfile, PROFILE_READ, FILE_SHARE_READ, OPEN_EXISTING);
    if (nullptr == hDstProfile) goto EXIT;

    /* create color transform */

    DWORD dwIntent = (DWORD)-1;
    HPROFILE hProfileList[2] = { hSrcProfile, hDstProfile };

    hColorTransform = CreateMultiProfileTransform(
        hProfileList,
        2,
        &dwIntent,
        1,
        NORMAL_MODE,
        INDEX_DONT_CARE
    );

    if (nullptr == hColorTransform) goto EXIT;

    /* transform colors */
    DWORD dwWidth = nBytes / 3; // 3 channels per pixel, 8 bits per channel, RGB format

    bResult = TranslateBitmapBits(
        hColorTransform,
        srcRgbColors,
        BM_RGBTRIPLETS,
        dwWidth,        // bitmap width
        1,              // bitmap height
        0,
        dstRgbColors,
        BM_RGBTRIPLETS,
        0,
        nullptr,
        0
    );

EXIT:
    /* free resources */
    if (nullptr != hColorTransform) {
        DeleteColorTransform(hColorTransform);
    }

    if (nullptr != hSrcProfile) {
        CloseColorProfile(hSrcProfile);
    }

    if (nullptr != hDstProfile) {
        CloseColorProfile(hDstProfile);
    }

    return bResult == FALSE ? false : true;
}

/* example usage */
BYTE srcBitmapData[3];
srcBitmapData[0] = 0x1c;
srcBitmapData[1] = 0x1a;
srcBitmapData[2] = 0x1a;

BYTE dstOutputBitmapData[3];
bool bResult = traslateColors(srcBitmapData, dstOutputBitmapData, 3);

希望这可以帮助。

于 2016-03-31T11:07:36.937 回答