void mitk::PicFileReader::GenerateOutputInformation() { Image::Pointer output = this->GetOutput(); if ((output->IsInitialized()) && (this->GetMTime() <= m_ReadHeaderTime.GetMTime())) return; itkDebugMacro(<<"Reading file for GenerateOutputInformation()" << m_FileName); // Check to see if we can read the file given the name or prefix // if ( m_FileName == "" && m_FilePrefix == "" ) { throw itk::ImageFileReaderException(__FILE__, __LINE__, "One of FileName or FilePrefix must be non-empty"); } if( m_FileName != "") { mitkIpPicDescriptor* header=mitkIpPicGetHeader(const_cast<char *>(m_FileName.c_str()), NULL); if ( !header ) { throw itk::ImageFileReaderException(__FILE__, __LINE__, "File could not be read."); } header=MITKipPicGetTags(const_cast<char *>(m_FileName.c_str()), header); int channels = 1; mitkIpPicTSV_t *tsv; if ( (tsv = mitkIpPicQueryTag( header, "SOURCE HEADER" )) != NULL) { if(tsv->n[0]>1e+06) { mitkIpPicTSV_t *tsvSH; tsvSH = mitkIpPicDelTag( header, "SOURCE HEADER" ); mitkIpPicFreeTag(tsvSH); } } if ( (tsv = mitkIpPicQueryTag( header, "ICON80x80" )) != NULL) { mitkIpPicTSV_t *tsvSH; tsvSH = mitkIpPicDelTag( header, "ICON80x80" ); mitkIpPicFreeTag(tsvSH); } if ( (tsv = mitkIpPicQueryTag( header, "VELOCITY" )) != NULL) { ++channels; mitkIpPicDelTag( header, "VELOCITY" ); } if( header == NULL || header->bpe == 0) { itk::ImageFileReaderException e(__FILE__, __LINE__); itk::OStringStream msg; msg << " Could not read file " << m_FileName.c_str(); e.SetDescription(msg.str().c_str()); throw e; return; } // First initialize the geometry of the output image by the pic-header SlicedGeometry3D::Pointer slicedGeometry = mitk::SlicedGeometry3D::New(); PicHelper::InitializeEvenlySpaced(header, header->n[2], slicedGeometry); // if pic image only 3D, the n[3] value is not initialized unsigned int timesteps = 1; if( header->dim > 3 ) timesteps = header->n[3]; TimeSlicedGeometry::Pointer timeSliceGeometry = TimeSlicedGeometry::New(); timeSliceGeometry->InitializeEvenlyTimed(slicedGeometry, timesteps); timeSliceGeometry->ImageGeometryOn(); // Cast the pic descriptor to ImageDescriptor and initialize the output output->Initialize( CastToImageDescriptor(header)); output->SetGeometry( timeSliceGeometry ); mitkIpPicFree ( header ); } else { int numberOfImages=0; m_StartFileIndex=0; mitkIpPicDescriptor* header=NULL; char fullName[1024]; while(m_StartFileIndex<10) { sprintf(fullName, m_FilePattern.c_str(), m_FilePrefix.c_str(), m_StartFileIndex+numberOfImages); FILE * f=fopen(fullName,"r"); if(f==NULL) { //already found an image? if(numberOfImages>0) break; //no? let's increase start ++m_StartFileIndex; } else { fclose(f); //only open the header of the first file found, //@warning what to do when images do not have the same size?? if(header==NULL) { header=mitkIpPicGetHeader(fullName, NULL); header=MITKipPicGetTags(fullName, header); } ++numberOfImages; } } printf("\n numberofimages %d\n",numberOfImages); if(numberOfImages==0) { itk::ImageFileReaderException e(__FILE__, __LINE__); itk::OStringStream msg; msg << "no images found"; e.SetDescription(msg.str().c_str()); throw e; return; } //@FIXME: was ist, wenn die Bilder nicht alle gleich gross sind? if(numberOfImages>1) { printf("\n numberofimages %d > 1\n",numberOfImages); header->dim=3; header->n[2]=numberOfImages; } printf(" \ninitialisize output\n"); output->Initialize( CastToImageDescriptor(header) ); mitkIpPicFree ( header ); } m_ReadHeaderTime.Modified(); }
mitk::Image::Pointer mitk::PicFileReader::CreateImage() { Image::Pointer output = Image::New(); std::string fileName = this->GetLocalFileName(); mitkIpPicDescriptor *header = mitkIpPicGetHeader(const_cast<char *>(fileName.c_str()), NULL); if (!header) { mitkThrow() << "File could not be read."; } header = mitkIpPicGetTags(const_cast<char *>(fileName.c_str()), header); int channels = 1; mitkIpPicTSV_t *tsv; if ((tsv = mitkIpPicQueryTag(header, "SOURCE HEADER")) != NULL) { if (tsv->n[0] > 1e+06) { mitkIpPicTSV_t *tsvSH; tsvSH = mitkIpPicDelTag(header, "SOURCE HEADER"); mitkIpPicFreeTag(tsvSH); } } if ((tsv = mitkIpPicQueryTag(header, "ICON80x80")) != NULL) { mitkIpPicTSV_t *tsvSH; tsvSH = mitkIpPicDelTag(header, "ICON80x80"); mitkIpPicFreeTag(tsvSH); } if ((tsv = mitkIpPicQueryTag(header, "VELOCITY")) != NULL) { ++channels; mitkIpPicDelTag(header, "VELOCITY"); } if (header == NULL || header->bpe == 0) { mitkThrow() << " Could not read file " << fileName; } // if pic image only 2D, the n[2] value is not initialized unsigned int slices = 1; if (header->dim == 2) { header->n[2] = slices; } // First initialize the geometry of the output image by the pic-header SlicedGeometry3D::Pointer slicedGeometry = mitk::SlicedGeometry3D::New(); PicHelper::InitializeEvenlySpaced(header, header->n[2], slicedGeometry); // if pic image only 3D, the n[3] value is not initialized unsigned int timesteps = 1; if (header->dim > 3) { timesteps = header->n[3]; } slicedGeometry->ImageGeometryOn(); ProportionalTimeGeometry::Pointer timeGeometry = ProportionalTimeGeometry::New(); timeGeometry->Initialize(slicedGeometry, timesteps); // Cast the pic descriptor to ImageDescriptor and initialize the output output->Initialize(CastToImageDescriptor(header)); output->SetTimeGeometry(timeGeometry); mitkIpPicFree(header); return output; }
void mitkIpPicPutSlice( const char *outfile_name, mitkIpPicDescriptor *pic, mitkIpUInt4_t slice ) { mitkIpPicDescriptor *pic_in; FILE *outfile; size_t ignored; pic_in = mitkIpPicGetHeader( outfile_name, NULL ); if( pic_in == NULL ) { if( slice == 1 ) { mitkIpBool_t compression; pic->n[pic->dim] = 1; pic->dim += 1; compression = mitkIpPicSetWriteCompression( mitkIpFalse ); mitkIpPicPut( outfile_name, pic ); mitkIpPicSetWriteCompression( compression ); pic->dim -= 1; pic->n[pic->dim] = 0; return; } else return; } pic_in = mitkIpPicGetTags( outfile_name, pic_in ); outfile = fopen( outfile_name, "r+b" ); if( outfile == NULL ) { /*ipPrintErr( "mitkIpPicPut: sorry, error opening outfile\n" );*/ /*return();*/ } if( pic->dim != pic_in->dim - 1 ) { fclose( outfile ); return; } else if( pic->n[0] != pic_in->n[0] ) { fclose( outfile ); return; } else if( pic->n[1] != pic_in->n[1] ) { fclose( outfile ); return; } if( slice > pic_in->n[pic_in->dim-1] ) pic_in->n[pic_in->dim-1] += 1; /* write outfile */ /*fseek( outfile, 0, SEEK_SET );*/ rewind( outfile ); ignored = fwrite( mitkIpPicVERSION, 1, sizeof(mitkIpPicTag_t), outfile ); fseek( outfile, sizeof(mitkIpUInt4_t), SEEK_CUR ); /* skip tags_len */ ignored = mitkIpFWriteLE( &(pic_in->type), sizeof(mitkIpUInt4_t), 1, outfile ); ignored = mitkIpFWriteLE( &(pic_in->bpe), sizeof(mitkIpUInt4_t), 1, outfile ); ignored = mitkIpFWriteLE( &(pic_in->dim), sizeof(mitkIpUInt4_t), 1, outfile ); ignored = mitkIpFWriteLE( pic_in->n, sizeof(mitkIpUInt4_t), pic_in->dim, outfile ); fseek( outfile, pic_in->info->pixel_start_in_file + _mitkIpPicSize(pic) * (slice - 1), SEEK_SET ); ignored = mitkIpFWriteLE( pic->data, pic->bpe / 8, _mitkIpPicElements(pic), outfile ); /*fseek( outfile, 0, SEEK_END );*/ fclose( outfile ); mitkIpPicFree(pic_in); /*return();*/ }
bool ToFCameraMITKPlayerController::OpenCameraConnection() { if(!this->m_ConnectionCheck) { try { mitkIpPicDescriptor* pic = NULL; mitkIpPicDescriptor* info = NULL; pic = mitkIpPicGetHeader(const_cast<char *>(this->m_DistanceImageFileName.c_str()), pic); if (pic==NULL) { MITK_ERROR << "Error opening ToF data file " << this->m_InputFileName; return false; } info = mitkIpPicGetTags(const_cast<char *>(this->m_DistanceImageFileName.c_str()), pic); this->m_PixelStartInFile = info->info->pixel_start_in_file; if (pic->dim == 2) { this->m_ToFImageType = ToFImageType2DPlusT; } else if (pic->dim == 3) { this->m_ToFImageType = ToFImageType3D; } else if (pic->dim == 4) { this->m_ToFImageType = ToFImageType2DPlusT; } else { MITK_ERROR << "Error opening ToF data file: Invalid dimension."; return false; } this->m_CaptureWidth = pic->n[0]; this->m_CaptureHeight = pic->n[1]; this->m_PixelNumber = this->m_CaptureWidth*this->m_CaptureHeight; this->m_NumberOfBytes = this->m_PixelNumber * sizeof(float); if (this->m_ToFImageType == ToFImageType2DPlusT) { this->m_NumOfFrames = pic->n[3]; } else { this->m_NumOfFrames = pic->n[2]; } // allocate buffer this->m_DistanceArray = new float[this->m_PixelNumber]; for(int i=0; i<this->m_PixelNumber; i++) {this->m_DistanceArray[i]=0.0;} this->m_AmplitudeArray = new float[this->m_PixelNumber]; for(int i=0; i<this->m_PixelNumber; i++) {this->m_AmplitudeArray[i]=0.0;} this->m_IntensityArray = new float[this->m_PixelNumber]; for(int i=0; i<this->m_PixelNumber; i++) {this->m_IntensityArray[i]=0.0;} OpenPicFileToData(&(this->m_DistanceInfile), this->m_DistanceImageFileName.c_str()); OpenPicFileToData(&(this->m_AmplitudeInfile), this->m_AmplitudeImageFileName.c_str()); OpenPicFileToData(&(this->m_IntensityInfile), this->m_IntensityImageFileName.c_str()); MITK_INFO << "NumOfFrames: " << this->m_NumOfFrames; this->m_ConnectionCheck = true; return this->m_ConnectionCheck; } catch(std::exception& e) { MITK_ERROR << "Error opening ToF data file " << this->m_InputFileName << " " << e.what(); throw std::logic_error("Error opening ToF data file"); return false; } } else return this->m_ConnectionCheck; }