c++ - 从 ProPhoto RGB 到显示器配置文件的颜色转换

标签 c++ wcs color-profile color-management

我已经为这个问题苦苦挣扎了很多天,找不到任何解释!

背景:

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

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

问题:

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

correct & error images

测试:

为了简化问题,我写了一些翻译单一颜色的测试代码,但测试结果确实让我很困惑。源颜色“c0”是RGB(0,0,65535),但输出颜色“c1”是RGB(0,0,0)!!函数“CheckColor”失败并出现错误“无效参数”...

怎么会这样?我做错了什么吗?

您可以在此处下载两个配置文件:Color Profiles

非常感谢!

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 );

最佳答案

我在 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);

希望这对您有所帮助。

关于c++ - 从 ProPhoto RGB 到显示器配置文件的颜色转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22626380/

相关文章:

ios - "Display P3"iOS模拟器截图

c++ - 使用异步调用与在线程中使用同步调用相同吗?

c++ - Vector::iterator 在与 begin 比较时崩溃

java - 如何为 GNU/Linux 设置正确的字符编码

c - C语言打印宽字符数组中的元素

cmyk - 颜色配置文件转换

java - 如何在java中获取图像的嵌入式ICC配置文件

c++ - ldd 输出静态链接到共享库

c++ - 初始化一个未知维度的数组

java - EC_GENERIC_REDIRECTVIEW 仅在生产服务器上且加载 30 秒后不起作用