]> Creatis software - gdcm.git/blob - Example/exUnMosaic.cxx
28126d9447bb7e5e3a0513196f422096ffd12030
[gdcm.git] / Example / exUnMosaic.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exUnMosaic.cxx,v $
5   Language:  C++
6   Date:      $Date: 2011/03/29 07:45:00 $
7   Version:   $Revision: 1.1 $
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 #include <stdlib.h> // for exit
20 #include <math.h> // for sqrt, ceil
21 #include "gdcmFile.h"
22 #include "gdcmFileHelper.h"
23
24 bool reorganize_mosaic(const unsigned short *input, const unsigned int *inputdims, unsigned int square,
25   const unsigned int *outputdims, unsigned short *output );
26   
27   
28   
29 bool reorganize_mosaic(const unsigned short *input, const unsigned int *inputdims, unsigned int nbImagesPerRow,
30   const unsigned int *outputdims, unsigned short *output )
31 {
32   for(unsigned x = 0; x < outputdims[0]; ++x)
33     {
34     for(unsigned y = 0; y < outputdims[1]; ++y)
35       {
36       for(unsigned z = 0; z < outputdims[2]; ++z)
37         {
38         output[ x + y*outputdims[0] + z*outputdims[0]*outputdims[1] ] =
39           input[ (x + z * outputdims[0]) + (y + (z/nbImagesPerRow)*outputdims[0])*inputdims[0] ];
40         }
41       }
42     }
43   return true;
44 }
45
46
47 int main(int argc, char *argv[])
48 {   
49
50    std::string fileName       = argv[1];
51    std::string outputFileName = argv[2];
52    unsigned int nbImagesPerRow = 8;
53    unsigned int numberOfImagesInMosaic = 52;
54    
55    unsigned int inputdims[2];
56    inputdims[0] = 1024;
57    inputdims[1] = 1024;
58    
59    unsigned int outputdims[3];
60      
61 // ============================================================
62 //   Read the input image.
63 // ============================================================
64
65    std::cout << argv[1] << std::endl;
66
67    GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
68    f->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
69    f->SetFileName( fileName );
70    bool res = f->Load();        
71
72    if (!res) {
73        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
74                  << "DICOM / ACR File"
75                  <<std::endl;
76        f->Delete();
77        return 0;
78    }
79    std::cout << " ... is readable " << std::endl;
80    
81    
82 // ============================================================
83 //   Read the Pixels
84 //
85 // ============================================================
86  
87    // Pixel Reading must be done here, to be sure 
88    // to load the Palettes Color (if any)
89
90    // First, create a GDCM_NAME_SPACE::FileHelper
91    GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
92
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();
97    
98 // ============================================================
99 //   Create a new GDCM_NAME_SPACE::Filehelper, to hold new image.
100 // ============================================================
101
102    GDCM_NAME_SPACE::FileHelper *copy = GDCM_NAME_SPACE::FileHelper::New( );
103    copy->SetFileName( outputFileName );
104   // copy->Load(); 
105
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;
110   
111 /*   
112   if (outputdims[0] *  outputdims[1] * outputdims[2] * 2 != dataRawSize)
113   {
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;
116      //exit(0);       
117   }
118 */ 
119
120    
121   unsigned short *input = (unsigned short *)imageDataRaw;
122   
123   unsigned short *output = (unsigned short *) malloc(outputdims[0] * outputdims[1] * numberOfImagesInMosaic * 2);
124   
125   reorganize_mosaic(input, inputdims,
126                     nbImagesPerRow,
127                     outputdims, output);
128
129   FILE *fPixels;
130   fPixels = fopen(outputFileName.c_str(), "w");
131   fwrite(output,2, outputdims[0] * outputdims[1] * numberOfImagesInMosaic, fPixels);
132
133