00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "mitkPicFileWriter.h"
00019 #include "mitkPicFileReader.h"
00020
00021 extern "C"
00022 {
00023 size_t _mitkIpPicFWrite( const void *ptr, size_t size, size_t nitems, mitkIpPicFile_t stream);
00024 }
00025
00026 mitk::PicFileWriter::PicFileWriter()
00027 {
00028 this->SetNumberOfRequiredInputs( 1 );
00029 }
00030
00031 mitk::PicFileWriter::~PicFileWriter()
00032 {
00033 }
00034
00035 void mitk::PicFileWriter::GenerateData()
00036 {
00037 if ( m_FileName == "" )
00038 {
00039 itkWarningMacro( << "Sorry, filename has not been set!" );
00040 return ;
00041 }
00042
00043 std::ofstream testfilehandle( m_FileName.c_str(), std::ios::out);
00044 if (!testfilehandle.good())
00045 {
00046 testfilehandle.close();
00047 itkExceptionMacro(<<"File location '" << m_FileName << "' not writeable");
00048 }
00049 else
00050 {
00051 testfilehandle.close();
00052 }
00053
00054 Image::Pointer input = const_cast<Image*>(this->GetInput());
00055
00056 if ( input.IsNull() )
00057 {
00058 itkExceptionMacro(<< "Nothing to write: Input is NULL." );
00059 }
00060
00061 mitkIpPicDescriptor * picImage = input->GetPic();
00062 SlicedGeometry3D* slicedGeometry = input->GetSlicedGeometry();
00063 if (slicedGeometry != NULL)
00064 {
00065
00066 const Vector3D & spacing = slicedGeometry->GetSpacing();
00067 mitkIpPicTSV_t *pixelSizeTag;
00068 pixelSizeTag = mitkIpPicQueryTag( picImage, "REAL PIXEL SIZE" );
00069 if (!pixelSizeTag)
00070 {
00071 pixelSizeTag = (mitkIpPicTSV_t *) malloc( sizeof(mitkIpPicTSV_t) );
00072 pixelSizeTag->type = mitkIpPicFloat;
00073 pixelSizeTag->bpe = 32;
00074 strcpy(pixelSizeTag->tag, "REAL PIXEL SIZE");
00075 pixelSizeTag->dim = 1;
00076 pixelSizeTag->n[0] = 3;
00077 pixelSizeTag->value = malloc( sizeof(float) * 3 );
00078 mitkIpPicAddTag (picImage, pixelSizeTag);
00079 }
00080 ((float*)pixelSizeTag->value)[0] = spacing[0];
00081 ((float*)pixelSizeTag->value)[1] = spacing[1];
00082 ((float*)pixelSizeTag->value)[2] = spacing[2];
00083
00084
00085
00086 mitkIpPicTSV_t *geometryTag;
00087 geometryTag = mitkIpPicQueryTag( picImage, "ISG" );
00088 if (!geometryTag)
00089 {
00090 geometryTag = (mitkIpPicTSV_t *) malloc( sizeof(mitkIpPicTSV_t) );
00091 geometryTag->type = mitkIpPicFloat;
00092 geometryTag->bpe = 32;
00093 strcpy(geometryTag->tag, "ISG");
00094 geometryTag->dim = 2;
00095 geometryTag->n[0] = 3;
00096 geometryTag->n[1] = 4;
00097 geometryTag->value = malloc( sizeof(float) * 3 * 4 );
00098 mitkIpPicAddTag (picImage, geometryTag);
00099 }
00100 const AffineTransform3D::OffsetType& offset = slicedGeometry->GetIndexToWorldTransform()->GetOffset();
00101 ((float*)geometryTag->value)[0] = offset[0];
00102 ((float*)geometryTag->value)[1] = offset[1];
00103 ((float*)geometryTag->value)[2] = offset[2];
00104
00105 const AffineTransform3D::MatrixType& matrix = slicedGeometry->GetIndexToWorldTransform()->GetMatrix();
00106 const AffineTransform3D::MatrixType::ValueType* row0 = matrix[0];
00107 const AffineTransform3D::MatrixType::ValueType* row1 = matrix[1];
00108 const AffineTransform3D::MatrixType::ValueType* row2 = matrix[2];
00109
00110 Vector3D v;
00111
00112 FillVector3D(v, row0[0], row1[0], row2[0]);
00113 v.Normalize();
00114 ((float*)geometryTag->value)[3] = v[0];
00115 ((float*)geometryTag->value)[4] = v[1];
00116 ((float*)geometryTag->value)[5] = v[2];
00117
00118 FillVector3D(v, row0[1], row1[1], row2[1]);
00119 v.Normalize();
00120 ((float*)geometryTag->value)[6] = v[0];
00121 ((float*)geometryTag->value)[7] = v[1];
00122 ((float*)geometryTag->value)[8] = v[2];
00123
00124 ((float*)geometryTag->value)[9] = spacing[0];
00125 ((float*)geometryTag->value)[10] = spacing[1];
00126 ((float*)geometryTag->value)[11] = spacing[2];
00127 }
00128 PicFileReader::ConvertHandedness(picImage);
00129
00130
00131
00132 int ret = MITKIpPicPut((char*)(m_FileName.c_str()), picImage);
00133
00134 if (ret != 0)
00135 {
00136 PicFileReader::ConvertHandedness(picImage);
00137 throw std::ios_base::failure("Error during .pic file writing in "__FILE__);
00138 }
00139
00140 PicFileReader::ConvertHandedness(picImage);
00141 }
00142
00143 void mitk::PicFileWriter::SetInput( Image* image )
00144 {
00145 this->ProcessObject::SetNthInput( 0, image );
00146 }
00147
00148 const mitk::Image* mitk::PicFileWriter::GetInput()
00149 {
00150 if ( this->GetNumberOfInputs() < 1 )
00151 {
00152 return NULL;
00153 }
00154 else
00155 {
00156 return static_cast< const Image * >( this->ProcessObject::GetInput( 0 ) );
00157 }
00158 }
00159
00160 int mitk::PicFileWriter::MITKIpPicPut( char *outfile_name, mitkIpPicDescriptor *pic )
00161 {
00162 FILE* outfile;
00163
00164 mitkIpUInt4_t len;
00165 mitkIpUInt4_t tags_len;
00166
00167 if( pic->info->write_protect )
00168 {
00169 fprintf( stderr, "mitkIpPicPut: sorry, can't write (missing tags !!!)\n" );
00170
00171 }
00172
00173 if( mitkIpPicEncryptionType(pic) != ' ' )
00174 {
00175 fprintf( stderr, "mitkIpPicPut: warning: was encrypted !!!\n" );
00176 }
00177
00178 if( outfile_name == NULL )
00179 outfile = stdout;
00180 else if( strcmp(outfile_name, "stdout") == 0 )
00181 outfile = stdout;
00182 else
00183 {
00184 mitkIpPicRemoveFile( outfile_name );
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 outfile = fopen( outfile_name, "wb" );
00200 }
00201
00202
00203 if( outfile == NULL )
00204 {
00205 fprintf( stderr, "mitkIpPicPut: sorry, error opening outfile\n" );
00206 return( -1 );
00207 }
00208
00209 tags_len = _mitkIpPicTagsSize( pic->info->tags_head );
00210
00211 len = tags_len + 3 * sizeof(mitkIpUInt4_t)
00212 + pic->dim * sizeof(mitkIpUInt4_t);
00213
00214
00215 if( mitkIpPicEncryptionType(pic) == ' ' )
00216 mitkIpPicFWrite( mitkIpPicVERSION, 1, sizeof(mitkIpPicTag_t), outfile );
00217 else
00218 mitkIpPicFWrite( pic->info->version, 1, sizeof(mitkIpPicTag_t), outfile );
00219
00220 mitkIpPicFWriteLE( &len, sizeof(mitkIpUInt4_t), 1, outfile );
00221
00222 mitkIpPicFWriteLE( &(pic->type), sizeof(mitkIpUInt4_t), 1, outfile );
00223 mitkIpPicFWriteLE( &(pic->bpe), sizeof(mitkIpUInt4_t), 1, outfile );
00224 mitkIpPicFWriteLE( &(pic->dim), sizeof(mitkIpUInt4_t), 1, outfile );
00225
00226 mitkIpPicFWriteLE( pic->n, sizeof(mitkIpUInt4_t), pic->dim, outfile );
00227
00228 _mitkIpPicWriteTags( pic->info->tags_head, outfile, mitkIpPicEncryptionType(pic) );
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 pic->info->pixel_start_in_file = ftell( outfile );
00239
00240 if( pic->data )
00241 {
00242 size_t number_of_elements = _mitkIpPicElements(pic);
00243 size_t bytes_per_element = pic->bpe / 8;
00244 size_t number_of_bytes = number_of_elements * bytes_per_element;
00245 size_t block_size = 1024*1024;
00246 size_t number_of_blocks = number_of_bytes / block_size;
00247 size_t remaining_bytes = number_of_bytes % block_size;
00248 size_t bytes_written = 0;
00249 size_t block_nr = 0;
00250 mitkIpUInt1_t* data = (mitkIpUInt1_t*) pic->data;
00251
00252 assert( data != NULL );
00253
00254 if( pic->type == mitkIpPicNonUniform )
00255 {
00256 for ( block_nr = 0 ; block_nr < number_of_blocks ; ++block_nr )
00257 bytes_written += mitkIpPicFWrite( data + ( block_nr * block_size ), 1, block_size, outfile );
00258 bytes_written += mitkIpPicFWrite( data + ( number_of_blocks * block_size ), 1, remaining_bytes, outfile );
00259 }
00260 else
00261 {
00262 for ( block_nr = 0 ; block_nr < number_of_blocks ; ++block_nr )
00263 bytes_written += mitkIpPicFWriteLE( data + ( block_nr * block_size ), 1, block_size, outfile );
00264 bytes_written += mitkIpPicFWriteLE( data + ( number_of_blocks * block_size ), 1, remaining_bytes, outfile );
00265 }
00266
00267 if ( bytes_written != number_of_bytes )
00268 {
00269 fprintf( stderr, "Error while writing (ferror indicates %u), only %u bytes were written! Eof indicator is %u.\n", ferror(outfile), ( (unsigned int) ( bytes_written ) ), feof(outfile) );
00270 fclose( outfile );
00271 return( -1 );
00272 }
00273 }
00274
00275 if( outfile != stdout )
00276 {
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 fclose( outfile );
00287 }
00288
00289 return( 0 );
00290 }
00291
00292 std::vector<std::string> mitk::PicFileWriter::GetPossibleFileExtensions()
00293 {
00294 std::vector<std::string> possibleFileExtensions;
00295 possibleFileExtensions.push_back(".pic");
00296 return possibleFileExtensions;
00297 }