Skip to content

Commit

Permalink
Merge topic 'AddGetNumberOfPixels'
Browse files Browse the repository at this point in the history
78bec2c Adding tests for Image::GetNumberOfPixels
2fcbf38 ENH: Add GetNumberOfPixels to Images
  • Loading branch information
blowekamp authored and kwrobot committed Sep 19, 2016
2 parents 93b92e5 + 78bec2c commit 3a6f398
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Code/Common/include/sitkImage.h
Expand Up @@ -150,6 +150,16 @@ namespace simple
*/
unsigned int GetNumberOfComponentsPerPixel( void ) const;

/** \brief Get the number of pixels in the image
*
* To Calculate the total number of values stored continuously for
* the image's buffer, the NumberOfPixels should be multiplied by
* NumberOfComponentsPerPixel in order to account for multiple
* component images.
*
*/
uint64_t GetNumberOfPixels( void ) const;

/** Get/Set the Origin
* @{
*/
Expand Down
6 changes: 6 additions & 0 deletions Code/Common/src/sitkImage.cxx
Expand Up @@ -126,6 +126,12 @@ namespace itk
return this->m_PimpleImage->GetNumberOfComponentsPerPixel();
}

uint64_t Image::GetNumberOfPixels( void ) const
{
assert( m_PimpleImage );
return this->m_PimpleImage->GetNumberOfPixels();
}

std::string Image::GetPixelIDTypeAsString( void ) const
{
return std::string( GetPixelIDValueAsString( this->GetPixelIDValue() ) );
Expand Down
2 changes: 2 additions & 0 deletions Code/Common/src/sitkPimpleImageBase.h
Expand Up @@ -44,6 +44,7 @@ namespace itk

virtual PixelIDValueEnum GetPixelID(void) const = 0;
virtual unsigned int GetDimension( void ) const = 0;
virtual uint64_t GetNumberOfPixels( void ) const = 0;
virtual unsigned int GetNumberOfComponentsPerPixel( void ) const = 0;

virtual PimpleImageBase *ShallowCopy(void) const = 0;
Expand All @@ -58,6 +59,7 @@ namespace itk
virtual std::vector< unsigned int > GetSize( void ) const = 0;
virtual unsigned int GetSize( unsigned int dimension ) const = 0;


virtual std::vector<double> GetOrigin( void ) const = 0;
virtual void SetOrigin( const std::vector<double> &orgn ) = 0;
virtual std::vector<double> GetSpacing( void ) const = 0;
Expand Down
5 changes: 5 additions & 0 deletions Code/Common/src/sitkPimpleImageBase.hxx
Expand Up @@ -273,6 +273,11 @@ namespace itk
return sitkITKVectorToSTL<unsigned int>( largestRegion.GetSize() );
}

virtual uint64_t GetNumberOfPixels( void ) const
{
return this->m_Image->GetLargestPossibleRegion().GetNumberOfPixels();
}

std::string ToString( void ) const
{
std::ostringstream out;
Expand Down
10 changes: 9 additions & 1 deletion Testing/Unit/sitkImageTests.cxx
Expand Up @@ -148,6 +148,8 @@ TEST_F(Image,Create) {
EXPECT_EQ ( size[1], shortImage->GetHeight() );
EXPECT_EQ ( size[2], shortImage->GetDepth() );

EXPECT_EQ ( 274560u, shortImage->GetNumberOfPixels() );

}

TEST_F(Image,ImageDataType) {
Expand Down Expand Up @@ -220,6 +222,7 @@ TEST_F(Image,Constructors) {
EXPECT_EQ ( 0u, image.GetWidth() );
EXPECT_EQ ( 0u, image.GetHeight() );
EXPECT_EQ ( 0u, image.GetDepth() );
EXPECT_EQ ( 0u, image.GetNumberOfPixels() );
}

itk::simple::Image image ( 64, 65, 66, itk::simple::sitkUInt8 );
Expand All @@ -231,6 +234,7 @@ TEST_F(Image,Constructors) {
EXPECT_EQ ( 64u, image.GetWidth() );
EXPECT_EQ ( 65u, image.GetHeight() );
EXPECT_EQ ( 66u, image.GetDepth() );
EXPECT_EQ ( 274560u, image.GetNumberOfPixels() );
EXPECT_EQ( image.GetDirection(), directionI3D );

image = itk::simple::Image ( 64, 65, 66, itk::simple::sitkInt16 );
Expand All @@ -242,6 +246,7 @@ TEST_F(Image,Constructors) {
EXPECT_EQ ( 64u, image.GetWidth() );
EXPECT_EQ ( 65u, image.GetHeight() );
EXPECT_EQ ( 66u, image.GetDepth() );
EXPECT_EQ ( 274560u, image.GetNumberOfPixels() );
EXPECT_EQ( image.GetDirection(), directionI3D );

image = itk::simple::Image ( 64, 65, itk::simple::sitkUInt16 );
Expand All @@ -253,6 +258,7 @@ TEST_F(Image,Constructors) {
EXPECT_EQ ( 64u, image.GetWidth() );
EXPECT_EQ ( 65u, image.GetHeight() );
EXPECT_EQ ( 0u, image.GetDepth() );
EXPECT_EQ ( 4160u, image.GetNumberOfPixels() );
EXPECT_EQ ( 1u, image.GetNumberOfComponentsPerPixel() );
EXPECT_EQ( image.GetDirection(), directionI2D );

Expand Down Expand Up @@ -299,6 +305,7 @@ TEST_F(Image,Constructors) {
EXPECT_EQ ( 64u, image.GetWidth() );
EXPECT_EQ ( 65u, image.GetHeight() );
EXPECT_EQ ( 66u, image.GetDepth() );
EXPECT_EQ ( 274560u, image.GetNumberOfPixels() );
EXPECT_EQ ( 1u, image.GetNumberOfComponentsPerPixel() );
EXPECT_EQ( image.GetDirection(), directionI3D );

Expand All @@ -315,6 +322,7 @@ TEST_F(Image,Constructors) {

image = itk::simple::Image ( 64, 65, 66, itk::simple::sitkVectorUInt16 );
EXPECT_EQ ( 3u, image.GetNumberOfComponentsPerPixel() );
EXPECT_EQ ( 274560u, image.GetNumberOfPixels() );
}

TEST_F(Image,Hash) {
Expand Down Expand Up @@ -452,7 +460,7 @@ TEST_F(Image, CopyInformation)
EXPECT_EQ( img1.GetSpacing(), img2.GetSpacing() );
EXPECT_EQ( img1.GetOrigin(), img2.GetOrigin() );
EXPECT_EQ( img1.GetDirection(), img2.GetDirection() );

EXPECT_EQ( img1.GetNumberOfPixels(), img2.GetNumberOfPixels() );
}

TEST_F(Image, CopyOnWrite)
Expand Down

0 comments on commit 3a6f398

Please sign in to comment.