1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html 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 notice for more information.
=========================================================================*/
/*
* This example shows how to either retrieve an Icon if present somewhere
* in the file, or else generate one.
*/
#include "gdcmImageReader.h"
#include "gdcmPNMCodec.h"
#include "gdcmIconImageFilter.h"
#include "gdcmIconImageGenerator.h"
bool WriteIconAsPNM(const char* filename, const gdcm::IconImage& icon)
{
gdcm::PNMCodec pnm;
pnm.SetDimensions( icon.GetDimensions() );
pnm.SetPixelFormat( icon.GetPixelFormat() );
pnm.SetPhotometricInterpretation( icon.GetPhotometricInterpretation() );
pnm.SetLUT( icon.GetLUT() );
const gdcm::DataElement& in = icon.GetDataElement();
bool b = pnm.Write( filename, in );
assert( b );
return b;
}
int main(int argc, char *argv [])
{
if( argc < 2 ) return 1;
const char *filename = argv[1];
gdcm::ImageReader reader;
reader.SetFileName( filename );
if( !reader.Read() )
{
std::cerr << "Failed to read (or not image): " << filename << std::endl;
return 1;
}
gdcm::IconImageFilter iif;
iif.SetFile( reader.GetFile() );
bool b = iif.Extract();
if( b )
{
const gdcm::IconImage &icon = iif.GetIconImage(0);
icon.Print( std::cout );
if( !icon.GetTransferSyntax().IsEncapsulated() )
{
// Let's write out this icon as PNM file
WriteIconAsPNM("icon.ppm", icon);
}
else if( icon.GetTransferSyntax() == gdcm::TransferSyntax::JPEGBaselineProcess1
|| icon.GetTransferSyntax() == gdcm::TransferSyntax::JPEGExtendedProcess2_4
)
{
const gdcm::DataElement& in = icon.GetDataElement();
const gdcm::ByteValue *bv = in.GetByteValue();
assert( bv );
std::ofstream out( "icon.jpg", std::ios::binary );
out.write( bv->GetPointer(), bv->GetLength() );
out.close();
}
}
else
{
assert( iif.GetNumberOfIconImages() == 0 );
std::cerr << "No Icon Found anywhere in file" << std::endl;
const gdcm::Image &img = reader.GetImage();
gdcm::IconImageGenerator iig;
iig.AutoPixelMinMax(true);
iig.SetPixmap( img );
const unsigned int idims[2] = { 64, 64 };
iig.SetOutputDimensions( idims );
//iig.SetPixelMinMax(60, 868);
if( !iig.Generate() ) return 1;
const gdcm::IconImage & icon = iig.GetIconImage();
WriteIconAsPNM("icon.ppm", icon);
}
return 0;
}
|