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