[Insight-users] strange problem about ITK : Isn't MS-Windows great ?

Luis Ibanez luis . ibanez at kitware . com
Sun, 07 Sep 2003 10:14:32 -0400


Hi Eiii,


First, the good news:

   You problem has nothing to do with ITK.


Then, the bad news:

   You have to love MS-Windows, the operating
   system that by default opens files in ASCII
   mode and takes the liberty of inserting
   carriage return characters whenever it finds
   a new line character.

   You are opening the output file with the
   default options, just "w". Please add "b"
   for using 'binary' mode. [or even better...
   install Linux :-)  ]

   Please don't use fopen(), fclose()... those are
   the old (now obsolete) methods for accessing files.
   You should use std::ofstream for managing your
   output files. and std::ifstream for the input files.


Finally, some advice:

    If what you want is to get a raw file with
    the binary data of your image, you can just
    use the normal itk::ImageFileWriter and
    provide a filename with one of the extensions:

    .mhd    for MetaImage
    .hdr    for Analyze

    Both of them save images as pairs of files
    (binary raw data) + (text header).

    MetaIMage uses extension .raw for the raw file
    while Analyze uses extension .img.

    They take care internally of setting the
    right options for every operating system
    (including MS-Windows and Mac OS).




Enjoy ITK !


     Luis


-------------
宋涛 wrote:
> Hi Luis,
>  
> source code 
> =================================================================================
> ====
> const unsigned int IMAGE_DIMENSION = 2 ;
> const int   IMAGE_WIDTH  = 400;
> const int   IMAGE_HEIGHT = 240;
> const unsigned int NUMBEROFPIXELS = IMAGE_WIDTH * IMAGE_HEIGHT ;
>  
> typedef unsigned char                                                  GrayPixelT
> ype   ;
> typedef itk::Image< GrayPixelType, IMAGE_DIMENSION >           &n 
> bsp;      GrayImageTy
> pe   ;
> typedef itk::ImportImageFilter< GrayPixelType, IMAGE_DIMENSION >       GrayImport
> FilterType ;
> typedef itk::ImageFileReader< GrayImageType >            GrayReaderType   ;
> typedef itk::ImageFileWriter< GrayImageType >            GrayWriterType   ;
>  
> typedef itk::RGBPixel< unsigned char >              RGBPixelType   ;
> typedef itk::Image< RGBPixelType, IMAGE_DIMENSION >           RGBImageType   ;
> typedef itk::Imp 
> ortImageFilter< RGBPixelType, IMAGE_DIMENSION >        RGBImportF
> ilterType  ;
> typedef itk::ImageFileReader< RGBImageType >            RGBReaderType   ;
> typedef itk::ImageFileWriter< RGBImageType >            RGBWriterType   ;
> 
> 
>  RGBReaderType::Pointer reader = RGBReaderType::New();
>   reader->SetFileName( "E:/InputRGB.png");
>  try
>  {
>   reader->Update();
>  }
>  catch( itk::ExceptionObject & exp ) 
>  {
>   std::cerr << "Exception caught !" << std::endl;
>   std::cerr << exp <& lt; std::endl;
>  }
> 
>  RGBImageType::Pointer image = reader->GetOutput();
>  RGBImageType::IndexType pixelIndex;
>  RGBImageType::PixelType pixelValue;
>  
>  int FileSize   = NUMBEROFPIXELS * 3;
>  unsigned char *Value = new unsigned char[FileSize];
>  if( Value == NULL)
>  {
>   printf( "Memmory is limit!\n" );
>  }
>  
>  for(unsigned int y = 0; y < IMAGE_HEIGHT; y++)
>  {
>   for(unsigned int x = 0; x < IMAGE_WIDTH; x++)
>   {
>    pixelIndex[0] = x;   // x position
>    pixelIndex[1] = y;   // y position
>  
>   &nb sp;pixelValue = image->GetPixel( pixelIndex );
>  
>    // B
>    Value[ ((IMAGE_HEIGHT - 1 -y) * IMAGE_WIDTH + x)*3 + 0] = pixelValue.GetBlue()
> ;
>  
>    // G
>    Value[ ((IMAGE_HEIGHT - 1 -y) * IMAGE_WIDTH + x)*3 + 1] = pixelValue.GetGreen(
> );
>  
>    // R
>    Value[ ((IMAGE_HEIGHT - 1 -y) * IMAGE_WIDTH + x)*3 + 2] = pixelValue.GetRed();
> 
>   }
>  }
>  
>  FILE *stream   = NULL;
>  int NumRead    = 0;
>  
>  if( (stream = fopen( "E:/OutputRGB.200", "w+" )) != NULL )
> & nbsp;{
>   NumRead = fwrite( Value, sizeof( unsigned char ), FileSize, stream );
>   printf( "Number of items write = %d\n", NumRead );
>  
>   fclose( stream );
>  }
>  else
>  {
>   printf( "File could not be opened\n" );
>   return 1;
>  }
>  
>  delete[] Value;
>  
> =================================================================================
> =======================
>  
> this is a very strange problem! 
>  
> i think the size of file("OutputRGB.200") is FileSize,
> the function (printf( "Number of items write = %d\n", NumRead );) also output  
> ;Fil
> eSize,
>  
> but ACTUALLY the size of file("OutputRGB.200") is greater than FileSize!!!!!!!!!!
> !!!!!!!
>  
> WHY?
> 
> ==============================================
> *安全稳定大容量,收费伊妹儿免费30日完美体验~* <http://vip . 163 . com>
> *中国最大的免费邮箱在等你 25兆空间 4兆附件!* <http://mail . 163 . com>
> *点击网易泡泡惊喜无限 全免费手机短信任你发!* <http://popo . 163 . com>
>