2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

imagerepresentationlocal.cpp 5.14 KB
Newer Older
1
2
3
4
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
6
7
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
11
12
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
13
14
15
16
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
17
// 
18
19
20
21
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
22
23
24
25
26
// 
// ================================================================================================

#include "imagerepresentationlocal.h"

27
28
#include <tbb/tbb.h>
#include <tbb/spin_mutex.h>
29

30
#include "core/datastructures/genericimagerepresentationlocal.h"
31

32
33
34
35
36
37
#include <limits>

namespace campvis {
    
    const std::string ImageRepresentationLocal::loggerCat_ = "CAMPVis.core.datastructures.ImageRepresentationLocal";

38
    ImageRepresentationLocal::ImageRepresentationLocal(ImageData* parent, WeaklyTypedPointer::BaseType baseType)
39
        : GenericAbstractImageRepresentation<ImageRepresentationLocal>(parent)
40
41
42
43
44
45
        , _baseType(baseType)
    {
        _intensityRangeDirty = true;
    }

    ImageRepresentationLocal::~ImageRepresentationLocal() {
46

47
48
49
50
51
52
53
54
55
56
    }

    const Interval<float>& ImageRepresentationLocal::getNormalizedIntensityRange() const {
        if (_intensityRangeDirty)
            computeNormalizedIntensityRange();

        return _normalizedIntensityRange;
    }

    void ImageRepresentationLocal::computeNormalizedIntensityRange() const {
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
        _normalizedIntensityRange = Interval<float>(); // reset interval to empty one
        tbb::spin_mutex _mutex; // mutex to protect for concurrent access

        tbb::parallel_for(tbb::blocked_range<size_t>(0, getNumElements()), [&] (const tbb::blocked_range<size_t>& range) {
            float localMin = std::numeric_limits<float>::max();
            float localMax = -std::numeric_limits<float>::max();

            for (size_t i = range.begin(); i != range.end(); ++i) {
                float value = this->getElementNormalized(i, 0);
                localMax = std::max(localMax, value);
                localMin = std::min(localMin, value);
            }

            {
                tbb::spin_mutex::scoped_lock(mutex);
                _normalizedIntensityRange.nibble(localMin);
                _normalizedIntensityRange.nibble(localMax);
            }
        });

77
78
79
        _intensityRangeDirty = false;
    }

80
    ImageRepresentationLocal* ImageRepresentationLocal::create(const ImageData* parent, WeaklyTypedPointer wtp) {
81
#define CONVERT_DISK_TO_GENERIC_LOCAL(baseType,numChannels) \
82
        return GenericImageRepresentationLocal<baseType, numChannels>::create( \
83
            const_cast<ImageData*>(parent), \
84
85
            reinterpret_cast< TypeTraits<baseType, numChannels>::ElementType*>(wtp._pointer));

86
#define DISPATCH_DISK_TO_GENERIC_LOCAL_CONVERSION(numChannels) \
87
        if (parent->getNumChannels() == (numChannels)) { \
88
            switch (wtp._baseType) { \
89
                case WeaklyTypedPointer::UINT8: \
90
                    CONVERT_DISK_TO_GENERIC_LOCAL(uint8_t, (numChannels)) \
91
                case WeaklyTypedPointer::INT8: \
92
                    CONVERT_DISK_TO_GENERIC_LOCAL(int8_t, (numChannels)) \
93
                case WeaklyTypedPointer::UINT16: \
94
                    CONVERT_DISK_TO_GENERIC_LOCAL(uint16_t, (numChannels)) \
95
                case WeaklyTypedPointer::INT16: \
96
                    CONVERT_DISK_TO_GENERIC_LOCAL(int16_t, (numChannels)) \
97
                case WeaklyTypedPointer::UINT32: \
98
                    CONVERT_DISK_TO_GENERIC_LOCAL(uint32_t, (numChannels)) \
99
                case WeaklyTypedPointer::INT32: \
100
                    CONVERT_DISK_TO_GENERIC_LOCAL(int32_t, (numChannels)) \
101
                case WeaklyTypedPointer::FLOAT: \
102
                    CONVERT_DISK_TO_GENERIC_LOCAL(float, (numChannels)) \
103
104
105
106
107
108
                default: \
                    tgtAssert(false, "Should not reach this - wrong base data type!"); \
                    return 0; \
            } \
        }

109
110
111
112
        DISPATCH_DISK_TO_GENERIC_LOCAL_CONVERSION(1)
        else DISPATCH_DISK_TO_GENERIC_LOCAL_CONVERSION(2)
        else DISPATCH_DISK_TO_GENERIC_LOCAL_CONVERSION(3)
        else DISPATCH_DISK_TO_GENERIC_LOCAL_CONVERSION(4)
113
        else DISPATCH_DISK_TO_GENERIC_LOCAL_CONVERSION(6)
114
115
116
117
118
        else {
            tgtAssert(false, "Should not reach this - wrong number of channel!");
            return 0;
        }
    }
119
120


121
}