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