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