Coverage Report

Created: 2026-08-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dcmtk/dcmimgle/libsrc/diutils.cc
Line
Count
Source
1
/*
2
 *
3
 *  Copyright (C) 1996-2021, 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
#include <cmath>
34
35
/*--------------------*
36
 *  global variables  *
37
 *--------------------*/
38
39
OFLogger DCM_dcmimgleLogger = OFLog::getLogger("dcmtk.dcmimgle");
40
41
42
/*------------------------*
43
 *  function definitions  *
44
 *------------------------*/
45
46
unsigned int DicomImageClass::rangeToBits(double minvalue,
47
                                          double maxvalue)
48
0
{
49
    /* assertion: min < max ! */
50
0
    if (minvalue > maxvalue)
51
0
    {
52
0
        const double temp = minvalue;
53
0
        minvalue = maxvalue;
54
0
        maxvalue = temp;
55
0
    }
56
    /* signed data? */
57
0
    if (minvalue < 0)
58
0
    {
59
0
        if (fabs(minvalue) > fabs(maxvalue))
60
0
            return tobits(OFstatic_cast(unsigned long, fabs(minvalue)), 1) + 1;
61
0
        else /* 'minvalue' is negative, 'maxvalue' is positive */
62
0
            return tobits(OFstatic_cast(unsigned long, fabs(maxvalue)), 0) + 1;
63
0
    }
64
0
    return tobits(OFstatic_cast(unsigned long, maxvalue), 0);
65
0
}
66
67
68
int DicomImageClass::isRepresentationSigned(EP_Representation repres)
69
0
{
70
    /* determine whether integer representation is signed or unsigned */
71
0
    return (repres == EPR_Sint8) || (repres == EPR_Sint16) || (repres == EPR_Sint32);
72
0
}
73
74
75
unsigned int DicomImageClass::getRepresentationBits(EP_Representation repres)
76
0
{
77
0
    unsigned int bits = 0;
78
    /* determine number of bits for specified representation */
79
0
    switch (repres)
80
0
    {
81
0
        case EPR_Uint8:
82
0
        case EPR_Sint8:
83
0
            bits = 8;
84
0
            break;
85
0
        case EPR_Uint16:
86
0
        case EPR_Sint16:
87
0
            bits = 16;
88
0
            break;
89
0
        case EPR_Uint32:
90
0
        case EPR_Sint32:
91
0
            bits = 32;
92
0
            break;
93
0
    }
94
0
    return bits;
95
0
}
96
97
98
EP_Representation DicomImageClass::determineRepresentation(double minvalue,
99
                                                           double maxvalue)
100
0
{
101
0
    if (minvalue > maxvalue)                        /* assertion: min < max ! */
102
0
    {
103
0
        const double temp = minvalue;
104
0
        minvalue = maxvalue;
105
0
        maxvalue = temp;
106
0
    }
107
0
    if (minvalue < 0)                               /* signed */
108
0
    {
109
0
        if ((-minvalue <= maxval(7, 0)) && (maxvalue <= maxval(7)))
110
0
            return EPR_Sint8;
111
0
        if ((-minvalue <= maxval(15, 0)) && (maxvalue <= maxval(15)))
112
0
            return EPR_Sint16;
113
#ifdef DEBUG
114
        if (-minvalue > maxval(MAX_BITS - 1, 0))
115
        {
116
            DCMIMGLE_WARN("minimum pixel value (" << minvalue << ") exceeds signed " << MAX_BITS
117
                << " bit " << "representation after modality transformation");
118
        }
119
        if (maxvalue > maxval(MAX_BITS - 1))
120
        {
121
            DCMIMGLE_WARN("maximum pixel value (" << maxvalue << ") exceeds signed " << MAX_BITS
122
                << " bit " << "representation after modality transformation");
123
        }
124
#endif
125
0
        return EPR_Sint32;
126
0
    }
127
0
    if (maxvalue <= maxval(8))
128
0
        return EPR_Uint8;
129
0
    if (maxvalue <= maxval(16))
130
0
        return EPR_Uint16;
131
#ifdef DEBUG
132
    if (maxvalue > maxval(MAX_BITS))
133
    {
134
        DCMIMGLE_WARN("maximum pixel value (" << maxvalue << ") exceeds unsigned " << MAX_BITS
135
            << " bit " << "representation after modality transformation");
136
    }
137
#endif
138
0
    return EPR_Uint32;
139
0
}