Messsage test
GO~~ GO~
Code test
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#if defined(__BORLANDC__) // fix for BCB
#undef LCS_SIGNATURE
#undef LCS_sRGB
#define LCS_SIGNATURE 'COSP'
#define LCS_sRGB 'BGRs'
#endif
// needed for ICM
#include
#pragma link "mscms.lib"
#include
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(
TComponent* Owner): TForm(Owner),
OrigBitmap_(new Graphics::TBitmap())
{
OrigBitmap_->Assign(
Image1->Picture->Bitmap
);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::
RestoreButtonClick(TObject *Sender)
{
Image1->Picture->Bitmap->Assign(
OrigBitmap_.get()
);
ShowMessage("Bitmap restored.");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::
ColorMatchButtonClick(TObject *Sender)
{
// get the file-name of the
// monitor's default profile
TCHAR monitor_profilename[MAX_PATH];
const HDC hScreenDC = GetDC(NULL);
DWORD num_chars = MAX_PATH;
const bool got_profile =
GetICMProfile(
hScreenDC, &num_chars,
monitor_profilename
);
ReleaseDC(NULL, hScreenDC);
if (!got_profile)
{
throw EWin32Error("no profile");
}
// initialize a PROFILE structure
PROFILE monitor_profile;
monitor_profile.dwType =
PROFILE_FILENAME;
monitor_profile.pProfileData =
static_cast
(monitor_profilename);
monitor_profile.cbDataSize =
(lstrlen(monitor_profilename) 1) *
sizeof(TCHAR);
// create the color profile object
HPROFILE hMonitorProfile =
OpenColorProfile(
&monitor_profile, PROFILE_READ,
FILE_SHARE_READ, OPEN_EXISTING
);
if (hMonitorProfile == NULL)
{
throw EWin32Error(
"OpenColorProfile() failed"
);
}
try
{
// validate the profile's contents
BOOL is_monitor_valid = FALSE;
IsColorProfileValid(
hMonitorProfile, &is_monitor_valid
);
if (!is_monitor_valid)
{
throw EWin32Error("bad profile");
}
// initialize a LOGCOLORSPACE struct
LOGCOLORSPACE lcs = {
LCS_SIGNATURE, 0x400,
sizeof(LOGCOLORSPACE)
};
// sRGB color space
lcs.lcsCSType = LCS_sRGB;
// perceptual rendering intent
lcs.lcsIntent = LCS_GM_IMAGES;
// create a color transform object
const HTRANSFORM hColorTransform =
CreateColorTransform(
&lcs, hMonitorProfile, NULL,
NORMAL_MODE ENABLE_GAMUT_CHECKING
);
if (hColorTransform == NULL)
{
throw EWin32Error(
"CreateColorTransform() failed"
);
}
try
{
//
// color-match the bitmap...
//
// grab a reference to
// the TBitmap object
Graphics::TBitmap& Bitmap =
*Image1->Picture->Bitmap;
Bitmap.PixelFormat = pf24bit;
// grab a pointer to the pixels
BITMAP bmp;
GetObject(Bitmap.Handle,
sizeof(BITMAP), &bmp
);
// color-match the bitmap's pixels
const bool match_ok =
TranslateBitmapBits(
hColorTransform,
bmp.bmBits, BM_RGBTRIPLETS,
bmp.bmWidth, bmp.bmHeight,
NULL, bmp.bmBits,
BM_RGBTRIPLETS, NULL, NULL, 0
);
if (match_ok)
{
// repaint
Image1->Refresh();
ShowMessage(
"Bitmap color-matched."
);
}
else
{
throw EWin32Error(
"TranslateBitmapBits() failed"
);
}
}
catch (...)
{
// destroy the color
// transform object
DeleteColorTransform(
hColorTransform
);
throw;
}
}
catch (...)
{
// destroy the color profile object
CloseColorProfile(hMonitorProfile);
throw;
}
// destroy the color profile object
CloseColorProfile(hMonitorProfile);
}
//---------------------------------------------------------------------------