1 /*=========================================================================
4 Module: $RCSfile: WriteDicomAsJPEG2000.cxx,v $
6 Date: $Date: 2007/08/28 09:40:19 $
7 Version: $Revision: 1.7 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
20 #include "gdcmFileHelper.h"
22 #include "gdcmDebug.h"
24 // Open a dicom file and compress it as JPEG2000 stream
25 int main(int argc, char *argv[])
29 std::cerr << argv[0] << " inputfilename.dcm [ outputfilename.dcm"
30 << "quality debug]\n";
34 std::string filename = argv[1];
35 std::string outfilename = "/tmp/bla.dcm";
37 outfilename = argv[2];
40 quality = atoi(argv[3]);
41 std::cerr << "Using quality: " << quality << std::endl;
44 GDCM_NAME_SPACE::Debug::DebugOn();
46 // Step 1 : Read the image
47 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
48 f->SetLoadMode ( GDCM_NAME_SPACE::LD_ALL ); // Load everything
49 f->SetFileName( filename );
52 GDCM_NAME_SPACE::FileHelper *tested = GDCM_NAME_SPACE::FileHelper::New( f );
53 std::string PixelType = tested->GetFile()->GetPixelType();
54 int xsize = f->GetXSize();
55 int ysize = f->GetYSize();
56 int zsize = f->GetZSize();
57 //tested->Print( std::cout );
59 int samplesPerPixel = f->GetSamplesPerPixel();
60 size_t testedDataSize = tested->GetImageDataSize();
61 uint8_t *testedImageData = tested->GetImageData();
63 if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
64 tested->Print( std::cout );
66 // Step 1 : Create the header of the new file
67 GDCM_NAME_SPACE::File *fileToBuild = GDCM_NAME_SPACE::File::New();
68 std::ostringstream str;
73 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
76 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
82 fileToBuild->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames
85 int bitsallocated = f->GetBitsAllocated();
86 int bitsstored = f->GetBitsStored();
87 int highbit = f->GetHighBitPosition();
88 //std::string pixtype = f->GetPixelType();
89 int sign = f->IsSignedPixelData();
94 fileToBuild->InsertEntryString(str.str(),0x0028,0x0100,"US");// Bits Allocated
97 fileToBuild->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored
101 fileToBuild->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
103 // Set the pixel representation
106 fileToBuild->InsertEntryString(str.str(),0x0028,0x0103,"US"); // Pixel Representation
108 // Set the samples per pixel
110 str << samplesPerPixel; //img.components;
111 fileToBuild->InsertEntryString(str.str(),0x0028,0x0002,"US"); // Samples per Pixel
113 // Set the Pixel Aspect Ratio
114 std::string par = f->GetEntryString(0x0028,0x0034);
115 std::cerr <<"Pixel Aspect Ratio [" << par << "]" << std::endl;
116 if ( par != GDCM_NAME_SPACE::GDCM_UNFOUND )
117 fileToBuild->InsertEntryString(par,0x0028,0x0034,"IS"); // Pixel Aspect Ratio
119 // Step 2 : Create the output image
120 size_t size = xsize * ysize * zsize
121 * samplesPerPixel * bitsallocated / 8;
123 GDCM_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New(fileToBuild);
125 assert( size == testedDataSize );
126 fileH->SetWriteTypeToJPEG2000( );
127 //fileH->SetImageData(testedImageData, testedDataSize);
129 // SetUserData will ensure the compression
130 fileH->SetUserData(testedImageData, testedDataSize);
131 if( !fileH->Write(outfilename) )
133 std::cerr << "write fails" << std::endl;
138 fileToBuild->Delete();