1 /*=========================================================================
4 Module: $RCSfile: WriteDicomAsJPEG2000.cxx,v $
6 Date: $Date: 2007/07/13 08:17:20 $
7 Version: $Revision: 1.5 $
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"
23 // Open a dicom file and compress it as JPEG 2000 stream
24 int main(int argc, char *argv[])
28 std::cerr << argv[0] << " inputfilename.dcm\n";
32 std::string filename = argv[1];
33 std::string outfilename = "/tmp/bla.dcm";
35 outfilename = argv[2];
38 quality = atoi(argv[3]);
39 std::cerr << "Using quality: " << quality << std::endl;
41 // Step 1 : Create the header of the image
42 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
43 f->SetLoadMode ( GDCM_NAME_SPACE::LD_ALL ); // Load everything
44 f->SetFileName( filename );
47 GDCM_NAME_SPACE::FileHelper *tested = GDCM_NAME_SPACE::FileHelper::New( f );
48 std::string PixelType = tested->GetFile()->GetPixelType();
49 int xsize = f->GetXSize();
50 int ysize = f->GetYSize();
51 int zsize = f->GetZSize();
52 //tested->Print( std::cout );
54 int samplesPerPixel = f->GetSamplesPerPixel();
55 size_t testedDataSize = tested->GetImageDataSize();
56 uint8_t *testedImageData = tested->GetImageData();
58 // Step 1 : Create the header of the image
60 GDCM_NAME_SPACE::File *fileToBuild = GDCM_NAME_SPACE::File::New();
61 std::ostringstream str;
66 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011, "US"); // Columns
69 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010, "US"); // Rows
75 fileToBuild->InsertEntryString(str.str(),0x0028,0x0008, "IS"); // Number of Frames
77 int bitsallocated = f->GetBitsAllocated();
78 int bitsstored = f->GetBitsStored();
79 int highbit = f->GetHighBitPosition();
80 //std::string pixtype = f->GetPixelType();
81 int sign = f->IsSignedPixelData();
86 fileToBuild->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
90 fileToBuild->InsertEntryString(str.str(),0x0028,0x0101, "US"); // Bits Stored
94 fileToBuild->InsertEntryString(str.str(),0x0028,0x0102, "US"); // High Bit
96 // Set the pixel representation
99 fileToBuild->InsertEntryString(str.str(),0x0028,0x0103, "US"); // Pixel Representation
101 // Set the samples per pixel
103 str << samplesPerPixel; //img.components;
104 fileToBuild->InsertEntryString(str.str(),0x0028,0x0002, "US"); // Samples per Pixel
106 // Step 2 : Create the output image
107 GDCM_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New(fileToBuild);
108 fileH->SetWriteTypeToJPEG2000( );
109 fileH->SetImageData(testedImageData, testedDataSize);
110 if( !fileH->Write(outfilename) )
112 std::cerr << "write fails" << std::endl;
117 fileToBuild->Delete();