diff --git a/Code/IO/otbGDALImageIO.cxx b/Code/IO/otbGDALImageIO.cxx index 13912961f8704a3908bc7d579b2f59f08df9bc1b..5d98a800ff8e763d50adade1e31e1981e58548e8 100644 --- a/Code/IO/otbGDALImageIO.cxx +++ b/Code/IO/otbGDALImageIO.cxx @@ -38,6 +38,9 @@ #include "otbGDALDriverManagerWrapper.h" +#include <boost/algorithm/string/predicate.hpp> +#include "otbOGRHelpers.h" + namespace otb { @@ -1108,19 +1111,32 @@ void GDALImageIO::Write(const void* buffer) itkExceptionMacro(<< "Unable to instantiate driver " << gdalDriverShortName << " to write " << m_FileName); } - // If JPEG, set the JPEG compression quality to 95. - char * option[2]; - option[0] = NULL; - option[1] = NULL; - // If JPEG, set the image quality + GDALCreationOptionsType creationOptions = m_CreationOptions; + + // If not initialized in m_CreationOptions, force JPEG quality to 95 if( gdalDriverShortName.compare("JPEG") == 0 ) { - option[0] = const_cast<char *>("QUALITY=95"); - + size_t i; + for (i = 0; i < creationOptions.size(); ++i) + { + if (boost::algorithm::starts_with(creationOptions[i], "QUALITY=")) + { + // User has set the QUALITY argument + // -> Do not touch it + break; + } + } + if (i == creationOptions.size()) + { + // User did not set the QUALITY argument + // Force it to 95 by default... + creationOptions.push_back("QUALITY=95"); + } } GDALDataset* hOutputDS = driver->CreateCopy( realFileName.c_str(), m_Dataset->GetDataSet(), FALSE, - option, NULL, NULL ); + otb::ogr::StringListConverter(creationOptions).to_ogr(), + NULL, NULL ); GDALClose(hOutputDS); } diff --git a/Code/IO/otbGDALImageIO.h b/Code/IO/otbGDALImageIO.h index 131eef395218d282ed5e68fd55b44358da7cb228..49230d7059df5d56ca2b5e8fd4938ad8da20182c 100644 --- a/Code/IO/otbGDALImageIO.h +++ b/Code/IO/otbGDALImageIO.h @@ -50,6 +50,8 @@ public: typedef itk::ImageIOBase Superclass; typedef itk::SmartPointer<Self> Pointer; + typedef std::vector<std::string> GDALCreationOptionsType; + /** Method for creation through the object factory. */ itkNewMacro(Self); @@ -73,6 +75,17 @@ public: itkSetMacro(DatasetNumber, unsigned int); itkGetMacro(DatasetNumber, unsigned int); + /** Set/Get the options */ + void SetOptions(const GDALCreationOptionsType& opts) + { + m_CreationOptions = opts; + } + + GDALCreationOptionsType GetOptions(void) + { + return m_CreationOptions; + } + /*-------- This part of the interface deals with reading data. ------ */ /** Determine the file type. Returns true if this ImageIO can read the @@ -175,6 +188,10 @@ private: * this information has to be provided by the reader */ bool m_IsVectorImage; + /** + * Creation Options */ + GDALCreationOptionsType m_CreationOptions; + }; } // end namespace otb