SetTag example for C++ Builder
LEADRasterIO * pRasterIO;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL, IID_ILEADRasterIO, (void**)&pRasterIO);
}
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if(pRasterIO)
pRasterIO-> Release( );
}
void __fastcall TForm1::SetAndReadClick(TObject *Sender)
{
long *pData;
ILEADRasterVariant * pRasterVar= NULL;
CoCreateInstance( CLSID_LEADRasterVariant,
NULL,
CLSCTX_ALL,
IID_ILEADRasterVariant,
(void**)&pRasterVar);
// create a variant containing 4 elements. Note that this
// is needed only if we use the TAG_LONG tags...
pRasterVar->ItemCount = 4;
pRasterVar->set_Type(VALUE_LONG);
pRasterVar->set_LongItemValue(0, 1);
pRasterVar->set_LongItemValue(1, 2);
pRasterVar->set_LongItemValue(2, 3);
pRasterVar->set_LongItemValue(3, 127);
pRasterIO->SetTag(32768, TAG_LONG, 4, pRasterVar);
// Write the tag. This file must already exist
pRasterIO->WriteTag(AnsiToOLESTR("c:\\Test.tif"), 1);
ReadTagClick(this);
if (pRasterVar )
pRasterVar->Release();
}
void __fastcall TForm1::ReadTagClick(TObject *Sender)
{
// read the tag back
pRasterIO->ReadTag (AnsiToOLESTR("c:\\ Test.tif"), 1, 32768L);
GetAndDisplayTag(0);
}
//This function will get a certain tag and display it according to the tag type.
void GetAndDisplayTag (long lTag)
{
int uTagType = pRasterIO->GetTagType (lTag);
long lTagCount = pRasterIO->GetTagCount (lTag);
AnsiString MsgString, Str1;
ILEADRasterVariant* pRasterVar= NULL;
pRasterVar= pRasterIO->GetTagData(lTag);
MsgString = "Type = " + IntToStr(uTagType);
Str1= "\r\nCount = " + IntToStr((int)lTagCount);
MsgString += Str1;
if(uTagType == TAG_ASCII)
{
MsgString += "\r\nData = ";
MsgString += pRasterVar->StringValue;
}
else if(uTagType == TAG_RATIONAL || uTagType == TAG_SRATIONAL)
{
long *pData;
pData= (long*)GlobalAlloc(GHND, pRasterVar->ItemCount);
for (int i= 0; i < pRasterVar->ItemCount; i ++ )
pData[i]= pRasterVar->get_ShortItemValue(i);
for(long i = 0; i < lTagCount; i++)
{
Str1= "\r\nData("+IntToStr((int)i)+")=" + IntToStr((int)pData[i*2]) + IntToStr((int)pData[i*2+1]);
MsgString += Str1;
}
GlobalFree(pData);
}
else
{
int* pData;
pData= (int*)GlobalAlloc(GHND, pRasterVar->ItemCount );
for (int i= 0; i < pRasterVar->ItemCount; i ++ )
pData[i]= pRasterVar->get_ShortItemValue(i);
for(int i = 0; i < lTagCount; i++)
{
switch(uTagType)
{
case TAG_LONG:
Str1= "\r\nData(" + IntToStr(i) +") = " + IntToStr((int)((unsigned long*)pData)[i]);
break;
case TAG_SLONG:
Str1= "\r\nData(" + IntToStr(i) +") = " + IntToStr((int)((signed long*)pData)[i]);
break;
case TAG_SHORT:
Str1= "\r\nData(" + IntToStr(i) +") = " + IntToStr((int)((unsigned short*)pData)[i]);
break;
case TAG_SSHORT:
Str1= "\r\nData(" + IntToStr(i) +") = " + IntToStr((int)((signed short*)pData)[i]);
break;
case TAG_BYTE:
case TAG_UNDEFINED:
Str1= "\r\nData(" + IntToStr(i) +") = " + IntToStr((int)((unsigned char*)pData)[i]);
break;
case TAG_SBYTE:
Str1= "\r\nData(" + IntToStr(i) +") = " + IntToStr((int)((signed char*)pData)[i]);
break;
case TAG_FLOAT:
Str1= "\r\nData(" + IntToStr(i) +") = " + FloatToStr((double)((float*)pData)[i]);
break;
case TAG_DOUBLE:
Str1= "\r\nData(" + IntToStr(i) +") = " + FloatToStr(((double*)pData)[i]);
break;
}
MsgString += Str1;
}
GlobalFree(pData);
}
ShowMessage (MsgString);
}