]> Creatis software - gdcm.git/blob - Testing/TestWriteSimple.cxx
* Testing/TestWriteSimple.cxx : fix bug... sorry
[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 18:43:38 $
7   Version:   $Revision: 1.21 $
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.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       delete[] imageData;
231       return 1;
232    }
233
234 // Step 6 : Compare to the written image
235    std::cout << "6...";
236    size_t dataSizeWritten = reread->GetImageDataSize();
237    uint8_t *imageDataWritten = reread->GetImageData();
238
239    // Test the image size
240    if (fileToBuild->GetXSize() != reread->GetFile()->GetXSize() ||
241        fileToBuild->GetYSize() != reread->GetFile()->GetYSize() ||
242        fileToBuild->GetZSize() != reread->GetFile()->GetZSize())
243    {
244       std::cout << "Failed" << std::endl
245          << "        X Size differs: "
246          << "X: " << fileToBuild->GetXSize() << " # " 
247                   << reread->GetFile()->GetXSize() << " | "
248          << "Y: " << fileToBuild->GetYSize() << " # " 
249                   << reread->GetFile()->GetYSize() << " | "
250          << "Z: " << fileToBuild->GetZSize() << " # " 
251                   << reread->GetFile()->GetZSize() << std::endl;
252       delete fileToBuild;
253       delete file;
254       delete reread;
255       delete[] imageData;
256
257       return 1;
258    }
259
260    // Test the data size
261    if (size != dataSizeWritten)
262    {
263       std::cout << "Failed" << std::endl
264          << "        Pixel areas lengths differ: "
265          << size << " # " << dataSizeWritten << std::endl;
266       delete fileToBuild;
267       delete file;
268       delete reread;
269       delete[] imageData;
270
271       return 1;
272    }
273
274    // Test the data's content
275    if (int res = memcmp(imageData, imageDataWritten, size) !=0)
276    {
277       (void)res;
278       std::cout << "Failed" << std::endl
279                 << "        Pixel differ (as expanded in memory)." << std::endl;
280       delete fileToBuild;
281       delete file;
282       delete reread;
283       delete[] imageData;
284
285       return 1;
286    }
287
288    std::cout << "OK" << std::endl;
289
290    delete fileToBuild;
291    delete file;
292    delete reread;
293    delete[] imageData;
294
295    return 0;
296 }
297
298 int TestWriteSimple(int argc, char *argv[])
299 {
300    if (argc < 1) 
301    {
302       std::cerr << "usage: \n" 
303                 << argv[0] << " (without parameters) " << std::endl 
304                 << std::endl;
305       return 1;
306    }
307
308    int ret=0;
309    int i=0;
310    while( Images[i].sizeX>0 && Images[i].sizeY>0 )
311    {
312       ret += WriteSimple(Images[i]);
313       i++;
314    }
315
316    return ret;
317 }