1 /*=========================================================================
4 Module: $RCSfile: gdcmJPEGFragment.cxx,v $
6 Date: $Date: 2005/01/17 03:05:55 $
7 Version: $Revision: 1.4 $
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
26 bool gdcm_read_JPEG2000_file (std::ifstream* fp, void* image_buffer);
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,
32 size_t *howManyRead, size_t *howManyWritten);
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,
38 size_t *howManyRead, size_t *howManyWritten);
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,
46 size_t *howManyRead, size_t *howManyWritten);
49 * \brief Default constructor.
51 JPEGFragment::JPEGFragment()
59 * @param os Stream to print to.
60 * @param indent Indentation string to be prepended during printing.
62 void JPEGFragment::Print( std::ostream &os, std::string indent )
65 << "JPEG fragment: offset : " << Offset
66 << " length : " << Length
71 * \brief Decompress 8bits JPEG Fragment
72 * @param buffer output (data decompress)
73 * @param nBits 8/12 or 16 bits jpeg
75 void JPEGFragment::DecompressJPEGFramesFromFile(std::ifstream *fp, uint8_t *buffer, int nBits)
77 // First thing need to reset file to proper position:
78 fp->seekg( Offset, std::ios::beg);
82 // JPEG Lossy : call to IJG 6b
83 if ( ! gdcm_read_JPEG_file8( fp, buffer) )
88 else if ( nBits <= 12 )
90 // Reading Fragment pixels
91 if ( ! gdcm_read_JPEG_file12 ( fp, buffer) )
96 else if ( nBits <= 16 )
98 // Reading Fragment pixels
99 if ( ! gdcm_read_JPEG_file16 ( fp, buffer) )
103 //gdcmAssertMacro( IsJPEGLossless );
107 // other JPEG lossy not supported
108 gdcmErrorMacro( "Unknown jpeg lossy compression ");
114 void JPEGFragment::DecompressJPEGSingleFrameFragmentsFromFile(JOCTET *buffer, size_t totalLength, uint8_t* raw, int nBits)
116 size_t howManyRead = 0;
117 size_t howManyWritten = 0;
121 if ( ! gdcm_read_JPEG_memory8( buffer, totalLength, raw,
122 &howManyRead, &howManyWritten ) )
124 gdcmErrorMacro( "Failed to read jpeg8 ");
129 else if ( nBits <= 12)
131 if ( ! gdcm_read_JPEG_memory12( buffer, totalLength, raw,
132 &howManyRead, &howManyWritten ) )
134 gdcmErrorMacro( "Failed to read jpeg12 ");
139 else if ( nBits <= 16)
142 if ( ! gdcm_read_JPEG_memory16( buffer, totalLength, raw,
143 &howManyRead, &howManyWritten ) )
145 gdcmErrorMacro( "Failed to read jpeg16 ");
152 // other JPEG lossy not supported
153 gdcmErrorMacro( "Unsupported jpeg lossy compression ");
160 void JPEGFragment::DecompressJPEGFragmentedFramesFromFile(JOCTET *buffer, uint8_t* raw, int nBits, size_t &howManyRead, size_t &howManyWritten, size_t totalLength)
164 if ( ! gdcm_read_JPEG_memory8( buffer+howManyRead, totalLength-howManyRead,
166 &howManyRead, &howManyWritten ) )
168 gdcmErrorMacro( "Failed to read jpeg8");
173 else if ( nBits <= 12 )
176 if ( ! gdcm_read_JPEG_memory12( buffer+howManyRead, totalLength-howManyRead,
178 &howManyRead, &howManyWritten ) )
180 gdcmErrorMacro( "Failed to read jpeg12");
185 else if ( nBits <= 16 )
188 if ( ! gdcm_read_JPEG_memory16( buffer+howManyRead, totalLength-howManyRead,
190 &howManyRead, &howManyWritten ) )
192 gdcmErrorMacro( "Failed to read jpeg16 ");
199 // other JPEG lossy not supported
200 gdcmErrorMacro( "Unsupported jpeg lossy compression ");
206 } // end namespace gdcm