1 /*=========================================================================
4 Module: $RCSfile: gdcmJPEGFragment.cxx,v $
6 Date: $Date: 2005/01/26 11:42:02 $
7 Version: $Revision: 1.9 $
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
85 * @param statesuspension state suspension
87 void JPEGFragment::DecompressJPEGFramesFromFile(std::ifstream *fp,
88 uint8_t *buffer, int nBits,
89 int & statesuspension)
91 // First thing need to reset file to proper position:
92 fp->seekg( Offset, std::ios::beg);
96 // JPEG Lossy : call to IJG 6b
97 if ( ! this->gdcm_read_JPEG_file8( fp, buffer, statesuspension) )
102 else if ( nBits <= 12 )
104 // Reading Fragment pixels
105 if ( ! this->gdcm_read_JPEG_file12 ( fp, buffer, statesuspension) )
110 else if ( nBits <= 16 )
112 // Reading Fragment pixels
113 if ( ! this->gdcm_read_JPEG_file16 ( fp, buffer, statesuspension) )
117 //gdcmAssertMacro( IsJPEGLossless );
121 // FIXME : only the bits number is checked,
122 // NOT the compression method
124 // other JPEG lossy not supported
125 gdcmErrorMacro( "Unknown jpeg lossy compression ");
131 void JPEGFragment::DecompressJPEGSingleFrameFragmentsFromFile(JOCTET *buffer,
132 size_t totalLength, uint8_t *raw, int nBits)
134 size_t howManyRead = 0;
135 size_t howManyWritten = 0;
139 if ( ! gdcm_read_JPEG_memory8( buffer, totalLength, raw,
140 &howManyRead, &howManyWritten ) )
142 gdcmErrorMacro( "Failed to read jpeg8 ");
147 else if ( nBits <= 12)
149 if ( ! gdcm_read_JPEG_memory12( buffer, totalLength, raw,
150 &howManyRead, &howManyWritten ) )
152 gdcmErrorMacro( "Failed to read jpeg12 ");
157 else if ( nBits <= 16)
160 if ( ! gdcm_read_JPEG_memory16( buffer, totalLength, raw,
161 &howManyRead, &howManyWritten ) )
163 gdcmErrorMacro( "Failed to read jpeg16 ");
170 // FIXME : only the bits number is checked,
171 // NOT the compression method
173 // other JPEG lossy not supported
174 gdcmErrorMacro( "Unsupported jpeg lossy compression ");
181 void JPEGFragment::DecompressJPEGFragmentedFramesFromFile(JOCTET *buffer,
182 uint8_t* raw, int nBits, size_t &howManyRead,
183 size_t &howManyWritten, size_t totalLength)
187 if ( ! gdcm_read_JPEG_memory8( buffer+howManyRead, totalLength-howManyRead,
189 &howManyRead, &howManyWritten ) )
191 gdcmErrorMacro( "Failed to read jpeg8");
196 else if ( nBits <= 12 )
199 if ( ! gdcm_read_JPEG_memory12( buffer+howManyRead, totalLength-howManyRead,
201 &howManyRead, &howManyWritten ) )
203 gdcmErrorMacro( "Failed to read jpeg12");
208 else if ( nBits <= 16 )
211 if ( ! gdcm_read_JPEG_memory16( buffer+howManyRead, totalLength-howManyRead,
213 &howManyRead, &howManyWritten ) )
215 gdcmErrorMacro( "Failed to read jpeg16 ");
222 // FIXME : only the bits number is checked,
223 // NOT the compression method
225 // other JPEG lossy not supported
226 gdcmErrorMacro( "Unsupported jpeg lossy compression ");
232 } // end namespace gdcm