imagerepresentationdisk.cpp 11.5 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
// 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 "imagerepresentationdisk.h"
31

32
33
#include "tgt/filesystem.h"

34

35
36
37
namespace campvis {
    const std::string ImageRepresentationDisk::loggerCat_ = "CAMPVis.core.datastructures.ImageRepresentationDisk";

38
39
    ImageRepresentationDisk* ImageRepresentationDisk::create(ImageData* parent, const std::string& url, WeaklyTypedPointer::BaseType type, size_t offset /*= 0*/, EndianHelper::Endianness endianness /*= EndianHelper::LITTLE_ENDIAN*/, const tgt::svec3& stride /*= tgt::svec3::zero */, bool multichannelSideBySide /*= false*/) {
        ImageRepresentationDisk* toReturn = new ImageRepresentationDisk(parent, url, type, offset, endianness, stride, multichannelSideBySide);
40
41
42
43
        toReturn->addToParent();
        return toReturn;
    }

44
    ImageRepresentationDisk::ImageRepresentationDisk(ImageData* parent, const std::string& url, WeaklyTypedPointer::BaseType type, size_t offset /*= 0*/, EndianHelper::Endianness endianness /*= EndianHelper::LITTLE_ENDIAN*/, const tgt::svec3& stride /*= tgt::svec2::zero */, bool multichannelSideBySide /*= false*/)
45
        : GenericAbstractImageRepresentation<ImageRepresentationDisk>(parent)
46
47
48
49
50
        , _url(url)
        , _offset(offset)
        , _type(type)
        , _endianess(endianness)
        , _stride(stride)
51
        , _multichannelSideBySide(multichannelSideBySide)
52
53
54
55
56
57
    {
    }

    ImageRepresentationDisk::~ImageRepresentationDisk() {
    }

58
    ImageRepresentationDisk* ImageRepresentationDisk::getSubImage(ImageData* parent, const tgt::svec3& llf, const tgt::svec3& urb) const {
59
60
        tgtAssert(tgt::hand(tgt::lessThan(llf, urb)), "Coordinates in LLF must be component-wise smaller than the ones in URB!");

61
        const tgt::svec3& size = getSize();
62
        tgt::svec3 newSize = urb - llf;
63
        if (newSize == size) {
64
            // nothing has changed, just provide a copy:
65
            return clone(parent);
66
67
        }

68
        size_t newOffset = _offset + WeaklyTypedPointer::numBytes(_type, _parent->getNumChannels()) * (llf.x + llf.y * size.y + llf.z * size.x * size.y);
69
70
        // the stride doesn't change!
        tgt::svec3 newStride = (_stride == tgt::svec3::zero) ? getCanonicStride(getSize()) : _stride;
71
        return new ImageRepresentationDisk(parent, _url, _type, newOffset, _endianess, newStride);
72
73
74
75
76
    }

    campvis::WeaklyTypedPointer ImageRepresentationDisk::getImageData() const {
        const tgt::svec3& size = getSize();
        size_t numElements = tgt::hmul(size);
77
        size_t numBytesPerElement = WeaklyTypedPointer::numBytes(_type, _parent->getNumChannels());
78
79
80
81
82
83
84
85
        size_t numBytes = numElements * numBytesPerElement;

        // open file and prepare for read
        std::ifstream file(_url.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
        if (file.is_open()) {
            size_t fileSize = static_cast<size_t>(file.tellg());
            if (fileSize < numBytes) {
                LERROR("File is smaller than expected.");
86
                return WeaklyTypedPointer(_type, _parent->getNumChannels(), 0);
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
            }

            file.seekg(_offset, std::ios::beg);

            // Reserve memory - because we have no type information we simply read into a char array.
            char* data = new char[numBytes];

            // handle stride:
            tgt::svec3 canonicStride = getCanonicStride(size);
            if (_stride == tgt::svec3::zero || _stride == canonicStride) {
                // no stride is easy - we have just one chunk of data:
                file.read(data, numBytes);
            }
            else {
                // TODO:    Check for correctness when being adequately awake! ;)
                size_t index = 0;

                // we have a stride at at least one direction
                if (_stride.z == 0 || canonicStride.z) {
                    if (_stride.y == 0 || canonicStride.y) {
                        // we have a stride only in the last dimension so we can read the elements from the first two dimension in a row
                        size_t sz = size.x*size.y;
                        for (size_t z = 0; z < size.z; ++z) {
                            size_t posZ = static_cast<size_t>(file.tellg());
                            file.read(data + index, sz * numBytesPerElement);
                            file.seekg(posZ + (_stride.z * numBytesPerElement), std::ios::beg);
                            index += sz;
                        }
                    }
                    else {
                        // we have a stride only in the last 2 dimensions so we can read the elements from the first dimension in a row
                        for (size_t z = 0; z < size.z; ++z) {
                            size_t posZ = static_cast<size_t>(file.tellg());
                            for (size_t y = 0; y < size.y; ++y) {
                                file.read(data + index, size.x * numBytesPerElement);
                                file.seekg((_stride.y - size.x) * numBytesPerElement, std::ios::cur);
                                index += size.x;
                            }
                            file.seekg(posZ + (_stride.z * numBytesPerElement), std::ios::beg);
                        }
                    }
                }
                else {
                    // read each element separately
                    for (size_t z = 0; z < size.z; ++z) {
                        size_t posZ = static_cast<size_t>(file.tellg());
                        for (size_t y = 0; y < size.y; ++y) {
                            size_t posY = static_cast<size_t>(file.tellg());
                            for (size_t x = 0; x < size.x; ++x) {    
                                file.read(data + index, numBytesPerElement);
                                file.seekg((_stride.x - 1) * numBytesPerElement, std::ios::cur);
                                ++index;
                            }
                            file.seekg(posY + (_stride.y * numBytesPerElement), std::ios::beg);
                        }
                        file.seekg(posZ + (_stride.z * numBytesPerElement), std::ios::beg);
                    }
                }
            }

            file.close();

            // handle endianess
            EndianHelper::Endianness localEndianess = EndianHelper::getLocalEndianness();
            if (_endianess != localEndianess) {
                // This is not the most beautiful design, but unfortunately swapEndian needs to know the number of bytes at compiletime...
                switch (_type) {
154
                    case WeaklyTypedPointer::UINT8: // fallthrough
155
                    case WeaklyTypedPointer::INT8:
156
                        // nothing to do here.
157
158
                        break;

159
                    case WeaklyTypedPointer::UINT16: // fallthrough
160
161
                    case WeaklyTypedPointer::INT16: {
                        for (size_t i = 0; i < numElements; ++i)
162
                            EndianHelper::swapEndian<2>(data + (2*i));
163
164
165
                        break;
                        }

166
167
                    case WeaklyTypedPointer::UINT32: // fallthrough
                    case WeaklyTypedPointer::INT32: // fallthrough
168
169
                    case WeaklyTypedPointer::FLOAT: {
                        for (size_t i = 0; i < numElements; ++i)
170
                            EndianHelper::swapEndian<4>(data + (4*i));
171
172
173
174
175
176
177
178
179
180
                        break;
                        }

                    default:
                        tgtAssert(false, "Should not reach this!");
                        LERROR("Tried to swap endianess with unsupported number of bytes per element (" << numBytesPerElement << ")");
                        break;
                }
            }

181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
            if (_multichannelSideBySide && _parent->getNumChannels() > 1) {
                // there is no simple in-place solution, so we copy everything around...
                char* reordered = new char[numBytes];
                size_t numBytesPerChannel = numBytesPerElement / _parent->getNumChannels();
                for (size_t i = 0; i < numElements; ++i) {
                    for (size_t j = 0; j < _parent->getNumChannels(); ++j) {
                        size_t fromOffset = (i + j*numElements) * numBytesPerChannel;
                        size_t toOffset = (j + i*_parent->getNumChannels()) * numBytesPerChannel;
                        memcpy(reordered + toOffset, data + fromOffset, numBytesPerChannel);
                    }
                }

                std::swap(data, reordered);
                delete [] reordered;
            }
196

197
            return WeaklyTypedPointer(_type, _parent->getNumChannels(), static_cast<void*>(data));
198
199
200
        }
        else {
            LERROR("Could not open file " << _url << " for reading.");
201
            return WeaklyTypedPointer(_type, _parent->getNumChannels(), 0);
202
203
204
205
206
207
208
209
        }

    }

    tgt::svec3 ImageRepresentationDisk::getCanonicStride(const tgt::svec3& size) const {
        return tgt::svec3(0, size.x, size.x * size.y);
    }

210
    ImageRepresentationDisk* ImageRepresentationDisk::clone(ImageData* newParent) const {
211
        return ImageRepresentationDisk::create(newParent, _url, _type, _offset, _endianess, _stride);
212
213
214
215
216
217
218
219
220
221
222
223
224
225
    }

    size_t ImageRepresentationDisk::getLocalMemoryFootprint() const {
        return sizeof(*this);
    }

    size_t ImageRepresentationDisk::getVideoMemoryFootprint() const {
        return 0;
    }

    WeaklyTypedPointer::BaseType ImageRepresentationDisk::getBaseType() const {
        return _type;
    }

226
227
228
229
230
    ImageRepresentationDisk* ImageRepresentationDisk::tryConvertFrom(const AbstractImageRepresentation* source) {
        // no conversion availible for now
        return 0;
    }

231
}