I had the exact same issue as yours on Windows 7 SP1 x64.
It seems that TranslateColors
function is either broken by design or not supposed to be used that way. I think it's Microsoft's fault as they could write more WCS samples on MSDN.
But I managed to solve the problem using TranslateBitmapBits
function instead. Here is a sample:
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);
Hope this helps.