1 /*=========================================================================
4 Module: $RCSfile: exUnMosaic.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"
24 bool reorganize_mosaic(const unsigned short *input, const unsigned int *inputdims, unsigned int square,
25 const unsigned int *outputdims, unsigned short *output );
29 bool reorganize_mosaic(const unsigned short *input, const unsigned int *inputdims, unsigned int nbImagesPerRow,
30 const unsigned int *outputdims, unsigned short *output )
32 for(unsigned x = 0; x < outputdims[0]; ++x)
34 for(unsigned y = 0; y < outputdims[1]; ++y)
36 for(unsigned z = 0; z < outputdims[2]; ++z)
38 output[ x + y*outputdims[0] + z*outputdims[0]*outputdims[1] ] =
39 input[ (x + z * outputdims[0]) + (y + (z/nbImagesPerRow)*outputdims[0])*inputdims[0] ];
47 int main(int argc, char *argv[])
50 std::string fileName = argv[1];
51 std::string outputFileName = argv[2];
52 unsigned int nbImagesPerRow = 8;
53 unsigned int numberOfImagesInMosaic = 52;
55 unsigned int inputdims[2];
59 unsigned int outputdims[3];
61 // ============================================================
62 // Read the input image.
63 // ============================================================
65 std::cout << argv[1] << std::endl;
67 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
68 f->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
69 f->SetFileName( fileName );
73 std::cerr << "Sorry, " << fileName <<" not a gdcm-readable "
79 std::cout << " ... is readable " << std::endl;
82 // ============================================================
85 // ============================================================
87 // Pixel Reading must be done here, to be sure
88 // to load the Palettes Color (if any)
90 // First, create a GDCM_NAME_SPACE::FileHelper
91 GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
93 // Load the pixels, DO NOT transform LUT (if any) into RGB Pixels
94 uint8_t *imageDataRaw = fh->GetImageDataRaw();
95 // Get the image data size
96 size_t dataRawSize = fh->GetImageDataRawSize();
98 // ============================================================
99 // Create a new GDCM_NAME_SPACE::Filehelper, to hold new image.
100 // ============================================================
102 GDCM_NAME_SPACE::FileHelper *copy = GDCM_NAME_SPACE::FileHelper::New( );
103 copy->SetFileName( outputFileName );
106 unsigned int div = (unsigned int)ceil(sqrt( (double)numberOfImagesInMosaic ) );
107 outputdims[0] = inputdims[0]/ div;
108 outputdims[1] = inputdims[1] / div;
109 outputdims[2] = numberOfImagesInMosaic;
112 if (outputdims[0] * outputdims[1] * outputdims[2] * 2 != dataRawSize)
114 std::cout << "outputdims[0] : " << outputdims[0] << " outputdims[1] : " << outputdims[1] << " numberOfImagesInMosaic : " << numberOfImagesInMosaic
115 << " ( = " << outputdims[0] * outputdims[1] * outputdims[2] * 2 << " ) doesn't match with dataRawSize : " << dataRawSize << std::endl;
121 unsigned short *input = (unsigned short *)imageDataRaw;
123 unsigned short *output = (unsigned short *) malloc(outputdims[0] * outputdims[1] * numberOfImagesInMosaic * 2);
125 reorganize_mosaic(input, inputdims,
130 fPixels = fopen(outputFileName.c_str(), "w");
131 fwrite(output,2, outputdims[0] * outputdims[1] * numberOfImagesInMosaic, fPixels);