1 /*
2  *
3  *  Copyright (C) 1996-2011, OFFIS e.V.
4  *  All rights reserved.  See COPYRIGHT file for details.
5  *
6  *  This software and supporting documentation were developed by
7  *
8  *    OFFIS e.V.
9  *    R&D Division Health
10  *    Escherweg 2
11  *    D-26121 Oldenburg, Germany
12  *
13  *
14  *  Module:  dcmimgle
15  *
16  *  Author:  Joerg Riesmeier
17  *
18  *  Purpose: Utilities (Source)
19  *
20  */
21 
22 
23 #include "dcmtk/config/osconfig.h"
24 
25 #include "dcmtk/dcmdata/dctypes.h"
26 #include "dcmtk/ofstd/ofstream.h"
27 
28 #include "dcmtk/dcmimgle/diutils.h"
29 
30 #define INCLUDE_CMATH
31 #include "dcmtk/ofstd/ofstdinc.h"
32 
33 
34 /*--------------------*
35  *  global variables  *
36  *--------------------*/
37 
38 OFLogger DCM_dcmimgleLogger = OFLog::getLogger("dcmtk.dcmimgle");
39 
40 
41 /*------------------------*
42  *  function definitions  *
43  *------------------------*/
44 
rangeToBits(double minvalue,double maxvalue)45 unsigned int DicomImageClass::rangeToBits(double minvalue,
46                                           double maxvalue)
47 {
48     /* assertion: min < max ! */
49     if (minvalue > maxvalue)
50     {
51         const double temp = minvalue;
52         minvalue = maxvalue;
53         maxvalue = temp;
54     }
55     /* signed data? */
56     if (minvalue < 0)
57     {
58         if (fabs(minvalue) > fabs(maxvalue))
59             return tobits(OFstatic_cast(unsigned long, fabs(minvalue)), 1) + 1;
60         else /* 'minvalue' is negative, 'maxvalue' is positive */
61             return tobits(OFstatic_cast(unsigned long, fabs(maxvalue)), 0) + 1;
62     }
63     return tobits(OFstatic_cast(unsigned long, maxvalue), 0);
64 }
65 
66 
isRepresentationSigned(EP_Representation repres)67 int DicomImageClass::isRepresentationSigned(EP_Representation repres)
68 {
69     /* determine whether integer representation is signed or unsigned */
70     return (repres == EPR_Sint8) || (repres == EPR_Sint16) || (repres == EPR_Sint32);
71 }
72 
73 
getRepresentationBits(EP_Representation repres)74 unsigned int DicomImageClass::getRepresentationBits(EP_Representation repres)
75 {
76     unsigned int bits = 0;
77     /* determine number of bits for specified representation */
78     switch (repres)
79     {
80         case EPR_Uint8:
81         case EPR_Sint8:
82             bits = 8;
83             break;
84         case EPR_Uint16:
85         case EPR_Sint16:
86             bits = 16;
87             break;
88         case EPR_Uint32:
89         case EPR_Sint32:
90             bits = 32;
91             break;
92     }
93     return bits;
94 }
95 
96 
determineRepresentation(double minvalue,double maxvalue)97 EP_Representation DicomImageClass::determineRepresentation(double minvalue,
98                                                            double maxvalue)
99 {
100     if (minvalue > maxvalue)                        /* assertion: min < max ! */
101     {
102         const double temp = minvalue;
103         minvalue = maxvalue;
104         maxvalue = temp;
105     }
106     if (minvalue < 0)                               /* signed */
107     {
108         if ((-minvalue <= maxval(7, 0)) && (maxvalue <= maxval(7)))
109             return EPR_Sint8;
110         if ((-minvalue <= maxval(15, 0)) && (maxvalue <= maxval(15)))
111             return EPR_Sint16;
112 #ifdef DEBUG
113         if (-minvalue > maxval(MAX_BITS - 1, 0))
114         {
115             DCMIMGLE_WARN("minimum pixel value (" << minvalue << ") exceeds signed " << MAX_BITS
116                 << " bit " << "representation after modality transformation");
117         }
118         if (maxvalue > maxval(MAX_BITS - 1))
119         {
120             DCMIMGLE_WARN("maximum pixel value (" << maxvalue << ") exceeds signed " << MAX_BITS
121                 << " bit " << "representation after modality transformation");
122         }
123 #endif
124         return EPR_Sint32;
125     }
126     if (maxvalue <= maxval(8))
127         return EPR_Uint8;
128     if (maxvalue <= maxval(16))
129         return EPR_Uint16;
130 #ifdef DEBUG
131     if (maxvalue > maxval(MAX_BITS))
132     {
133         DCMIMGLE_WARN("maximum pixel value (" << maxvalue << ") exceeds unsigned " << MAX_BITS
134             << " bit " << "representation after modality transformation");
135     }
136 #endif
137     return EPR_Uint32;
138 }
139