1 /*=========================================================================
4 Module: $RCSfile: TestPapyrus.cxx,v $
6 Date: $Date: 2005/01/21 11:40:52 $
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 =========================================================================*/
19 #include "gdcmFileHelper.h"
20 #include "gdcmDocument.h"
21 #include "gdcmValEntry.h"
22 #include "gdcmBinEntry.h"
23 #include "gdcmSeqEntry.h"
24 #include "gdcmSQItem.h"
25 #include "gdcmDebug.h"
31 #include <unistd.h> //for access, unlink
33 #include <io.h> //for _access
36 // return true if the file exists
37 bool FileExists(const char* filename)
40 # define access _access
45 if ( access(filename, R_OK) != 0 )
55 bool RemoveFile(const char* source)
58 #define _unlink unlink
60 return unlink(source) != 0 ? false : true;
63 // ----------------------------------------------------------------------
64 // Here we load a supposed to be Papyrus File (gdcm::File compliant)
65 // and then try to get the pixels, using low-level SeqEntry accessors.
66 // Since it's not a general purpose Papyrus related program
67 // (just a light example) we suppose *everything* is clean
68 // and we don't perform any integrity check
69 // ----------------------------------------------------------------------
71 int main(int argc, char* argv[])
75 std::cerr << "Usage :" << std::endl <<
76 argv[0] << " input_papyrus output_dicom verbose" << std::endl;
80 std::string filename = argv[1];
81 std::string output = argv[2];
84 gdcm::Debug::SetDebugOn();
86 if( FileExists( output.c_str() ) )
88 if( !RemoveFile( output.c_str() ) )
90 std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
94 gdcm::FileHelper *original = new gdcm::FileHelper( filename );
95 gdcm::File *h = original->GetFile();
97 // Look for private Papyrus Sequence
98 gdcm::SeqEntry *seqPapyrus= h->GetSeqEntry(0x0041, 0x1050);
101 std::cout << "NOT a Papyrus File" << std::endl;
106 gdcm::SQItem *sqi = seqPapyrus->GetFirstSQItem();
109 std::cout << "NO SQItem found within private Papyrus Sequence"
115 std::string TransferSyntax;
116 std::string StudyDate;
117 std::string StudyTime;
118 std::string Modality;
119 std::string PatientName;
120 std::string MediaStSOPinstUID;
122 // Get informations on the file :
123 // Modality, Transfer Syntax, Study Date, Study Time
124 // Patient Name, Media Storage SOP Instance UID, etc
126 MediaStSOPinstUID = h->GetEntry(0x0002,0x0002);
127 TransferSyntax = h->GetEntry(0x0002,0x0010);
128 StudyDate = sqi->GetEntry(0x0008,0x0020);
129 StudyTime = sqi->GetEntry(0x0008,0x0030);
130 Modality = sqi->GetEntry(0x0008,0x0060);
131 PatientName = sqi->GetEntry(0x0010,0x0010);
133 std::cout << "TransferSyntax " << TransferSyntax << std::endl;
137 std::string SamplesPerPixel;
138 std::string BitsAllocated;
139 std::string BitsStored;
141 std::string PixelRepresentation;
144 // we brutally suppose all the images within a Papyrus file
145 // have the same caracteristics.
146 // if you're aware they have not, just move the GetEntry
149 // Get caracteristics of the first image
151 SamplesPerPixel = sqi->GetEntry(0x0028,0x0002);
152 Rows = sqi->GetEntry(0x0028,0x0010);
153 Columns = sqi->GetEntry(0x0028,0x0011);
154 BitsAllocated = sqi->GetEntry(0x0028,0x0100);
155 BitsStored = sqi->GetEntry(0x0028,0x0101);
156 HighBit = sqi->GetEntry(0x0028,0x0102);
157 PixelRepresentation = sqi->GetEntry(0x0028,0x0102);
159 // just convert those needed to compute PixelArea length
160 int iRows = (uint32_t) atoi( Rows.c_str() );
161 int iColumns = (uint32_t) atoi( Columns.c_str() );
162 int iSamplesPerPixel = (uint32_t) atoi( SamplesPerPixel.c_str() );
163 int iBitsAllocated = (uint32_t) atoi( BitsAllocated.c_str() );
165 int lgrImage = iRows*iColumns * iSamplesPerPixel * (iBitsAllocated/8);
167 // compute number of images
172 sqi = seqPapyrus->GetNextSQItem();
174 std::cout <<"Number of frames :" << nbImages << std::endl;
176 // allocate enough room to get the pixels of all images.
178 uint8_t *PixelArea = new uint8_t[lgrImage*nbImages];
179 uint8_t *currentPosition = PixelArea;
180 gdcm::BinEntry *pixels;
182 // declare and open the file
184 Fp = new std::ifstream(filename.c_str(), std::ios::in | std::ios::binary);
187 std::cout << "Cannot open file: " << filename << std::endl;
188 //gdcmDebugMacro( "Cannot open file: " << filename.c_str() );
193 // to be sure to be at the beginning
194 Fp->seekg(0, std::ios::end);
197 std::string previousRows = Rows;
198 sqi = seqPapyrus->GetFirstSQItem();
201 std::cout << "One more image read. Keep waiting" << std::endl;
202 Rows = sqi->GetEntry(0x0028,0x0010);
203 // minimum integrity check
204 if (Rows != previousRows)
206 std::cout << "Consistency check failed " << std::endl;
209 // get the images pixels
210 pixels = sqi->GetBinEntry(0x7fe0,0x0010);
211 offset = pixels->GetOffset();
212 // perform a fseek, on offset length on the 'right' length
213 Fp->seekg(offset, std::ios::beg);
214 // perform a fread into the right place
215 Fp->read((char *)currentPosition, (size_t)lgrImage);
216 currentPosition +=lgrImage;
218 std::string previousRowNb = Rows;
220 sqi = seqPapyrus->GetNextSQItem();
223 // build up a new File, with file info + images info + global pixel area.
225 std::string NumberOfFrames = gdcm::Util::Format("%d", nbImages);
227 gdcm::File *n = new gdcm::File();
228 n->InitializeDefaultFile();
230 n->ReplaceOrCreate(MediaStSOPinstUID, 0x0002,0x0002);
231 // Whe keep default gdcm Transfer Syntax (Explicit VR Little Endian)
232 // since using Papyrus one (Implicit VR Little Endian) is a mess
233 //n->ReplaceOrCreate(TransferSyntax, 0x0002,0x0010);
234 n->ReplaceOrCreate(StudyDate, 0x0008,0x0020);
235 n->ReplaceOrCreate(StudyTime, 0x0008,0x0030);
236 n->ReplaceOrCreate(Modality, 0x0008,0x0060);
237 n->ReplaceOrCreate(PatientName, 0x0010,0x0010);
239 n->ReplaceOrCreate(SamplesPerPixel, 0x0028,0x0002);
240 n->ReplaceOrCreate(NumberOfFrames, 0x0028,0x0008);
241 n->ReplaceOrCreate(Rows, 0x0028,0x0010);
242 n->ReplaceOrCreate(Columns, 0x0028,0x0011);
243 n->ReplaceOrCreate(BitsAllocated, 0x0028,0x0100);
244 n->ReplaceOrCreate(BitsStored, 0x0028,0x0101);
245 n->ReplaceOrCreate(HighBit, 0x0028,0x0102);
246 n->ReplaceOrCreate(PixelRepresentation,0x0028,0x0102);
249 gdcm::FileHelper *file = new gdcm::FileHelper(n);
251 file->SetImageData(PixelArea,lgrImage*nbImages);
252 file->SetWriteTypeToDcmExplVR();
254 //file->SetPrintLevel(2);
258 file->Write(argv[2]);
261 std::cout <<"Fail to open (write) file:[" << argv[2]<< "]" << std::endl;;