Available in the LEADTOOLS Imaging toolkit. |
SetTag example for C++ 4.0 and later
void CTest4Dlg::OnSetAndReadTag()
{
COleVariant myArray;
long *pData;
SAFEARRAYBOUND rgsabound;
// create a variant containing 4 elements. Note that this
// is needed only if we use the TAG_LONG tags...
rgsabound.lLbound = 0;
rgsabound.cElements = 4;
V_ARRAY(&myArray) = SafeArrayCreate(VT_I4, 1, &rgsabound);
if(!V_ARRAY(&myArray))
{
MessageBox("Not enough memory");
return;
}
// initialize the array
V_VT(&myArray) = VT_ARRAY|VT_I4;
SafeArrayAccessData(V_ARRAY(&myArray), (void HUGEP * FAR*)&pData);
pData[0] = 1;
pData[1] = 2;
pData[2] = 3;
pData[3] = 127;
SafeArrayUnaccessData(V_ARRAY(&myArray));
// set a tag using one of the following:
// m_Lead1.SetTag(32768, TAG_ASCII, 0, COleVariant("My text string"));
// m_Lead1.SetTag(32768, TAG_BYTE, 4, myArray);
// m_Lead1.SetTag(32768, TAG_DOUBLE, 4, myArray);
// m_Lead1.SetTag(32768, TAG_FLOAT, 4, myArray);
// m_Lead1.SetTag(32768, TAG_LONG, 4, myArray);
// m_Lead1.SetTag(32768, TAG_RATIONAL, 2, myArray);
// m_Lead1.SetTag(32768, TAG_SBYTE, 4, myArray);
// m_Lead1.SetTag(32768, TAG_SHORT, 4, myArray);
// m_Lead1.SetTag(32768, TAG_SLONG, 4, myArray);
// m_Lead1.SetTag(32768, TAG_SRATIONAL, 2, myArray);
// m_Lead1.SetTag(32768, TAG_SSHORT, 4, myArray);
m_Lead1.SetTag(32768, TAG_UNDEFINED, 4, myArray);
// Write the tag. This file must already exist
m_Lead1.WriteTag("Test.tif", 1);
OnReadTag();
}
void CTestDlg::OnReadTag()
{
// read the tag back
m_Lead1.ReadTag("Test.tif", 1, 32768L);
GetAndDisplayTag(0);
}
void CTestDlg::GetAndDisplayTag(long lTag)
{
COleVariant myVar(m_Lead1.GetTagData(lTag));
int uTagType = m_Lead1.GetTagType(lTag);
long lTagCount = m_Lead1.GetTagCount(lTag);
CString MsgString, Str1;
MsgString.Format("Type = %d", uTagType);
Str1.Format("\r\nCount = %ld", lTagCount);
MsgString += Str1;
if(uTagType == TAG_ASCII)
{
MsgString += "\r\nData = ";
MsgString += CString(V_BSTR(&myVar));
}
else if(uTagType == TAG_RATIONAL || uTagType == TAG_SRATIONAL)
{
long *pData;
SafeArrayAccessData(V_ARRAY(&myVar), (void **)&pData);
for(long i = 0; i < lTagCount; i++)
{
Str1.Format("\r\nData(%d) = %ld/%ld", i, pData[i*2], pData[i*2+1]);
MsgString += Str1;
}
SafeArrayUnaccessData(V_ARRAY(&myVar));
}
else
{
void *pData;
SafeArrayAccessData(V_ARRAY(&myVar), &pData);
for(long i = 0; i < lTagCount; i++)
{
switch(uTagType)
{
case TAG_LONG:
Str1.Format("\r\nData(%d) = %u", i, ((unsigned long*)pData)[i]); break;
case TAG_SLONG:
Str1.Format("\r\nData(%d) = %d", i, ((signed long*)pData)[i]); break;
case TAG_SHORT:
Str1.Format("\r\nData(%d) = %u", i, (int)((unsigned short*)pData)[i]); break;
case TAG_SSHORT:
Str1.Format("\r\nData(%d) = %d", i, (int)((signed short*)pData)[i]); break;
case TAG_BYTE:
case TAG_UNDEFINED:
Str1.Format("\r\nData(%d) = %u", i, (int)((unsigned char*)pData)[i]); break;
case TAG_SBYTE:
Str1.Format("\r\nData(%d) = %d", i, (int)((signed char*)pData)[i]); break;
case TAG_FLOAT:
Str1.Format("\r\nData(%d) = %f", i, (double)((float*)pData)[i]); break;
case TAG_DOUBLE:
Str1.Format("\r\nData(%d) = %f", i, ((double*)pData)[i]); break;
}
MsgString += Str1;
}
SafeArrayUnaccessData(V_ARRAY(&myVar));
}
MessageBox(MsgString);
}
void CTestDlg::OnGetTag()
{
GetAndDisplayTag (32768L);
}