From 446ab2af0e3045cd559b086ced08a8cc5a61d2e2 Mon Sep 17 00:00:00 2001 From: regrain Date: Thu, 9 Dec 2004 11:31:51 +0000 Subject: [PATCH] * Example/WriteDicomSimple.cxx : example to write a dicom file from nothing. At this time, this image isn't readable by e-film... waiting JPR help to solve it. -- BeNours --- ChangeLog | 5 + Example/CMakeLists.txt | 1 + Example/WriteDicomSimple.cxx | 191 +++++++++++++++++++++++++++++++++++ vtk/vtkGdcmWriter.cxx | 40 ++++---- 4 files changed, 217 insertions(+), 20 deletions(-) create mode 100644 Example/WriteDicomSimple.cxx diff --git a/ChangeLog b/ChangeLog index bf159f0a..341459e5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2004-12-09 Benoit Regrain + * Example/WriteDicomSimple.cxx : example to write a dicom file from nothing. + At this time, this image isn't readable by e-film... waiting JPR help to + solve it. + 2004-12-09 Benoit Regrain * src/gdcmPixelReadConvert.cxx : bug fix when would forcing load of a DocEntry. Now use methods of the Document ! diff --git a/Example/CMakeLists.txt b/Example/CMakeLists.txt index 4a16d5b9..328f2f03 100644 --- a/Example/CMakeLists.txt +++ b/Example/CMakeLists.txt @@ -20,6 +20,7 @@ SET(EXAMPLE_SOURCES TestWrite TestWriteSimple Volume2Dicom + WriteDicomSimple Write WriteRead WriteDicom diff --git a/Example/WriteDicomSimple.cxx b/Example/WriteDicomSimple.cxx new file mode 100644 index 00000000..8503e531 --- /dev/null +++ b/Example/WriteDicomSimple.cxx @@ -0,0 +1,191 @@ +/*========================================================================= + + Program: gdcm + Module: $RCSfile: WriteDicomSimple.cxx,v $ + Language: C++ + Date: $Date: 2004/12/09 11:31:52 $ + Version: $Revision: 1.1 $ + + Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de + l'Image). All rights reserved. See Doc/License.txt or + http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ + +/** + * Write a dicom file from nothing + * The written image is 256x256, 8 bits, unsigned char + * The image content is a horizontal grayscale from + * + */ +#include "gdcmHeader.h" +#include "gdcmFile.h" + +#include + +// Image size +#define SIZE_X 256 +#define SIZE_Y 256 +// Number of components in the image (3 for RGB) +#define COMPONENT 1 +// Size of each component (in byte) +#define COMPONENT_SIZE 1 +// Window / Level +#define COLOR_WINDOW 256 +#define COLOR_LEVEL 128 + +int main(int argc, char* argv[]) +{ + if (argc < 3) + { + std::cerr << "usage: \n" + << argv[0] << " Output Mode " << std::endl + << "Output : output file name\n" + << "Mode : \n" + << " a : ACR, produces a file named Output.ACR\n" + << " e : DICOM Explicit VR, produces a file named Output.E.DCM\n" + << " i : DICOM Implicit VR, produces a file named Output.I.DCM\n" + << " r : RAW, produces a file named Output.RAW\n" + << std::endl; + return 1; + } + + +// Step 1 : Create the header of the image + gdcm::Header *header = new gdcm::Header(); + std::ostringstream str; + + // Set the image size + str.str(""); + str << SIZE_X; + header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0011); + + str.str(""); + str << SIZE_Y; + header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0010); + + // Set the pixel type + str.str(""); + str << COMPONENT_SIZE * 8; + header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0100); + header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0101); + + str.str(""); + str << COMPONENT_SIZE * 8 - 1; + header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0102); + + // Set the pixel representation + str.str(""); + str << "0"; // Unsigned + header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0103); + + // Set the samples per pixel + str.str(""); + str << COMPONENT; + header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0002); + + // Set the Window / Level + str.str(""); + str << COLOR_WINDOW; + header->ReplaceOrCreateByNumber(str.str(),0x0028,0x1051); + str.str(""); + str << COLOR_LEVEL; + header->ReplaceOrCreateByNumber(str.str(),0x0028,0x1050); + + if( !header->IsReadable() ) + { + std::cerr << "-------------------------------\n" + << "Error while creating the file\n" + << "This file is considered to be not readable\n"; + + return 1; + } + +// Step 2 : Create the output image + size_t size = SIZE_X * SIZE_Y * COMPONENT * COMPONENT_SIZE; + unsigned char *imageData = new unsigned char[size]; + + unsigned char *tmp = imageData; + for(int j=0;jSetImageData(imageData,size); + +// Step 4 : Set the writting mode and write the image + std::string fileName = argv[1]; + std::string mode = argv[2]; + + file->SetWriteModeToDecompressed(); + switch (mode[0]) + { + case 'a' : // Write an ACR file + fileName += ".ACR"; + file->SetWriteTypeToAcr(); + std::cout << "Write ACR" << std::endl + << "File :" << fileName << std::endl; + break; + + case 'e' : // Write a DICOM Explicit VR file + fileName += ".E.DCM"; + file->SetWriteTypeToDcmExplVR(); + std::cout << "Write DCM Explicit VR" << std::endl + << "File :" << fileName << std::endl; + break; + + case 'i' : // Write a DICOM Implicit VR file + fileName += ".I.DCM"; + file->SetWriteTypeToDcmImplVR(); + std::cout << "Write DCM Implicit VR" << std::endl + << "File :" << fileName << std::endl; + break; + + case 'r' : // Write a RAW file + fileName += ".RAW"; + file->WriteRawData(fileName); + std::cout << "Write Raw" << std::endl + << "File :" << fileName << std::endl; + + delete[] imageData; + delete file; + delete header; + return 0; + + default : + std::cout << "-------------------------------\n" + << "Write mode undefined...\n" + << "No file written\n"; + + delete[] imageData; + delete file; + delete header; + return 1; + } + + if( !file->Write(fileName) ) + { + std::cout << "-------------------------------\n" + << "Error when writting the file " << fileName << "\n" + << "No file written\n"; + } + + delete[] imageData; + delete file; + delete header; + + return 0; +} diff --git a/vtk/vtkGdcmWriter.cxx b/vtk/vtkGdcmWriter.cxx index c104c2a2..e4ba6795 100644 --- a/vtk/vtkGdcmWriter.cxx +++ b/vtk/vtkGdcmWriter.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: vtkGdcmWriter.cxx,v $ Language: C++ - Date: $Date: 2004/12/09 10:59:59 $ - Version: $Revision: 1.4 $ + Date: $Date: 2004/12/09 11:31:52 $ + Version: $Revision: 1.5 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -26,7 +26,7 @@ #include #include -vtkCxxRevisionMacro(vtkGdcmWriter, "$Revision: 1.4 $"); +vtkCxxRevisionMacro(vtkGdcmWriter, "$Revision: 1.5 $"); vtkStandardNewMacro(vtkGdcmWriter); //----------------------------------------------------------------------------- @@ -95,28 +95,28 @@ void SetImageInformation(gdcm::File *file,vtkImageData *image) int *dim = image->GetDimensions(); str.str(""); - str<ReplaceOrCreateByNumber(str.str(),0x0028,0x0011); str.str(""); - str<ReplaceOrCreateByNumber(str.str(),0x0028,0x0010); if(dim[2]>1) { str.str(""); - str<ReplaceOrCreateByNumber(str.str(),0x0028,0x0012); } // Pixel type str.str(""); - str<GetScalarSize()*8; + str << image->GetScalarSize()*8; file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0100); file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0101); str.str(""); - str<GetScalarSize()*8-1; + str << image->GetScalarSize()*8-1; file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0102); // Pixel Repr @@ -128,44 +128,44 @@ void SetImageInformation(gdcm::File *file,vtkImageData *image) image->GetScalarType() == VTK_UNSIGNED_INT || image->GetScalarType() == VTK_UNSIGNED_LONG ) { - str<<"0"; // Unsigned + str << "0"; // Unsigned } else { - str<<"1"; // Signed + str << "1"; // Signed } file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0103); // Samples per pixel str.str(""); - str<GetNumberOfScalarComponents(); + str << image->GetNumberOfScalarComponents(); file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0002); // Spacing double *sp = image->GetSpacing(); str.str(""); - str<ReplaceOrCreateByNumber(str.str(),0x0028,0x0030); str.str(""); - str<ReplaceOrCreateByNumber(str.str(),0x0018,0x0088); // Origin double *org = image->GetOrigin(); str.str(""); - str<ReplaceOrCreateByNumber(str.str(),0x0020,0x0032); // Window / Level double *rng=image->GetScalarRange(); str.str(""); - str<ReplaceOrCreateByNumber(str.str(),0x0028,0x1051); str.str(""); - str<<(rng[1]+rng[0])/2.0; + str << (rng[1]+rng[0])/2.0; file->ReplaceOrCreateByNumber(str.str(),0x0028,0x1050); // Pixels @@ -183,7 +183,7 @@ void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *image, ofstream *file (void)axis; // To avoid warning if(file) { - vtkErrorMacro(<< "File musn't be opened"); + vtkErrorMacro( << "File musn't be opened"); return; } @@ -219,9 +219,9 @@ void vtkGdcmWriter::WriteDcmFile(char *fileName,vtkImageData *image) // Write the image if(!dcmFile->Write(fileName)) { - vtkErrorMacro(<< "File " << this->FileName << "couldn't be written by " - << " the gdcm library"); - std::cerr<<"not written \n"; + vtkErrorMacro( << "File " << this->FileName << "couldn't be written by " + << " the gdcm library"); + std::cerr << "not written \n"; } delete dcmFile; -- 2.48.1