1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
/*=========================================================================
Program: The OpenIGTLink Library
Language: C++
Web page: http://openigtlink.org/
Copyright (c) Insight Software Consortium. All rights reserved.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "igtlLabelMetaMessage.h"
#include "igtl_header.h"
#include "igtl_imgmeta.h"
#include "igtl_lbmeta.h"
#include "igtl_image.h"
// Disable warning C4996 (strncpy() may be unsafe) in Windows.
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <vector>
namespace igtl {
//----------------------------------------------------------------------
// igtl::LabelMetaElement class
LabelMetaElement::LabelMetaElement() : Object()
{
this->m_Name = "";
this->m_DeviceName = "";
this->m_Label = 0;
this->m_RGBA[0] = 0;
this->m_RGBA[1] = 0;
this->m_RGBA[2] = 0;
this->m_RGBA[3] = 0;
this->m_Size[0] = 0;
this->m_Size[1] = 0;
this->m_Size[2] = 0;
this->m_Owner = "";
}
LabelMetaElement::~LabelMetaElement()
{
}
int LabelMetaElement::SetName(const char* name)
{
if (strlen(name) <= IGTL_LBMETA_LEN_NAME)
{
this->m_Name = name;
return 1;
}
else
{
return 0;
}
}
int LabelMetaElement::SetDeviceName(const char* devname)
{
if (strlen(devname) <= IGTL_LBMETA_LEN_DEVICE_NAME)
{
this->m_DeviceName = devname;
return 1;
}
else
{
return 0;
}
}
void LabelMetaElement::SetRGBA(igtlUint8 rgba[4])
{
this->m_RGBA[0] = rgba[0];
this->m_RGBA[1] = rgba[1];
this->m_RGBA[2] = rgba[2];
this->m_RGBA[3] = rgba[3];
}
void LabelMetaElement::SetRGBA(igtlUint8 r, igtlUint8 g, igtlUint8 b, igtlUint8 a)
{
this->m_RGBA[0] = r;
this->m_RGBA[1] = g;
this->m_RGBA[2] = b;
this->m_RGBA[3] = a;
}
void LabelMetaElement::GetRGBA(igtlUint8* rgba)
{
rgba[0] = this->m_RGBA[0];
rgba[1] = this->m_RGBA[1];
rgba[2] = this->m_RGBA[2];
rgba[3] = this->m_RGBA[3];
}
void LabelMetaElement::GetRGBA(igtlUint8& r, igtlUint8& g, igtlUint8& b, igtlUint8& a)
{
r = this->m_RGBA[0];
g = this->m_RGBA[1];
b = this->m_RGBA[2];
a = this->m_RGBA[3];
}
void LabelMetaElement::SetSize(igtlUint16 size[3])
{
this->m_Size[0] = size[0];
this->m_Size[1] = size[1];
this->m_Size[2] = size[2];
}
void LabelMetaElement::SetSize(igtlUint16 si, igtlUint16 sj, igtlUint16 sk)
{
this->m_Size[0] = si;
this->m_Size[1] = sj;
this->m_Size[2] = sk;
}
void LabelMetaElement::GetSize(igtlUint16* size)
{
size[0] = this->m_Size[0];
size[1] = this->m_Size[1];
size[2] = this->m_Size[2];
}
void LabelMetaElement::GetSize(igtlUint16& si, igtlUint16& sj, igtlUint16& sk)
{
si = this->m_Size[0];
sj = this->m_Size[1];
sk = this->m_Size[2];
}
int LabelMetaElement::SetOwner(const char* owner)
{
if (strlen(owner) <= IGTL_LBMETA_LEN_OWNER)
{
this->m_Owner = owner;
return 1;
}
else
{
return 0;
}
}
//----------------------------------------------------------------------
// igtl::LabelMetaMessage class
LabelMetaMessage::LabelMetaMessage()
{
this->m_SendMessageType = "LBMETA";
this->m_LabelMetaList.clear();
}
LabelMetaMessage::~LabelMetaMessage()
{
}
int LabelMetaMessage::AddLabelMetaElement(LabelMetaElement::Pointer& elem)
{
this->m_LabelMetaList.push_back(elem);
return this->m_LabelMetaList.size();
}
void LabelMetaMessage::ClearLabelMetaElement()
{
this->m_LabelMetaList.clear();
}
int LabelMetaMessage::GetNumberOfLabelMetaElement()
{
return this->m_LabelMetaList.size();
}
void LabelMetaMessage::GetLabelMetaElement(int index, LabelMetaElement::Pointer& elem)
{
if (index >= 0 && index < (int)this->m_LabelMetaList.size())
{
elem = this->m_LabelMetaList[index];
}
}
int LabelMetaMessage::CalculateContentBufferSize()
{
// The body size sum of the header size and status message size.
return IGTL_LBMETA_ELEMENT_SIZE * this->m_LabelMetaList.size();
}
int LabelMetaMessage::PackContent()
{
// Allocate buffer
AllocateBuffer();
igtl_lbmeta_element* element;
element = (igtl_lbmeta_element*)this->m_Content;
std::vector<LabelMetaElement::Pointer>::iterator iter;
for (iter = this->m_LabelMetaList.begin(); iter != this->m_LabelMetaList.end(); iter ++)
{
strncpy((char*)element->name, (*iter)->GetName(), IGTL_LBMETA_LEN_NAME);
strncpy((char*)element->device_name, (*iter)->GetDeviceName(), IGTL_LBMETA_LEN_DEVICE_NAME);
element->label = (*iter)->GetLabel();
element->reserved = 0;
igtlUint8 rgba[4];
(*iter)->GetRGBA(rgba);
element->rgba[0] = rgba[0];
element->rgba[1] = rgba[1];
element->rgba[2] = rgba[2];
element->rgba[3] = rgba[3];
igtlUint16 size[3];
(*iter)->GetSize(size);
element->size[0] = size[0];
element->size[1] = size[1];
element->size[2] = size[2];
strncpy((char*)element->owner, (*iter)->GetOwner(), IGTL_LBMETA_LEN_OWNER);
element ++;
}
igtl_lbmeta_convert_byte_order((igtl_lbmeta_element*)this->m_Content, this->m_LabelMetaList.size());
return 1;
}
int LabelMetaMessage::UnpackContent()
{
this->m_LabelMetaList.clear();
igtl_lbmeta_element* element = (igtl_lbmeta_element*) this->m_Content;
int nElement = igtl_lbmeta_get_data_n(this->m_BodySizeToRead);
igtl_lbmeta_convert_byte_order(element, nElement);
char strbuf[128];
for (int i = 0; i < nElement; i ++)
{
LabelMetaElement::Pointer elemClass = LabelMetaElement::New();
// Add '\n' at the end of each string
// (neccesary for a case, where a string reaches the maximum length.)
strbuf[IGTL_LBMETA_LEN_NAME] = '\n';
strncpy(strbuf, (char*)element->name, IGTL_LBMETA_LEN_NAME);
elemClass->SetName((const char*)strbuf);
strbuf[IGTL_LBMETA_LEN_DEVICE_NAME] = '\n';
strncpy(strbuf, (char*)element->device_name, IGTL_LBMETA_LEN_DEVICE_NAME);
elemClass->SetDeviceName(strbuf);
elemClass->SetLabel(element->label);
elemClass->SetRGBA(element->rgba);
elemClass->SetSize(element->size);
strbuf[IGTL_LBMETA_LEN_OWNER] = '\n';
strncpy(strbuf, (char*)element->owner, IGTL_LBMETA_LEN_OWNER);
elemClass->SetOwner(strbuf);
this->m_LabelMetaList.push_back(elemClass);
element ++;
}
return 1;
}
} // namespace igtl
|