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