From b7d4cd84daa08265c486dedc8b2acaf4a633bae3 Mon Sep 17 00:00:00 2001 From: Shreeraj Jadhav Date: Fri, 20 May 2022 15:55:07 -0400 Subject: [PATCH] ENH: Migrate example RGBImageReadWrite --- src/IO/ImageBase/CMakeLists.txt | 8 ++ .../ReadWriteAnRGBImage/CMakeLists.txt | 37 +++++ src/IO/ImageBase/ReadWriteAnRGBImage/Code.cxx | 131 ++++++++++++++++++ src/IO/ImageBase/ReadWriteAnRGBImage/Code.py | 47 +++++++ .../ReadWriteAnRGBImage/Documentation.rst | 50 +++++++ .../OutputBaseline.png.sha512 | 1 + .../ReadWriteAnRGBImage/peppers.png.sha512 | 1 + 7 files changed, 275 insertions(+) create mode 100644 src/IO/ImageBase/ReadWriteAnRGBImage/CMakeLists.txt create mode 100644 src/IO/ImageBase/ReadWriteAnRGBImage/Code.cxx create mode 100644 src/IO/ImageBase/ReadWriteAnRGBImage/Code.py create mode 100644 src/IO/ImageBase/ReadWriteAnRGBImage/Documentation.rst create mode 100644 src/IO/ImageBase/ReadWriteAnRGBImage/OutputBaseline.png.sha512 create mode 100644 src/IO/ImageBase/ReadWriteAnRGBImage/peppers.png.sha512 diff --git a/src/IO/ImageBase/CMakeLists.txt b/src/IO/ImageBase/CMakeLists.txt index 6f53ddd6d..2ba008a9d 100644 --- a/src/IO/ImageBase/CMakeLists.txt +++ b/src/IO/ImageBase/CMakeLists.txt @@ -33,6 +33,14 @@ compare_to_baseline( add_example(ConvertImageToAnotherType) +add_example( ReadWriteAnRGBImage ) +compare_to_baseline( + EXAMPLE_NAME ReadWriteAnRGBImage + BASELINE_PREFIX OutputBaseline + TEST_IMAGE Output.png + ) + + #TODO: When example has valid input, uncomment below #add_example(Create3DFromSeriesOf2D) #compare_to_baseline(EXAMPLE_NAME Create3DFromSeriesOf2D diff --git a/src/IO/ImageBase/ReadWriteAnRGBImage/CMakeLists.txt b/src/IO/ImageBase/ReadWriteAnRGBImage/CMakeLists.txt new file mode 100644 index 000000000..4ee6c1d88 --- /dev/null +++ b/src/IO/ImageBase/ReadWriteAnRGBImage/CMakeLists.txt @@ -0,0 +1,37 @@ +cmake_minimum_required(VERSION 3.16.3) + +project( ReadWriteAnRGBImage ) + +find_package( ITK REQUIRED ) +include( ${ITK_USE_FILE} ) + +add_executable( ReadWriteAnRGBImage Code.cxx ) +target_link_libraries( ReadWriteAnRGBImage ${ITK_LIBRARIES} ) + +install( TARGETS ReadWriteAnRGBImage + DESTINATION bin/ITKSphinxExamples/IO/ImageBase/ + COMPONENT Runtime +) + +install( FILES Code.cxx CMakeLists.txt + DESTINATION share/ITKSphinxExamples/Code/IO/ImageBase/ReadWriteAnRGBImage + COMPONENT Code +) + +set( input_image ${CMAKE_CURRENT_BINARY_DIR}/peppers.png ) +set( output_image Output.png ) + +enable_testing() +add_test( NAME ReadWriteAnRGBImageTest + COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ReadWriteAnRGBImage + ${input_image} + ${output_image} + ) + +if(ITK_WRAP_PYTHON) + add_test(NAME ReadWriteAnRGBImageTestPython + COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Code.py + ${input_image} + ${output_image} + ) +endif() diff --git a/src/IO/ImageBase/ReadWriteAnRGBImage/Code.cxx b/src/IO/ImageBase/ReadWriteAnRGBImage/Code.cxx new file mode 100644 index 000000000..40426cb5c --- /dev/null +++ b/src/IO/ImageBase/ReadWriteAnRGBImage/Code.cxx @@ -0,0 +1,131 @@ +/*========================================================================= + * + * Copyright NumFOCUS + * + * 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 + * + * https://www.apache.org/licenses/LICENSE-2.0.txt + * + * 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. + * + *=========================================================================*/ + +// Software Guide : BeginLatex +// +// RGB images are commonly used for representing data acquired from +// cryogenic sections, optical microscopy and endoscopy. This example +// illustrates how to read and write RGB color images to and from a file. +// This requires the following headers as shown. +// +// \index{itk::RGBPixel!Image} +// \index{RGB!writing Image} +// \index{RGB!reading Image} +// +// Software Guide : EndLatex + +// Software Guide : BeginCodeSnippet +#include "itkRGBPixel.h" +#include "itkImage.h" +#include "itkImageFileReader.h" +#include "itkImageFileWriter.h" +// Software Guide : EndCodeSnippet + + +int +main(int argc, char ** argv) +{ + // Verify the number of parameters in the command line + if (argc < 3) + { + std::cerr << "Usage: " << std::endl; + std::cerr << argv[0] << " inputRGBImageFile outputRGBImageFile " + << std::endl; + return EXIT_FAILURE; + } + + + // Software Guide : BeginLatex + // + // The \doxygen{RGBPixel} class is templated over the type used to + // represent each one of the red, green and blue components. A typical + // instantiation of the RGB image class might be as follows. + // + // \index{itk::RGBPixel!Instantiation} + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + using PixelType = itk::RGBPixel; + using ImageType = itk::Image; + // Software Guide : EndCodeSnippet + + + // Software Guide : BeginLatex + // + // The image type is used as a template parameter to instantiate + // the reader and writer. + // + // \index{itk::ImageFileReader!RGB Image} + // \index{itk::ImageFileWriter!RGB Image} + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + using ReaderType = itk::ImageFileReader; + using WriterType = itk::ImageFileWriter; + + auto reader = ReaderType::New(); + auto writer = WriterType::New(); + // Software Guide : EndCodeSnippet + + + const char * inputFilename = argv[1]; + const char * outputFilename = argv[2]; + + + // Software Guide : BeginLatex + // + // The filenames of the input and output files must be provided to the + // reader and writer respectively. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + reader->SetFileName(inputFilename); + writer->SetFileName(outputFilename); + // Software Guide : EndCodeSnippet + + + ImageType::Pointer image = reader->GetOutput(); + writer->SetInput(image); + + + // Software Guide : BeginLatex + // + // Finally, execution of the pipeline can be triggered by invoking the + // Update() method in the writer. + // + // Software Guide : EndLatex + + // Software Guide : BeginCodeSnippet + writer->Update(); + // Software Guide : EndCodeSnippet + + // Software Guide : BeginLatex + // + // You may have noticed that apart from the declaration of the + // \code{PixelType} there is nothing in this code specific to RGB + // images. All the actions required to support color images are implemented + // internally in the \doxygen{ImageIO} objects. + // + // Software Guide : EndLatex + + + return EXIT_SUCCESS; +} diff --git a/src/IO/ImageBase/ReadWriteAnRGBImage/Code.py b/src/IO/ImageBase/ReadWriteAnRGBImage/Code.py new file mode 100644 index 000000000..a304d6926 --- /dev/null +++ b/src/IO/ImageBase/ReadWriteAnRGBImage/Code.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python + +# Copyright NumFOCUS +# +# 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.txt +# +# 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. + +import itk +import argparse + +parser = argparse.ArgumentParser(description="Write An Image.") +parser.add_argument("output_image", nargs="?") +args = parser.parse_args() + +if args.output_image: + output_filename = args.output_image +else: + output_filename = "testPython.png" + +pixel_type = itk.UC +dimension = 2 +image_type = itk.Image[pixel_type, dimension] + +start = itk.Index[dimension]() +start.Fill(0) + +size = itk.Size[dimension]() +size[0] = 200 +size[1] = 300 + +region = itk.ImageRegion[dimension]() +region.SetIndex(start) +region.SetSize(size) + +image = image_type.New(Regions=region) +image.Allocate() + +itk.imwrite(image, output_filename) diff --git a/src/IO/ImageBase/ReadWriteAnRGBImage/Documentation.rst b/src/IO/ImageBase/ReadWriteAnRGBImage/Documentation.rst new file mode 100644 index 000000000..bedccddaa --- /dev/null +++ b/src/IO/ImageBase/ReadWriteAnRGBImage/Documentation.rst @@ -0,0 +1,50 @@ +:name: WriteAnImage + +Write an Image +============== + +.. index:: + single: itk::Image + single: itk::ImageFileReader + single: itk::ImageFileWriter + single: itk::RGBPixelImage + + +Synopsis +-------- + +RGB images are commonly used for representing data acquired from +cryogenic sections, optical microscopy and endoscopy. This example +illustrates how to read and write RGB color images to and from a file. + + +Results +------- +.. figure:: WriteAnImage.png + :scale: 70% + :alt: test.png + + test.png + + +Code +---- + +Python +...... + +.. literalinclude:: Code.py + :language: python + :lines: 1, 16- + +C++ +... + +.. literalinclude:: Code.cxx + :lines: 18- + + +Classes demonstrated +-------------------- + +.. breathelink:: itk::RGBPixelImage diff --git a/src/IO/ImageBase/ReadWriteAnRGBImage/OutputBaseline.png.sha512 b/src/IO/ImageBase/ReadWriteAnRGBImage/OutputBaseline.png.sha512 new file mode 100644 index 000000000..becc3cf3a --- /dev/null +++ b/src/IO/ImageBase/ReadWriteAnRGBImage/OutputBaseline.png.sha512 @@ -0,0 +1 @@ +4e1e0621f19c2613d5a4c6774fe8933af4b8abad1afdb1d1b5918eacb0755340dea5fdc6135f52dcf865cb8ee8c7ba61096e4f707de64695c6c351e4cbce3f38 diff --git a/src/IO/ImageBase/ReadWriteAnRGBImage/peppers.png.sha512 b/src/IO/ImageBase/ReadWriteAnRGBImage/peppers.png.sha512 new file mode 100644 index 000000000..becc3cf3a --- /dev/null +++ b/src/IO/ImageBase/ReadWriteAnRGBImage/peppers.png.sha512 @@ -0,0 +1 @@ +4e1e0621f19c2613d5a4c6774fe8933af4b8abad1afdb1d1b5918eacb0755340dea5fdc6135f52dcf865cb8ee8c7ba61096e4f707de64695c6c351e4cbce3f38