[Insight-users] Writing ITK images in BMP
Luis Ibanez
luis . ibanez at kitware . com
Sat, 31 May 2003 22:47:12 -0400
This is a multi-part message in MIME format.
--------------090808030903030102010009
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Hi Marisa,
The attache code will save an ITK image in BMP.
It is assuming pixel type char and 2D.
Regards,
Luis
---------------------------------------
marisa aurelio wrote:
> Hi Luiz,
>
> Yes, I want something that can be called from my code. Like I said I
> am using MFC's and I need to visualiza a png image, but that isn't
> possible in MFC's. You need to have a bmp image. And thats why I need
> to convert my png image into a bmp image. Do you know how??
>
> Thanks,
> Marisa
>
>
--------------------------------------------------------------
--------------090808030903030102010009
Content-Type: text/x-c++src;
name="ImageReadWriteBMP.cxx"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="ImageReadWriteBMP.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;
}
--------------090808030903030102010009--