]> Creatis software - gdcm.git/blob - src/gdcmJPEGFragment.cxx
- Add empty nutshell for JPEG-LS
[gdcm.git] / src / gdcmJPEGFragment.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmJPEGFragment.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/24 16:03:58 $
7   Version:   $Revision: 1.7 $
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  */
86 void JPEGFragment::DecompressJPEGFramesFromFile(std::ifstream *fp,
87                                                 uint8_t *buffer, int nBits, 
88                                                 int & statesuspension)
89 {
90    // First thing need to reset file to proper position:
91    fp->seekg( Offset, std::ios::beg);
92
93    if ( nBits == 8 )
94    {
95       // JPEG Lossy : call to IJG 6b
96       if ( ! this->gdcm_read_JPEG_file8( fp, buffer, statesuspension) )
97       {
98          //return false;
99       }
100    }
101    else if ( nBits <= 12 )
102    {
103       // Reading Fragment pixels
104       if ( ! this->gdcm_read_JPEG_file12 ( fp, buffer, statesuspension) )
105       {
106          //return false;
107       }
108    }
109    else if ( nBits <= 16 )
110    {
111       // Reading Fragment pixels
112       if ( ! this->gdcm_read_JPEG_file16 ( fp, buffer, statesuspension) )
113       {
114          //return false;
115       }
116       //gdcmAssertMacro( IsJPEGLossless );
117    }
118    else
119    {
120       // FIXME : only the bits number is checked,
121       //         NOT the compression method
122
123       // other JPEG lossy not supported
124       gdcmErrorMacro( "Unknown jpeg lossy compression ");
125       //return false;
126    }
127
128 }
129
130 void JPEGFragment::DecompressJPEGSingleFrameFragmentsFromFile(JOCTET *buffer,
131                                  size_t totalLength, uint8_t *raw, int nBits)
132 {
133    size_t howManyRead = 0;
134    size_t howManyWritten = 0;
135    
136    if ( nBits == 8)
137    {
138       if ( ! gdcm_read_JPEG_memory8( buffer, totalLength, raw,
139                                      &howManyRead, &howManyWritten ) ) 
140       {
141          gdcmErrorMacro( "Failed to read jpeg8 ");
142          delete [] buffer;
143          //return false;
144       }
145    }
146    else if ( nBits <= 12)
147    {
148       if ( ! gdcm_read_JPEG_memory12( buffer, totalLength, raw,
149                                       &howManyRead, &howManyWritten ) ) 
150       {
151          gdcmErrorMacro( "Failed to read jpeg12 ");
152             delete [] buffer;
153             //return false;
154       }
155    }
156    else if ( nBits <= 16)
157    {
158       
159       if ( ! gdcm_read_JPEG_memory16( buffer, totalLength, raw,
160                                       &howManyRead, &howManyWritten ) ) 
161       {
162          gdcmErrorMacro( "Failed to read jpeg16 ");
163          delete [] buffer;
164          //return false;
165       }
166    }
167    else
168    {
169       // FIXME : only the bits number is checked,
170       //         NOT the compression method
171
172       // other JPEG lossy not supported
173       gdcmErrorMacro( "Unsupported jpeg lossy compression ");
174       delete [] buffer;
175       //return false;
176    }      
177
178 }
179
180 void JPEGFragment::DecompressJPEGFragmentedFramesFromFile(JOCTET *buffer, 
181                     uint8_t* raw, int nBits, size_t &howManyRead, 
182                     size_t &howManyWritten, size_t totalLength)
183 {
184    if ( nBits == 8 )
185    {
186      if ( ! gdcm_read_JPEG_memory8( buffer+howManyRead, totalLength-howManyRead,
187                                   raw+howManyWritten,
188                                   &howManyRead, &howManyWritten ) ) 
189        {
190          gdcmErrorMacro( "Failed to read jpeg8");
191          //delete [] buffer;
192          //return false;
193        }
194    }
195    else if ( nBits <= 12 )
196    {
197    
198      if ( ! gdcm_read_JPEG_memory12( buffer+howManyRead, totalLength-howManyRead,
199                                    raw+howManyWritten,
200                                    &howManyRead, &howManyWritten ) ) 
201        {
202          gdcmErrorMacro( "Failed to read jpeg12");
203          //delete [] buffer;
204          //return false;
205       }
206    }
207    else if ( nBits <= 16 )
208    {
209    
210      if ( ! gdcm_read_JPEG_memory16( buffer+howManyRead, totalLength-howManyRead,
211                                    raw+howManyWritten,
212                                    &howManyRead, &howManyWritten ) ) 
213        {
214          gdcmErrorMacro( "Failed to read jpeg16 ");
215          //delete [] buffer;
216          //return false;
217        }
218    }
219    else
220    {
221       // FIXME : only the bits number is checked,
222       //         NOT the compression method
223
224       // other JPEG lossy not supported
225       gdcmErrorMacro( "Unsupported jpeg lossy compression ");
226       //delete [] buffer;
227       //return false;
228    }
229 }
230
231 } // end namespace gdcm
232