]> Creatis software - gdcm.git/blob - Example/exUnMosaicStack.cxx
re indent
[gdcm.git] / Example / exUnMosaicStack.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exUnMosaicStack.cxx,v $
5   Language:  C++
6   Date:      $Date: 2011/08/25 14:37:05 $
7   Version:   $Revision: 1.3 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18
19
20 //  UNFINISHED !  JPRx
21
22
23 #include <stdlib.h> // for exit
24 #include <math.h> // for sqrt, ceil
25 #include "gdcmFile.h"
26 #include "gdcmFileHelper.h"
27
28 bool reorganize_mosaic(const unsigned short *input, const unsigned int *inputdims, unsigned int square,
29   const unsigned int *outputdims, unsigned short *output );
30   
31   
32   
33 bool reorganize_mosaic(const unsigned short *input, const unsigned int *inputdims, unsigned int nbImagesPerRow,
34   const unsigned int *outputdims, unsigned short *output )
35 {
36   for(unsigned x = 0; x < outputdims[0]; ++x)
37     {
38     for(unsigned y = 0; y < outputdims[1]; ++y)
39       {
40       for(unsigned z = 0; z < outputdims[2]; ++z)
41         {
42         output[ x + y*outputdims[0] + z*outputdims[0]*outputdims[1] ] =
43           input[ (x + z * outputdims[0]) + (y + (z/nbImagesPerRow)*outputdims[0])*inputdims[0] ];
44         }
45       }
46     }
47   return true;
48 }
49
50
51 bool reorganize_mosaic_stack(const unsigned short *input, const unsigned int *inputdims, unsigned int nbImagesPerRow,
52   const unsigned int *outputdims, unsigned short *output )
53 {
54   for(unsigned x = 0; x < outputdims[0]; ++x)
55     {
56     for(unsigned y = 0; y < outputdims[1]; ++y)
57       {
58       for(unsigned z = 0; z < outputdims[2]; ++z)
59         {
60         output[ x + y*outputdims[0] + z*outputdims[0]*outputdims[1] ] =
61           input[ (x + z * outputdims[0]) + (y + (z/nbImagesPerRow)*outputdims[0])*inputdims[0] ];
62         }
63       }
64     }
65   return true;
66 }
67
68
69 int main(int argc, char *argv[])
70 {   
71
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;
77    
78    unsigned int inputdims[2];
79    inputdims[0] = 1024;
80    inputdims[1] = 1024;
81    
82    unsigned int outputdims[3];
83      
84 // ============================================================
85 //   Read the input image.
86 // ============================================================
87
88    std::cout << argv[1] << std::endl;
89
90       std::cout << "dirName [" << dirName << "]" << std::endl;
91       
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;
95       bool res;
96
97       if (fileList.size() == 0)
98       {
99          std::cout << "No file found in : [" << dirName << "]" << std::endl;
100       }
101
102
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();        
107
108    if (!res) {
109        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
110                  << "DICOM / ACR File"
111                  <<std::endl;
112        f->Delete();
113        return 0;
114    }
115    std::cout << " ... is readable " << std::endl;
116    
117    
118 // ============================================================
119 //   Read the Pixels
120 //
121 // ============================================================
122  
123    // Pixel Reading must be done here, to be sure 
124    // to load the Palettes Color (if any)
125
126    // First, create a GDCM_NAME_SPACE::FileHelper
127    GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
128
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();
133    
134 // ============================================================
135 //   Create a new GDCM_NAME_SPACE::Filehelper, to hold new image.
136 // ============================================================
137
138    GDCM_NAME_SPACE::FileHelper *copy = GDCM_NAME_SPACE::FileHelper::New( );
139    copy->SetFileName( outputFileName );
140   // copy->Load(); 
141
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;
146   
147 /*   
148   if (outputdims[0] *  outputdims[1] * outputdims[2] * 2 != dataRawSize)
149   {
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;
152      //exit(0);
153   }
154 */ 
155
156    
157   unsigned short *input = (unsigned short *)imageDataRaw;
158   
159   unsigned short *output = (unsigned short *) malloc(outputdims[0] * outputdims[1] * numberOfImagesInMosaic * 2);
160   
161   reorganize_mosaic(input, inputdims,
162                     nbImagesPerRow,
163                     outputdims, output);
164
165   FILE *fPixels;
166   fPixels = fopen(outputFileName.c_str(), "w");
167   fwrite(output,2, outputdims[0] * outputdims[1] * numberOfImagesInMosaic, fPixels);
168
169