/*========================================================================= 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; }