]> Creatis software - gdcm.git/blob - Testing/TestWriteSimple.cxx
* src/gdcmFileHelper.[h|cxx] : now correctly set the VR of the datas when
[gdcm.git] / Testing / TestWriteSimple.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestWriteSimple.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/09 16:28:41 $
7   Version:   $Revision: 1.23 $
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    {0,   0,   1,  1, 8, 8,  0, 'i'} // to find the end
74 };
75
76 int WriteSimple(Image &img)
77 {
78    std::string fileName = "TestWriteSimple.dcm";
79
80 // Step 1 : Create the header of the image
81    std::cout << "        1...";
82    gdcm::File *fileToBuild = new gdcm::File();
83    std::ostringstream str;
84
85    // Set the image size
86    str.str("");
87    str << img.sizeX;
88    fileToBuild->InsertValEntry(str.str(),0x0028,0x0011); // Columns
89
90    str.str("");
91    str << img.sizeY;
92    fileToBuild->InsertValEntry(str.str(),0x0028,0x0010); // Rows
93
94    if(img.sizeZ>1)
95    {
96       str.str("");
97       str << img.sizeZ;
98       fileToBuild->InsertValEntry(str.str(),0x0028,0x0008); // Number of Frames
99    }
100
101    // Set the pixel type
102    str.str("");
103    str << img.componentSize;
104    fileToBuild->InsertValEntry(str.str(),0x0028,0x0100); // Bits Allocated
105
106    str.str("");
107    str << img.componentUse;
108    fileToBuild->InsertValEntry(str.str(),0x0028,0x0101); // Bits Stored
109
110    str.str("");
111    str << img.componentSize - 1;
112    fileToBuild->InsertValEntry(str.str(),0x0028,0x0102); // High Bit
113
114    // Set the pixel representation
115    str.str("");
116    str << img.sign;
117    fileToBuild->InsertValEntry(str.str(),0x0028,0x0103); // Pixel Representation
118
119    // Set the samples per pixel
120    str.str("");
121    str << img.components;
122    fileToBuild->InsertValEntry(str.str(),0x0028,0x0002); // Samples per Pixel
123
124    if( !fileToBuild->IsReadable() )
125    {
126       std::cout << "Failed\n"
127                 << "        Prepared image isn't readable\n";
128
129       delete fileToBuild;
130       return 1;
131    }
132
133 // Step 2 : Create the output image
134    std::cout << "2...";
135    if( img.componentSize%8 > 0 )
136    {
137       img.componentSize += 8-img.componentSize%8;
138    }
139    size_t size = img.sizeX * img.sizeY * img.sizeZ 
140                * img.components * img.componentSize / 8;
141    unsigned char *imageData = new unsigned char[size];
142
143    // FIXME : find a better heuristic to create the image
144    unsigned char *tmp = imageData;
145    for(int k=0;k<img.sizeZ;k++)
146    {
147       for(int j=0;j<img.sizeY;j++)
148       {
149          for(int i=0;i<img.sizeX;i++)
150          {
151             for(int c=0;c<img.components;c++)
152             {
153                *tmp = j%256;
154                if( img.componentSize>8 )
155                {
156                   *(tmp+1) = j/256;
157                }
158                tmp += img.components/8;
159             }
160          }
161       }
162    }
163
164 // Step 3 : Create the file of the image
165    std::cout << "3...";
166    gdcm::FileHelper *file = new gdcm::FileHelper(fileToBuild);
167    file->SetImageData(imageData,size);
168
169 // Step 4 : Set the writting mode and write the image
170    std::cout << "4...";
171
172    file->SetWriteModeToRaw();
173    switch (img.writeMode)
174    {
175       case 'a' : // Write an ACR file
176          file->SetWriteTypeToAcr();
177          break;
178
179       case 'e' : // Write a DICOM Explicit VR file
180          file->SetWriteTypeToDcmExplVR();
181          break;
182
183       case 'i' : // Write a DICOM Implicit VR file
184          file->SetWriteTypeToDcmImplVR();
185          break;
186
187       default :
188          std::cout << "Failed\n"
189                    << "        Write mode '"<<img.writeMode<<"' is undefined\n";
190
191          delete file;
192          delete fileToBuild;
193          delete[] imageData;
194          return 1;
195    }
196
197    if( !file->Write(fileName) )
198    {
199       std::cout << "Failed\n"
200                 << "File in unwrittable\n";
201
202       delete file;
203       delete fileToBuild;
204       delete[] imageData;
205       return 1;
206    }
207
208 // Step 5 : Read the written image
209    std::cout << "5...";
210    gdcm::FileHelper *reread = new gdcm::FileHelper( fileName );
211    if( !reread->GetFile()->IsReadable() )
212    {
213       std::cerr << "Failed" << std::endl
214                 << "Could not reread image written: " << fileName << std::endl;
215       delete fileToBuild;
216       delete file;
217       delete reread;
218       delete[] imageData;
219       return 1;
220    }
221
222 // Step 6 : Compare to the written image
223    std::cout << "6...";
224    size_t dataSizeWritten = reread->GetImageDataSize();
225    uint8_t *imageDataWritten = reread->GetImageData();
226
227    // Test the image size
228    if (fileToBuild->GetXSize() != reread->GetFile()->GetXSize() ||
229        fileToBuild->GetYSize() != reread->GetFile()->GetYSize() ||
230        fileToBuild->GetZSize() != reread->GetFile()->GetZSize())
231    {
232       std::cout << "Failed" << std::endl
233          << "        X Size differs: "
234          << "X: " << fileToBuild->GetXSize() << " # " 
235                   << reread->GetFile()->GetXSize() << " | "
236          << "Y: " << fileToBuild->GetYSize() << " # " 
237                   << reread->GetFile()->GetYSize() << " | "
238          << "Z: " << fileToBuild->GetZSize() << " # " 
239                   << reread->GetFile()->GetZSize() << std::endl;
240       delete fileToBuild;
241       delete file;
242       delete reread;
243       delete[] imageData;
244
245       return 1;
246    }
247
248    // Test the data size
249    if (size != dataSizeWritten)
250    {
251       std::cout << "Failed" << std::endl
252          << "        Pixel areas lengths differ: "
253          << size << " # " << dataSizeWritten << std::endl;
254       delete fileToBuild;
255       delete file;
256       delete reread;
257       delete[] imageData;
258
259       return 1;
260    }
261
262    // Test the data's content
263    if (int res = memcmp(imageData, imageDataWritten, size) !=0)
264    {
265       (void)res;
266       std::cout << "Failed" << std::endl
267                 << "        Pixel differ (as expanded in memory)." << std::endl;
268       delete fileToBuild;
269       delete file;
270       delete reread;
271       delete[] imageData;
272
273       return 1;
274    }
275
276    std::cout << "OK" << std::endl;
277
278    delete fileToBuild;
279    delete file;
280    delete reread;
281    delete[] imageData;
282
283    return 0;
284 }
285
286 int TestWriteSimple(int argc, char *argv[])
287 {
288    if (argc < 1) 
289    {
290       std::cerr << "usage: \n" 
291                 << argv[0] << " (without parameters) " << std::endl 
292                 << std::endl;
293       return 1;
294    }
295
296    int ret=0;
297    int i=0;
298    while( Images[i].sizeX>0 && Images[i].sizeY>0 )
299    {
300       ret += WriteSimple(Images[i]);
301       i++;
302    }
303
304    return ret;
305 }