File: TestRescaler2.cxx

package info (click to toggle)
gdcm 3.0.24-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 27,560 kB
  • sloc: cpp: 203,722; ansic: 76,471; xml: 48,131; python: 3,473; cs: 2,308; java: 1,629; lex: 1,290; sh: 334; php: 128; makefile: 97
file content (202 lines) | stat: -rw-r--r-- 6,492 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*=========================================================================

  Program: GDCM (Grassroots DICOM). A DICOM library

  Copyright (c) 2006-2011 Mathieu Malaterre
  All rights reserved.
  See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "gdcmRescaler.h"

#include <vector>
#include <limits>

template < typename T >
struct TypeToPixelFormat;

template<> struct TypeToPixelFormat<double> { enum { Type = gdcm::PixelFormat::FLOAT64 }; };
template<> struct TypeToPixelFormat<float> { enum { Type = gdcm::PixelFormat::FLOAT32 }; };
template<> struct TypeToPixelFormat<short> { enum { Type = gdcm::PixelFormat::INT16 }; };
template<> struct TypeToPixelFormat<unsigned short> { enum { Type = gdcm::PixelFormat::UINT16 }; };
template<> struct TypeToPixelFormat<unsigned char> { enum { Type = gdcm::PixelFormat::UINT8 }; };
template<> struct TypeToPixelFormat<char> { enum { Type = gdcm::PixelFormat::INT8 }; };
template<> struct TypeToPixelFormat<int> { enum { Type = gdcm::PixelFormat::INT32 }; };

template <typename input_pixel, typename output_pixel>
bool TestRescaler2Func(
  const double intercept,
  const double slope,
  const unsigned short bitsallocated,
  const unsigned short bitsstored,
  const unsigned short pixelrepresentation,
  const bool best_fit
)
{
  const gdcm::PixelFormat pixeltype(1,bitsallocated,bitsstored,
    (unsigned short)(bitsstored-1),pixelrepresentation);

  uint8_t ps = pixeltype.GetPixelSize();
  // programmer error:
  if( ps != sizeof( input_pixel ) ) return false;
  if( pixelrepresentation ) // signed
    {
    if( std::numeric_limits<input_pixel>::min() == 0 ) return false;
    }
  else
    {
    if( std::numeric_limits<input_pixel>::min() != 0 ) return false;
    }

  gdcm::Rescaler r;
  r.SetIntercept( intercept );
  r.SetSlope( slope );
  r.SetPixelFormat( pixeltype );

  const int64_t min = pixeltype.GetMin();
  const int64_t max = pixeltype.GetMax();

  gdcm::PixelFormat::ScalarType outputpt;
  outputpt = r.ComputeInterceptSlopePixelType();

  gdcm::PixelFormat::ScalarType targetpixeltype =
    (gdcm::PixelFormat::ScalarType)TypeToPixelFormat<output_pixel>::Type;
  if( best_fit )
    {
    if( targetpixeltype != outputpt )
      {
      return false;
      }
    }
  else
    {
    if( targetpixeltype == outputpt )
      {
      return false;
      }
    }
  r.SetTargetPixelType( targetpixeltype );
  r.SetUseTargetPixelType(true);

  std::vector<input_pixel> values;
  const int nvalues = 1 << bitsstored;
  values.reserve( nvalues );
  for( int i = 0; i < nvalues; ++i )
    {
    //values.push_back( (input_pixel)(std::numeric_limits<input_pixel>::min() + i) );
    values.push_back( (input_pixel)(min + i) );
    }

  std::vector<output_pixel> output;
  output.resize( nvalues );
  if( !r.Rescale((char*)output.data(),(char*)values.data(),nvalues * sizeof( values[0] ) ) )
    return false;

  // get the min/max:
  double min2 = (double)output[0];
  double max2 = (double)output[nvalues-1];
  if( min2 > max2 ) return false;

  if( intercept == 0 && slope == 1 )
    {
    if( min != min2 ) return false;
    if( max != max2 ) return false;
    }

  gdcm::Rescaler ir;
  ir.SetIntercept( intercept );
  ir.SetSlope( slope );
  ir.SetPixelFormat( targetpixeltype );
  ir.SetMinMaxForPixelType( min2, max2 );
  const gdcm::PixelFormat pf2 = ir.ComputePixelTypeFromMinMax();

  if( pf2 != pixeltype ) return false;

  std::vector<input_pixel> check;
  check.resize( nvalues );

  if( check == values ) // dummy check
    return false;

  if( !ir.InverseRescale((char*)check.data(),(char*)output.data(), nvalues * sizeof( output[0] ) ) )
    return false;

  if( check != values )
    return false;

  return true;
}

int TestRescaler2(int, char *[])
{
    {
    double intercept = 0.000061;
    double slope     = 3.774114;
    // Case 1.
    // gdcmData/MR-MONO2-12-shoulder.dcm
    // (0028,0100) US 16         # 2,1 Bits Allocated
    // (0028,0101) US 12         # 2,1 Bits Stored
    // (0028,0102) US 11         # 2,1 High Bit
    // (0028,0103) US 0          # 2,1 Pixel Representation
    // [...]
    // (0028,1052) DS [0.000061] # 8,1 Rescale Intercept
    // (0028,1053) DS [3.774114] # 8,1 Rescale Slope
    if( !TestRescaler2Func<unsigned short,double>(intercept, slope,16,12,0,true) ) return 1;

    // Case 2.
    // use float instead of default double
    if( !TestRescaler2Func<unsigned short,float>(intercept, slope,16,12,0,false) ) return 1;
    }

    {
    double intercept = 0;
    double slope     = 1;

    // Case 3.
    if( !TestRescaler2Func<unsigned short,double>(intercept, slope,16,12,0,false) ) return 1;
    // Case 4.
    if( !TestRescaler2Func<unsigned short,float>(intercept, slope,16,12,0,false) ) return 1;
    // Case 5. best fit
    if( !TestRescaler2Func<unsigned short,unsigned short>(intercept, slope,16,12,0,true) ) return 1;
    // Case 6. unsigned char:
    if( !TestRescaler2Func<unsigned char,unsigned char>(intercept,slope,8,8,0,true) ) return 1;
    // Case 7. char
    if( !TestRescaler2Func<unsigned char,char>(intercept,slope,8,7,0,false) ) return 1;
    }

    {
    double intercept = 0;
    double slope     = 1;

    // Case 8.
    if( !TestRescaler2Func<short,double>(intercept,slope,16,12,1,false) ) return 1;
    // Case 9.
    if( !TestRescaler2Func<short,float>(intercept, slope,16,12,1,false) ) return 1;
    // Case 10. best fit
    if( !TestRescaler2Func<short,short>(intercept, slope,16,12,1,true) ) return 1;
    // Case 11. unsigned char:
    if( !TestRescaler2Func<char,char>(intercept,slope,8,8,1,true) ) return 1;
    // Case 12. char
    if( !TestRescaler2Func<char,char>(intercept,slope,8,7,1,true) ) return 1;
    }

    {
    double intercept = -1024;
    double slope     = 1;

    // Case 13.
    if( !TestRescaler2Func<unsigned short,short>(intercept, slope,16,12,0,true) ) return 1;
    // Case 14.
    if( !TestRescaler2Func<unsigned short,int>(intercept, slope,16,12,0,false) ) return 1;
    // Case 15.
    if( !TestRescaler2Func<unsigned char,short>(intercept, slope,8,8,0,true) ) return 1;
    // Case 16.
    if( !TestRescaler2Func<unsigned char,int>(intercept, slope,8,8,0,false) ) return 1;
    }

  return 0;
}