]> Creatis software - gdcm.git/blob - src/gdcmJPEGFragmentsInfo.cxx
* Remove friend between PixelReadConverter and RLEFramesInfo,
[gdcm.git] / src / gdcmJPEGFragmentsInfo.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmJPEGFragmentsInfo.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/28 15:42:22 $
7   Version:   $Revision: 1.12 $
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 "gdcmJPEGFragmentsInfo.h"
20 #include "gdcmDebug.h"
21
22 #include <fstream>
23
24 namespace gdcm 
25 {
26
27 JPEGFragmentsInfo::JPEGFragmentsInfo()
28 {
29    StateSuspension = 0;
30 }
31
32 /**
33  * \brief Default destructor
34  */
35 JPEGFragmentsInfo::~JPEGFragmentsInfo()
36 {
37    for(JPEGFragmentsList::iterator it  = Fragments.begin();
38                                    it != Fragments.end();
39                                  ++it )
40    {
41       delete *it;
42    }
43    Fragments.clear();
44 }
45
46 /**
47  * \brief        Print self.
48  * @param os     Stream to print to.
49  * @param indent Indentation string to be prepended during printing.
50  */
51 void JPEGFragmentsInfo::Print( std::ostream &os, std::string const &indent )
52 {
53    os << std::endl;
54    os << indent
55       << "----------------- JPEG fragments --------------------------------"
56       << std::endl << std::endl;
57    os << indent
58       << "Total number of fragments : " << Fragments.size()
59       << std::endl;
60    int fragmentNumber = 0;
61    for(JPEGFragmentsList::iterator it  = Fragments.begin();
62                                    it != Fragments.end();
63                                  ++it)
64    {
65       os << indent
66          << "   fragment number :" << fragmentNumber++;
67       (*it)->Print( os, indent + "   ");
68    }
69    os << std::endl;
70 }
71
72 /**
73  * \brief  Calculate sum of all fragments length and return total
74  * @return Total size of JPEG fragments length
75  */
76 size_t JPEGFragmentsInfo::GetFragmentsLength()
77 {
78    // Loop on the fragment[s] to get total length
79    size_t totalLength = 0;
80    JPEGFragmentsList::const_iterator it;
81    for( it  = Fragments.begin();
82         it != Fragments.end();
83         ++it )
84    {
85       totalLength += (*it)->GetLength();
86    }
87    return totalLength;
88 }
89
90 /**
91  * \brief Read the all the JPEG Fragment into the input buffer
92  */
93 void JPEGFragmentsInfo::ReadAllFragments(std::ifstream *fp, JOCTET *buffer )
94 {
95    JOCTET *p = buffer;
96
97    // Loop on the fragment[s]
98    JPEGFragmentsList::const_iterator it;
99    for( it  = Fragments.begin();
100         it != Fragments.end();
101         ++it )
102    {
103       fp->seekg( (*it)->GetOffset(), std::ios::beg);
104       size_t len = (*it)->GetLength();
105       fp->read((char *)p,len);
106       p += len;
107    }
108
109 }
110
111 void JPEGFragmentsInfo::DecompressJPEGFramesFromFile(std::ifstream *fp, uint8_t *buffer, int nBits, int numBytes, int length)
112 {
113    // Pointer to the Raw image
114    uint8_t *localRaw = buffer;
115
116   // Loop on the fragment[s]
117    JPEGFragmentsList::const_iterator it;
118    for( it  = Fragments.begin();
119         it != Fragments.end();
120         ++it )
121    {
122      //(*it)->pimage = localRaw;
123      (*it)->DecompressJPEGFramesFromFile(fp, localRaw, nBits, StateSuspension);
124      // update pointer to image after some scanlines read:
125      localRaw = (*it)->GetImage();
126       // Advance to next free location in Raw 
127       // for next fragment decompression (if any)
128
129       //localRaw += length * numBytes;
130      //std::cerr << "Used to increment by: " << length * numBytes << std::endl;
131    }
132 }
133
134 void JPEGFragmentsInfo::AddFragment(JPEGFragment *fragment)
135 {
136    Fragments.push_back(fragment);
137 }
138
139 JPEGFragment *JPEGFragmentsInfo::GetFirstFragment()
140 {
141    ItFragments = Fragments.begin();
142    if (ItFragments != Fragments.end())
143       return  *ItFragments;
144    return NULL;
145 }
146
147 JPEGFragment *JPEGFragmentsInfo::GetNextFragment()
148 {
149    gdcmAssertMacro (ItFragments != Fragments.end());
150
151    ++ItFragments;
152    if (ItFragments != Fragments.end())
153       return  *ItFragments;
154    return NULL;
155 }
156
157 unsigned int JPEGFragmentsInfo::GetFragmentCount()
158 {
159    return Fragments.size();
160 }
161
162 } // end namespace gdcm
163