]> Creatis software - gdcm.git/blob - Testing/TestWriteSimple.cxx
Upgrade TestWriteSimple to conform with harden File->IsReadable()
[gdcm.git] / Testing / TestWriteSimple.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestWriteSimple.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/06/03 10:15:19 $
7   Version:   $Revision: 1.30 $
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
19 /**
20  * Write a dicom file from nothing
21  * The written image is 256x256, 8 bits, unsigned char
22  * The image content is a horizontal grayscale from 
23  * 
24  */
25 #include "gdcmFile.h"
26 #include "gdcmFileHelper.h"
27 #include "gdcmDebug.h"
28
29 #include <iostream>
30 #include <sstream>
31
32 typedef struct
33 {
34    int sizeX;         // Size X of the image
35    int sizeY;         // Size Y of the image
36    int sizeZ;         // Size Z of the image
37    int components;    // Number of components for a pixel
38    int componentSize; // Component size (in bits : 8, 16)
39    int componentUse ; // Component size (in bits)
40    int sign;          // Sign of components
41    char writeMode;    // Write mode
42                       //  - 'a' : ACR
43                       //  - 'e' : Explicit VR
44                       //  - 'i' : Implicit VR
45 } Image;
46
47 Image Images [] = {
48    {256, 256, 1, 1, 8,  8,  0, 'a'},
49    {256, 256, 1, 1, 8,  8,  0, 'e'},
50    {256, 256, 1, 1, 8,  8,  0, 'i'},
51
52    {512, 256, 1, 1, 8,  8,  0, 'a'},
53    {512, 256, 1, 1, 8,  8,  0, 'e'},
54    {512, 256, 1, 1, 8,  8,  0, 'i'},
55
56    {256, 512, 1, 1, 8,  8,  0, 'a'},
57    {256, 512, 1, 1, 8,  8,  0, 'e'},
58    {256, 512, 1, 1, 8,  8,  0, 'i'},
59
60    {256, 512, 1, 1, 16, 16, 0, 'a'},
61    {256, 512, 1, 1, 16, 16, 0, 'e'},
62    {256, 512, 1, 1, 16, 16, 0, 'i'},
63    {256, 512, 1, 1, 16, 16, 0, 'a'},
64    {256, 512, 1, 1, 16, 16, 0, 'e'},
65    {256, 512, 1, 1, 16, 16, 0, 'i'},
66
67    {512, 256, 10, 1, 8, 8,  0, 'a'},
68    {512, 256, 10, 1, 8, 8,  0, 'e'},
69    {512, 256, 10, 1, 8, 8,  0, 'i'},
70    {512, 256, 10, 3, 8, 8,  0, 'a'},
71    {512, 256, 10, 3, 8, 8,  0, 'e'},
72    {512, 256, 10, 3, 8, 8,  0, 'i'},
73
74    {256, 256, 1, 1, 8,  8,  1, 'a'},
75    {256, 256, 1, 1, 8,  8,  1, 'e'},
76    {256, 256, 1, 1, 8,  8,  1, 'i'},
77
78    {512, 256, 1, 1, 8,  8,  1, 'a'},
79    {512, 256, 1, 1, 8,  8,  1, 'e'},
80    {512, 256, 1, 1, 8,  8,  1, 'i'},
81
82    {256, 512, 1, 1, 8,  8,  1, 'a'},
83    {256, 512, 1, 1, 8,  8,  1, 'e'},
84    {256, 512, 1, 1, 8,  8,  1, 'i'},
85
86    {256, 512, 1, 1, 16, 16, 1, 'a'},
87    {256, 512, 1, 1, 16, 16, 1, 'e'},
88    {256, 512, 1, 1, 16, 16, 1, 'i'},
89    {256, 512, 1, 1, 16, 16, 1, 'a'},
90    {256, 512, 1, 1, 16, 16, 1, 'e'},
91    {256, 512, 1, 1, 16, 16, 1, 'i'},
92
93    {512, 256, 10, 1, 8, 8,  1, 'a'},
94    {512, 256, 10, 1, 8, 8,  1, 'e'},
95    {512, 256, 10, 1, 8, 8,  1, 'i'},
96    {512, 256, 10, 3, 8, 8,  1, 'a'},
97    {512, 256, 10, 3, 8, 8,  1, 'e'},
98    {512, 256, 10, 3, 8, 8,  1, 'i'},
99    {0,   0,   1,  1, 8, 8,  0, 'i'} // to find the end
100 };
101
102 int WriteSimple(Image &img)
103 {
104    std::ostringstream fileName;
105    fileName.str("");
106    fileName << "TestWriteSimple";
107
108 // Step 1 : Create the header of the image
109    std::cout << "        1...";
110    gdcm::File *fileToBuild = new gdcm::File();
111    std::ostringstream str;
112
113    // Set the image size
114    str.str("");
115    str << img.sizeX;
116    fileToBuild->InsertValEntry(str.str(),0x0028,0x0011); // Columns
117    str.str("");
118    str << img.sizeY;
119    fileToBuild->InsertValEntry(str.str(),0x0028,0x0010); // Rows
120
121    if(img.sizeZ>1)
122    {
123       str.str("");
124       str << img.sizeZ;
125       fileToBuild->InsertValEntry(str.str(),0x0028,0x0008); // Number of Frames
126    }
127
128    fileName << "-" << img.sizeX << "-" << img.sizeY << "-" << img.sizeZ;
129
130    // Set the pixel type
131    str.str("");
132    str << img.componentSize;
133    fileToBuild->InsertValEntry(str.str(),0x0028,0x0100); // Bits Allocated
134
135    str.str("");
136    str << img.componentUse;
137    fileToBuild->InsertValEntry(str.str(),0x0028,0x0101); // Bits Stored
138
139    str.str("");
140    str << ( img.componentSize - 1 );
141    fileToBuild->InsertValEntry(str.str(),0x0028,0x0102); // High Bit
142
143    // Set the pixel representation
144    str.str("");
145    str << img.sign;
146    fileToBuild->InsertValEntry(str.str(),0x0028,0x0103); // Pixel Representation
147
148    fileName << "-" << img.componentSize;
149    if(img.sign == 0)
150       fileName << "U";
151    else
152       fileName << "S";
153    
154    switch (img.writeMode)
155    {
156       case 'a' :
157          fileName << ".ACR"; break; 
158       case 'e' :
159          fileName << ".EXPL"; break; 
160       case 'i' :
161          fileName << ".IMPL"; break;
162
163
164    // Set the samples per pixel
165    str.str("");
166    str << img.components;
167    fileToBuild->InsertValEntry(str.str(),0x0028,0x0002); // Samples per Pixel
168
169
170
171 // Step 2 : Create the output image
172    std::cout << "2...";
173    if( img.componentSize%8 > 0 )
174    {
175       img.componentSize += 8-img.componentSize%8;
176    }
177    size_t size = img.sizeX * img.sizeY * img.sizeZ 
178                * img.components * img.componentSize / 8;
179    unsigned char *imageData = new unsigned char[size];
180
181    // FIXME : find a better heuristic to create the image
182    unsigned char *tmp = imageData;
183    for(int k=0;k<img.sizeZ;k++)
184    {
185       for(int j=0;j<img.sizeY;j++)
186       {
187          for(int i=0;i<img.sizeX;i++)
188          {
189             for(int c=0;c<img.components;c++)
190             {
191                *tmp = (unsigned char)(j%256);
192                if( img.componentSize>8 )
193                {
194                   *(tmp+1) = (unsigned char)(j/256);
195                }
196                tmp += img.componentSize/8;
197             }
198          }
199       }
200    }
201
202 // Step 3 : Create the file of the image
203    std::cout << "3...";
204    gdcm::FileHelper *fileH = new gdcm::FileHelper(fileToBuild);
205    fileH->SetImageData(imageData,size);
206
207 // Step 4 : Set the writting mode and write the image
208    std::cout << "4...";
209
210    fileH->SetWriteModeToRaw();
211    switch (img.writeMode)
212    {
213       case 'a' : // Write an ACR file
214          fileH->SetWriteTypeToAcr();
215          break;
216
217       case 'e' : // Write a DICOM Explicit VR file
218          fileH->SetWriteTypeToDcmExplVR();
219          break;
220
221       case 'i' : // Write a DICOM Implicit VR file
222          fileH->SetWriteTypeToDcmImplVR();
223          break;
224
225       default :
226          std::cout << "Failed\n"
227                    << "        Write mode '"<<img.writeMode<<"' is undefined\n";
228
229          delete fileH;
230          delete fileToBuild;
231          delete[] imageData;
232          return 1;
233    }
234
235    if( !fileH->Write(fileName.str()) )
236    {
237       std::cout << "Failed\n"
238                 << "           File in unwrittable\n";
239
240       delete fileH;
241       delete fileToBuild;
242       delete[] imageData;
243       return 1;
244    }
245
246 // Step 5 : Read the written image
247    std::cout << "5...";
248    gdcm::FileHelper *reread = new gdcm::FileHelper( fileName.str() );
249    if( !reread->GetFile()->IsReadable() )
250    {
251       std::cerr << "Failed" << std::endl
252                 << "Could not read written image : " << fileName << std::endl;
253       delete fileToBuild;
254       delete fileH;
255       delete reread;
256       delete[] imageData;
257       return 1;
258    }
259
260 // Step 6 : Compare to the written image
261    std::cout << "6...";
262    size_t dataSizeWritten = reread->GetImageDataSize();
263    uint8_t *imageDataWritten = reread->GetImageData();
264
265    // Test the image write mode
266    if (reread->GetFile()->GetFileType() != fileH->GetWriteType())
267    {
268       std::cout << "Failed" << std::endl
269          << "        File type differ: "
270          << fileH->GetWriteType() << " # " 
271          << reread->GetFile()->GetFileType() << std::endl;
272       delete fileToBuild;
273       delete fileH;
274       delete reread;
275       delete[] imageData;
276
277       return 1;
278    }
279
280    // Test the image size
281    if (fileToBuild->GetXSize() != reread->GetFile()->GetXSize() ||
282        fileToBuild->GetYSize() != reread->GetFile()->GetYSize() ||
283        fileToBuild->GetZSize() != reread->GetFile()->GetZSize())
284    {
285       std::cout << "Failed" << std::endl
286          << "        X Size differs: "
287          << "X: " << fileToBuild->GetXSize() << " # " 
288                   << reread->GetFile()->GetXSize() << " | "
289          << "Y: " << fileToBuild->GetYSize() << " # " 
290                   << reread->GetFile()->GetYSize() << " | "
291          << "Z: " << fileToBuild->GetZSize() << " # " 
292                   << reread->GetFile()->GetZSize() << std::endl;
293       delete fileToBuild;
294       delete fileH;
295       delete reread;
296       delete[] imageData;
297
298       return 1;
299    }
300
301    // Test the data size
302    if (size != dataSizeWritten)
303    {
304       std::cout << "Failed" << std::endl
305          << "        Pixel areas lengths differ: "
306          << size << " # " << dataSizeWritten << std::endl;
307       delete fileToBuild;
308       delete fileH;
309       delete reread;
310       delete[] imageData;
311
312       return 1;
313    }
314
315    // Test the data's content
316    if ( memcmp(imageData, imageDataWritten, size) !=0 )
317    {
318       std::cout << "Failed" << std::endl
319                 << "        Pixel differ (as expanded in memory)." << std::endl;
320       delete fileToBuild;
321       delete fileH;
322       delete reread;
323       delete[] imageData;
324
325       return 1;
326    }
327
328    std::cout << "OK" << std::endl;
329
330    delete fileToBuild;
331    delete fileH;
332    delete reread;
333    delete[] imageData;
334
335    return 0;
336 }
337
338 int TestWriteSimple(int argc, char *argv[])
339 {
340    if (argc < 1) 
341    {
342       std::cerr << "usage: \n" 
343                 << argv[0] << " (without parameters) " << std::endl 
344                 << std::endl;
345       return 1;
346    }
347
348    gdcm::Debug::DebugOn();
349        
350    int ret=0;
351    int i=0;
352    while( Images[i].sizeX>0 && Images[i].sizeY>0 )
353    {
354       std::cout << "Test n :" << i; 
355       ret += WriteSimple(Images[i] );
356       i++;
357    }
358
359    return ret;
360 }