1 /*=========================================================================
4 Module: $RCSfile: exUnMosaicStack.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 =========================================================================*/
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;
105 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
106 f->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
107 f->SetFileName( fileName );
108 bool res = f->Load();
111 std::cerr << "Sorry, " << fileName <<" not a gdcm-readable "
112 << "DICOM / ACR File"
117 std::cout << " ... is readable " << std::endl;
120 // ============================================================
123 // ============================================================
125 // Pixel Reading must be done here, to be sure
126 // to load the Palettes Color (if any)
128 // First, create a GDCM_NAME_SPACE::FileHelper
129 GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
131 // Load the pixels, DO NOT transform LUT (if any) into RGB Pixels
132 uint8_t *imageDataRaw = fh->GetImageDataRaw();
133 // Get the image data size
134 size_t dataRawSize = fh->GetImageDataRawSize();
136 // ============================================================
137 // Create a new GDCM_NAME_SPACE::Filehelper, to hold new image.
138 // ============================================================
140 GDCM_NAME_SPACE::FileHelper *copy = GDCM_NAME_SPACE::FileHelper::New( );
141 copy->SetFileName( outputFileName );
144 unsigned int div = (unsigned int)ceil(sqrt( (double)numberOfImagesInMosaic ) );
145 outputdims[0] = inputdims[0]/ div;
146 outputdims[1] = inputdims[1] / div;
147 outputdims[2] = numberOfImagesInMosaic;
150 if (outputdims[0] * outputdims[1] * outputdims[2] * 2 != dataRawSize)
152 std::cout << "outputdims[0] : " << outputdims[0] << " outputdims[1] : " << outputdims[1] << " numberOfImagesInMosaic : " << numberOfImagesInMosaic
153 << " ( = " << outputdims[0] * outputdims[1] * outputdims[2] * 2 << " ) doesn't match with dataRawSize : " << dataRawSize << std::endl;
159 unsigned short *input = (unsigned short *)imageDataRaw;
161 unsigned short *output = (unsigned short *) malloc(outputdims[0] * outputdims[1] * numberOfImagesInMosaic * 2);
163 reorganize_mosaic(input, inputdims,
168 fPixels = fopen(outputFileName.c_str(), "w");
169 fwrite(output,2, outputdims[0] * outputdims[1] * numberOfImagesInMosaic, fPixels);