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 10.9 KB
Newer Older
1
2
3
4
5
6
7
// ================================================================================================
// 
// 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
8
9
//      Technische Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, Germany
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 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"

32
#include "tgt/glcontextmanager.h"
33
34
#include <tbb/tbb.h>
#include <tbb/spin_mutex.h>
35
#include "core/datastructures/imagerepresentationdisk.h"
36
#include "core/datastructures/imagerepresentationgl.h"
37
#include "core/datastructures/genericimagerepresentationlocal.h"
38
39
#include "core/tools/opengljobprocessor.h"
#include "core/tools/job.h"
40

41
42
43
44
#ifdef CAMPVIS_HAS_MODULE_ITK
#include "modules/itk/core/genericimagerepresentationitk.h"
#endif

45
46
47
48
49
50
#include <limits>

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

51
    ImageRepresentationLocal::ImageRepresentationLocal(ImageData* parent, WeaklyTypedPointer::BaseType baseType)
52
        : GenericAbstractImageRepresentation<ImageRepresentationLocal>(parent)
53
54
55
56
57
58
59
60
61
62
        , _baseType(baseType)
        , _intensityHistogram(0)
    {
        _intensityRangeDirty = true;
    }

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

63
64
65
66
67
68
    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)) {
69
            return create(tester->getParent(), tester->getImageData());
70
71
        }
        else if (const ImageRepresentationGL* tester = dynamic_cast<const ImageRepresentationGL*>(source)) {
72
            // FIXME: this here deadlocks, if called from OpenGL context (GLJobProc)!!!
73
74
75
76
            tgt::GLCanvas* context = GLJobProc.iKnowWhatImDoingGetArbitraryContext();
            ImageRepresentationLocal* toReturn = 0;
            GLJobProc.pause();
            try {
77
                tgt::GLContextScopedLock lock(context);
78
                WeaklyTypedPointer wtp = tester->getWeaklyTypedPointer();
79
                toReturn = create(source->getParent(), wtp);
80
81
82
83
84
85
            }
            catch (...) {
                LERROR("An unknown error occured during conversion...");
            }
            GLJobProc.resume();
            return toReturn;
86
87
        }

88
89
90
91
92
93
#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) \
94
95
        if (const GenericImageRepresentationItk<basetype, 1, 3>* tester = dynamic_cast< const GenericImageRepresentationItk<basetype, 1, 3>* >(source)) { \
            typedef GenericImageRepresentationItk<basetype, 1, 3>::ItkImageType ImageType; \
96
97
98
99
100
101
102
103
104
105
            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)]; \
106
            memcpy(pixelDataCopy, pixelData, tgt::hmul(size) * TypeTraits<basetype, 1>::elementSize); \
107
            return GenericImageRepresentationLocal<PixelType, 1>::create(const_cast<ImageData*>(source->getParent()), pixelDataCopy); \
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
        }

#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

131
132
133
        return 0;
    }

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    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 {
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
        _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);
            }

            {
                // TODO: there is probably a more elegant method...
                tbb::spin_mutex::scoped_lock(mutex);
                _normalizedIntensityRange.nibble(localMin);
                _normalizedIntensityRange.nibble(localMax);
            }
        });

170
171
172
173
174
175
176
177
178
179
180
        _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);
181
182
183
184
185
186
        tbb::parallel_for(tbb::blocked_range<size_t>(0, getNumElements()), [&] (const tbb::blocked_range<size_t>& range) {
            for (size_t i = range.begin(); i != range.end(); ++i) {
                float value = this->getElementNormalized(i, 0);
                _intensityHistogram->addSample(&value);
            }
        });
187
188
    }

189
190
    
    ImageRepresentationLocal* ImageRepresentationLocal::create(const ImageData* parent, WeaklyTypedPointer wtp) {
191
#define CONVERT_DISK_TO_GENERIC_LOCAL(baseType,numChannels) \
192
        return GenericImageRepresentationLocal<baseType, numChannels>::create( \
193
            const_cast<ImageData*>(parent), \
194
195
            reinterpret_cast< TypeTraits<baseType, numChannels>::ElementType*>(wtp._pointer));

196
#define DISPATCH_DISK_TO_GENERIC_LOCAL_CONVERSION(numChannels) \
197
        if (parent->getNumChannels() == (numChannels)) { \
198
            switch (wtp._baseType) { \
199
                case WeaklyTypedPointer::UINT8: \
200
                    CONVERT_DISK_TO_GENERIC_LOCAL(uint8_t, (numChannels)) \
201
                case WeaklyTypedPointer::INT8: \
202
                    CONVERT_DISK_TO_GENERIC_LOCAL(int8_t, (numChannels)) \
203
                case WeaklyTypedPointer::UINT16: \
204
                    CONVERT_DISK_TO_GENERIC_LOCAL(uint16_t, (numChannels)) \
205
                case WeaklyTypedPointer::INT16: \
206
                    CONVERT_DISK_TO_GENERIC_LOCAL(int16_t, (numChannels)) \
207
                case WeaklyTypedPointer::UINT32: \
208
                    CONVERT_DISK_TO_GENERIC_LOCAL(uint32_t, (numChannels)) \
209
                case WeaklyTypedPointer::INT32: \
210
                    CONVERT_DISK_TO_GENERIC_LOCAL(int32_t, (numChannels)) \
211
                case WeaklyTypedPointer::FLOAT: \
212
                    CONVERT_DISK_TO_GENERIC_LOCAL(float, (numChannels)) \
213
214
215
216
217
218
                default: \
                    tgtAssert(false, "Should not reach this - wrong base data type!"); \
                    return 0; \
            } \
        }

219
220
221
222
        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)
223
224
225
226
227
        else {
            tgtAssert(false, "Should not reach this - wrong number of channel!");
            return 0;
        }
    }
228
229


230
}