1 /*=========================================================================
4 Module: $RCSfile: WriteDicomAsJPEG2000.cxx,v $
6 Date: $Date: 2006/07/12 09:35:38 $
7 Version: $Revision: 1.2 $
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::File *f = new gdcm::File();
43 // gdcm1.3 syntax. Sorry
44 gdcm::File *f = gdcm::File::New();
45 f->SetLoadMode ( gdcm::LD_ALL ); // Load everything
46 f->SetFileName( filename );
49 //gdcm::FileHelper *tested = new gdcm::FileHelper( f );
50 // gdcm1.3 syntax. Sorry
51 gdcm::FileHelper *tested = gdcm::FileHelper::New( f );
52 std::string PixelType = tested->GetFile()->GetPixelType();
53 int xsize = f->GetXSize();
54 int ysize = f->GetYSize();
55 int zsize = f->GetZSize();
56 //tested->Print( std::cout );
58 int samplesPerPixel = f->GetSamplesPerPixel();
59 size_t testedDataSize = tested->GetImageDataSize();
60 uint8_t *testedImageData = tested->GetImageData();
62 // Step 1 : Create the header of the image
64 // gdcm::File *fileToBuild = new gdcm::File();
65 // gdcm1.3 syntax. Sorry !
66 gdcm::File *fileToBuild = gdcm::File::New();
68 std::ostringstream str;
73 //fileToBuild->InsertValEntry(str.str(),0x0028,0x0011); // Columns
74 // gdcm1.3 syntax. Sorry !
75 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011, "UI"); // Columns
78 //fileToBuild->InsertValEntry(str.str(),0x0028,0x0010); // Rows
79 // gdcm1.3 syntax. Sorry !
80 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010, "UI"); // Rows
86 //fileToBuild->InsertValEntry(str.str(),0x0028,0x0008); // Number of Frames
87 // gdcm1.3 syntax. Sorry !
88 fileToBuild->InsertEntryString(str.str(),0x0028,0x0008, "UI"); // Number of Frames
90 int bitsallocated = f->GetBitsAllocated();
91 int bitsstored = f->GetBitsStored();
92 int highbit = f->GetHighBitPosition();
93 //std::string pixtype = f->GetPixelType();
94 int sign = f->IsSignedPixelData();
99 //fileToBuild->InsertValEntry(str.str(),0x0028,0x0100); // Bits Allocated
100 // gdcm1.3 syntax. Sorry !
101 fileToBuild->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
105 //fileToBuild->InsertValEntry(str.str(),0x0028,0x0101); // Bits Stored
106 // gdcm1.3 syntax. Sorry !
107 fileToBuild->InsertEntryString(str.str(),0x0028,0x0101, "UI"); // Bits Stored
110 //fileToBuild->InsertValEntry(str.str(),0x0028,0x0102); // High Bit
111 // gdcm1.3 syntax. Sorry !
112 fileToBuild->InsertEntryString(str.str(),0x0028,0x0102, "UI"); // High Bit
114 // Set the pixel representation
117 //fileToBuild->InsertValEntry(str.str(),0x0028,0x0103); // Pixel Representation
118 // gdcm1.3 syntax. Sorry !
119 fileToBuild->InsertEntryString(str.str(),0x0028,0x0103, "UI"); // Pixel Representation
122 // Set the samples per pixel
124 str << samplesPerPixel; //img.components;
125 //fileToBuild->InsertValEntry(str.str(),0x0028,0x0002); // Samples per Pixel
126 // gdcm1.3 syntax. Sorry !
127 fileToBuild->InsertEntryString(str.str(),0x0028,0x0002, "UI"); // Samples per Pixel
129 // Step 2 : Create the output image
130 //gdcm::FileHelper *fileH = new gdcm::FileHelper(fileToBuild);
131 // gdcm1.3 syntax. Sorry !
132 gdcm::FileHelper *fileH = gdcm::FileHelper::New(fileToBuild);
133 fileH->SetWriteTypeToJPEG2000( );
134 fileH->SetImageData(testedImageData, testedDataSize);
135 if( !fileH->Write(outfilename) )
137 std::cerr << "write fails" << std::endl;
141 // gdcm1.3 syntax. Sorry !
145 //delete fileToBuild;
146 fileToBuild->Delete();