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