1 /*=========================================================================
4 Module: $RCSfile: exUnMosaic2.cxx,v $
6 Date: $Date: 2011/03/31 21:45:08 $
7 Version: $Revision: 1.2 $
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 =========================================================================*/
19 #include <stdlib.h> // for exit
20 #include <math.h> // for sqrt, ceil
22 #include "gdcmFileHelper.h"
23 #include "vtkGdcmReader.h"
25 bool reorganize_mosaic(const unsigned short *input, const unsigned int *inputdims, unsigned int square,
26 const unsigned int *outputdims, unsigned short *output );
30 bool reorganize_mosaic(const unsigned short *input, const unsigned int *inputdims, unsigned int nbImagesPerRow,
31 const unsigned int *outputdims, unsigned short *output )
33 for(unsigned x = 0; x < outputdims[0]; ++x)
35 for(unsigned y = 0; y < outputdims[1]; ++y)
37 for(unsigned z = 0; z < outputdims[2]; ++z)
39 output[ x + y*outputdims[0] + z*outputdims[0]*outputdims[1] ] =
40 input[ (x + z * outputdims[0]) + (y + (z/nbImagesPerRow)*outputdims[0])*inputdims[0] ];
48 int main(int argc, char *argv[])
51 std::string fileName = argv[1];
52 std::string outputFileName = argv[2];
53 unsigned int nbImagesPerRow = 8;
54 unsigned int numberOfImagesInMosaic = 52;
56 unsigned int inputdims[3];
57 // inputdims[0] = 1024;
58 // inputdims[1] = 1024;
60 unsigned int outputdims[3];
62 // ============================================================
63 // Read the input image.
64 // ============================================================
66 vtkGdcmReader *reader = vtkGdcmReader::New();
67 reader->SetFileName( fileName );
70 vtkImageData *vtkimage = reader->GetOutput();
74 std::cout << argv[1] << std::endl;
76 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
77 f->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
78 f->SetFileName( fileName );
82 std::cerr << "Sorry, " << fileName <<" not a gdcm-readable "
88 std::cout << " ... is readable " << std::endl;
92 // ============================================================
95 // ============================================================
98 // Pixel Reading must be done here, to be sure
99 // to load the Palettes Color (if any)
101 // First, create a GDCM_NAME_SPACE::FileHelper
102 GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
104 // Load the pixels, DO NOT transform LUT (if any) into RGB Pixels
105 uint8_t *imageDataRaw = fh->GetImageDataRaw();
106 // Get the image data size
107 size_t dataRawSize = fh->GetImageDataRawSize();
111 // ============================================================
112 // Create a new GDCM_NAME_SPACE::Filehelper, to hold new image.
113 // ============================================================
116 GDCM_NAME_SPACE::FileHelper *copy = GDCM_NAME_SPACE::FileHelper::New( );
117 copy->SetFileName( outputFileName );
121 unsigned int div = (unsigned int)ceil(sqrt( (double)numberOfImagesInMosaic ) );
122 outputdims[0] = inputdims[0]/ div;
123 outputdims[1] = inputdims[1] / div;
124 outputdims[2] = numberOfImagesInMosaic;
127 if (outputdims[0] * outputdims[1] * outputdims[2] * 2 != dataRawSize)
129 std::cout << "outputdims[0] : " << outputdims[0] << " outputdims[1] : " << outputdims[1] << " numberOfImagesInMosaic : " << numberOfImagesInMosaic
130 << " ( = " << outputdims[0] * outputdims[1] * outputdims[2] * 2 << " ) doesn't match with dataRawSize : " << dataRawSize << std::endl;
136 unsigned short *input = (unsigned short *)imageDataRaw;
138 unsigned short *output = (unsigned short *) malloc(outputdims[0] * outputdims[1] * numberOfImagesInMosaic * 2);
140 reorganize_mosaic(input, inputdims,
145 fPixels = fopen(outputFileName.c_str(), "w");
146 fwrite(output,2, outputdims[0] * outputdims[1] * numberOfImagesInMosaic, fPixels);