]> Creatis software - gdcm.git/blob - Example/TestPapyrus.cxx
e0f956f17e0e1e3ad172f91462658e3ad28355e7
[gdcm.git] / Example / TestPapyrus.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestPapyrus.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/20 13:28:23 $
7   Version:   $Revision: 1.3 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18 #include "gdcmHeader.h"
19 #include "gdcmFile.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"
26 #include "gdcmUtil.h"
27
28 //#include <fstream>
29
30 #ifndef _WIN32
31 #include <unistd.h> //for access, unlink
32 #else
33 #include <io.h> //for _access
34 #endif
35
36 // return true if the file exists
37 bool FileExists(const char* filename)
38 {
39 #ifdef _MSC_VER
40 # define access _access
41 #endif
42 #ifndef R_OK
43 # define R_OK 04
44 #endif
45   if ( access(filename, R_OK) != 0 )
46     {
47     return false;
48     }
49   else
50     {
51     return true;
52     }
53 }
54
55 bool RemoveFile(const char* source)
56 {
57 #ifdef _MSC_VER
58 #define _unlink unlink
59 #endif
60   return unlink(source) != 0 ? false : true;
61 }
62
63 // ----------------------------------------------------------------------
64 // Here we load a supposed to be Papyrus File (gdcm::Header 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 // ----------------------------------------------------------------------
70
71 int main(int argc, char* argv[])
72 {
73    if (argc < 3)
74    {
75       std::cerr << "Usage :" << std::endl << 
76       argv[0] << " input_papyrus output_dicom verbose" << std::endl;
77       return 1;
78    }
79
80    std::string filename = argv[1];
81    std::string output = argv[2];
82
83    if (argc > 3)
84       gdcm::Debug::SetDebugOn();
85
86    if( FileExists( output.c_str() ) )
87    {
88       if( !RemoveFile( output.c_str() ) )
89       {
90          std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
91          return 1;
92       }
93    }
94    gdcm::File *original = new gdcm::File( filename );
95    gdcm::Header *h = original->GetHeader();
96
97    // Look for private Papyrus Sequence
98    gdcm::SeqEntry *seqPapyrus= h->GetSeqEntry(0x0041, 0x1050);
99    if (!h)
100    {
101       std::cout << "NOT a Papyrus File" << std::endl;
102       delete h;
103       return 1;
104    }
105
106    gdcm::SQItem *sqi = seqPapyrus->GetFirstSQItem();
107    if (sqi == 0)
108    {
109       std::cout << "NO SQItem found within private Papyrus Sequence"
110           << std::endl;
111       delete h;
112       return 1;
113    }
114       
115    std::string TransferSyntax;
116    std::string StudyDate;
117    std::string StudyTime;
118    std::string Modality;
119    std::string PatientName;
120    std::string MediaStSOPinstUID;
121
122 // Get informations on the file : 
123 //  Modality, Transfer Syntax, Study Date, Study Time
124 // Patient Name, Media Storage SOP Instance UID, etc
125
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);
132
133    std::cout << "TransferSyntax " << TransferSyntax << std::endl;
134
135    std::string Rows;
136    std::string Columns;
137    std::string SamplesPerPixel;
138    std::string BitsAllocated;
139    std::string BitsStored;
140    std::string HighBit;
141    std::string PixelRepresentation;
142    
143
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
147    // inside the loop
148
149    // Get caracteristics of the first image
150
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);
158
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() );
164
165    int lgrImage = iRows*iColumns * iSamplesPerPixel * (iBitsAllocated/8);
166
167    // compute number of images
168    int nbImages = 0;
169    while (sqi)
170    {
171       nbImages++;
172       sqi =  seqPapyrus->GetNextSQItem();
173    }
174    std::cout <<"Number of frames :" << nbImages << std::endl;  
175
176    //  allocate enough room to get the pixels of all images.
177
178    uint8_t *PixelArea = new uint8_t[lgrImage*nbImages];
179    uint8_t *currentPosition = PixelArea;
180   gdcm::BinEntry *pixels;
181
182    // declare and open the file
183    std::ifstream *Fp;
184    Fp = new std::ifstream(filename.c_str(), std::ios::in | std::ios::binary);
185    if( ! *Fp )
186    {
187       std::cout <<  "Cannot open file: " << filename << std::endl;
188       //gdcmDebugMacro( "Cannot open file: " << filename.c_str() );
189       delete Fp;
190       Fp = 0;
191       return 0;
192    }
193    // to be sure to be at the beginning
194    Fp->seekg(0,  std::ios::end);
195
196    uint32_t offset;
197    std::string previousRows = Rows;
198    sqi = seqPapyrus->GetFirstSQItem();
199    while (sqi)
200    {
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)
205       {
206          std::cout << "Consistency check failed " << std::endl;
207          return 1;
208       }
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;
217
218       std::string previousRowNb = Rows;
219
220       sqi =  seqPapyrus->GetNextSQItem();
221    }
222
223    // build up a new File, with file info + images info + global pixel area.
224
225    std::string NumberOfFrames = gdcm::Util::Format("%d", nbImages); 
226
227    gdcm::Header *n = new gdcm::Header();
228    n->InitializeDefaultHeader();
229
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);
238
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);
247
248    // create the file
249    gdcm::File *file = new gdcm::File(n);
250
251    file->SetImageData(PixelArea,lgrImage*nbImages);
252    file->SetWriteTypeToDcmExplVR();
253
254    //file->SetPrintLevel(2);
255    file->Print();
256
257    // Write the file
258    file->Write(argv[2]); 
259    if (!file)
260    {
261       std::cout <<"Fail to open (write) file:[" << argv[2]<< "]" << std::endl;;
262       return 1;  
263    }
264    return 0;
265 }