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