fw4spl
GzBufferImageWriter.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwDataIO/writer/GzBufferImageWriter.hpp"
8 
9 #include "fwDataIO/writer/registry/macros.hpp"
10 
11 #include <fwData/Image.hpp>
12 
13 #include <fwDataTools/helper/ImageGetter.hpp>
14 
15 #include <zlib.h>
16 
17 #include <iostream>
18 
19 fwDataIOWriterRegisterMacro( ::fwDataIO::writer::GzBufferImageWriter);
20 
21 namespace fwDataIO
22 {
23 
24 namespace writer
25 {
26 
27 //------------------------------------------------------------------------------
28 
30  ::fwData::location::enableSingleFile< ::fwDataIO::writer::IObjectWriter >(this)
31 {
32 }
33 
34 //------------------------------------------------------------------------------
35 
37 {
38 }
39 
40 //------------------------------------------------------------------------------
41 
43 {
44  assert( getFile().empty() == false );
45 
46  ::fwData::Image::csptr image = getConcreteObject();
47  OSLM_TRACE( "GzBufferImageWriter::write()" << image.get() << " " << image->getClassname());
48 
50  gzFile rawFile = gzopen( getFile().string().c_str(), "wb1");
51  SLM_ASSERT("rawFile not instanced", rawFile);
52  if ( rawFile == 0 )
53  {
54  std::string str = "GzBufferImageWriter::write unable to open ";
55  str += getFile().string();
56  gzclose(rawFile);
57  throw std::ios_base::failure(str);
58  }
59 
60  ::fwDataTools::helper::ImageGetter imageHelper(image);
61 
62  // file is OK : process now
63  const size_t imageSizeInBytes = image->getSizeInBytes();
64 
65  const char* ptr = static_cast<char*>(imageHelper.getBuffer());
66  size_t writtenBytes = 0;
67 
68  int uncompressedbyteswrited;
69 
70  while ( writtenBytes < imageSizeInBytes
71  && (uncompressedbyteswrited = gzwrite(rawFile, ptr+writtenBytes, imageSizeInBytes-writtenBytes)) > 0 )
72  {
73  writtenBytes += uncompressedbyteswrited;
74  }
75 
76  gzclose(rawFile);
77 
78  assert( uncompressedbyteswrited != 0 && writtenBytes == imageSizeInBytes);
79 
80  if ( uncompressedbyteswrited != 0 && writtenBytes == imageSizeInBytes)
81  {
82  std::string str = "GzBufferImageWriter::write unable to write ";
83  str += getFile().string();
84  throw std::ios_base::failure(str);
85  }
86 }
87 
88 //------------------------------------------------------------------------------
89 
91 {
92  return ".raw.gz";
93 }
94 
95 //------------------------------------------------------------------------------
96 
97 } // namespace writer
98 
99 } // namespace fwDataIO
virtual FWDATAIO_API std::string extension() override
Defines extension supported by this writer ".raw.gz".
virtual FWDATAIO_API void write() override
Read the file with zlib API.
This namespace fwDataIO contains reader and writer for several framework&#39;s data.
FWDATAIO_API GzBufferImageWriter(::fwDataIO::writer::IObjectWriter::Key key)
Constructor. Do nothing.
virtual std::shared_ptr< const DataType > getConcreteObject() const
m_object getter.
FWDATATOOLS_API void * getBuffer() const
Returns image buffer.
Definition: ImageGetter.cpp:41
#define OSLM_TRACE(message)
Definition: spyLog.hpp:230
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
Base class for all object writers.
Image Writer. Write file format .raw.gz.
Defines an helper to modify an fwData::Image by adding few medical fields and create in parallel the ...
Definition: ImageGetter.hpp:23
Contains the representation of the data objects used in the framework.
virtual FWDATAIO_API ~GzBufferImageWriter()
Destructor. Do nothing.