GDCM 3.0.24
CompressLossyJPEG.cs
/*=========================================================================
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.
=========================================================================*/
/*
* Usage:
* $ export LD_LIBRARY_PATH=$HOME/Perso/gdcm/debug-gcc/bin
* $ mono bin/CompressLossyJPEG.exe input.dcm output.dcm
*/
using System;
using gdcm;
public class CompressLossyJPEG
{
public static int Main(string[] args)
{
if( args.Length < 2 )
{
System.Console.WriteLine( " input.dcm output.dcm" );
return 1;
}
string filename = args[0];
string outfilename = args[1];
ImageReader reader = new ImageReader();
reader.SetFileName( filename );
if( !reader.Read() )
{
System.Console.WriteLine( "Could not read: " + filename );
return 1;
}
// The output of gdcm::Reader is a gdcm::File
File file = reader.GetFile();
// the dataset is the the set of element we are interested in:
DataSet ds = file.GetDataSet();
Image image = reader.GetImage();
//image.Print( cout );
TransferSyntax targetts = new TransferSyntax( TransferSyntax.TSType.JPEGBaselineProcess1 );
change.SetTransferSyntax( targetts );
// Setup our JPEGCodec, warning it should be compatible with JPEGBaselineProcess1
JPEGCodec jpegcodec = new JPEGCodec();
if( !jpegcodec.CanCode( targetts ) )
{
System.Console.WriteLine( "Something went really wrong, JPEGCodec cannot handle JPEGBaselineProcess1" );
return 1;
}
jpegcodec.SetLossless( false );
jpegcodec.SetQuality( 50 ); // poor quality !
change.SetUserCodec( jpegcodec ); // specify the codec to use to the ImageChangeTransferSyntax
change.SetInput( image );
bool b = change.Change();
if( !b )
{
System.Console.WriteLine( "Could not change the Transfer Syntax" );
return 1;
}
ImageWriter writer = new ImageWriter();
writer.SetImage( (gdcm.Image)change.GetOutput() );
writer.SetFile( reader.GetFile() );
writer.SetFileName( outfilename );
if( !writer.Write() )
{
System.Console.WriteLine( "Could not write: " + outfilename );
return 1;
}
return 0;
}
}
void SetInput(const Bitmap &image)
Set input image.
Class to represent a Data Set (which contains Data Elements)
Definition gdcmDataSet.h:56
a DICOM File
Definition gdcmFile.h:34
const DataSet & GetDataSet() const
Get Data Set.
Definition gdcmFile.h:57
ImageChangeTransferSyntax class.
Definition gdcmImageChangeTransferSyntax.h:40
void SetUserCodec(ImageCodec *ic)
Definition gdcmImageChangeTransferSyntax.h:68
void SetTransferSyntax(const TransferSyntax &ts)
Set target Transfer Syntax.
Definition gdcmImageChangeTransferSyntax.h:46
ImageReader.
Definition gdcmImageReader.h:34
const Image & GetImage() const
Return the read image.
bool Read() override
const Image & GetOutput() const
Get Output image.
ImageWriter.
Definition gdcmImageWriter.h:33
bool Write() override
Write.
Image.
Definition gdcmImage.h:47
JPEG codec.
Definition gdcmJPEGCodec.h:41
bool CanCode(TransferSyntax const &ts) const override
Return whether this coder support this transfer syntax (can code it)
void SetLossless(bool l)
void SetQuality(double q)
virtual void SetImage(Pixmap const &img)
const File & GetFile() const
Set/Get File.
Definition gdcmReader.h:72
void SetFileName(const char *filename_native)
Class to do system operation.
Definition gdcmSystem.h:27
Class to manipulate Transfer Syntax.
Definition gdcmTransferSyntax.h:40
TSType
Definition gdcmTransferSyntax.h:61
void SetFile(const File &f)
Set/Get the DICOM file (DataSet + Header)
Definition gdcmWriter.h:66
void SetFileName(const char *filename_native)
Set the filename of DICOM file to write:
Definition gdcmASN1.h:21