Sébastien Dinot committed Mar 08, 2017 1 2 /* * Copyright (C) 1999-2011 Insight Software Consortium  Julien Michel committed Jan 14, 2019 3  * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)  Sébastien Dinot committed Mar 08, 2017 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20  * * 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. */  Jordi Inglada committed Apr 10, 2006 21   Emmanuel Christophe committed May 07, 2011 22   Jordi Inglada committed Apr 10, 2006 23 24 25 26 27 28 // The classes responsible for reading and writing images are located at the // beginning and end of the data processing pipeline. These classes are // known as data sources (readers) and data sinks (writers). // Generally speaking they are referred to as filters, although readers have // no pipeline input and writers have no pipeline output. //  Jordi Inglada committed Jul 03, 2006 29 30 // The reading of images is managed by the class \doxygen{otb}{ImageFileReader} // while writing is performed by the class \doxygen{otb}{ImageFileWriter}. These  Jordi Inglada committed Apr 10, 2006 31 32 33 // two classes are independent of any particular file format. The actual low // level task of reading and writing specific file formats is done behind // the scenes by a family of classes of type  Jordi Inglada committed Jul 03, 2006 34 // \doxygen{itk}{ImageIO}. Actually, the OTB image Readers and  Jordi Inglada committed Apr 10, 2006 35 36 37 38 39 40 // Writers are very similar to those of ITK, but provide new // functionnalities which are specific to remote sensing images. // // The first step for performing reading and writing is to include the // following headers. //  Mickael Savinaud committed Dec 09, 2014 41 // \index{otb::ImageFileReader}  Jordi Inglada committed Apr 10, 2006 42 43 // \index{otb::ImageFileReader!header} //  Mickael Savinaud committed Dec 09, 2014 44 // \index{otb::ImageFileWriter}  Jordi Inglada committed Apr 10, 2006 45 46 47 48 49 // \index{otb::ImageFileWriter!header} #include "otbImageFileReader.h" #include "otbImageFileWriter.h"  Jordi Inglada committed May 18, 2006 50 #include "otbImage.h"  Jordi Inglada committed Apr 10, 2006 51   Victor Poughon committed Feb 20, 2019 52 int main(int argc, char* argv[])  Jordi Inglada committed Apr 10, 2006 53 54 { // Verify the number of parameters in the command line  OTB Bot committed Apr 01, 2010 55  if (argc < 3)  Victor Poughon committed Feb 20, 2019 56  {  Jordi Inglada committed Apr 10, 2006 57 58 59  std::cerr << "Usage: " << std::endl; std::cerr << argv[0] << " inputImageFile outputImageFile " << std::endl; return EXIT_FAILURE;  Victor Poughon committed Feb 20, 2019 60  }  Jordi Inglada committed Apr 10, 2006 61 62 63 64 65 66 67 68 69 70 71 72 73 74  // Then, as usual, a decision must be made about the type of pixel used to // represent the image processed by the pipeline. Note that when reading // and writing images, the pixel type of the image \textbf{is not // necessarily} the same as the pixel type stored in the file. Your // choice of the pixel type (and hence template parameter) should be // driven mainly by two considerations: // // \begin{itemize} // \item It should be possible to cast the file pixel type in the file to // the pixel type you select. This casting will be performed using the // standard C-language rules, so you will have to make sure that the // conversion does not result in information being lost. // \item The pixel type in memory should be appropriate to the type of  Emmanuel Christophe committed Dec 06, 2008 75  // processing you intended to apply on the images.  Jordi Inglada committed Apr 10, 2006 76 77 78 79 80  // \end{itemize} // // A typical selection for remote sensing images is illustrated in // the following lines.  Victor Poughon committed May 16, 2019 81 82 83  using PixelType = unsigned short; const unsigned int Dimension = 2; using ImageType = otb::Image;  Jordi Inglada committed Apr 10, 2006 84 85 86 87 88 89 90 91 92 93  // Note that the dimension of the image in memory should match the one of // the image in file. There are a couple of special cases in which this // condition may be relaxed, but in general it is better to ensure that both // dimensions match. This is not a real issue in remote sensing, // unless you want to consider multi-band images as volumes (3D) of data. // // We can now instantiate the types of the reader and writer. These two // classes are parameterized over the image type. //  Jordi Inglada committed May 18, 2006 94 95  // \index{otb::ImageFileReader!Instantiation} // \index{otb::ImageFileWriter!Instantiation}  Jordi Inglada committed Apr 10, 2006 96   Victor Poughon committed May 16, 2019 97 98  using ReaderType = otb::ImageFileReader; using WriterType = otb::ImageFileWriter;  Jordi Inglada committed Apr 10, 2006 99 100  // Then, we create one object of each type using the New() method and  Jordi Inglada committed Jul 03, 2006 101  // assigning the result to a \doxygen{itk}{SmartPointer}.  Jordi Inglada committed Apr 10, 2006 102 103 104 105 106 107 108 109 110 111 112  // // \index{otb::ImageFileReader!New()} // \index{otb::ImageFileWriter!New()} // \index{otb::ImageFileReader!SmartPointer} // \index{otb::ImageFileWriter!SmartPointer} ReaderType::Pointer reader = ReaderType::New(); WriterType::Pointer writer = WriterType::New(); // Here we recover the file names from the command line arguments //  Victor Poughon committed Feb 20, 2019 113 114  const char* inputFilename = argv[1]; const char* outputFilename = argv[2];  Jordi Inglada committed Apr 10, 2006 115 116  // The name of the file to be read or written is passed with the  Emmanuel Christophe committed Dec 06, 2008 117  // SetFileName() method.  Jordi Inglada committed Apr 10, 2006 118 119 120 121 122 123  // // \index{otb::ImageFileReader!SetFileName()} // \index{otb::ImageFileWriter!SetFileName()} // \index{SetFileName()!otb::ImageFileReader} // \index{SetFileName()!otb::ImageFileWriter}  OTB Bot committed Apr 01, 2010 124 125  reader->SetFileName(inputFilename); writer->SetFileName(outputFilename);  Jordi Inglada committed Apr 10, 2006 126 127 128 129 130  // We can now connect these readers and writers to filters to create a // pipeline. For example, we can create a short pipeline by passing // the output of the reader directly to the input of the writer.  OTB Bot committed Apr 01, 2010 131  writer->SetInput(reader->GetOutput());  Jordi Inglada committed Apr 10, 2006 132 133 134 135 136 137 138 139 140  // At first view, this may seem as a quite useless program, but it is // actually implementing a powerful file format conversion tool! The // execution of the pipeline is triggered by the invocation of the // \code{Update()} methods in one of the final objects. In this case, the final // data pipeline object is the writer. It is a wise practice of defensive // programming to insert any \code{Update()} call inside a \code{try/catch} block // in case exceptions are thrown during the execution of the pipeline.  Emmanuel Christophe committed Dec 06, 2008 141  try  Victor Poughon committed Feb 20, 2019 142  {  Emmanuel Christophe committed Dec 06, 2008 143  writer->Update();  Victor Poughon committed Feb 20, 2019 144  }  OTB Bot committed Apr 01, 2010 145  catch (itk::ExceptionObject& err)  Victor Poughon committed Feb 20, 2019 146  {  Emmanuel Christophe committed Dec 06, 2008 147 148  std::cerr << "ExceptionObject caught !" << std::endl; std::cerr << err << std::endl;  Jordi Inglada committed Apr 10, 2006 149  return EXIT_FAILURE;  Victor Poughon committed Feb 20, 2019 150  }  Jordi Inglada committed Apr 10, 2006 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166  // Note that exceptions should only be caught by pieces of code that know // what to do with them. In a typical application this \code{catch} block // should probably reside on the GUI code. The action on the \code{catch} // block could inform the user about the failure of the IO operation. // // The IO architecture of the toolkit makes it possible to avoid explicit // specification of the file format used to read or write // images.\footnote{In this example no file format is specified; this // program can be used as a general file conversion utility.} The object // factory mechanism enables the ImageFileReader and ImageFileWriter to // determine (at run-time) with which file format it is working // with. Typically, file formats are chosen based on the filename // extension, but the architecture supports arbitrarily complex processes // to determine whether a file can be read or written. Alternatively, the // user can specify the data file format by explicit instantiation and  Jordi Inglada committed Jul 03, 2006 167  // assignment the appropriate \doxygen{itk}{ImageIO} subclass.  Jordi Inglada committed Apr 10, 2006 168 169 170 171  // return EXIT_SUCCESS; }