00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "mitkPicFileReader.h"
00020 #include <itkImageFileReader.h>
00021
00022 extern "C"
00023 {
00024 mitkIpPicDescriptor * MITKipPicGet( char *infile_name, mitkIpPicDescriptor *pic );
00025 mitkIpPicDescriptor * MITKipPicGetTags( char *infile_name, mitkIpPicDescriptor *pic );
00026 }
00027
00028 void mitk::PicFileReader::GenerateOutputInformation()
00029 {
00030 Image::Pointer output = this->GetOutput();
00031
00032 if ((output->IsInitialized()) && (this->GetMTime() <= m_ReadHeaderTime.GetMTime()))
00033 return;
00034
00035 itkDebugMacro(<<"Reading file for GenerateOutputInformation()" << m_FileName);
00036
00037
00038
00039 if ( m_FileName == "" && m_FilePrefix == "" )
00040 {
00041 throw itk::ImageFileReaderException(__FILE__, __LINE__, "One of FileName or FilePrefix must be non-empty");
00042 }
00043
00044 if( m_FileName != "")
00045 {
00046 mitkIpPicDescriptor* header=mitkIpPicGetHeader(const_cast<char *>(m_FileName.c_str()), NULL);
00047
00048 header=MITKipPicGetTags(const_cast<char *>(m_FileName.c_str()), header);
00049
00050 int channels = 1;
00051
00052 mitkIpPicTSV_t *tsv;
00053 if ( (tsv = mitkIpPicQueryTag( header, "SOURCE HEADER" )) != NULL)
00054 {
00055 if(tsv->n[0]>1e+06)
00056 {
00057 mitkIpPicTSV_t *tsvSH;
00058 tsvSH = mitkIpPicDelTag( header, "SOURCE HEADER" );
00059 mitkIpPicFreeTag(tsvSH);
00060 }
00061 }
00062 if ( (tsv = mitkIpPicQueryTag( header, "ICON80x80" )) != NULL)
00063 {
00064 mitkIpPicTSV_t *tsvSH;
00065 tsvSH = mitkIpPicDelTag( header, "ICON80x80" );
00066 mitkIpPicFreeTag(tsvSH);
00067 }
00068 if ( (tsv = mitkIpPicQueryTag( header, "VELOCITY" )) != NULL)
00069 {
00070 ++channels;
00071 mitkIpPicDelTag( header, "VELOCITY" );
00072 }
00073
00074 if( header == NULL)
00075 {
00076 itk::ImageFileReaderException e(__FILE__, __LINE__);
00077 itk::OStringStream msg;
00078 msg << " Could not read file "
00079 << m_FileName.c_str();
00080 e.SetDescription(msg.str().c_str());
00081 throw e;
00082 return;
00083 }
00084
00085 output->Initialize(header, channels);
00086 mitkIpPicFree ( header );
00087 }
00088 else
00089 {
00090 int numberOfImages=0;
00091 m_StartFileIndex=0;
00092
00093 mitkIpPicDescriptor* header=NULL;
00094
00095 char fullName[1024];
00096
00097 while(m_StartFileIndex<10)
00098 {
00099 sprintf(fullName, m_FilePattern.c_str(), m_FilePrefix.c_str(), m_StartFileIndex+numberOfImages);
00100 FILE * f=fopen(fullName,"r");
00101 if(f==NULL)
00102 {
00103
00104 if(numberOfImages>0)
00105 break;
00106
00107 ++m_StartFileIndex;
00108 }
00109 else
00110 {
00111 fclose(f);
00112
00113
00114 if(header==NULL)
00115 {
00116 header=mitkIpPicGetHeader(fullName, NULL);
00117 header=MITKipPicGetTags(fullName, header);
00118 }
00119 ++numberOfImages;
00120 }
00121 }
00122
00123 printf("\n numberofimages %d\n",numberOfImages);
00124
00125 if(numberOfImages==0)
00126 {
00127 itk::ImageFileReaderException e(__FILE__, __LINE__);
00128 itk::OStringStream msg;
00129 msg << "no images found";
00130 e.SetDescription(msg.str().c_str());
00131 throw e;
00132 return;
00133 }
00134
00135
00136 if(numberOfImages>1)
00137 {
00138 printf("\n numberofimages %d > 1\n",numberOfImages);
00139 header->dim=3;
00140 header->n[2]=numberOfImages;
00141 }
00142
00143 printf(" \ninitialisize output\n");
00144 output->Initialize(header);
00145 mitkIpPicFree ( header );
00146 }
00147
00148 m_ReadHeaderTime.Modified();
00149 }
00150
00151 void mitk::PicFileReader::ConvertHandedness(mitkIpPicDescriptor* pic)
00152 {
00153
00154 if(pic->dim>=3)
00155 {
00156 mitkIpPicDescriptor* slice=mitkIpPicCopyHeader(pic, NULL);
00157 slice->dim=2;
00158 size_t size=_mitkIpPicSize(slice);
00159 slice->data=malloc(size);
00160
00161 size_t v, volumes = (pic->dim>3? pic->n[3] : 1);
00162 size_t volume_size = size*pic->n[2];
00163
00164 for(v=0; v<volumes; ++v)
00165 {
00166 unsigned char *p_first=(unsigned char *)pic->data;
00167 unsigned char *p_last=(unsigned char *)pic->data;
00168 p_first+=v*volume_size;
00169 p_last+=size*(pic->n[2]-1)+v*volume_size;
00170
00171 size_t i, smid=pic->n[2]/2;
00172 for(i=0; i<smid;++i,p_last-=size, p_first+=size)
00173 {
00174 memcpy(slice->data, p_last, size);
00175 memcpy(p_last, p_first, size);
00176 memcpy(p_first, slice->data, size);
00177 }
00178 }
00179 mitkIpPicFree(slice);
00180 }
00181 }
00182
00183 void mitk::PicFileReader::GenerateData()
00184 {
00185 Image::Pointer output = this->GetOutput();
00186
00187
00188
00189 if ( m_FileName == "" && m_FilePrefix == "" )
00190 {
00191 throw itk::ImageFileReaderException(__FILE__, __LINE__, "One of FileName or FilePrefix must be non-empty");
00192 }
00193
00194 if( m_FileName != "")
00195 {
00196 mitkIpPicDescriptor* pic=MITKipPicGet(const_cast<char *>(m_FileName.c_str()), output->GetPic());
00197
00198 ConvertHandedness(pic);
00199
00200 mitkIpPicTSV_t *tsv;
00201 if ( (tsv = mitkIpPicQueryTag( pic, "SOURCE HEADER" )) != NULL)
00202 {
00203 if(tsv->n[0]>1e+06)
00204 {
00205 mitkIpPicTSV_t *tsvSH;
00206 tsvSH = mitkIpPicDelTag( pic, "SOURCE HEADER" );
00207 mitkIpPicFreeTag(tsvSH);
00208 }
00209 }
00210 if ( (tsv = mitkIpPicQueryTag( pic, "ICON80x80" )) != NULL)
00211 {
00212 mitkIpPicTSV_t *tsvSH;
00213 tsvSH = mitkIpPicDelTag( pic, "ICON80x80" );
00214 mitkIpPicFreeTag(tsvSH);
00215 }
00216 if ( (tsv = mitkIpPicQueryTag( pic, "VELOCITY" )) != NULL)
00217 {
00218 mitkIpPicDescriptor* header = mitkIpPicCopyHeader(pic, NULL);
00219 header->data = tsv->value;
00220 ConvertHandedness(header);
00221 output->SetChannel(header->data, 1);
00222 header->data = NULL;
00223 mitkIpPicFree(header);
00224 mitkIpPicDelTag( pic, "VELOCITY" );
00225 }
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 }
00251 else
00252 {
00253 int position;
00254 mitkIpPicDescriptor* pic=NULL;
00255
00256 int zDim=(output->GetDimension()>2?output->GetDimensions()[2]:1);
00257 printf("\n zdim is %u \n",zDim);
00258
00259 for (position = 0; position < zDim; ++position)
00260 {
00261 char fullName[1024];
00262
00263 sprintf(fullName, m_FilePattern.c_str(), m_FilePrefix.c_str(), m_StartFileIndex+position);
00264
00265 pic=MITKipPicGet(fullName, pic);
00266 if(pic==NULL)
00267 {
00268 itkDebugMacro("Pic file '" << fullName << "' does not exist.");
00269 }
00270 else
00271 if(output->SetPicSlice(pic, position)==false)
00272 {
00273 itkDebugMacro("Image '" << fullName << "' could not be added to Image.");
00274 }
00275 }
00276 if(pic!=NULL)
00277 mitkIpPicFree(pic);
00278 }
00279 }
00280
00281 void mitk::PicFileReader::EnlargeOutputRequestedRegion(itk::DataObject *output)
00282 {
00283 output->SetRequestedRegionToLargestPossibleRegion();
00284 }
00285
00286 bool mitk::PicFileReader::CanReadFile(const std::string filename, const std::string filePrefix, const std::string filePattern)
00287 {
00288
00289 if( filename == "" )
00290 {
00291
00292 return false;
00293 }
00294
00295
00296 if( filePattern != "" && filePrefix != "" )
00297 return false;
00298
00299 bool extensionFound = false;
00300 std::string::size_type PICPos = filename.rfind(".pic");
00301 if ((PICPos != std::string::npos)
00302 && (PICPos == filename.length() - 4))
00303 {
00304 extensionFound = true;
00305 }
00306
00307 PICPos = filename.rfind(".PIC");
00308 if ((PICPos != std::string::npos)
00309 && (PICPos == filename.length() - 4))
00310 {
00311 extensionFound = true;
00312 }
00313
00314 PICPos = filename.rfind(".pic.gz");
00315 if ((PICPos != std::string::npos)
00316 && (PICPos == filename.length() - 7))
00317 {
00318 extensionFound = true;
00319 }
00320
00321 PICPos = filename.rfind(".seq");
00322 if ((PICPos != std::string::npos)
00323 && (PICPos == filename.length() - 4))
00324 {
00325 extensionFound = true;
00326 }
00327
00328 if( !extensionFound )
00329 {
00330
00331 return false;
00332 }
00333
00334 return true;
00335 }
00336
00337 mitk::PicFileReader::PicFileReader()
00338 : m_FileName(""), m_FilePrefix(""), m_FilePattern("")
00339 {
00340 }
00341
00342 mitk::PicFileReader::~PicFileReader()
00343 {
00344 }