1 /*=========================================================================
4 Module: $RCSfile: gdcmJPEGFragment.cxx,v $
6 Date: $Date: 2005/01/24 16:03:58 $
7 Version: $Revision: 1.7 $
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.
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.
17 =========================================================================*/
19 #include "gdcmJPEGFragment.h"
20 #include "gdcmDebug.h"
25 // For JPEG 2000, body in file gdcmJpeg2000.cxx
27 bool gdcm_read_JPEG2000_file (std::ifstream* fp, void* image_buffer);
29 // For JPEG-LS, body in file gdcmJpegLS.cxx
31 bool gdcm_read_JPEGLS_file (std::ifstream* fp, void* image_buffer);
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,
37 size_t *howManyRead, size_t *howManyWritten);
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,
43 size_t *howManyRead, size_t *howManyWritten);
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,
51 size_t *howManyRead, size_t *howManyWritten);
54 * \brief Default constructor.
56 JPEGFragment::JPEGFragment()
61 // StateSuspension = 0;
69 * @param os Stream to print to.
70 * @param indent Indentation string to be prepended during printing.
72 void JPEGFragment::Print( std::ostream &os, std::string indent )
75 << "JPEG fragment: offset : " << Offset
76 << " length : " << Length
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
86 void JPEGFragment::DecompressJPEGFramesFromFile(std::ifstream *fp,
87 uint8_t *buffer, int nBits,
88 int & statesuspension)
90 // First thing need to reset file to proper position:
91 fp->seekg( Offset, std::ios::beg);
95 // JPEG Lossy : call to IJG 6b
96 if ( ! this->gdcm_read_JPEG_file8( fp, buffer, statesuspension) )
101 else if ( nBits <= 12 )
103 // Reading Fragment pixels
104 if ( ! this->gdcm_read_JPEG_file12 ( fp, buffer, statesuspension) )
109 else if ( nBits <= 16 )
111 // Reading Fragment pixels
112 if ( ! this->gdcm_read_JPEG_file16 ( fp, buffer, statesuspension) )
116 //gdcmAssertMacro( IsJPEGLossless );
120 // FIXME : only the bits number is checked,
121 // NOT the compression method
123 // other JPEG lossy not supported
124 gdcmErrorMacro( "Unknown jpeg lossy compression ");
130 void JPEGFragment::DecompressJPEGSingleFrameFragmentsFromFile(JOCTET *buffer,
131 size_t totalLength, uint8_t *raw, int nBits)
133 size_t howManyRead = 0;
134 size_t howManyWritten = 0;
138 if ( ! gdcm_read_JPEG_memory8( buffer, totalLength, raw,
139 &howManyRead, &howManyWritten ) )
141 gdcmErrorMacro( "Failed to read jpeg8 ");
146 else if ( nBits <= 12)
148 if ( ! gdcm_read_JPEG_memory12( buffer, totalLength, raw,
149 &howManyRead, &howManyWritten ) )
151 gdcmErrorMacro( "Failed to read jpeg12 ");
156 else if ( nBits <= 16)
159 if ( ! gdcm_read_JPEG_memory16( buffer, totalLength, raw,
160 &howManyRead, &howManyWritten ) )
162 gdcmErrorMacro( "Failed to read jpeg16 ");
169 // FIXME : only the bits number is checked,
170 // NOT the compression method
172 // other JPEG lossy not supported
173 gdcmErrorMacro( "Unsupported jpeg lossy compression ");
180 void JPEGFragment::DecompressJPEGFragmentedFramesFromFile(JOCTET *buffer,
181 uint8_t* raw, int nBits, size_t &howManyRead,
182 size_t &howManyWritten, size_t totalLength)
186 if ( ! gdcm_read_JPEG_memory8( buffer+howManyRead, totalLength-howManyRead,
188 &howManyRead, &howManyWritten ) )
190 gdcmErrorMacro( "Failed to read jpeg8");
195 else if ( nBits <= 12 )
198 if ( ! gdcm_read_JPEG_memory12( buffer+howManyRead, totalLength-howManyRead,
200 &howManyRead, &howManyWritten ) )
202 gdcmErrorMacro( "Failed to read jpeg12");
207 else if ( nBits <= 16 )
210 if ( ! gdcm_read_JPEG_memory16( buffer+howManyRead, totalLength-howManyRead,
212 &howManyRead, &howManyWritten ) )
214 gdcmErrorMacro( "Failed to read jpeg16 ");
221 // FIXME : only the bits number is checked,
222 // NOT the compression method
224 // other JPEG lossy not supported
225 gdcmErrorMacro( "Unsupported jpeg lossy compression ");
231 } // end namespace gdcm