1 /*=========================================================================
4 Module: $RCSfile: gdcmPixelWriteConvert.cxx,v $
6 Date: $Date: 2007/08/28 16:47:48 $
7 Version: $Revision: 1.19 $
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 "gdcmDebug.h"
20 #include "gdcmPixelWriteConvert.h"
26 #define WITHOFFSETTABLE 1
28 namespace GDCM_NAME_SPACE
30 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
39 // Constructor / Destructor
43 PixelWriteConvert::PixelWriteConvert()
56 PixelWriteConvert::~PixelWriteConvert()
58 gdcmDebugMacro("PixelWriteConvert::~PixelWriteConvert()" );
66 * \brief sets Read Data (and size)
67 * @param data data (uint8_t is for prototyping. if your data is not uint8_t
68 * just cast the pointer for calling the method)
69 * @param size data size, in bytes
71 void PixelWriteConvert::SetReadData(uint8_t *data, size_t size)
78 * \brief Sets the internal pointer to the caller's inData
79 * image representation, WITHOUT COPYING THE DATA.
80 * - 'image' Pixels are presented as C-like 2D arrays : line per line.
81 * - 'volume'Pixels are presented as C-like 3D arrays : plane per plane
82 * \warning Since the pixels are not copied, it is the caller's responsability
83 * not to deallocate its data before gdcm uses them (e.g. with
84 * the Write() method )
85 * @param data data (uint8_t is for prototyping. if your data is not uint8_t
86 * just cast the pointer for calling the method)
87 * @param size size, in bytes.
89 void PixelWriteConvert::SetUserData(uint8_t *data, size_t size)
96 * \brief Get Data (UserData or ReadData)
97 * @return data (uint8_t is for prototyping. if your data is *not* uint8_t
98 * just cast the returned pointer)
100 uint8_t *PixelWriteConvert::GetData()
113 * \brief Get Data Size (UserData or ReadData)
114 * @return size, in bytes.
116 size_t PixelWriteConvert::GetDataSize()
129 typedef std::pair<size_t, uint32_t> JpegPair; //offset, jpeg size
130 typedef std::vector<JpegPair> JpegVector;
132 bool gdcm_write_JPEG2000_file (std::ostream *of, char *inputdata, size_t inputlength,
133 int image_width, int image_height, int numZ, int sample_pixel, int bitsallocated,
134 int sign, int quality);
137 void WriteDICOMItems(std::ostream *fp, JpegVector &v)
140 uint16_t group = 0xfffe;
141 uint16_t elem = 0xe000;
142 GDCM_NAME_SPACE::binary_write(*fp, group);
143 GDCM_NAME_SPACE::binary_write(*fp, elem);
145 uint32_t dummy = 0x12345678;
146 size_t offset = fp->tellp();
150 GDCM_NAME_SPACE::binary_write(*fp, dummy);
154 void EncodeWithoutBasicOffsetTable(std::ostream *fp, int numFrag)// JpegVector& v) //, uint32_t length)
156 assert( numFrag == 1);
159 uint16_t group = 0xfffe;
160 uint16_t elem = 0xe000;
161 GDCM_NAME_SPACE::binary_write(*fp, group);
162 GDCM_NAME_SPACE::binary_write(*fp, elem);
164 uint32_t item_length = 0x0000;
165 GDCM_NAME_SPACE::binary_write(*fp, item_length);
170 void EncodeWithBasicOffsetTable(std::ostream *fp, int numFrag, size_t &start)
173 uint16_t group = 0xfffe;
174 uint16_t elem = 0xe000;
175 GDCM_NAME_SPACE::binary_write(*fp, group);
176 GDCM_NAME_SPACE::binary_write(*fp, elem);
178 uint32_t item_length = numFrag*4; // sizeof(uint32_t)
179 GDCM_NAME_SPACE::binary_write(*fp, item_length);
181 // Just prepare the space
182 start = fp->tellp(); //to be able to rewind
183 for(int i=0; i<numFrag;++i)
185 uint32_t dummy = 0x0000;
186 GDCM_NAME_SPACE::binary_write(*fp, dummy);
190 void UpdateBasicOffsetTable(std::ostream *fp, JpegVector const &v, size_t pos)
192 JpegVector::const_iterator i;
194 const JpegPair &first = v[0];
195 for(i=v.begin(); i!=v.end(); ++i)
197 const JpegPair &jp = *i;
198 if(i == v.begin() ){ assert( jp.first - first.first == 0); }
199 uint32_t offset = (uint32_t)(jp.first - first.first);
200 GDCM_NAME_SPACE::binary_write(*fp, offset);
201 //std::cerr << "Updating Table:" << jp.first - first.first << std::endl;
205 void UpdateJpegFragmentSize(std::ostream *fp, JpegVector const &v)
207 JpegVector::const_iterator i;
208 for(i= v.begin(); i!=v.end(); ++i)
210 const JpegPair &jp = *i;
211 fp->seekp( jp.first );
212 uint32_t length = jp.second;
213 GDCM_NAME_SPACE::binary_write(*fp, length);
214 //std::cerr << "Updating:" << jp.first << "," << jp.second << std::endl;
218 void CloseJpeg(std::ostream *fp, JpegVector &v)
220 // sequence terminator
221 uint16_t group = 0xfffe;
222 uint16_t elem = 0xe0dd;
223 GDCM_NAME_SPACE::binary_write(*fp, group);
224 GDCM_NAME_SPACE::binary_write(*fp, elem);
226 uint32_t length = 0x0;
227 GDCM_NAME_SPACE::binary_write(*fp, length);
229 // Jpeg is done, now update the frag length
230 UpdateJpegFragmentSize(fp, v);
233 // I need to pass the File*. I do not understand how PixelWriteConvert is supposed
234 // to access this information otherwise
235 // size can now be computed from File attributes (what an API...)
236 void PixelWriteConvert::SetCompressJPEG2000UserData(uint8_t *data, size_t size, File *image)
239 //char * userData = reinterpret_cast<char*>(UserData);
241 std::ostringstream *of = new std::ostringstream();
242 int xsize = image->GetXSize();
243 int ysize = image->GetYSize();
244 int zsize = image->GetZSize();
245 int samplesPerPixel = image->GetSamplesPerPixel();
246 //std::cout << "X: " << xsize << std::endl;
247 //std::cout << "Y: " << ysize << std::endl;
248 //std::cout << "Sample: " << samplesPerPixel << std::endl;
249 int bitsallocated = image->GetBitsAllocated();
250 int sign = image->IsSignedPixelData();
251 unsigned int fragment_size = xsize*ysize*samplesPerPixel * (bitsallocated / 8);
252 //assert( fragment_size*zsize == size );
253 gdcmDebugMacro("fragment_size " << fragment_size << " zsize " << zsize << " size " << size);
254 assert( abs(fragment_size*zsize-size) <=1 );
255 JpegVector JpegFragmentSize;
257 size_t bots; //basic offset table start
258 EncodeWithBasicOffsetTable(of, zsize, bots);
260 EncodeWithoutBasicOffsetTable(of, 1);
262 uint8_t *pImageData = data;
263 for(int i=0; i<zsize;i++)
265 WriteDICOMItems(of, JpegFragmentSize);
266 size_t beg = of->tellp();
267 gdcm_write_JPEG2000_file(of, (char*)pImageData,size,
268 image->GetXSize(), image->GetYSize(), image->GetZSize(), image->GetSamplesPerPixel(),
269 image->GetBitsAllocated(), sign, 100);
270 //userData, UserDataSize);
271 // CreateOneFrame(of, pImageData, fragment_size, xsize, ysize, zsize,
272 // samplesPerPixel, quality, JpegFragmentSize);
273 //assert( !(fragment_size % 2) );
274 // Update the JpegVector with offset
275 size_t end = of->tellp();
277 JpegPair &jp = JpegFragmentSize[i];
278 jp.second = (uint32_t)(end-beg);
279 if( ((end-beg) % 2) )
284 assert( !(jp.second % 2) );
285 //std::cerr << "DIFF: " << i <<" -> " << jp.second << std::endl;
287 pImageData += fragment_size;
289 CloseJpeg(of, JpegFragmentSize);
291 UpdateBasicOffsetTable(of, JpegFragmentSize, bots);
295 size_t of_size = of->str().size();
296 UserData = new uint8_t[of_size];
297 memcpy(UserData, of->str().c_str(), of_size);
298 UserDataSize = of_size;
302 bool gdcm_write_JPEG_file8 (std::ostream *fp, char *inputdata, size_t inputlength,
303 int image_width, int image_height, int numZ,
304 int sample_pixel, int bitsallocated, int quality);
305 bool gdcm_write_JPEG_file12 (std::ostream *fp, char *inputdata, size_t inputlength,
306 int image_width, int image_height, int numZ,
307 int sample_pixel, int bitsallocated, int quality);
308 bool gdcm_write_JPEG_file16 (std::ostream *fp, char *inputdata, size_t inputlength,
309 int image_width, int image_height, int numZ,
310 int sample_pixel, int bitsallocated, int quality);
312 void PixelWriteConvert::SetCompressJPEGUserData(uint8_t *data, size_t size, File *image)
318 //char * userData = reinterpret_cast<char*>(UserData);
320 std::ostringstream *of = new std::ostringstream();
321 int xsize = image->GetXSize();
322 int ysize = image->GetYSize();
323 int zsize = image->GetZSize();
324 int samplesPerPixel = image->GetSamplesPerPixel();
325 //std::cout << "X: " << xsize << std::endl;
326 //std::cout << "Y: " << ysize << std::endl;
327 //std::cout << "Sample: " << samplesPerPixel << std::endl;
328 int bitsallocated = image->GetBitsAllocated();
329 unsigned int fragment_size = xsize*ysize*samplesPerPixel * (bitsallocated / 8);
330 gdcmDebugMacro("fragment_size " << fragment_size << " zsize " << zsize << " size " << size);
331 assert( abs(fragment_size*zsize-size) <=1 );
333 JpegVector JpegFragmentSize;
335 size_t bots; //basic offset table start
336 EncodeWithBasicOffsetTable(of, zsize, bots);
338 EncodeWithoutBasicOffsetTable(of, 1);
340 uint8_t *pImageData = data;
341 for(int i=0; i<zsize;i++)
343 WriteDICOMItems(of, JpegFragmentSize);
344 size_t beg = of->tellp();
345 if( bitsallocated == 8 )
347 gdcm_write_JPEG_file8(of, (char*)pImageData,size,
348 image->GetXSize(), image->GetYSize(), image->GetZSize(), image->GetSamplesPerPixel(),
349 image->GetBitsAllocated(), 100 );
351 else if (bitsallocated <= 12)
353 assert( bitsallocated >= 8 );
354 gdcm_write_JPEG_file12(of, (char*)pImageData,size,
355 image->GetXSize(), image->GetYSize(), image->GetZSize(), image->GetSamplesPerPixel(),
356 image->GetBitsAllocated(), 100);
358 else if (bitsallocated <= 16)
360 assert( bitsallocated >= 12 );
361 gdcm_write_JPEG_file16(of, (char*)pImageData,size,
362 image->GetXSize(), image->GetYSize(), image->GetZSize(), image->GetSamplesPerPixel(),
363 image->GetBitsAllocated(), 100);
369 size_t end = of->tellp();
371 JpegPair &jp = JpegFragmentSize[i];
372 jp.second = (uint32_t)(end-beg);
373 if( ((end-beg) % 2) )
378 assert( !(jp.second % 2) );
379 //std::cerr << "DIFF: " << i <<" -> " << jp.second << std::endl;
382 //JpegPair &jp = v[0];
386 //userData, UserDataSize);
387 // CreateOneFrame(of, pImageData, fragment_size, xsize, ysize, zsize,
388 // samplesPerPixel, quality, JpegFragmentSize);
389 //assert( !(fragment_size % 2) );
390 pImageData += fragment_size;
392 CloseJpeg(of, JpegFragmentSize);
394 UpdateBasicOffsetTable(of, JpegFragmentSize, bots);
398 size_t of_size = of->str().size();
399 UserData = new uint8_t[of_size];
400 memcpy(UserData, of->str().c_str(), of_size);
401 UserDataSize = of_size;
406 //-----------------------------------------------------------------------------
408 //bool PixelWriteConvert::CompressJPEG2000(uint8_t *data, size_t size)
412 //-----------------------------------------------------------------------------
415 //-----------------------------------------------------------------------------
418 //-----------------------------------------------------------------------------
419 } // end namespace gdcm