GDCM 3.2.1

ExtractIconFromFile.cxx
/*=========================================================================
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 );
gdcm_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();
gdcm_assert( bv );
std::ofstream out( "icon.jpg", std::ios::binary );
out.write( bv->GetPointer(), bv->GetLength() );
out.close();
}
}
else
{
gdcm_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;
}
gdcm::Bitmap::GetLUT
const LookupTable & GetLUT() const
Definition gdcmBitmap.h:87
gdcm::Bitmap::GetPixelFormat
const PixelFormat & GetPixelFormat() const
Get/Set PixelFormat.
Definition gdcmBitmap.h:107
gdcm::Bitmap::GetPhotometricInterpretation
const PhotometricInterpretation & GetPhotometricInterpretation() const
return the photometric interpretation
gdcm::Bitmap::GetDataElement
const DataElement & GetDataElement() const
Definition gdcmBitmap.h:79
gdcm::Bitmap::Print
void Print(std::ostream &) const override
gdcm::Bitmap::GetDimensions
const unsigned int * GetDimensions() const
Return the dimension of the pixel data, first dimension (x), then 2nd (y), then 3rd (z)....
gdcm::Bitmap::GetTransferSyntax
const TransferSyntax & GetTransferSyntax() const
Definition gdcmBitmap.h:72
gdcm::ByteValue
Class to represent binary value (array of bytes)
Definition gdcmByteValue.h:36
gdcm::ByteValue::GetPointer
const char * GetPointer() const
Definition gdcmByteValue.h:114
gdcm::ByteValue::GetLength
VL GetLength() const override
Definition gdcmByteValue.h:81
gdcm::DataElement
Class to represent a Data Element either Implicit or Explicit.
Definition gdcmDataElement.h:59
gdcm::DataElement::GetByteValue
const ByteValue * GetByteValue() const
Definition gdcmDataElement.h:133
gdcm::IconImageFilter
IconImageFilter.
Definition gdcmIconImageFilter.h:50
gdcm::IconImageFilter::GetNumberOfIconImages
unsigned int GetNumberOfIconImages() const
Retrieve extract IconImage (need to call Extract first)
gdcm::IconImageFilter::SetFile
void SetFile(const File &f)
Set/Get File.
Definition gdcmIconImageFilter.h:56
gdcm::IconImageFilter::GetIconImage
IconImage & GetIconImage(unsigned int i) const
gdcm::IconImageFilter::Extract
bool Extract()
Extract all Icon found in File.
gdcm::IconImageGenerator
IconImageGenerator.
Definition gdcmIconImageGenerator.h:42
gdcm::IconImageGenerator::SetPixmap
void SetPixmap(const Pixmap &p)
Set/Get File.
Definition gdcmIconImageGenerator.h:48
gdcm::IconImageGenerator::SetOutputDimensions
void SetOutputDimensions(const unsigned int dims[2])
Set Target dimension of output Icon.
gdcm::IconImageGenerator::Generate
bool Generate()
Generate Icon.
gdcm::IconImageGenerator::GetIconImage
const IconImage & GetIconImage() const
Retrieve generated Icon.
Definition gdcmIconImageGenerator.h:80
gdcm::IconImageGenerator::AutoPixelMinMax
void AutoPixelMinMax(bool b)
gdcm::ImageCodec::SetDimensions
void SetDimensions(const unsigned int d[3])
gdcm::ImageCodec::SetPixelFormat
virtual void SetPixelFormat(PixelFormat const &pf)
Definition gdcmImageCodec.h:70
gdcm::ImageCodec::SetLUT
void SetLUT(LookupTable const &lut)
Definition gdcmImageCodec.h:89
gdcm::ImageCodec::SetPhotometricInterpretation
void SetPhotometricInterpretation(PhotometricInterpretation const &pi)
gdcm::ImageReader
ImageReader.
Definition gdcmImageReader.h:34
gdcm::ImageReader::GetImage
const Image & GetImage() const
Return the read image.
gdcm::ImageReader::Read
bool Read() override
gdcm::Image
Image.
Definition gdcmImage.h:47
gdcm::PNMCodec
Class to do PNM.
Definition gdcmPNMCodec.h:30
gdcm::PNMCodec::Write
bool Write(const char *filename, const DataElement &out) const
gdcm::Reader::GetFile
const File & GetFile() const
Set/Get File.
Definition gdcmReader.h:72
gdcm::Reader::SetFileName
void SetFileName(const char *filename_native)
gdcm::TransferSyntax::IsEncapsulated
bool IsEncapsulated() const
gdcm::TransferSyntax::JPEGBaselineProcess1
@ JPEGBaselineProcess1
Definition gdcmTransferSyntax.h:67
gdcm::TransferSyntax::JPEGExtendedProcess2_4
@ JPEGExtendedProcess2_4
Definition gdcmTransferSyntax.h:68
gdcm_assert
#define gdcm_assert(cond)
Definition gdcmException.h:104
gdcmIconImageFilter.h
gdcmIconImageGenerator.h
gdcmImageReader.h
gdcmPNMCodec.h
gdcm::IconImage
Bitmap IconImage
Definition gdcmIconImage.h:91