Skip to content
Snippets Groups Projects
Commit 46e4a28b authored by Marina Bertolino's avatar Marina Bertolino
Browse files

ENH: detect band range in otb::ImageFileWriter

parent 97674187
No related branches found
No related tags found
No related merge requests found
......@@ -258,6 +258,15 @@ private:
bool m_IsObserving;
unsigned long m_ObserverID;
InputIndexType m_ShiftOutputIndex;
/** Mapping between origin components and output components (before any
* conversion) */
std::vector<unsigned int> m_BandList;
/** Store the number of components to be exported to the output image
* This variable can be the number of components in m_ImageIO or the
* number of components in the m_BandList (if used) */
unsigned int m_IOComponents;
};
} // end namespace otb
......
......@@ -707,6 +707,19 @@ ImageFileWriter<TInputImage>
typedef typename InputImageType::AccessorFunctorType AccessorFunctorType;
m_ImageIO->SetNumberOfComponents(AccessorFunctorType::GetVectorLength(input));
m_IOComponents = m_ImageIO->GetNumberOfComponents();
if (m_FilenameHelper->BandRangeIsSet())
{
// get band range
bool retBandRange = m_FilenameHelper->ResolveBandRange(m_IOComponents, this->m_BandList);
if (retBandRange == false || m_BandList.size() == 0)
{
// invalid range
itkGenericExceptionMacro("The given band range is either empty or invalid for a " << m_IOComponents <<" bands input image!");
}
}
}
else
{
......@@ -731,8 +744,10 @@ ImageFileWriter<TInputImage>
Convert(m_ImageIO->GetIORegion(), ioRegion, m_ShiftOutputIndex);
InputImageRegionType bufferedRegion = input->GetBufferedRegion();
// before this test, bad stuff would happened when they don't match
if (bufferedRegion != ioRegion)
// before this test, bad stuff would happened when they don't match.
// In case of the buffer has not enough components, adapt the region.
if ((bufferedRegion != ioRegion) || (m_FilenameHelper->BandRangeIsSet()
&& (m_IOComponents < m_BandList.size())))
{
if ( m_NumberOfDivisions > 1 || m_UserSpecifiedIORegion)
{
......@@ -741,9 +756,22 @@ ImageFileWriter<TInputImage>
cacheImage = InputImageType::New();
cacheImage->CopyInformation(input);
// set number of components at the band range size
if (m_BandList.size() != 0 || m_FilenameHelper->BandRangeIsSet())
{
cacheImage->SetNumberOfComponentsPerPixel(m_BandList.size());
}
cacheImage->SetBufferedRegion(ioRegion);
cacheImage->Allocate();
// set number of components at the initial size
if (m_BandList.size() != 0 || m_FilenameHelper->BandRangeIsSet())
{
cacheImage->SetNumberOfComponentsPerPixel(m_IOComponents);
}
typedef itk::ImageRegionConstIterator<TInputImage> ConstIteratorType;
typedef itk::ImageRegionIterator<TInputImage> IteratorType;
......@@ -774,6 +802,14 @@ ImageFileWriter<TInputImage>
}
}
if (m_FilenameHelper->BandRangeIsSet() && (m_BandList.size() != 0))
{
// Adapt the image size with the region and take into account a potential
// remapping of the components. m_BandList is empty if no band range is set
m_ImageIO->DoMapBuffer(const_cast< void* >(dataPtr), bufferedRegion.GetNumberOfPixels(), this->m_BandList);
m_ImageIO->SetNumberOfComponents(m_BandList.size());
}
m_ImageIO->Write(dataPtr);
if (m_WriteGeomFile || m_FilenameHelper->GetWriteGEOMFile())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment