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 #include "itkImageFileReader.h"
20 #include "itkDCMTKImageIO.h"
21 #include "itkImageRegionConstIterator.h"
22 #include "itkMultiplyImageFilter.h"
23 #include "itkAddImageFilter.h"
24 #include "itkSubtractImageFilter.h"
25 #include "itkStatisticsImageFilter.h"
26 
itkDCMTKImageIOSlopeInterceptTest(int ac,char * av[])27 int itkDCMTKImageIOSlopeInterceptTest(int ac, char * av[])
28 {
29   if(ac < 3)
30     {
31     std::cerr << "Usage: " << av[0]
32               << " <original image> <slope intercept image>"
33               << std::endl;
34     return EXIT_FAILURE;
35     }
36 
37   using PixelType = short;
38   using ImageType = itk::Image< PixelType, 3 >;
39   using ReaderType = itk::ImageFileReader< ImageType >;
40   using ImageIOType = itk::DCMTKImageIO;
41 
42   const PixelType rescaleSlope(2);
43   const PixelType rescaleIntercept(-99);
44 
45   ImageIOType::Pointer dcmImageIO = ImageIOType::New();
46 
47   ImageType::Pointer images[2];
48   for(unsigned i = 0; i < 2; ++i)
49     {
50     ReaderType::Pointer reader = ReaderType::New();
51     reader->SetFileName( av[i+1] );
52     reader->SetImageIO( dcmImageIO );
53 
54     try
55       {
56       reader->Update();
57       }
58     catch (itk::ExceptionObject & e)
59       {
60       std::cerr << "exception in file reader " << std::endl;
61       std::cerr << e << std::endl;
62       return EXIT_FAILURE;
63       }
64     images[i] = reader->GetOutput();
65     }
66 
67   //
68   // the two inputs are a DICOM image without slope/intercept tags,
69   // and the same image with slpe/intercept tags.  I read the first
70   // image and apply the slope/intercept, and then subtract the first
71   // from the second, then look for non-zero min/max/mean.  They
72   // should be identical.
73   using ItType = itk::ImageRegionConstIterator<ImageType>;
74   ItType it1(images[0],images[0]->GetLargestPossibleRegion());
75   ItType it2(images[1],images[1]->GetLargestPossibleRegion());
76   for(it1.GoToBegin(), it2.GoToBegin(); !it1.IsAtEnd() && !it2.IsAtEnd(); ++it1, ++it2)
77     {
78     PixelType pix1(it1.Get());
79     const PixelType pix2(it2.Get());
80     pix1 = (pix1 * rescaleSlope) + rescaleIntercept;
81     if(pix1 != pix2)
82       {
83       std::cerr << "computed pixel doesn't match pixel from slopeIntercept image: computed = "
84                 << pix1 << " slopeIntercept image = " << pix2 << std::endl;
85       return EXIT_FAILURE;
86       }
87     }
88   return EXIT_SUCCESS;
89 }
90