GDCM 3.2.1

Fake_Image_Using_Stream_Image_Writer.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 work was realised during the GSOC 2011 by Manoj Alwani
#include "gdcmReader.h"
#include "gdcmMediaStorage.h"
#include "gdcmWriter.h"
#include "gdcmItem.h"
#include "gdcmImageReader.h"
#include "gdcmAttribute.h"
#include "gdcmFile.h"
#include "gdcmTag.h"
#include "gdcmTransferSyntax.h"
#include "gdcmUIDGenerator.h"
#include "gdcmAnonymizer.h"
#include "gdcmStreamImageWriter.h"
#include "gdcmImageHelper.h"
#include "gdcmTrace.h"
int main(int, char *[])
{
char * buffer = new char[ 256 * 256 *3 ];
// *p = (uint8_t*)buffer;
char * p = buffer;
gdcm::Trace::DebugOn();
gdcm::Trace::WarningOn();
for(int row = 0; row < 256; ++row)
{
for(int col = 0; col < 256; ++col)
//for(int b = 0; b < 256; ++b)
{
*p++ = 255;
*p++ = 0;
*p++ = 0;
}
}
gdcm::Writer w;
gdcm::File &file = w.GetFile();
gdcm::DataSet &ds = file.GetDataSet();
file.GetHeader().SetDataSetTransferSyntax( gdcm::TransferSyntax::ExplicitVRLittleEndian );
gdcm::UIDGenerator uid;
gdcm::DataElement de( gdcm::Tag(0x8,0x18) ); // SOP Instance UID
de.SetVR( gdcm::VR::UI );
const char *u = uid.Generate();
de.SetByteValue( u, strlen(u) );
ds.Insert( de );
gdcm::DataElement de1( gdcm::Tag(0x8,0x16) );
de1.SetVR( gdcm::VR::UI );
gdcm::MediaStorage ms( gdcm::MediaStorage::VLWholeSlideMicroscopyImageStorage );
de1.SetByteValue( ms.GetString(), strlen(ms.GetString()));
ds.Insert( de1 );
const char mystr[] = "RGB";
gdcm::DataElement de2( gdcm::Tag(0x28,0x04) );
//de.SetTag(gdcm::Tag(0x28,0x04));
de2.SetVR( gdcm::VR::CS );
de2.SetByteValue(mystr, strlen(mystr));
ds.Insert( de2 );
gdcm::Attribute<0x0028,0x0010> row = {256};
//row.SetValue(512);
ds.Insert( row.GetAsDataElement() );
// w.SetCheckFileMetaInformation( true );
gdcm::Attribute<0x0028,0x0011> col = {256};
ds.Insert( col.GetAsDataElement() );
gdcm::Attribute<0x0028,0x0008> Number_Of_Frames = {1};
ds.Insert( Number_Of_Frames.GetAsDataElement() );
gdcm::Attribute<0x0028,0x0100> at = {8};
ds.Insert( at.GetAsDataElement() );
gdcm::Attribute<0x0028,0x0002> at1 = {3}; //bits per pixel
ds.Insert( at1.GetAsDataElement() );
gdcm::Attribute<0x0028,0x0101> at2 = {8};
ds.Insert( at2.GetAsDataElement() );
gdcm::Attribute<0x0028,0x0102> at3 = {7};
ds.Insert( at3.GetAsDataElement() );
gdcm::Attribute<0x0028,0x006> at4 = {0};
ds.Insert( at4.GetAsDataElement() );
gdcm::Attribute<0x0028,0x0103> at5 = {0};
ds.Insert( at5.GetAsDataElement() );
//de.SetTag(gdcm::Tag(0x7fe0,0x0010));
//ds.Insert(de);
gdcm::StreamImageWriter theStreamWriter;
gdcm::SmartPointer<gdcm::SequenceOfItems> sq = new gdcm::SequenceOfItems();
sq->SetLengthToUndefined();
uint16_t row1 = 256;
uint16_t col1 = 256;
//std::cout << row;
gdcm::Element<gdcm::VR::IS,gdcm::VM::VM1> el2;
el2.SetValue(1);
gdcm::DataElement rfn = el2.GetAsDataElement(); //rfn ---> reference frame number
rfn.SetTag( gdcm::Tag(0x0008,0x1160) );
gdcm::Element<gdcm::VR::US,gdcm::VM::VM2> el;
el.SetValue(1,0);
el.SetValue(1,1);
gdcm::DataElement ulr = el.GetAsDataElement(); //ulr --> upper left col/row
ulr.SetTag( gdcm::Tag(0x0048,0x0201) );
gdcm::Element<gdcm::VR::US,gdcm::VM::VM2> el1;
el1.SetValue(col1,0);
el1.SetValue(row1,1);
gdcm::DataElement brr = el1.GetAsDataElement();
brr.SetTag( gdcm::Tag(0x0048,0x0202) ); //brr --> bottom right col/row
gdcm::Item it;
gdcm::DataSet &nds = it.GetNestedDataSet();
nds.Insert( rfn );
nds.Insert(ulr);
nds.Insert(brr);
sq->AddItem(it);
gdcm::DataElement des( gdcm::Tag(0x0048,0x0200) );
des.SetVR(gdcm::VR::SQ);
des.SetValue(*sq);
des.SetVLToUndefined();
ds.Insert(des);
theStreamWriter.SetFile(file);
std::ofstream of;
of.open( "output.dcm", std::ios::out | std::ios::binary );
theStreamWriter.SetStream(of);
if (!theStreamWriter.CanWriteFile()){
delete [] buffer;
std::cout << "Not able to write";
return 0;//this means that the file was unwritable, period.
//very similar to a ReadImageInformation failure
}
else
std::cout<<"\nabletoread";
if (!theStreamWriter.WriteImageInformation()){
std::cerr << "unable to write image information" << std::endl;
delete [] buffer;
return 1; //the CanWrite function should prevent getting here, else,
//that's a test failure
}
std::vector<unsigned int> extent =
gdcm::ImageHelper::GetDimensionsValue(file);
unsigned short xmax = extent[0];
unsigned short ymax = extent[1];
unsigned short theChunkSize = 1;
unsigned short ychunk = extent[1]/theChunkSize; //go in chunk sizes of theChunkSize
unsigned short zmax = extent[2];
std::cout << xmax << ymax << zmax;
if (xmax == 0 || ymax == 0)
{
std::cerr << "Image has no size, unable to write zero-sized image." << std::endl;
return 0;
}
int z, y, nexty;
unsigned long prevLen = 0; //when going through the char buffer, make sure to grab
//the bytes sequentially. So, store how far you got in the buffer with each iteration.
for (z = 0; z < zmax; ++z){
for (y = 0; y < ymax; y += ychunk){
nexty = y + ychunk;
if (nexty > ymax) nexty = ymax;
theStreamWriter.DefinePixelExtent(0, xmax, y, nexty, z, z+1);
unsigned long len = theStreamWriter.DefineProperBufferLength();
std::cout << "\n" <<len;
char* finalBuffer = new char[len];
memcpy(finalBuffer, &(buffer[prevLen]), len);
std::cout << "\nable to write";
if (!theStreamWriter.Write(finalBuffer, len)){
std::cerr << "writing failure:" << "output.dcm" << " at y = " << y << " and z= " << z << std::endl;
delete [] buffer;
delete [] finalBuffer;
return 1;
}
delete [] finalBuffer;
prevLen += len;
}
}
delete buffer;
uint16_t firstTag1 = 0xfffe;
uint16_t secondTag1 = 0xe0dd;
uint32_t thirdTag1 = 0x00000000;
//uint16_t fourthTag1 = 0xffff;
const int theBufferSize1 = 2*sizeof(uint16_t)+sizeof(uint32_t);
char* tmpBuffer2 = new char[theBufferSize1];
memcpy(&(tmpBuffer2[0]), &firstTag1, sizeof(uint16_t));
memcpy(&(tmpBuffer2[sizeof(uint16_t)]), &secondTag1, sizeof(uint16_t));
memcpy(&(tmpBuffer2[2*sizeof(uint16_t)]), &thirdTag1, sizeof(uint32_t));
//memcpy(&(tmpBuffer2[3*sizeof(uint16_t)]), &fourthTag1, sizeof(uint16_t));
gdcm_assert( of && !of.eof() && of.good() );
of.write(tmpBuffer2, theBufferSize1);
of.flush();
gdcm_assert( of );
return 0;
}
gdcm::Attribute
Attribute class This class use template metaprograming tricks to let the user know when the template ...
Definition gdcmAttribute.h:86
gdcm::Attribute::GetAsDataElement
DataElement GetAsDataElement() const
Definition gdcmAttribute.h:183
gdcm::DataElement
Class to represent a Data Element either Implicit or Explicit.
Definition gdcmDataElement.h:59
gdcm::DataElement::SetTag
void SetTag(const Tag &t)
Definition gdcmDataElement.h:71
gdcm::DataElement::SetVLToUndefined
void SetVLToUndefined()
gdcm::DataElement::SetValue
void SetValue(Value const &vl)
Definition gdcmDataElement.h:100
gdcm::DataElement::SetByteValue
void SetByteValue(const char *array, VL length)
Definition gdcmDataElement.h:126
gdcm::DataElement::SetVR
void SetVR(VR const &vr)
Definition gdcmDataElement.h:88
gdcm::DataSet
Class to represent a Data Set (which contains Data Elements)
Definition gdcmDataSet.h:56
gdcm::DataSet::Insert
void Insert(const DataElement &de)
Definition gdcmDataSet.h:128
gdcm::Element
Element class.
Definition gdcmElement.h:70
gdcm::Element::GetAsDataElement
DataElement GetAsDataElement() const
Definition gdcmElement.h:127
gdcm::Element::SetValue
void SetValue(typename VRToType< TVR >::Type v, unsigned int idx=0)
Definition gdcmElement.h:105
gdcm::FileMetaInformation::SetDataSetTransferSyntax
void SetDataSetTransferSyntax(const TransferSyntax &ts)
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::ImageHelper::GetDimensionsValue
static std::vector< unsigned int > GetDimensionsValue(const File &f)
gdcm::Item
Class to represent an Item.
Definition gdcmItem.h:46
gdcm::Item::GetNestedDataSet
const DataSet & GetNestedDataSet() const
Definition gdcmItem.h:80
gdcm::MediaStorage
MediaStorage.
Definition gdcmMediaStorage.h:44
gdcm::MediaStorage::VLWholeSlideMicroscopyImageStorage
@ VLWholeSlideMicroscopyImageStorage
Definition gdcmMediaStorage.h:121
gdcm::MediaStorage::GetString
const char * GetString() const
Return the Media String of the object.
gdcm::SequenceOfItems
Class to represent a Sequence Of Items.
Definition gdcmSequenceOfItems.h:40
gdcm::SmartPointer
Class for Smart Pointer.
Definition gdcmSmartPointer.h:40
gdcm::StreamImageWriter
StreamImageReader.
Definition gdcmStreamImageWriter.h:43
gdcm::StreamImageWriter::SetFile
void SetFile(const File &inFile)
gdcm::StreamImageWriter::CanWriteFile
bool CanWriteFile() const
gdcm::StreamImageWriter::DefinePixelExtent
void DefinePixelExtent(uint16_t inXMin, uint16_t inXMax, uint16_t inYMin, uint16_t inYMax, uint16_t inZMin=0, uint16_t inZMax=1)
gdcm::StreamImageWriter::DefineProperBufferLength
uint32_t DefineProperBufferLength()
gdcm::StreamImageWriter::SetStream
void SetStream(std::ostream &inStream)
gdcm::StreamImageWriter::Write
bool Write(void *inWriteBuffer, const std::size_t &inBufferLength)
gdcm::StreamImageWriter::WriteImageInformation
virtual bool WriteImageInformation()
gdcm::Tag
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39
gdcm::Trace::WarningOn
static void WarningOn()
gdcm::Trace::DebugOn
static void DebugOn()
gdcm::TransferSyntax::ExplicitVRLittleEndian
@ ExplicitVRLittleEndian
Definition gdcmTransferSyntax.h:64
gdcm::UIDGenerator
Class for generating unique UID.
Definition gdcmUIDGenerator.h:28
gdcm::UIDGenerator::Generate
const char * Generate()
gdcm::VR::UI
@ UI
Definition gdcmVR.h:87
gdcm::VR::CS
@ CS
Definition gdcmVR.h:63
gdcm::VR::SQ
@ SQ
Definition gdcmVR.h:81
gdcm::Writer
Writer ala DOM (Document Object Model)
Definition gdcmWriter.h:49
gdcm::Writer::GetFile
File & GetFile()
Definition gdcmWriter.h:67
gdcmAnonymizer.h
gdcmAttribute.h
gdcm_assert
#define gdcm_assert(cond)
Definition gdcmException.h:104
gdcmFile.h
gdcmImageHelper.h
gdcmImageReader.h
gdcmItem.h
gdcmMediaStorage.h
gdcmReader.h
gdcmStreamImageWriter.h
gdcmTag.h
gdcmTrace.h
gdcmTransferSyntax.h
gdcmUIDGenerator.h
gdcmWriter.h