]> Creatis software - gdcm.git/blob - src/gdcmJPEGFragment.cxx
Doxygen warnings : I forgot this one
[gdcm.git] / src / gdcmJPEGFragment.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmJPEGFragment.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/25 15:37:51 $
7   Version:   $Revision: 1.8 $
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 "gdcmJPEGFragment.h"
20 #include "gdcmDebug.h"
21
22 namespace gdcm
23 {
24
25 // For JPEG 2000, body in file gdcmJpeg2000.cxx
26 // Not yet made
27 bool gdcm_read_JPEG2000_file (std::ifstream* fp, void* image_buffer);
28
29 // For JPEG-LS, body in file gdcmJpegLS.cxx
30 // Not yet made
31 bool gdcm_read_JPEGLS_file (std::ifstream* fp, void* image_buffer);
32
33 // For JPEG 8 Bits, body in file gdcmJpeg8.cxx
34 //bool gdcm_read_JPEG_file8 (JPEGFragment *frag, std::ifstream *fp, void *image_buffer);
35 bool gdcm_read_JPEG_memory8    (const JOCTET *buffer, const size_t buflen, 
36                                 void *image_buffer,
37                                 size_t *howManyRead, size_t *howManyWritten);
38 //
39 // For JPEG 12 Bits, body in file gdcmJpeg12.cxx
40 //bool gdcm_read_JPEG_file12 (JPEGFragment *frag, std::ifstream *fp, void *image_buffer);
41 bool gdcm_read_JPEG_memory12   (const JOCTET *buffer, const size_t buflen, 
42                                 void *image_buffer,
43                                 size_t *howManyRead, size_t *howManyWritten);
44
45 // For JPEG 16 Bits, body in file gdcmJpeg16.cxx
46 // Beware this is misleading there is no 16bits DCT algorithm, only
47 // jpeg lossless compression exist in 16bits.
48 //bool gdcm_read_JPEG_file16 (JPEGFragment *frag, std::ifstream *fp, void *image_buffer);
49 bool gdcm_read_JPEG_memory16   (const JOCTET *buffer, const size_t buflen, 
50                                 void* image_buffer,
51                                 size_t *howManyRead, size_t *howManyWritten);
52
53 /**
54  * \brief Default constructor.
55  */
56 JPEGFragment::JPEGFragment()
57 {
58    Offset = 0;
59    Length = 0;
60
61 //   StateSuspension = 0;
62 //   void *SampBuffer;
63    pimage = 0;
64
65 }
66
67 /**
68  * \brief        Print self.
69  * @param os     Stream to print to.
70  * @param indent Indentation string to be prepended during printing.
71  */
72 void JPEGFragment::Print( std::ostream &os, std::string indent )
73 {
74    os << indent
75       << "JPEG fragment: offset : " <<  Offset
76       << "   length : " <<  Length
77       << std::endl;
78 }
79
80 /**
81  * \brief Decompress 8bits JPEG Fragment
82  * @param fp ifstream to write to
83  * @param buffer     output (data decompress)
84  * @param nBits      8/12 or 16 bits jpeg
85  * @param statesuspension state suspension
86  */
87 void JPEGFragment::DecompressJPEGFramesFromFile(std::ifstream *fp,
88                                                 uint8_t *buffer, int nBits, 
89                                                 int & statesuspension)
90 {
91    // First thing need to reset file to proper position:
92    fp->seekg( Offset, std::ios::beg);
93
94    if ( nBits == 8 )
95    {
96       // JPEG Lossy : call to IJG 6b
97       if ( ! this->gdcm_read_JPEG_file8( fp, buffer, statesuspension) )
98       {
99          //return false;
100       }
101    }
102    else if ( nBits <= 12 )
103    {
104       // Reading Fragment pixels
105       if ( ! this->gdcm_read_JPEG_file12 ( fp, buffer, statesuspension) )
106       {
107          //return false;
108       }
109    }
110    else if ( nBits <= 16 )
111    {
112       // Reading Fragment pixels
113       if ( ! this->gdcm_read_JPEG_file16 ( fp, buffer, statesuspension) )
114       {
115          //return false;
116       }
117       //gdcmAssertMacro( IsJPEGLossless );
118    }
119    else
120    {
121       // FIXME : only the bits number is checked,
122       //         NOT the compression method
123
124       // other JPEG lossy not supported
125       gdcmErrorMacro( "Unknown jpeg lossy compression ");
126       //return false;
127    }
128
129 }
130
131 void JPEGFragment::DecompressJPEGSingleFrameFragmentsFromFile(JOCTET *buffer,
132                                  size_t totalLength, uint8_t *raw, int nBits)
133 {
134    size_t howManyRead = 0;
135    size_t howManyWritten = 0;
136    
137    if ( nBits == 8)
138    {
139       if ( ! gdcm_read_JPEG_memory8( buffer, totalLength, raw,
140                                      &howManyRead, &howManyWritten ) ) 
141       {
142          gdcmErrorMacro( "Failed to read jpeg8 ");
143          delete [] buffer;
144          //return false;
145       }
146    }
147    else if ( nBits <= 12)
148    {
149       if ( ! gdcm_read_JPEG_memory12( buffer, totalLength, raw,
150                                       &howManyRead, &howManyWritten ) ) 
151       {
152          gdcmErrorMacro( "Failed to read jpeg12 ");
153             delete [] buffer;
154             //return false;
155       }
156    }
157    else if ( nBits <= 16)
158    {
159       
160       if ( ! gdcm_read_JPEG_memory16( buffer, totalLength, raw,
161                                       &howManyRead, &howManyWritten ) ) 
162       {
163          gdcmErrorMacro( "Failed to read jpeg16 ");
164          delete [] buffer;
165          //return false;
166       }
167    }
168    else
169    {
170       // FIXME : only the bits number is checked,
171       //         NOT the compression method
172
173       // other JPEG lossy not supported
174       gdcmErrorMacro( "Unsupported jpeg lossy compression ");
175       delete [] buffer;
176       //return false;
177    }      
178
179 }
180
181 void JPEGFragment::DecompressJPEGFragmentedFramesFromFile(JOCTET *buffer, 
182                     uint8_t* raw, int nBits, size_t &howManyRead, 
183                     size_t &howManyWritten, size_t totalLength)
184 {
185    if ( nBits == 8 )
186    {
187      if ( ! gdcm_read_JPEG_memory8( buffer+howManyRead, totalLength-howManyRead,
188                                   raw+howManyWritten,
189                                   &howManyRead, &howManyWritten ) ) 
190        {
191          gdcmErrorMacro( "Failed to read jpeg8");
192          //delete [] buffer;
193          //return false;
194        }
195    }
196    else if ( nBits <= 12 )
197    {
198    
199      if ( ! gdcm_read_JPEG_memory12( buffer+howManyRead, totalLength-howManyRead,
200                                    raw+howManyWritten,
201                                    &howManyRead, &howManyWritten ) ) 
202        {
203          gdcmErrorMacro( "Failed to read jpeg12");
204          //delete [] buffer;
205          //return false;
206       }
207    }
208    else if ( nBits <= 16 )
209    {
210    
211      if ( ! gdcm_read_JPEG_memory16( buffer+howManyRead, totalLength-howManyRead,
212                                    raw+howManyWritten,
213                                    &howManyRead, &howManyWritten ) ) 
214        {
215          gdcmErrorMacro( "Failed to read jpeg16 ");
216          //delete [] buffer;
217          //return false;
218        }
219    }
220    else
221    {
222       // FIXME : only the bits number is checked,
223       //         NOT the compression method
224
225       // other JPEG lossy not supported
226       gdcmErrorMacro( "Unsupported jpeg lossy compression ");
227       //delete [] buffer;
228       //return false;
229    }
230 }
231
232 } // end namespace gdcm
233