/usr/include/OTB-6.4/otbImageFileWriter.txx is in libotb-dev 6.4.0+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 | /*
* Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef otbImageFileWriter_txx
#define otbImageFileWriter_txx
#include "otbImageFileWriter.h"
#include "itkImageFileWriter.h"
#include "itkObjectFactoryBase.h"
#include "itkImageRegionMultidimensionalSplitter.h"
#include "otbImageIOFactory.h"
#include "itkImageRegionIterator.h"
#include "itkMetaDataObject.h"
#include "otbImageKeywordlist.h"
#include "otbMetaDataKey.h"
#include "otbConfigure.h"
#include "otbNumberOfDivisionsStrippedStreamingManager.h"
#include "otbNumberOfDivisionsTiledStreamingManager.h"
#include "otbNumberOfLinesStrippedStreamingManager.h"
#include "otbRAMDrivenStrippedStreamingManager.h"
#include "otbTileDimensionTiledStreamingManager.h"
#include "otbRAMDrivenTiledStreamingManager.h"
#include "otbRAMDrivenAdaptativeStreamingManager.h"
#include "otb_boost_tokenizer_header.h"
#include "otbStringUtils.h"
namespace otb
{
/**
*
*/
template <class TInputImage>
ImageFileWriter<TInputImage>
::ImageFileWriter()
: m_NumberOfDivisions(0),
m_CurrentDivision(0),
m_DivisionProgress(0.0),
m_UserSpecifiedImageIO(true),
m_UserSpecifiedIORegion(false),
m_FactorySpecifiedImageIO(false),
m_UseCompression(false),
m_UseInputMetaDataDictionary(false),
m_WriteGeomFile(false),
m_FilenameHelper(),
m_IsObserving(true),
m_ObserverID(0),
m_IOComponents(0)
{
//Init output index shift
m_ShiftOutputIndex.Fill(0);
// By default, we use tiled streaming, with automatic tile size
// We don't set any parameter, so the memory size is retrieved from the OTB configuration options
this->SetAutomaticAdaptativeStreaming();
m_FilenameHelper = FNameHelperType::New();
}
/**
*
*/
template <class TInputImage>
ImageFileWriter<TInputImage>
::~ImageFileWriter()
{
}
template <class TInputImage>
void
ImageFileWriter<TInputImage>
::SetNumberOfDivisionsStrippedStreaming(unsigned int nbDivisions)
{
typedef NumberOfDivisionsStrippedStreamingManager<TInputImage> NumberOfDivisionsStrippedStreamingManagerType;
typename NumberOfDivisionsStrippedStreamingManagerType::Pointer streamingManager = NumberOfDivisionsStrippedStreamingManagerType::New();
streamingManager->SetNumberOfDivisions(nbDivisions);
m_StreamingManager = streamingManager;
}
template <class TInputImage>
void
ImageFileWriter<TInputImage>
::SetNumberOfDivisionsTiledStreaming(unsigned int nbDivisions)
{
typedef NumberOfDivisionsTiledStreamingManager<TInputImage> NumberOfDivisionsTiledStreamingManagerType;
typename NumberOfDivisionsTiledStreamingManagerType::Pointer streamingManager = NumberOfDivisionsTiledStreamingManagerType::New();
streamingManager->SetNumberOfDivisions(nbDivisions);
m_StreamingManager = streamingManager;
}
template <class TInputImage>
void
ImageFileWriter<TInputImage>
::SetNumberOfLinesStrippedStreaming(unsigned int nbLinesPerStrip)
{
typedef NumberOfLinesStrippedStreamingManager<TInputImage> NumberOfLinesStrippedStreamingManagerType;
typename NumberOfLinesStrippedStreamingManagerType::Pointer streamingManager = NumberOfLinesStrippedStreamingManagerType::New();
streamingManager->SetNumberOfLinesPerStrip(nbLinesPerStrip);
m_StreamingManager = streamingManager;
}
template <class TInputImage>
void
ImageFileWriter<TInputImage>
::SetAutomaticStrippedStreaming(unsigned int availableRAM, double bias)
{
typedef RAMDrivenStrippedStreamingManager<TInputImage> RAMDrivenStrippedStreamingManagerType;
typename RAMDrivenStrippedStreamingManagerType::Pointer streamingManager = RAMDrivenStrippedStreamingManagerType::New();
streamingManager->SetAvailableRAMInMB(availableRAM);
streamingManager->SetBias(bias);
m_StreamingManager = streamingManager;
}
template <class TInputImage>
void
ImageFileWriter<TInputImage>
::SetTileDimensionTiledStreaming(unsigned int tileDimension)
{
typedef TileDimensionTiledStreamingManager<TInputImage> TileDimensionTiledStreamingManagerType;
typename TileDimensionTiledStreamingManagerType::Pointer streamingManager = TileDimensionTiledStreamingManagerType::New();
streamingManager->SetTileDimension(tileDimension);
m_StreamingManager = streamingManager;
}
template <class TInputImage>
void
ImageFileWriter<TInputImage>
::SetAutomaticTiledStreaming(unsigned int availableRAM, double bias)
{
typedef RAMDrivenTiledStreamingManager<TInputImage> RAMDrivenTiledStreamingManagerType;
typename RAMDrivenTiledStreamingManagerType::Pointer streamingManager = RAMDrivenTiledStreamingManagerType::New();
streamingManager->SetAvailableRAMInMB(availableRAM);
streamingManager->SetBias(bias);
m_StreamingManager = streamingManager;
}
template <class TInputImage>
void
ImageFileWriter<TInputImage>
::SetAutomaticAdaptativeStreaming(unsigned int availableRAM, double bias)
{
typedef RAMDrivenAdaptativeStreamingManager<TInputImage> RAMDrivenAdaptativeStreamingManagerType;
typename RAMDrivenAdaptativeStreamingManagerType::Pointer streamingManager = RAMDrivenAdaptativeStreamingManagerType::New();
streamingManager->SetAvailableRAMInMB(availableRAM);
streamingManager->SetBias(bias);
m_StreamingManager = streamingManager;
}
#ifndef ITK_LEGACY_REMOVE
#endif // ITK_LEGACY_REMOVE
/**
*
*/
template <class TInputImage>
void
ImageFileWriter<TInputImage>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "File Name: "
<< (m_FileName.data() ? m_FileName.data() : "(none)") << std::endl;
os << indent << "Image IO: ";
if (m_ImageIO.IsNull())
{
os << "(none)\n";
}
else
{
os << m_ImageIO << "\n";
}
os << indent << "IO Region: " << m_IORegion << "\n";
if (m_UseCompression)
{
os << indent << "Compression: On\n";
}
else
{
os << indent << "Compression: Off\n";
}
if (m_UseInputMetaDataDictionary)
{
os << indent << "UseInputMetaDataDictionary: On\n";
}
else
{
os << indent << "UseInputMetaDataDictionary: Off\n";
}
if (m_FactorySpecifiedImageIO)
{
os << indent << "FactorySpecifiedmageIO: On\n";
}
else
{
os << indent << "FactorySpecifiedmageIO: Off\n";
}
}
//---------------------------------------------------------
template<class TInputImage>
void
ImageFileWriter<TInputImage>
::SetIORegion(const itk::ImageIORegion& region)
{
itkDebugMacro("setting IORegion to " << region);
if (m_IORegion != region)
{
m_IORegion = region;
this->Modified();
m_UserSpecifiedIORegion = true;
}
}
template<class TInputImage>
void
ImageFileWriter<TInputImage>
::SetInput(const InputImageType* input)
{
this->ProcessObject::SetNthInput(0,const_cast<InputImageType*>(input));
}
template<class TInputImage>
const TInputImage*
ImageFileWriter<TInputImage>
::GetInput()
{
if (this->GetNumberOfInputs() < 1)
{
return ITK_NULLPTR;
}
return static_cast<const InputImageType*>(this->ProcessObject::GetInput(0));
}
/**
* Update method : update output information of input and write to file
*/
template<class TInputImage>
void
ImageFileWriter<TInputImage>
::Update()
{
// Update output information on input image
InputImagePointer inputPtr =
const_cast<InputImageType *>(this->GetInput());
// Make sure input is available
if ( inputPtr.IsNull() )
{
itkExceptionMacro(<< "No input to writer");
}
/** Parse streaming modes */
if(m_FilenameHelper->StreamingTypeIsSet())
{
itkWarningMacro(<<"Streaming configuration through extended filename is used. Any previous streaming configuration (ram value, streaming mode ...) will be ignored.");
std::string type = m_FilenameHelper->GetStreamingType();
std::string sizemode = "auto";
if(m_FilenameHelper->StreamingSizeModeIsSet())
{
sizemode = m_FilenameHelper->GetStreamingSizeMode();
}
double sizevalue = 0.;
if(m_FilenameHelper->StreamingSizeValueIsSet())
{
sizevalue = m_FilenameHelper->GetStreamingSizeValue();
}
if(type == "auto")
{
if(sizemode != "auto")
{
itkWarningMacro(<<"In auto streaming type, the sizemode option will be ignored.");
}
if(sizevalue == 0.)
{
itkWarningMacro("sizemode is auto but sizevalue is 0. Value will be fetched from the OTB_MAX_RAM_HINT environment variable if set, or else use the default value");
}
this->SetAutomaticAdaptativeStreaming(sizevalue);
}
else if(type == "tiled")
{
if(sizemode == "auto")
{
if(sizevalue == 0.)
{
itkWarningMacro("sizemode is auto but sizevalue is 0. Value will be fetched from the OTB_MAX_RAM_HINT environment variable if set, or else use the default value");
}
this->SetAutomaticTiledStreaming(sizevalue);
}
else if(sizemode == "nbsplits")
{
if(sizevalue == 0.)
{
itkWarningMacro("Streaming sizemode is set to nbsplits but sizevalue is 0. This will result in upredicted behaviour. Please consider setting the sizevalue by using &streaming:sizevalue=x.");
}
this->SetNumberOfDivisionsTiledStreaming(static_cast<unsigned int>(sizevalue));
}
else if(sizemode == "height")
{
if(sizevalue == 0.)
{
itkWarningMacro("Streaming sizemode is set to height but sizevalue is 0. This will result in upredicted behaviour. Please consider setting the sizevalue by using &streaming:sizevalue=x.");
}
this->SetTileDimensionTiledStreaming(static_cast<unsigned int>(sizevalue));
}
}
else if(type == "stripped")
{
if(sizemode == "auto")
{
if(sizevalue == 0.)
{
itkWarningMacro("sizemode is auto but sizevalue is 0. Value will be fetched from configuration file if any, or from cmake configuration otherwise.");
}
this->SetAutomaticStrippedStreaming(sizevalue);
}
else if(sizemode == "nbsplits")
{
if(sizevalue == 0.)
{
itkWarningMacro("Streaming sizemode is set to nbsplits but sizevalue is 0. This will result in upredicted behaviour. Please consider setting the sizevalue by using &streaming:sizevalue=x.");
}
this->SetNumberOfDivisionsStrippedStreaming(static_cast<unsigned int>(sizevalue));
}
else if(sizemode == "height")
{
if(sizevalue == 0.)
{
itkWarningMacro("Streaming sizemode is set to height but sizevalue is 0. This will result in upredicted behaviour. Please consider setting the sizevalue by using &streaming:sizevalue=x.");
}
this->SetNumberOfLinesStrippedStreaming(static_cast<unsigned int>(sizevalue));
}
}
else if (type == "none")
{
if(sizemode!="" || sizevalue!=0.)
{
itkWarningMacro("Streaming is explicitly disabled, sizemode and sizevalue will be ignored.");
}
this->SetNumberOfDivisionsTiledStreaming(0);
}
}
else
{
if(m_FilenameHelper->StreamingSizeValueIsSet() || m_FilenameHelper->StreamingSizeModeIsSet())
{
itkWarningMacro(<<"No streaming type is set, streaming sizemode and sizevalue will be ignored.");
}
}
this->SetAbortGenerateData(0);
this->SetProgress(0.0);
/**
* Tell all Observers that the filter is starting
*/
this->InvokeEvent(itk::StartEvent());
/** Prepare ImageIO : create ImageFactory */
if (m_FileName == "")
{
// Make sure that we can write the file given the name
itkExceptionMacro(<< "No filename was specified");
}
// Make sure CanWriteFile is called
// either from ImageIOFactory::CreateImageIO or directly in this file
// GDALImageIO uses it to store the filename
// and later answer to CanStreamWrite()
// This is a needed workaround to a defect in the itk::ImageIO interface
if (m_ImageIO.IsNull()) //try creating via factory
{
itkDebugMacro(<< "Attempting factory creation of ImageIO for file: "
<< m_FileName);
this->SetImageIO(ImageIOFactory::CreateImageIO(m_FileName.c_str(),
otb::ImageIOFactory::WriteMode));
m_FactorySpecifiedImageIO = true;
}
else
{
if (!m_ImageIO->CanWriteFile(m_FileName.c_str()))
{
itkDebugMacro(<< "ImageIO exists but doesn't know how to write file:"
<< m_FileName);
if (m_FactorySpecifiedImageIO)
{
itkDebugMacro(<< "Attempting creation of ImageIO with a factory for file:"
<< m_FileName);
m_ImageIO = ImageIOFactory::CreateImageIO(m_FileName.c_str(),
otb::ImageIOFactory::WriteMode);
m_FactorySpecifiedImageIO = true;
}
}
}
if (m_ImageIO.IsNull())
{
itk::ImageFileWriterException e(__FILE__, __LINE__);
std::ostringstream msg;
msg << "Cannot write image " << m_FileName.c_str() << ". Probably unsupported format or incorrect filename extension.";
e.SetDescription(msg.str().c_str());
e.SetLocation(ITK_LOCATION);
throw e;
}
// Manage extended filename
if ((strcmp(m_ImageIO->GetNameOfClass(), "GDALImageIO") == 0)
&& (m_FilenameHelper->gdalCreationOptionsIsSet() || m_FilenameHelper->WriteRPCTagsIsSet()) )
{
typename GDALImageIO::Pointer imageIO = dynamic_cast<GDALImageIO*>(m_ImageIO.GetPointer());
if(imageIO.IsNull())
{
itk::ImageFileWriterException e(__FILE__, __LINE__);
std::ostringstream msg;
msg << " ImageIO is of kind GDALImageIO, but fails to dynamic_cast (this should never happen)."<< std::endl;
e.SetDescription(msg.str().c_str());
throw e;
}
imageIO->SetOptions(m_FilenameHelper->GetgdalCreationOptions());
imageIO->SetWriteRPCTags(m_FilenameHelper->GetWriteRPCTags());
}
/** End of Prepare ImageIO : create ImageFactory */
/**
* Grab the input
*/
inputPtr->UpdateOutputInformation();
InputImageRegionType inputRegion = inputPtr->GetLargestPossibleRegion();
/** Parse region size modes */
if(m_FilenameHelper->BoxIsSet())
{
std::vector<int> boxVector;
Utils::ConvertStringToVector(
m_FilenameHelper->GetBox(), boxVector, "ExtendedFileName:box", ":");
typename InputImageRegionType::IndexType start;
typename InputImageRegionType::SizeType size;
start[0] = boxVector[0]; // first index on X
start[1] = boxVector[1]; // first index on Y
size[0] = boxVector[2]; // size along X
size[1] = boxVector[3]; // size along Y
inputRegion.SetSize(size);
m_ShiftOutputIndex = start;
inputRegion.SetIndex(m_ShiftOutputIndex);
if (!inputRegion.Crop(inputPtr->GetLargestPossibleRegion()))
{
// Couldn't crop the region (requested region is outside the largest
// possible region). Throw an exception.
// build an exception
itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
e.SetLocation(ITK_LOCATION);
e.SetDescription("Requested box region is (at least partially) outside the largest possible region.");
e.SetDataObject(inputPtr);
throw e;
}
otbMsgDevMacro(<< "inputRegion " << inputRegion);
}
/**
* Determine of number of pieces to divide the input. This will be the
* minimum of what the user specified via SetNumberOfDivisionsStrippedStreaming()
* and what the Splitter thinks is a reasonable value.
*/
/** Control if the ImageIO is CanStreamWrite */
if (m_ImageIO->CanStreamWrite() == false)
{
otbWarningMacro(
<< "The ImageFactory selected for the image file <" << m_FileName.c_str() <<
"> does not support streaming.");
this->SetNumberOfDivisionsStrippedStreaming(1);
}
/** Compare the buffered region with the inputRegion which is the largest
* possible region or a user defined region through extended filename
* Not sure that if this modification is needed */
else if (inputPtr->GetBufferedRegion() == inputRegion)
{
otbMsgDevMacro(<< "Buffered region is the largest possible region, there is no need for streaming.");
this->SetNumberOfDivisionsStrippedStreaming(1);
}
m_StreamingManager->PrepareStreaming(inputPtr, inputRegion);
m_NumberOfDivisions = m_StreamingManager->GetNumberOfSplits();
otbMsgDebugMacro(<< "Number Of Stream Divisions : " << m_NumberOfDivisions);
/**
* Loop over the number of pieces, execute the upstream pipeline on each
* piece, and copy the results into the output image.
*/
InputImageRegionType streamRegion;
//
// Setup the ImageIO with information from inputPtr
//
m_ImageIO->SetNumberOfDimensions(TInputImage::ImageDimension);
const typename TInputImage::SpacingType& spacing = inputPtr->GetSpacing();
const typename TInputImage::PointType& origin = inputPtr->GetOrigin();
const typename TInputImage::DirectionType& direction = inputPtr->GetDirection();
int direction_sign(0);
for (unsigned int i = 0; i < TInputImage::ImageDimension; ++i)
{
if ( direction[i][i] < 0 )
direction_sign = -1;
else
direction_sign = 1;
// Final image size
m_ImageIO->SetDimensions(i, inputRegion.GetSize(i));
m_ImageIO->SetSpacing(i, direction_sign * spacing[i]);
m_ImageIO->SetOrigin(i, origin[i] + static_cast<double>(inputRegion.GetIndex()[i]) * spacing[i]);
vnl_vector<double> axisDirection(TInputImage::ImageDimension);
// Please note: direction cosines are stored as columns of the
// direction matrix
for (unsigned int j = 0; j < TInputImage::ImageDimension; ++j)
{
axisDirection[j] = direction_sign * direction[j][i];
}
m_ImageIO->SetDirection(i, axisDirection);
}
m_ImageIO->SetUseCompression(m_UseCompression);
m_ImageIO->SetMetaDataDictionary(inputPtr->GetMetaDataDictionary());
/** Create Image file */
// Setup the image IO for writing.
//
m_ImageIO->SetFileName(m_FileName.c_str());
m_ImageIO->WriteImageInformation();
this->UpdateProgress(0);
m_CurrentDivision = 0;
m_DivisionProgress = 0;
// Get the source process object
itk::ProcessObject* source = inputPtr->GetSource();
m_IsObserving = false;
m_ObserverID = 0;
// Check if source exists
if(source)
{
typedef itk::MemberCommand<Self> CommandType;
typedef typename CommandType::Pointer CommandPointerType;
CommandPointerType command = CommandType::New();
command->SetCallbackFunction(this, &Self::ObserveSourceFilterProgress);
m_ObserverID = source->AddObserver(itk::ProgressEvent(), command);
m_IsObserving = true;
}
else
{
itkWarningMacro(<< "Could not get the source process object. Progress report might be buggy");
}
for (m_CurrentDivision = 0;
m_CurrentDivision < m_NumberOfDivisions && !this->GetAbortGenerateData();
m_CurrentDivision++, m_DivisionProgress = 0, this->UpdateFilterProgress())
{
streamRegion = m_StreamingManager->GetSplit(m_CurrentDivision);
inputPtr->SetRequestedRegion(streamRegion);
inputPtr->PropagateRequestedRegion();
inputPtr->UpdateOutputData();
// Write the whole image
itk::ImageIORegion ioRegion(TInputImage::ImageDimension);
for (unsigned int i = 0; i < TInputImage::ImageDimension; ++i)
{
ioRegion.SetSize(i, streamRegion.GetSize(i));
ioRegion.SetIndex(i, streamRegion.GetIndex(i));
//Set the ioRegion index using the shifted index ( (0,0 without box parameter))
ioRegion.SetIndex(i, streamRegion.GetIndex(i) - m_ShiftOutputIndex[i]);
}
this->SetIORegion(ioRegion);
m_ImageIO->SetIORegion(m_IORegion);
// Start writing stream region in the image file
this->GenerateData();
}
/**
* If we ended due to aborting, push the progress up to 1.0 (since
* it probably didn't end there)
*/
if (!this->GetAbortGenerateData())
{
this->UpdateProgress(1.0);
}
// Notify end event observers
this->InvokeEvent(itk::EndEvent());
if (m_IsObserving)
{
m_IsObserving = false;
source->RemoveObserver(m_ObserverID);
}
// Write the image keyword list if any
// ossimKeywordlist geom_kwl;
// ImageKeywordlist otb_kwl;
// itk::MetaDataDictionary dict = this->GetInput()->GetMetaDataDictionary();
// itk::ExposeMetaData<ImageKeywordlist>(dict, MetaDataKey::OSSIMKeywordlistKey, otb_kwl);
// otb_kwl.convertToOSSIMKeywordlist(geom_kwl);
//FIXME: why nothing is done with otb_kwl in that case???
// If required, put a call to WriteGeometry() here
/**
* Release any inputs if marked for release
*/
this->ReleaseInputs();
//Reset global shift on input region (box parameter)
//It allows calling multiple update over the writer
m_ShiftOutputIndex.Fill(0);
}
/**
*
*/
template<class TInputImage>
void
ImageFileWriter<TInputImage>
::GenerateData(void)
{
const InputImageType * input = this->GetInput();
InputImagePointer cacheImage;
// Make sure that the image is the right type and no more than
// four components.
typedef typename InputImageType::PixelType ImagePixelType;
if (strcmp(input->GetNameOfClass(), "VectorImage") == 0)
{
typedef typename InputImageType::InternalPixelType VectorImagePixelType;
m_ImageIO->SetPixelTypeInfo(typeid(VectorImagePixelType));
typedef typename InputImageType::AccessorFunctorType AccessorFunctorType;
m_ImageIO->SetNumberOfComponents(AccessorFunctorType::GetVectorLength(input));
m_IOComponents = m_ImageIO->GetNumberOfComponents();
m_BandList.clear();
if (m_FilenameHelper->BandRangeIsSet())
{
// get band range
bool retBandRange = m_FilenameHelper->ResolveBandRange(m_FilenameHelper->GetBandRange(), m_IOComponents, m_BandList);
if (retBandRange == false || m_BandList.empty())
{
// invalid range
itkGenericExceptionMacro("The given band range is either empty or invalid for a " << m_IOComponents <<" bands input image!");
}
}
}
else
{
// Set the pixel and component type; the number of components.
m_ImageIO->SetPixelTypeInfo(typeid(ImagePixelType));
}
// Setup the image IO for writing.
//
//okay, now extract the data as a raw buffer pointer
const void* dataPtr = (const void*) input->GetBufferPointer();
// check that the image's buffered region is the same as
// ImageIO is expecting and we requested
InputImageRegionType ioRegion;
// No shift of the ioRegion from the buffered region is expected
typename InputImageRegionType::IndexType tmpIndex;
tmpIndex.Fill(0);
itk::ImageIORegionAdaptor<TInputImage::ImageDimension>::
//Convert(m_ImageIO->GetIORegion(), ioRegion, tmpIndex);
Convert(m_ImageIO->GetIORegion(), ioRegion, m_ShiftOutputIndex);
InputImageRegionType bufferedRegion = input->GetBufferedRegion();
// 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)
{
itkDebugMacro("Requested stream region does not match generated output");
itkDebugMacro("input filter may not support streaming well");
cacheImage = InputImageType::New();
cacheImage->CopyInformation(input);
// set number of components at the band range size
if (m_FilenameHelper->BandRangeIsSet() && (m_IOComponents < m_BandList.size()))
{
cacheImage->SetNumberOfComponentsPerPixel(m_BandList.size());
}
cacheImage->SetBufferedRegion(ioRegion);
cacheImage->Allocate();
// set number of components at the initial size
if (m_FilenameHelper->BandRangeIsSet() && (m_IOComponents < m_BandList.size()))
{
cacheImage->SetNumberOfComponentsPerPixel(m_IOComponents);
}
typedef itk::ImageRegionConstIterator<TInputImage> ConstIteratorType;
typedef itk::ImageRegionIterator<TInputImage> IteratorType;
ConstIteratorType in(input, ioRegion);
IteratorType out(cacheImage, ioRegion);
// copy the data into a buffer to match the ioregion
for (in.GoToBegin(), out.GoToBegin(); !in.IsAtEnd(); ++in, ++out)
{
out.Set(in.Get());
}
dataPtr = (const void*) cacheImage->GetBufferPointer();
}
else
{
itk::ImageFileWriterException e(__FILE__, __LINE__);
std::ostringstream msg;
msg << "Did not get requested region!" << std::endl;
msg << "Requested:" << std::endl;
msg << ioRegion;
msg << "Actual:" << std::endl;
msg << bufferedRegion;
e.SetDescription(msg.str().c_str());
e.SetLocation(ITK_LOCATION);
throw e;
}
}
if (m_FilenameHelper->BandRangeIsSet() && (!m_BandList.empty()))
{
// 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())
{
ImageKeywordlist otb_kwl;
itk::MetaDataDictionary dict = this->GetInput()->GetMetaDataDictionary();
itk::ExposeMetaData<ImageKeywordlist>(dict, MetaDataKey::OSSIMKeywordlistKey, otb_kwl);
WriteGeometry(otb_kwl, this->GetFileName());
}
}
template <class TInputImage>
void
ImageFileWriter<TInputImage>
::SetFileName(std::string extendedFileName)
{
this->SetFileName(extendedFileName.c_str());
}
template <class TInputImage>
void
ImageFileWriter<TInputImage>
::SetFileName(const char* extendedFileName)
{
this->m_FilenameHelper->SetExtendedFileName(extendedFileName);
m_FileName = this->m_FilenameHelper->GetSimpleFileName();
m_ImageIO = ITK_NULLPTR;
this->Modified();
}
template <class TInputImage>
const char*
ImageFileWriter<TInputImage>
::GetFileName () const
{
return this->m_FilenameHelper->GetSimpleFileName();
}
} // end namespace otb
#endif
|