]> Creatis software - gdcm.git/blob - src/gdcmRLEFramesInfo.cxx
Fix mistypings
[gdcm.git] / src / gdcmRLEFramesInfo.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmRLEFramesInfo.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/05/23 14:18:11 $
7   Version:   $Revision: 1.22 $
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 "gdcmRLEFramesInfo.h"
20 #include "gdcmDebug.h"
21 #include "gdcmUtil.h"
22
23 #if defined(__BORLANDC__)
24    #include <mem.h> // for memset
25 #endif 
26
27 namespace GDCM_NAME_SPACE 
28 {
29 //-------------------------------------------------------------------------
30 // Constructor / Destructor
31 RLEFramesInfo::~RLEFramesInfo()
32 {
33    for(RLEFrameList::iterator it = Frames.begin(); it != Frames.end(); ++it)
34    {
35       delete (*it);
36    }
37    Frames.clear();
38 }
39
40 //-----------------------------------------------------------------------------
41 // Public
42 void RLEFramesInfo::AddFrame(RLEFrame *frame)
43 {
44    Frames.push_back(frame);
45 }
46
47 RLEFrame *RLEFramesInfo::GetFirstFrame()
48 {
49    ItFrames = Frames.begin();
50    if (ItFrames != Frames.end())
51       return  *ItFrames;
52    return NULL;
53 }
54
55 RLEFrame *RLEFramesInfo::GetNextFrame()
56 {
57    gdcmAssertMacro (ItFrames != Frames.end());
58
59    ++ItFrames;
60    if (ItFrames != Frames.end())
61       return  *ItFrames;
62    return NULL;
63 }
64
65 /**
66  * \brief     Reads from disk the Pixel Data of 'Run Length Encoded'
67  *            Dicom encapsulated file and decompress it.
68  * @param     fp already open File Pointer
69  *            from which the pixel data should be read
70  * @param raw raw
71  * @param xSize x Size
72  * @param ySize y Size
73  * @param zSize z Size
74  * @param tSize t Size 
75  * @param bitsAllocated Bits allocated
76  * @return    Boolean
77  */
78 bool RLEFramesInfo::DecompressRLEFile( std::ifstream *fp , uint8_t *raw, 
79                                        int xSize, int ySize, int zSize, 
80                                        int tSize, int bitsAllocated )
81 {
82    uint8_t *subRaw = raw;
83    long rawSegmentSize = xSize * ySize * tSize;
84
85    // Loop on the frame[s]
86    for(RLEFrameList::iterator it = Frames.begin(); it != Frames.end(); ++it)
87    {
88       subRaw = (*it)->ReadAndDecompressRLEFrame( subRaw, rawSegmentSize, fp);
89    }
90
91    if ( bitsAllocated == 16 )
92    {
93       // Try to deal with RLE 16 Bits
94       ConvertRLE16BitsFromRLE8Bits( raw, xSize, ySize, zSize, tSize );
95    }
96
97    return true;
98 }
99
100 /**
101  * \brief  We assume Raw contains the decoded RLE pixels but as
102  *         8 bits per pixel. We convert those pixels to 16 bits
103  *         per pixel.
104  * @param raw raw 
105  * @param xSize x Size
106  * @param ySize y Size
107  * @param tSize t Size 
108  * @param numberOfFrames number of frames 
109  * @return    Boolean always true
110  */
111 bool RLEFramesInfo::ConvertRLE16BitsFromRLE8Bits(uint8_t *raw, int xSize, 
112                                                  int ySize, int tSize,
113                                                  int numberOfFrames)
114 {
115    size_t pixelNumber = xSize * ySize * tSize;
116    size_t rawSize     = pixelNumber * numberOfFrames * 2;
117
118    // We assumed Raw contains the decoded RLE pixels but as
119    // 8 bits per pixel. In order to convert those pixels to 16 bits
120    // per pixel we cannot work in place within Raw and hence
121    // we copy it in a safe place, say copyRaw.
122
123    uint8_t *copyRaw = new uint8_t[rawSize];
124    memmove( copyRaw, raw, rawSize );
125
126    uint8_t *x = raw;
127    uint8_t *a;
128    uint8_t *b;
129
130    // Warning : unckecked patch to see the behaviour on Big Endian Processors
131
132    if ( !Util::IsCurrentProcessorBigEndian() )
133    { 
134       a = copyRaw;         // beginning of 'low bytes'
135       b = a + pixelNumber; // beginning of 'hight bytes'
136    }
137    else
138    {
139       b = copyRaw;         // beginning of 'low bytes'
140       a = b + pixelNumber; // beginning of 'hight bytes'
141    } 
142
143    // Re order bytes
144    for ( int i = 0; i < numberOfFrames; i++ )
145    {
146       for ( unsigned int j = 0; j < pixelNumber; j++ )
147       {
148          *(x++) = *(b++);
149          *(x++) = *(a++);
150       }
151    }
152
153    delete[] copyRaw;
154
155    return true;
156 }
157
158 //-----------------------------------------------------------------------------
159 // Protected
160
161 //-----------------------------------------------------------------------------
162 // Private
163
164 //-----------------------------------------------------------------------------
165 // Print
166 /**
167  * \brief        Print self.
168  * @param indent Indentation string to be prepended during printing.
169  * @param os     Stream to print to.
170  */
171 void RLEFramesInfo::Print( std::ostream &os, std::string indent )
172 {
173    os << std::endl;
174    os << indent
175       << "----------------- RLE frames --------------------------------"
176       << std::endl;
177    os << indent
178       << "Total number of Frames : " << Frames.size()
179       << std::endl;
180    int frameNumber = 0;
181    /// \todo : find an example, to know haow 3rd and 4th dimension
182    ///         works together
183    for(RLEFrameList::iterator it = Frames.begin(); it != Frames.end(); ++it)
184    {
185       os << indent
186          << "   frame number :" << frameNumber++
187          << std::endl;
188       (*it)->Print( os, indent + "   " );
189    }
190 }
191
192 //-----------------------------------------------------------------------------
193 } // end namespace gdcm