]> Creatis software - gdcm.git/blob - src/gdcmRLEFramesInfo.cxx
Try to solve RLE pb
[gdcm.git] / src / gdcmRLEFramesInfo.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmRLEFramesInfo.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/06/17 12:27:52 $
7   Version:   $Revision: 1.16 $
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    gdcmAssertMacro (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;         // begining of 'low bytes'
128       b = a + pixelNumber; // begining of 'hight bytes'
129    }
130    else
131    {
132       b = copyRaw;         // begining of 'low bytes'
133       a = b + pixelNumber; // begining 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
147    delete[] copyRaw;
148
149    return true;
150 }
151
152 //-----------------------------------------------------------------------------
153 // Protected
154
155 //-----------------------------------------------------------------------------
156 // Private
157
158 //-----------------------------------------------------------------------------
159 // Print
160 /**
161  * \brief        Print self.
162  * @param indent Indentation string to be prepended during printing.
163  * @param os     Stream to print to.
164  */
165 void RLEFramesInfo::Print( std::ostream &os, std::string indent )
166 {
167    os << std::endl;
168    os << indent
169       << "----------------- RLE frames --------------------------------"
170       << std::endl;
171    os << indent
172       << "Total number of Frames : " << Frames.size()
173       << std::endl;
174    int frameNumber = 0;
175    for(RLEFrameList::iterator it = Frames.begin(); it != Frames.end(); ++it)
176    {
177       os << indent
178          << "   frame number :" << frameNumber++
179          << std::endl;
180       (*it)->Print( os, indent + "   " );
181    }
182 }
183
184 //-----------------------------------------------------------------------------
185 } // end namespace gdcm