1 /*=========================================================================
4 Module: $RCSfile: exUnMosaicStack.cxx,v $
6 Date: $Date: 2011/08/25 14:37:05 $
7 Version: $Revision: 1.3 $
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.
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.
17 =========================================================================*/
23 #include <stdlib.h> // for exit
24 #include <math.h> // for sqrt, ceil
26 #include "gdcmFileHelper.h"
28 bool reorganize_mosaic(const unsigned short *input, const unsigned int *inputdims, unsigned int square,
29 const unsigned int *outputdims, unsigned short *output );
33 bool reorganize_mosaic(const unsigned short *input, const unsigned int *inputdims, unsigned int nbImagesPerRow,
34 const unsigned int *outputdims, unsigned short *output )
36 for(unsigned x = 0; x < outputdims[0]; ++x)
38 for(unsigned y = 0; y < outputdims[1]; ++y)
40 for(unsigned z = 0; z < outputdims[2]; ++z)
42 output[ x + y*outputdims[0] + z*outputdims[0]*outputdims[1] ] =
43 input[ (x + z * outputdims[0]) + (y + (z/nbImagesPerRow)*outputdims[0])*inputdims[0] ];
51 bool reorganize_mosaic_stack(const unsigned short *input, const unsigned int *inputdims, unsigned int nbImagesPerRow,
52 const unsigned int *outputdims, unsigned short *output )
54 for(unsigned x = 0; x < outputdims[0]; ++x)
56 for(unsigned y = 0; y < outputdims[1]; ++y)
58 for(unsigned z = 0; z < outputdims[2]; ++z)
60 output[ x + y*outputdims[0] + z*outputdims[0]*outputdims[1] ] =
61 input[ (x + z * outputdims[0]) + (y + (z/nbImagesPerRow)*outputdims[0])*inputdims[0] ];
69 int main(int argc, char *argv[])
72 std::string fileName = argv[1];
73 std::string outputFileName = argv[2];
74 unsigned int nbImagesPerRow = 8;
75 unsigned int numberOfImagesInMosaic = 52;
76 unsigned int numberMosaics = 52;
78 unsigned int inputdims[2];
82 unsigned int outputdims[3];
84 // ============================================================
85 // Read the input image.
86 // ============================================================
88 std::cout << argv[1] << std::endl;
90 std::cout << "dirName [" << dirName << "]" << std::endl;
92 GDCM_NAME_SPACE::DirList dirList(dirName,rec); // gets recursively (or not) the file list
93 GDCM_NAME_SPACE::DirListType fileList = dirList.GetFilenames();
94 GDCM_NAME_SPACE::File *f;
97 if (fileList.size() == 0)
99 std::cout << "No file found in : [" << dirName << "]" << std::endl;
103 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
104 f->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
105 f->SetFileName( fileName );
106 bool res = f->Load();
109 std::cerr << "Sorry, " << fileName <<" not a gdcm-readable "
110 << "DICOM / ACR File"
115 std::cout << " ... is readable " << std::endl;
118 // ============================================================
121 // ============================================================
123 // Pixel Reading must be done here, to be sure
124 // to load the Palettes Color (if any)
126 // First, create a GDCM_NAME_SPACE::FileHelper
127 GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
129 // Load the pixels, DO NOT transform LUT (if any) into RGB Pixels
130 uint8_t *imageDataRaw = fh->GetImageDataRaw();
131 // Get the image data size
132 size_t dataRawSize = fh->GetImageDataRawSize();
134 // ============================================================
135 // Create a new GDCM_NAME_SPACE::Filehelper, to hold new image.
136 // ============================================================
138 GDCM_NAME_SPACE::FileHelper *copy = GDCM_NAME_SPACE::FileHelper::New( );
139 copy->SetFileName( outputFileName );
142 unsigned int div = (unsigned int)ceil(sqrt( (double)numberOfImagesInMosaic ) );
143 outputdims[0] = inputdims[0]/ div;
144 outputdims[1] = inputdims[1] / div;
145 outputdims[2] = numberOfImagesInMosaic;
148 if (outputdims[0] * outputdims[1] * outputdims[2] * 2 != dataRawSize)
150 std::cout << "outputdims[0] : " << outputdims[0] << " outputdims[1] : " << outputdims[1] << " numberOfImagesInMosaic : " << numberOfImagesInMosaic
151 << " ( = " << outputdims[0] * outputdims[1] * outputdims[2] * 2 << " ) doesn't match with dataRawSize : " << dataRawSize << std::endl;
157 unsigned short *input = (unsigned short *)imageDataRaw;
159 unsigned short *output = (unsigned short *) malloc(outputdims[0] * outputdims[1] * numberOfImagesInMosaic * 2);
161 reorganize_mosaic(input, inputdims,
166 fPixels = fopen(outputFileName.c_str(), "w");
167 fwrite(output,2, outputdims[0] * outputdims[1] * numberOfImagesInMosaic, fPixels);