The expiration time for new job artifacts in CI/CD pipelines is now 30 days (GitLab default). Previously generated artifacts in already completed jobs will not be affected by the change. The latest artifacts for all jobs in the latest successful pipelines will be kept. More information: https://gitlab.lrz.de/help/user/admin_area/settings/continuous_integration.html#default-artifacts-expiration

imagerepresentationlocal.cpp 11.2 KB
Newer Older
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
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
//      Technische Universitt Mnchen
//      Boltzmannstr. 3, 85748 Garching b. Mnchen, Germany
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
// 
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// 
// ================================================================================================

#include "imagerepresentationlocal.h"

schultezub's avatar
schultezub committed
32
33
#include "tbb/tbb.h"
#include "tbb/spin_mutex.h"
34
35
#include "core/datastructures/imagerepresentationdisk.h"
#include "core/datastructures/genericimagerepresentationlocal.h"
36

37
38
39
40
#ifdef CAMPVIS_HAS_MODULE_ITK
#include "modules/itk/core/genericimagerepresentationitk.h"
#endif

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
#include <limits>

namespace campvis {
    
    class NormalizedIntensityRangeGenerator {
    public:
        NormalizedIntensityRangeGenerator(const ImageRepresentationLocal* intensityData, Interval<float>* interval)
            : _intensityData(intensityData)
            , _interval(interval)
        {
            *_interval = Interval<float>();
        }

        void operator() (const tbb::blocked_range<size_t>& range) const {
            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 = _intensityData->getElementNormalized(i, 0);
                localMax = std::max(localMax, value);
                localMin = std::min(localMin, value);
            }

            {
                // TODO: there is probably a more elegant method...
                tbb::spin_mutex::scoped_lock(_mutex);
                _interval->nibble(localMin);
                _interval->nibble(localMax);
            }
        }

    protected:
        const ImageRepresentationLocal* _intensityData;
        Interval<float>* _interval;
        tbb::spin_mutex _mutex;
    };

    // ================================================================================================

    class IntensityHistogramGenerator {
    public:
        IntensityHistogramGenerator(const ImageRepresentationLocal* intensityData, ImageRepresentationLocal::IntensityHistogramType* histogram)
            : _intensityData(intensityData)
            , _histogram(histogram)
        {}

        void operator() (const tbb::blocked_range<size_t>& range) const {
            for (size_t i = range.begin(); i != range.end(); ++i) {
                float value = _intensityData->getElementNormalized(i, 0);
                _histogram->addSample(&value);
            }
        }

    protected:
        const ImageRepresentationLocal* _intensityData;
        ImageRepresentationLocal::IntensityHistogramType* _histogram;
    };

// ================================================================================================

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

103
    ImageRepresentationLocal::ImageRepresentationLocal(ImageData* parent, WeaklyTypedPointer::BaseType baseType)
104
        : GenericAbstractImageRepresentation<ImageRepresentationLocal>(parent)
105
106
107
108
109
110
111
112
113
114
        , _baseType(baseType)
        , _intensityHistogram(0)
    {
        _intensityRangeDirty = true;
    }

    ImageRepresentationLocal::~ImageRepresentationLocal() {
        delete _intensityHistogram;
    }

115
116
117
118
119
120
121
122
123
    ImageRepresentationLocal* ImageRepresentationLocal::tryConvertFrom(const AbstractImageRepresentation* source) {
        if (source == 0)
            return 0;

        // test source image type via dynamic cast
        if (const ImageRepresentationDisk* tester = dynamic_cast<const ImageRepresentationDisk*>(source)) {
            return convertToGenericLocal(tester);
        }

124
125
126
127
128
129
#ifdef CAMPVIS_HAS_MODULE_ITK
        // There is no way to determine basetype, number of channels and dimensionality of
        // an ITK image at runtime. So there are currently 7*4*2 = 56 different possibilities
        // what source could be. Thank god, there exists macro magic to create the 56
        // different templated conversion codes.
#define CONVERT_ITK_TO_GENERIC_LOCAL(basetype, numchannels, dimensionality) \
130
131
        if (const GenericImageRepresentationItk<basetype, 1, 3>* tester = dynamic_cast< const GenericImageRepresentationItk<basetype, 1, 3>* >(source)) { \
            typedef GenericImageRepresentationItk<basetype, 1, 3>::ItkImageType ImageType; \
132
133
134
135
136
137
138
139
140
141
            typedef ImageType::PixelType PixelType; \
            const PixelType* pixelData = tester->getItkImage()->GetBufferPointer(); \
            \
            ImageType::RegionType region; \
            region = tester->getItkImage()->GetBufferedRegion(); \
            \
            ImageType::SizeType s = region.GetSize(); \
            tgt::svec3 size(s[0], s[1], s[2]); \
            \
            PixelType* pixelDataCopy = new PixelType[tgt::hmul(size)]; \
142
            memcpy(pixelDataCopy, pixelData, tgt::hmul(size) * TypeTraits<basetype, 1>::elementSize); \
143
            return GenericImageRepresentationLocal<PixelType, 1>::create(const_cast<ImageData*>(source->getParent()), pixelDataCopy); \
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
        }

#define DISPATCH_ITK_TO_GENERIC_LOCAL_CONVERSION_ND(numchannels, dimensionality) \
    CONVERT_ITK_TO_GENERIC_LOCAL(uint8_t, numchannels, dimensionality) \
    else CONVERT_ITK_TO_GENERIC_LOCAL(int8_t, numchannels, dimensionality) \
    else CONVERT_ITK_TO_GENERIC_LOCAL(uint16_t, numchannels, dimensionality) \
    else CONVERT_ITK_TO_GENERIC_LOCAL(int16_t, numchannels, dimensionality) \
    else CONVERT_ITK_TO_GENERIC_LOCAL(uint32_t, numchannels, dimensionality) \
    else CONVERT_ITK_TO_GENERIC_LOCAL(int32_t, numchannels, dimensionality) \
    else CONVERT_ITK_TO_GENERIC_LOCAL(float, numchannels, dimensionality)

#define DISPATCH_ITK_TO_GENERIC_LOCAL_CONVERSION_D(dimensionality) \
    DISPATCH_ITK_TO_GENERIC_LOCAL_CONVERSION_ND(1, dimensionality) \
    else DISPATCH_ITK_TO_GENERIC_LOCAL_CONVERSION_ND(2, dimensionality) \
    else DISPATCH_ITK_TO_GENERIC_LOCAL_CONVERSION_ND(3, dimensionality) \
    else DISPATCH_ITK_TO_GENERIC_LOCAL_CONVERSION_ND(4, dimensionality) 

        // okay we've defined our macros. Now we just need to call them so that they call 
        // each other and create 56 different conversion checks - hooray
        DISPATCH_ITK_TO_GENERIC_LOCAL_CONVERSION_D(2)
        else DISPATCH_ITK_TO_GENERIC_LOCAL_CONVERSION_D(3)
#endif

167
168
169
        return 0;
    }

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
    const Interval<float>& ImageRepresentationLocal::getNormalizedIntensityRange() const {
        if (_intensityRangeDirty)
            computeNormalizedIntensityRange();

        return _normalizedIntensityRange;
    }

    const ConcurrentGenericHistogramND<float, 1>& ImageRepresentationLocal::getIntensityHistogram() const {
        if (_intensityHistogram == 0)
            computeIntensityHistogram();

        return *_intensityHistogram;
    }

    void ImageRepresentationLocal::computeNormalizedIntensityRange() const {
        tbb::parallel_for(tbb::blocked_range<size_t>(0, getNumElements()), NormalizedIntensityRangeGenerator(this, &_normalizedIntensityRange));
        _intensityRangeDirty = false;
    }

    void ImageRepresentationLocal::computeIntensityHistogram() const {
        delete _intensityHistogram;

        const Interval<float>& i = getNormalizedIntensityRange();
        float mins = i.getLeft();
        float maxs = i.getRight();
        size_t numBuckets = 1024;
        _intensityHistogram = new IntensityHistogramType(&mins, &maxs, &numBuckets);
        tbb::parallel_for(tbb::blocked_range<size_t>(0, getNumElements()), IntensityHistogramGenerator(this, _intensityHistogram));
    }

200
201
202
    ImageRepresentationLocal* ImageRepresentationLocal::convertToGenericLocal(const ImageRepresentationDisk* source) {
        WeaklyTypedPointer wtp = source->getImageData();

203
#define CONVERT_DISK_TO_GENERIC_LOCAL(baseType,numChannels) \
204
        return GenericImageRepresentationLocal<baseType, numChannels>::create( \
205
            const_cast<ImageData*>(source->getParent()), \
206
207
            reinterpret_cast< TypeTraits<baseType, numChannels>::ElementType*>(wtp._pointer));

208
#define DISPATCH_DISK_TO_GENERIC_LOCAL_CONVERSION(numChannels) \
209
        if (source->getParent()->getNumChannels() == (numChannels)) { \
210
211
            switch (source->getBaseType()) { \
                case WeaklyTypedPointer::UINT8: \
212
                    CONVERT_DISK_TO_GENERIC_LOCAL(uint8_t, (numChannels)) \
213
                case WeaklyTypedPointer::INT8: \
214
                    CONVERT_DISK_TO_GENERIC_LOCAL(int8_t, (numChannels)) \
215
                case WeaklyTypedPointer::UINT16: \
216
                    CONVERT_DISK_TO_GENERIC_LOCAL(uint16_t, (numChannels)) \
217
                case WeaklyTypedPointer::INT16: \
218
                    CONVERT_DISK_TO_GENERIC_LOCAL(int16_t, (numChannels)) \
219
                case WeaklyTypedPointer::UINT32: \
220
                    CONVERT_DISK_TO_GENERIC_LOCAL(uint32_t, (numChannels)) \
221
                case WeaklyTypedPointer::INT32: \
222
                    CONVERT_DISK_TO_GENERIC_LOCAL(int32_t, (numChannels)) \
223
                case WeaklyTypedPointer::FLOAT: \
224
                    CONVERT_DISK_TO_GENERIC_LOCAL(float, (numChannels)) \
225
226
227
228
229
230
                default: \
                    tgtAssert(false, "Should not reach this - wrong base data type!"); \
                    return 0; \
            } \
        }

231
232
233
234
        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)
235
236
237
238
239
240
241
242
        else {
            tgtAssert(false, "Should not reach this - wrong number of channel!");
            return 0;
        }

    }


243
}