1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *=========================================================================*/
18 /*=========================================================================
19  *
20  *  Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  *  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  *  For complete copyright, license and disclaimer of warranty information
25  *  please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
28 #include "itkImageFileReader.h"
29 #include "itkImageFileWriter.h"
30 #include "itkSimpleFilterWatcher.h"
31 
32 #include "itkLabelImageToLabelMapFilter.h"
33 #include "itkCropLabelMapFilter.h"
34 #include "itkLabelMapToLabelImageFilter.h"
35 
36 #include "itkTestingMacros.h"
37 
itkCropLabelMapFilterTest1(int argc,char * argv[])38 int itkCropLabelMapFilterTest1(int argc, char * argv[])
39 {
40 
41   if( argc != 5 )
42     {
43     std::cerr << "usage: " << argv[0] << " input output size0 size1" << std::endl;
44     return EXIT_FAILURE;
45     }
46 
47   constexpr unsigned int dim = 2;
48 
49   using ImageType = itk::Image< unsigned char, dim >;
50 
51   using LabelObjectType = itk::LabelObject< unsigned char, dim >;
52   using LabelMapType = itk::LabelMap< LabelObjectType >;
53 
54   using ReaderType = itk::ImageFileReader< ImageType >;
55   ReaderType::Pointer reader = ReaderType::New();
56   reader->SetFileName( argv[1] );
57 
58   using I2LType = itk::LabelImageToLabelMapFilter< ImageType, LabelMapType>;
59   I2LType::Pointer i2l = I2LType::New();
60   i2l->SetInput( reader->GetOutput() );
61 
62   using CropType = itk::CropLabelMapFilter< LabelMapType >;
63   CropType::Pointer crop = CropType::New();
64   // test the behavior without input
65   TRY_EXPECT_EXCEPTION( crop->Update() );
66   crop->ResetPipeline();
67 
68   crop->SetInput( i2l->GetOutput() );
69   CropType::SizeType size;
70   size[0] = std::stoi( argv[3] );
71   size[1] = std::stoi( argv[4] );
72 
73   crop->SetCropSize( size );
74   TEST_SET_GET_VALUE( size, crop->GetUpperBoundaryCropSize() );
75   TEST_SET_GET_VALUE( size, crop->GetLowerBoundaryCropSize() );
76 
77   itk::SimpleFilterWatcher watcher6(crop, "filter");
78 
79   using L2IType = itk::LabelMapToLabelImageFilter< LabelMapType, ImageType>;
80   L2IType::Pointer l2i = L2IType::New();
81   l2i->SetInput( crop->GetOutput() );
82 
83   using WriterType = itk::ImageFileWriter< ImageType >;
84   WriterType::Pointer writer = WriterType::New();
85   writer->SetInput( l2i->GetOutput() );
86   writer->SetFileName( argv[2] );
87   writer->UseCompressionOn();
88 
89   TRY_EXPECT_NO_EXCEPTION( writer->Update() );
90 
91   return EXIT_SUCCESS;
92 }
93