ITK  5.4.0
Insight Toolkit
SphinxExamples/src/IO/ImageBase/ConvertImageToAnotherType/Code.cxx
/*=========================================================================
*
* 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.
*
*=========================================================================*/
#include "itkRGBPixel.h"
#include "itkRGBAPixel.h"
#include "itkImage.h"
int
main()
{
constexpr int xDimension = 200;
constexpr int yDimension = 100;
using ComponentType = unsigned char;
using RGBPixelType = itk::RGBPixel<ComponentType>;
using RGBAPixelType = itk::RGBAPixel<ComponentType>;
using RGBImageType = itk::Image<RGBPixelType, 2>;
using RGBAImageType = itk::Image<RGBAPixelType, 2>;
auto rgbImg = RGBImageType::New();
auto rgbaImg = RGBAImageType::New();
// Create the two images
// RGBAImage
rgbaStart[0] = 0;
rgbaStart[1] = 0;
rgbaSize[0] = xDimension;
rgbaSize[1] = yDimension;
itk::ImageRegion<2> rgbaRegion(rgbaStart, rgbaSize);
rgbaImg->SetRegions(rgbaRegion);
rgbaImg->Allocate();
RGBAPixelType rgbaDefault;
rgbaDefault[0] = 127;
rgbaDefault[1] = 100;
rgbaDefault[2] = 230;
rgbaDefault[3] = 255;
rgbaImg->FillBuffer(rgbaDefault);
// RGBImage
itk::ImageRegion<2> rgbRegion = rgbaRegion;
rgbImg->SetRegions(rgbRegion);
rgbImg->Allocate();
size_t numberOfPixels = rgbImg->GetLargestPossibleRegion().GetNumberOfPixels();
// Convert a raw buffer to a buffer of pixel types
RGBAConverterType::Convert(rgbaImg->GetBufferPointer()->GetDataPointer(),
rgbaImg->GetNumberOfComponentsPerPixel(),
rgbImg->GetBufferPointer(),
numberOfPixels);
// Check a few random values
itk::ImageRandomConstIteratorWithIndex<RGBImageType> rgbIterator(rgbImg, rgbImg->GetLargestPossibleRegion());
rgbIterator.SetNumberOfSamples(numberOfPixels / 10);
rgbIterator.GoToBegin();
while (!rgbIterator.IsAtEnd())
{
if (rgbImg->GetPixel(rgbIterator.GetIndex())[0] != rgbaImg->GetPixel(rgbIterator.GetIndex())[0] ||
rgbImg->GetPixel(rgbIterator.GetIndex())[1] != rgbaImg->GetPixel(rgbIterator.GetIndex())[1] ||
rgbImg->GetPixel(rgbIterator.GetIndex())[2] != rgbaImg->GetPixel(rgbIterator.GetIndex())[2])
{
std::cout << "Copy failed for index " << rgbIterator.GetIndex() << " got "
<< rgbImg->GetPixel(rgbIterator.GetIndex()) << " but expected "
<< rgbaImg->GetPixel(rgbIterator.GetIndex()) << std::endl;
}
++rgbIterator;
}
return EXIT_SUCCESS;
}
itk::RGBPixel
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
itkConvertPixelBuffer.h
itkRGBPixel.h
itk::ImageRegion
An image region represents a structured region of data.
Definition: itkImageRegion.h:80
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itkImage.h
itkRGBAPixel.h
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itkDefaultConvertPixelTraits.h
itkImageRandomConstIteratorWithIndex.h
itk::DefaultConvertPixelTraits
Traits class used to by ConvertPixels to convert blocks of pixels.
Definition: itkDefaultConvertPixelTraits.h:41
itk::ImageRandomConstIteratorWithIndex::SetNumberOfSamples
void SetNumberOfSamples(SizeValueType number)
Definition: itkImageRandomConstIteratorWithIndex.h:214
itk::RGBAPixel
Represent Red, Green, Blue and Alpha components for color images.
Definition: itkRGBAPixel.h:59
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
New
static Pointer New()
itk::ImageRegion::GetNumberOfPixels
SizeValueType GetNumberOfPixels() const
itk::ImageRandomConstIteratorWithIndex
A multi-dimensional image iterator that visits a random set of pixels within an image region.
Definition: itkImageRandomConstIteratorWithIndex.h:116
itk::ConvertPixelBuffer
Class to convert blocks of data from one type to another.
Definition: itkConvertPixelBuffer.h:46