13 #include <vtkImageImport.h>
14 #include <vtkImageData.h>
15 #include <vtkImageExtractComponents.h>
16 #include <vtkImageAppendComponents.h>
18 #include "igtl_status.h"
29 void GetIdentityMatrix(igtl::Matrix4x4& matrix)
31 matrix[0][0] = 1.0; matrix[1][0] = 0.0; matrix[2][0] = 0.0; matrix[3][0] = 0.0;
32 matrix[0][1] = 0.0; matrix[1][1] = 1.0; matrix[2][1] = 0.0; matrix[3][1] = 0.0;
33 matrix[0][2] = 0.0; matrix[1][2] = 0.0; matrix[2][2] = 1.0; matrix[3][2] = 0.0;
34 matrix[0][3] = 0.0; matrix[1][3] = 0.0; matrix[2][3] = 0.0; matrix[3][3] = 1.0;
43 QString newUid = msg->getUid();
44 QString format = this->extractColorFormat(msg->getUid(), &newUid);
63 scalarType = message->GetScalarType();
64 message->GetDimensions(size);
65 message->GetSpacing(spacing);
66 message->GetSubVolume(svsize, svoffset);
67 QString deviceName = message->GetDeviceName();
69 imageImport->SetNumberOfScalarComponents(1);
73 case igtl::ImageMessage::TYPE_INT8:
74 CX_LOG_WARNING() <<
"signed char is not supported. Falling back to unsigned char.";
75 imageImport->SetDataScalarTypeToUnsignedChar();
77 case igtl::ImageMessage::TYPE_UINT8:
78 imageImport->SetDataScalarTypeToUnsignedChar();
80 case igtl::ImageMessage::TYPE_INT16:
81 imageImport->SetDataScalarTypeToShort();
83 case igtl::ImageMessage::TYPE_UINT16:
84 imageImport->SetNumberOfScalarComponents(2);
85 imageImport->SetDataScalarTypeToUnsignedChar();
87 case igtl::ImageMessage::TYPE_INT32:
88 case igtl::ImageMessage::TYPE_UINT32:
89 imageImport->SetNumberOfScalarComponents(4);
90 imageImport->SetDataScalarTypeToUnsignedChar();
92 case igtl::ImageMessage::TYPE_FLOAT32:
93 imageImport->SetDataScalarTypeToFloat();
95 case igtl::ImageMessage::TYPE_FLOAT64:
96 imageImport->SetDataScalarTypeToDouble();
99 CX_LOG_WARNING() <<
"Unknown image type. Falling back to unsigned char.";
100 imageImport->SetDataScalarTypeToUnsignedChar();
103 imageImport->SetDataOrigin(0, 0, 0);
104 imageImport->SetDataSpacing(spacing[0], spacing[1], spacing[2]);
105 imageImport->SetWholeExtent(0, size[0] - 1, 0, size[1] - 1, 0, size[2] - 1);
106 imageImport->SetDataExtentToWholeExtent();
107 imageImport->SetImportVoidPointer(message->GetScalarPointer());
109 imageImport->Modified();
110 imageImport->Update();
112 ImagePtr retval(
new Image(deviceName, imageImport->GetOutput()));
115 retval = this->
decode(retval);
124 int size[] = {256, 256, 1};
125 rawImage->GetDimensions(size);
129 rawImage->GetSpacing(spacingD);
130 spacingF[0] = spacingD[0];
131 spacingF[1] = spacingD[1];
132 spacingF[2] = spacingD[2];
134 int svoffset[] = {0, 0, 0};
137 if (rawImage->GetNumberOfScalarComponents()==4)
139 if (rawImage->GetScalarType()==VTK_UNSIGNED_CHAR)
141 scalarType = igtl::ImageMessage::TYPE_UINT32;
145 if (rawImage->GetNumberOfScalarComponents()==1)
147 if (rawImage->GetScalarType()==VTK_UNSIGNED_SHORT)
149 scalarType = igtl::ImageMessage::TYPE_UINT16;
151 else if (rawImage->GetScalarType()==VTK_UNSIGNED_CHAR)
153 scalarType = igtl::ImageMessage::TYPE_UINT8;
159 std::cerr <<
"Unsupported file type based on vtk " << rawImage->GetScalarTypeAsString() << std::endl;
160 return igtl::ImageMessage::Pointer();
165 igtl::ImageMessage::Pointer imgMsg = igtl::ImageMessage::New();
166 imgMsg->SetDimensions(size);
167 imgMsg->SetSpacing(spacingF);
168 imgMsg->SetScalarType(scalarType);
170 imgMsg->SetSubVolume(svsize, svoffset);
171 imgMsg->AllocateScalars();
176 int fsize = imgMsg->GetImageSize();
177 memcpy(imgMsg->GetScalarPointer(), rawImage->GetScalarPointer(0,0,0), fsize);
179 igtl::Matrix4x4 matrix;
180 GetIdentityMatrix(matrix);
181 imgMsg->SetMatrix(matrix);
189 QString newUid = msg->getUid();
190 QString format = this->extractColorFormat(msg->getUid(), &newUid);
191 vtkImageDataPtr imageRGB = this->createFilterFormat2RGB(format, msg->getBaseVtkImageData());
195 copy->DeepCopy(imageRGB);
198 retval->setAcquisitionTime(msg->getAcquisitionTime());
205 QString format = this->extractColorFormat(deviceName, &dummy);
206 return !format.isEmpty();
209 QString IGTLinkConversionSonixCXLegacy::extractColorFormat(QString deviceName, QString* cleanedDeviceName)
212 QRegExp colorFormat(
"\\[[A-Za-z]{1,4}\\]");
213 if (colorFormat.indexIn(deviceName) > 0)
215 format = colorFormat.cap(0).remove(
"[").remove(
"]");
218 if (cleanedDeviceName)
220 *cleanedDeviceName = deviceName.remove(colorFormat).trimmed();
228 int R = format.indexOf(
'R', 0, Qt::CaseInsensitive);
229 int G = format.indexOf(
'G', 0, Qt::CaseInsensitive);
230 int B = format.indexOf(
'B', 0, Qt::CaseInsensitive);
231 if (R<0 || G<0 || B<0 || format.size()>4)
238 return this->createFilterAny2RGB(R, G, B, input);
243 if (input->GetNumberOfScalarComponents() == 1)
245 if (( input->GetNumberOfScalarComponents()==3 )&&( R==0 )&&( G==1 )&&( B==2 ))
250 splitterRGB->SetInputData(input);
251 splitterRGB->SetComponents(R, G, B);
252 merger->AddInputConnection(splitterRGB->GetOutputPort());
254 return merger->GetOutput();