[Insight-users] Linux/Windows: Read BMP Images

marisa aurelio asiram00 at hotmail . com
Mon, 09 Jun 2003 10:36:57 +0000


This is a multi-part message in MIME format.

------=_NextPart_000_960_17b3_2570
Content-Type: text/plain; format=flowed

Hi  itkUsers,

If someone is interested in reading BMP Images, starting with a PNG image, 
here is the code.
There are two codes, for Windows and for Linux.

Marisa

_________________________________________________________________
MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*. 
http://join . msn . com/?page=features/virus

------=_NextPart_000_960_17b3_2570
Content-Type: text/plain; name="ImageReadWriteBMP_Linux.cxx"; format=flowed
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="ImageReadWriteBMP_Linux.cxx"

/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: ImageReadWrite.cxx,v $
  Language:  C++
  Date:      $Date: 2003/03/19 16:32:11 $
  Version:   $Revision: 1.6 $

  Copyright (c) 2002 Insight Consortium. All rights reserved.
  See ITKCopyright.txt or http://www . itk . org/HTML/Copyright . htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#include "itkImageFileReader.h"
#include "itkImage.h"
#include "itkImageLinearConstIteratorWithIndex.h"
#include <fstream>

int main( int argc, char ** argv )
{

  if( argc < 3 )
  {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << " inputImageFile  outputImageFile " << 
std::endl;
    return -1;
  }

  typedef unsigned char PixelType;
  const   unsigned int  Dimension = 2;

  typedef itk::Image< PixelType, Dimension >   ImageType;
  typedef itk::ImageFileReader< ImageType >    ReaderType;


  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName( argv[1] );
  reader->Update();

  ImageType::Pointer image = reader->GetOutput();

  ImageType::RegionType region = image->GetLargestPossibleRegion();
  ImageType::SizeType   size   = region.GetSize();

  const double * spacing = image->GetSpacing();

  unsigned int reserved         =   0;
  unsigned int sizeOfHeader     =  40;
  unsigned int imageWidth       =  size[0];
  unsigned int imageHeight      =  size[1];

  const unsigned int lineSizePadded = (imageWidth % 4) ?  4 - imageWidth % 4 
+ imageWidth : imageWidth;

  const unsigned int imageSizeInPixels = lineSizePadded * imageHeight;

  unsigned short int bitPlanes        =   1;
  unsigned short int bitsPerPixel     =   8;

  unsigned int compressionType  =  0;
  unsigned int sizeCompressed   =  imageSizeInPixels;
  unsigned int horizontalResolution = static_cast< unsigned int >( 1000.0 / 
spacing[0] );
  unsigned int verticalResolution   = static_cast< unsigned int >( 1000.0 / 
spacing[1] );

  unsigned int numberOfColorsUsed      = 256;
  unsigned int numberOfImportantColors = 256;

  const unsigned int numberOfColorComponents  = 4;

  unsigned int offsetToPicture  =   54 + numberOfColorsUsed * 
numberOfColorComponents;
  unsigned int fileSize         =   offsetToPicture + imageSizeInPixels;

  unsigned char *palette = new unsigned char[numberOfColorsUsed * 
numberOfColorComponents];

  for(unsigned int i=0; i<numberOfColorsUsed; i++)
  {
    unsigned int index = i * numberOfColorComponents;
    for(unsigned char j=0; j<numberOfColorComponents; j++)
    {
      palette[index+j] = i;
    }
  }


  std::ofstream bmpFile;
  bmpFile.open( argv[2] );

  bmpFile.write("BM",2);  // signature
  bmpFile.write((char *)&fileSize, 4 );
  bmpFile.write((char *)&reserved, 4 );
  bmpFile.write((char *)&offsetToPicture, 4 );
  bmpFile.write((char *)&sizeOfHeader, 4 );
  bmpFile.write((char *)&imageWidth, 4 );
  bmpFile.write((char *)&imageHeight, 4 );
  bmpFile.write((char *)&bitPlanes, 2 );
  bmpFile.write((char *)&bitsPerPixel, 2 );
  bmpFile.write((char *)&compressionType, 4 );
  bmpFile.write((char *)&sizeCompressed, 4 );
  bmpFile.write((char *)&horizontalResolution, 4 );
  bmpFile.write((char *)&verticalResolution, 4 );
  bmpFile.write((char *)&numberOfColorsUsed, 4 );
  bmpFile.write((char *)&numberOfImportantColors, 4 );
  bmpFile.write((char *)palette, numberOfColorsUsed * 
numberOfColorComponents );*/

  delete [] palette;


  std::cout << "line padded = " << lineSizePadded << std::endl;

  unsigned char * line = new unsigned char [lineSizePadded];
  for(unsigned int c=0; c<lineSizePadded; c++)
  {
    line[c] = 0;
  }

  typedef itk::ImageLinearConstIteratorWithIndex< ImageType > IteratorType;

  IteratorType it( image, region );
  it.SetDirection(0);
  it.GoToBegin();
  while( !it.IsAtEnd() )
  {
    it.GoToBeginOfLine();
    unsigned int i=0;
    while( !it.IsAtEndOfLine() )
    {
      line[i] = it.Get();
      ++it;
      ++i;
    }

	bmpFile.write((char *)line, lineSizePadded );
    it.NextLine();
  }

  bmpFile.close();

  return 0;
}





------=_NextPart_000_960_17b3_2570
Content-Type: text/plain; name="ImageReadWriteBMP_Windows.cxx"; format=flowed
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="ImageReadWriteBMP_Windows.cxx"

/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: ImageReadWrite.cxx,v $
  Language:  C++
  Date:      $Date: 2003/03/19 16:32:11 $
  Version:   $Revision: 1.6 $

  Copyright (c) 2002 Insight Consortium. All rights reserved.
  See ITKCopyright.txt or http://www . itk . org/HTML/Copyright . htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#include "itkImageFileReader.h"
#include "itkImage.h"
#include "itkImageLinearConstIteratorWithIndex.h"

int main( int argc, char ** argv )
{

  if( argc < 3 )
  {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << " inputImageFile  outputImageFile " << 
std::endl;
    return -1;
  }

  typedef unsigned char PixelType;
  const   unsigned int  Dimension = 2;

  typedef itk::Image< PixelType, Dimension >   ImageType;
  typedef itk::ImageFileReader< ImageType >    ReaderType;


  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName( argv[1] );
  reader->Update();

  ImageType::Pointer image = reader->GetOutput();

  ImageType::RegionType region = image->GetLargestPossibleRegion();
  ImageType::SizeType   size   = region.GetSize();

  const double * spacing = image->GetSpacing();

  unsigned int reserved         =   0;
  unsigned int sizeOfHeader     =  40;
  unsigned int imageWidth       =  size[0];
  unsigned int imageHeight      =  size[1];

  const unsigned int lineSizePadded = (imageWidth % 4) ?  4 - imageWidth % 4 
+ imageWidth : imageWidth;

  const unsigned int imageSizeInPixels = lineSizePadded * imageHeight;

  unsigned short int bitPlanes        =   1;
  unsigned short int bitsPerPixel     =   8;

  unsigned int compressionType  =  0;
  unsigned int sizeCompressed   =  imageSizeInPixels;
  unsigned int horizontalResolution = static_cast< unsigned int >( 1000.0 / 
spacing[0] );
  unsigned int verticalResolution   = static_cast< unsigned int >( 1000.0 / 
spacing[1] );

  unsigned int numberOfColorsUsed      = 256;
  unsigned int numberOfImportantColors = 256;

  const unsigned int numberOfColorComponents  = 4;

  unsigned int offsetToPicture  =   54 + numberOfColorsUsed * 
numberOfColorComponents;
  unsigned int fileSize         =   offsetToPicture + imageSizeInPixels;

  unsigned char *palette = new unsigned char[numberOfColorsUsed * 
numberOfColorComponents];

  for(unsigned int i=0; i<numberOfColorsUsed; i++)
  {
    unsigned int index = i * numberOfColorComponents;
    for(unsigned char j=0; j<numberOfColorComponents; j++)
    {
      palette[index+j] = i;
    }
  }


  FILE* fp = fopen(argv[2],"wb");

  char* h = "BM";
  fwrite(h,2,1,fp);
  fwrite(&fileSize, 4,1,fp);
  fwrite(&reserved, 4,1,fp);
  fwrite(&offsetToPicture, 4,1,fp);
  fwrite(&sizeOfHeader, 4,1,fp);
  fwrite(&imageWidth, 4,1,fp);
  fwrite(&imageHeight, 4,1,fp);
  fwrite(&bitPlanes, 2,1,fp);
  fwrite(&bitsPerPixel, 2,1,fp);
  fwrite(&compressionType, 4,1,fp);
  fwrite(&sizeCompressed, 4,1,fp);
  fwrite(&horizontalResolution, 4,1,fp);
  fwrite(&verticalResolution, 4,1,fp);
  fwrite(&numberOfColorsUsed, 4,1,fp);
  fwrite(&numberOfImportantColors, 4,1,fp);
  fwrite(&compressionType, 4,1,fp);
  fwrite(palette, numberOfColorsUsed * numberOfColorComponents ,1,fp);

  delete [] palette;


  std::cout << "line padded = " << lineSizePadded << std::endl;

  unsigned char * line = new unsigned char [lineSizePadded];
  for(unsigned int c=0; c<lineSizePadded; c++)
  {
    line[c] = 0;
  }

  typedef itk::ImageLinearConstIteratorWithIndex< ImageType > IteratorType;

  IteratorType it( image, region );
  it.SetDirection(0);
  it.GoToBegin();
  while( !it.IsAtEnd() )
  {
    it.GoToBeginOfLine();
    unsigned int i=0;
    while( !it.IsAtEndOfLine() )
    {
      line[i] = it.Get();
      ++it;
      ++i;
    }
	fwrite((char *)line, lineSizePadded ,1,fp);
    it.NextLine();
  }

  fclose(fp);

  return 0;
}





------=_NextPart_000_960_17b3_2570--