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