GDCM: GetJPEGSamplePrecision.cxx

GDCM 3.0.25

GetJPEGSamplePrecision.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 is a little helper to detect the famous SIEMENS JPEG lossless compressed image
* where DICOM is declared as:
*
* (0028,0100) US 16 # 2,1 Bits Allocated
* (0028,0101) US 12 # 2,1 Bits Stored
* (0028,0102) US 11 # 2,1 High Bit
* (0028,0103) US 0 # 2,1 Pixel Representation
*
* But where JPEG is:
*
* JPEG_SOF_Parameters:
* SamplePrecision = 16
* nLines = 192
* nSamplesPerLine = 192
* nComponentsInFrame = 1
* component 0
* ComponentIdentifier = 1
* HorizontalSamplingFactor = 1
* VerticalSamplingFactor = 1
* QuantizationTableDestinationSelector = 0
*
*
* This case is valid. One simply has to use the 16bits jpeg decoder to decode the 12bits stored image.
* This used to be an issue in GDCM 1.2.x (fixed in GDCM 1.2.5)
*
* The main return 0 (no error) when the file read is actually a potential problem. At the end of the main
* function, the jpeg stream is stored in the filename specified as second argument
*/
#include "gdcmImageReader.h"
#include "gdcmSequenceOfFragments.h"
#include "gdcmJPEGCodec.h"
#include <iostream>
#include <fstream>
int main(int argc, char *argv[])
{
if( argc < 3 )
{
std::cerr << argv[0] << " input.dcm output.jpg" << std::endl;
return 1;
}
const char *filename = argv[1];
const char *outfilename = argv[2];
gdcm::ImageReader reader;
reader. SetFileName( filename );
if( !reader. Read() )
{
std::cerr << "Could not read: " << filename << std::endl;
return 1;
}
// The output of gdcm::Reader is a gdcm::File
const gdcm::File &file = reader. GetFile();
const gdcm::Image &image = reader. GetImage();
const gdcm::TransferSyntax &ts = file. GetHeader().GetDataSetTransferSyntax();
if( ts != gdcm::TransferSyntax::JPEGLosslessProcess14 && ts != gdcm::TransferSyntax::JPEGLosslessProcess14_1 )
{
std::cerr << "Input is not a lossless JPEG" << std::endl;
return 1;
}
// the dataset is the the set of element we are interested in:
const gdcm::DataSet &ds = file. GetDataSet();
const gdcm::Tag rawTag(0x7fe0, 0x0010); // Default to Pixel Data
const gdcm::DataElement& pdde = ds. GetDataElement( rawTag );
const gdcm::SequenceOfFragments *sf = pdde. GetSequenceOfFragments();
if( sf )
{
std::ofstream output(outfilename, std::ios::binary);
sf->WriteBuffer(output);
}
else
{
std::cerr << "Error" << std::endl;
return 1;
}
gdcm::JPEGCodec jpeg;
std::ifstream is(outfilename, std::ios::binary);
gdcm::PixelFormat pf ( gdcm::PixelFormat::UINT8 ); // let's pretend it's a 8bits jpeg
jpeg. SetPixelFormat( pf );
gdcm::TransferSyntax ts_jpg;
bool b = jpeg. GetHeaderInfo( is, ts_jpg );
if( !b )
{
return 1;
}
//jpeg.Print( std::cout );
if( jpeg. GetPixelFormat().GetBitsAllocated() != image. GetPixelFormat().GetBitsAllocated()
|| jpeg.GetPixelFormat().GetBitsStored() != image.GetPixelFormat().GetBitsStored() )
{
std::cerr << "There is a mismatch in between DICOM declared Pixel Format and Sample Precision used in the JPEG stream" << std::endl;
return 0;
}
std::cout << jpeg.GetPixelFormat() << std::endl;
std::cout << image.GetPixelFormat() << std::endl;
return 1;
}
gdcm::Bitmap::GetPixelFormat
const PixelFormat & GetPixelFormat() const
Get/Set PixelFormat.
Definition gdcmBitmap.h:107
gdcm::DataElement
Class to represent a Data Element either Implicit or Explicit.
Definition gdcmDataElement.h:59
gdcm::DataElement::GetSequenceOfFragments
const SequenceOfFragments * GetSequenceOfFragments() const
gdcm::DataSet
Class to represent a Data Set (which contains Data Elements)
Definition gdcmDataSet.h:56
gdcm::DataSet::GetDataElement
const DataElement & GetDataElement(const Tag &t) const
Definition gdcmDataSet.h:188
gdcm::FileMetaInformation::GetDataSetTransferSyntax
const TransferSyntax & GetDataSetTransferSyntax() const
Definition gdcmFileMetaInformation.h:53
gdcm::File
a DICOM File
Definition gdcmFile.h:34
gdcm::File::GetDataSet
const DataSet & GetDataSet() const
Get Data Set.
Definition gdcmFile.h:57
gdcm::File::GetHeader
const FileMetaInformation & GetHeader() const
Get File Meta Information.
Definition gdcmFile.h:48
gdcm::ImageCodec::GetPixelFormat
PixelFormat & GetPixelFormat()
Definition gdcmImageCodec.h:62
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::JPEGCodec
JPEG codec.
Definition gdcmJPEGCodec.h:41
gdcm::JPEGCodec::SetPixelFormat
void SetPixelFormat(PixelFormat const &pf) override
gdcm::JPEGCodec::GetHeaderInfo
bool GetHeaderInfo(std::istream &is, TransferSyntax &ts) override
gdcm::PixelFormat
PixelFormat.
Definition gdcmPixelFormat.h:46
gdcm::PixelFormat::GetBitsAllocated
unsigned short GetBitsAllocated() const
BitsAllocated see Tag (0028,0100) US Bits Allocated.
Definition gdcmPixelFormat.h:100
gdcm::PixelFormat::GetBitsStored
unsigned short GetBitsStored() const
BitsStored see Tag (0028,0101) US Bits Stored.
Definition gdcmPixelFormat.h:129
gdcm::PixelFormat::UINT8
@ UINT8
Definition gdcmPixelFormat.h:52
gdcm::Reader::GetFile
const File & GetFile() const
Set/Get File.
Definition gdcmReader.h:72
gdcm::Reader::SetFileName
void SetFileName(const char *filename_native)
gdcm::SequenceOfFragments
Class to represent a Sequence Of Fragments.
Definition gdcmSequenceOfFragments.h:32
gdcm::SequenceOfFragments::WriteBuffer
bool WriteBuffer(std::ostream &os) const
gdcm::Tag
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39
gdcm::TransferSyntax
Class to manipulate Transfer Syntax.
Definition gdcmTransferSyntax.h:40
gdcm::TransferSyntax::JPEGLosslessProcess14_1
@ JPEGLosslessProcess14_1
Definition gdcmTransferSyntax.h:73
gdcm::TransferSyntax::JPEGLosslessProcess14
@ JPEGLosslessProcess14
Definition gdcmTransferSyntax.h:72
gdcmImageReader.h
gdcmJPEGCodec.h
gdcmSequenceOfFragments.h