]> Creatis software - gdcm.git/blob - src/gdcmJPEGFragment.cxx
9b5733b5fc65b3b3f0affddaf5ea3459fe67c205
[gdcm.git] / src / gdcmJPEGFragment.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmJPEGFragment.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/18 14:28:32 $
7   Version:   $Revision: 1.5 $
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 (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 (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 (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
57 /**
58  * \brief        Print self.
59  * @param os     Stream to print to.
60  * @param indent Indentation string to be prepended during printing.
61  */
62 void JPEGFragment::Print( std::ostream &os, std::string indent )
63 {
64    os << indent
65       << "JPEG fragment: offset : " <<  Offset
66       << "   length : " <<  Length
67       << std::endl;
68 }
69
70 /**
71  * \brief Decompress 8bits JPEG Fragment
72  * @param fp ifstream to write to
73  * @param buffer     output (data decompress)
74  * @param nBits      8/12 or 16 bits jpeg
75  */
76 void JPEGFragment::DecompressJPEGFramesFromFile(std::ifstream *fp, uint8_t *buffer, int nBits)
77 {
78    // First thing need to reset file to proper position:
79    fp->seekg( Offset, std::ios::beg);
80
81    if ( nBits == 8 )
82    {
83       // JPEG Lossy : call to IJG 6b
84       if ( ! gdcm_read_JPEG_file8( fp, buffer) )
85       {
86          //return false;
87       }
88    }
89    else if ( nBits <= 12 )
90    {
91       // Reading Fragment pixels
92       if ( ! gdcm_read_JPEG_file12 ( fp, buffer) )
93       {
94          //return false;
95       }
96    }
97    else if ( nBits <= 16 )
98    {
99       // Reading Fragment pixels
100       if ( ! gdcm_read_JPEG_file16 ( fp, buffer) )
101       {
102          //return false;
103       }
104       //gdcmAssertMacro( IsJPEGLossless );
105    }
106    else
107    {
108       // other JPEG lossy not supported
109       gdcmErrorMacro( "Unknown jpeg lossy compression ");
110       //return false;
111    }
112
113 }
114
115 void JPEGFragment::DecompressJPEGSingleFrameFragmentsFromFile(JOCTET *buffer, size_t totalLength, uint8_t* raw, int nBits)
116 {
117    size_t howManyRead = 0;
118    size_t howManyWritten = 0;
119    
120    if ( nBits == 8)
121    {
122       if ( ! gdcm_read_JPEG_memory8( buffer, totalLength, raw,
123                                      &howManyRead, &howManyWritten ) ) 
124       {
125          gdcmErrorMacro( "Failed to read jpeg8 ");
126          delete [] buffer;
127          //return false;
128       }
129    }
130    else if ( nBits <= 12)
131    {
132       if ( ! gdcm_read_JPEG_memory12( buffer, totalLength, raw,
133                                       &howManyRead, &howManyWritten ) ) 
134       {
135          gdcmErrorMacro( "Failed to read jpeg12 ");
136             delete [] buffer;
137             //return false;
138       }
139    }
140    else if ( nBits <= 16)
141    {
142       
143       if ( ! gdcm_read_JPEG_memory16( buffer, totalLength, raw,
144                                       &howManyRead, &howManyWritten ) ) 
145       {
146          gdcmErrorMacro( "Failed to read jpeg16 ");
147          delete [] buffer;
148          //return false;
149       }
150    }
151    else
152    {
153       // other JPEG lossy not supported
154       gdcmErrorMacro( "Unsupported jpeg lossy compression ");
155       delete [] buffer;
156       //return false;
157    }      
158
159 }
160
161 void JPEGFragment::DecompressJPEGFragmentedFramesFromFile(JOCTET *buffer, uint8_t* raw, int nBits, size_t &howManyRead, size_t &howManyWritten, size_t totalLength)
162 {
163    if ( nBits == 8 )
164    {
165      if ( ! gdcm_read_JPEG_memory8( buffer+howManyRead, totalLength-howManyRead,
166                                   raw+howManyWritten,
167                                   &howManyRead, &howManyWritten ) ) 
168        {
169          gdcmErrorMacro( "Failed to read jpeg8");
170          //delete [] buffer;
171          //return false;
172        }
173    }
174    else if ( nBits <= 12 )
175    {
176    
177      if ( ! gdcm_read_JPEG_memory12( buffer+howManyRead, totalLength-howManyRead,
178                                    raw+howManyWritten,
179                                    &howManyRead, &howManyWritten ) ) 
180        {
181          gdcmErrorMacro( "Failed to read jpeg12");
182          //delete [] buffer;
183          //return false;
184       }
185    }
186    else if ( nBits <= 16 )
187    {
188    
189      if ( ! gdcm_read_JPEG_memory16( buffer+howManyRead, totalLength-howManyRead,
190                                    raw+howManyWritten,
191                                    &howManyRead, &howManyWritten ) ) 
192        {
193          gdcmErrorMacro( "Failed to read jpeg16 ");
194          //delete [] buffer;
195          //return false;
196        }
197    }
198    else
199    {
200       // other JPEG lossy not supported
201       gdcmErrorMacro( "Unsupported jpeg lossy compression ");
202       //delete [] buffer;
203       //return false;
204    }
205 }
206
207 } // end namespace gdcm
208