]> Creatis software - gdcm.git/blob - src/gdcmRLEFramesInfo.cxx
Add #include for BCC
[gdcm.git] / src / gdcmRLEFramesInfo.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmRLEFramesInfo.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/01/27 10:01:34 $
7   Version:   $Revision: 1.20 $
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 
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 bitsAllocated Bits allocated
75  * @return    Boolean
76  */
77 bool RLEFramesInfo::DecompressRLEFile( std::ifstream *fp , uint8_t *raw, 
78                                        int xSize, int ySize, int zSize, 
79                                        int bitsAllocated )
80 {
81    uint8_t *subRaw = raw;
82    long rawSegmentSize = xSize * ySize;
83
84    // Loop on the frame[s]
85    for(RLEFrameList::iterator it = Frames.begin(); it != Frames.end(); ++it)
86    {
87       subRaw = (*it)->ReadAndDecompressRLEFrame( subRaw, rawSegmentSize, fp);
88    }
89
90    if ( bitsAllocated == 16 )
91    {
92       // Try to deal with RLE 16 Bits
93       ConvertRLE16BitsFromRLE8Bits( raw, xSize, ySize, zSize );
94    }
95
96    return true;
97 }
98
99 /**
100  * \brief  We assume Raw contains the decoded RLE pixels but as
101  *         8 bits per pixel. We convert those pixels to 16 bits
102  *         per pixel.
103  * @param raw raw 
104  * @param xSize x Size
105  * @param ySize y Size
106  * @param numberOfFrames number of frames 
107  * @return    Boolean always true
108  */
109 bool RLEFramesInfo::ConvertRLE16BitsFromRLE8Bits(uint8_t *raw, int xSize, 
110                                                  int ySize, int numberOfFrames)
111 {
112    size_t pixelNumber = xSize * ySize;
113    size_t rawSize     = pixelNumber * numberOfFrames * 2;
114
115    // We assumed Raw contains the decoded RLE pixels but as
116    // 8 bits per pixel. In order to convert those pixels to 16 bits
117    // per pixel we cannot work in place within Raw and hence
118    // we copy it in a safe place, say copyRaw.
119
120    uint8_t *copyRaw = new uint8_t[rawSize];
121    memmove( copyRaw, raw, rawSize );
122
123    uint8_t *x = raw;
124    uint8_t *a;
125    uint8_t *b;
126
127    // Warning : unckecked patch to see the behaviour on Big Endian Processors
128
129    if ( !Util::IsCurrentProcessorBigEndian() )
130    { 
131       a = copyRaw;         // beginning of 'low bytes'
132       b = a + pixelNumber; // beginning of 'hight bytes'
133    }
134    else
135    {
136       b = copyRaw;         // beginning of 'low bytes'
137       a = b + pixelNumber; // beginning of 'hight bytes'
138    } 
139
140    // Re order bytes
141    for ( int i = 0; i < numberOfFrames; i++ )
142    {
143       for ( unsigned int j = 0; j < pixelNumber; j++ )
144       {
145          *(x++) = *(b++);
146          *(x++) = *(a++);
147       }
148    }
149
150    delete[] copyRaw;
151
152    return true;
153 }
154
155 //-----------------------------------------------------------------------------
156 // Protected
157
158 //-----------------------------------------------------------------------------
159 // Private
160
161 //-----------------------------------------------------------------------------
162 // Print
163 /**
164  * \brief        Print self.
165  * @param indent Indentation string to be prepended during printing.
166  * @param os     Stream to print to.
167  */
168 void RLEFramesInfo::Print( std::ostream &os, std::string indent )
169 {
170    os << std::endl;
171    os << indent
172       << "----------------- RLE frames --------------------------------"
173       << std::endl;
174    os << indent
175       << "Total number of Frames : " << Frames.size()
176       << std::endl;
177    int frameNumber = 0;
178    for(RLEFrameList::iterator it = Frames.begin(); it != Frames.end(); ++it)
179    {
180       os << indent
181          << "   frame number :" << frameNumber++
182          << std::endl;
183       (*it)->Print( os, indent + "   " );
184    }
185 }
186
187 //-----------------------------------------------------------------------------
188 } // end namespace gdcm