imagerepresentationdisk.cpp 10.4 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
// ================================================================================================
// 
// 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 "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
    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 */)
39
        : GenericAbstractImageRepresentation<ImageRepresentationDisk>(parent)
40
41
42
43
44
45
46
47
48
49
50
        , _url(url)
        , _offset(offset)
        , _type(type)
        , _endianess(endianness)
        , _stride(stride)
    {
    }

    ImageRepresentationDisk::~ImageRepresentationDisk() {
    }

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

54
        const tgt::svec3& size = getSize();
55
        tgt::svec3 newSize = urb - llf;
56
        if (newSize == size) {
57
            // nothing has changed, just provide a copy:
58
            return clone(parent);
59
60
        }

61
        size_t newOffset = _offset + WeaklyTypedPointer::numBytes(_type, _parent->getNumChannels()) * (llf.x + llf.y * size.y + llf.z * size.x * size.y);
62
63
        // the stride doesn't change!
        tgt::svec3 newStride = (_stride == tgt::svec3::zero) ? getCanonicStride(getSize()) : _stride;
64
        return new ImageRepresentationDisk(parent, _url, _type, newOffset, _endianess, newStride);
65
66
67
68
69
    }

    campvis::WeaklyTypedPointer ImageRepresentationDisk::getImageData() const {
        const tgt::svec3& size = getSize();
        size_t numElements = tgt::hmul(size);
70
        size_t numBytesPerElement = WeaklyTypedPointer::numBytes(_type, _parent->getNumChannels());
71
72
73
74
75
76
77
78
        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.");
79
                return WeaklyTypedPointer(_type, _parent->getNumChannels(), 0);
80
81
82
83
84
85
86
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
            }

            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) {
                    case WeaklyTypedPointer::UINT8:
                    case WeaklyTypedPointer::INT8:
                        for (size_t i = 0; i < numElements; ++i)
                            data[i] = EndianHelper::swapEndian(data[i]);
                        break;

                    case WeaklyTypedPointer::UINT16:
                    case WeaklyTypedPointer::INT16: {
                        int16_t* tmp = reinterpret_cast<int16_t*>(data);
                        for (size_t i = 0; i < numElements; ++i)
                            tmp[i] = EndianHelper::swapEndian(tmp[i]);
                        break;
                        }

                    case WeaklyTypedPointer::UINT32:
                    case WeaklyTypedPointer::INT32: {
                        int32_t* tmp = reinterpret_cast<int32_t*>(data);
                        for (size_t i = 0; i < numElements; ++i)
                            tmp[i] = EndianHelper::swapEndian(tmp[i]);
                        break;
                        }

                    case WeaklyTypedPointer::FLOAT: {
                        float* tmp = reinterpret_cast<float*>(data);
                        for (size_t i = 0; i < numElements; ++i)
                            tmp[i] = EndianHelper::swapEndian(tmp[i]);
                        break;
                        }

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


184
            return WeaklyTypedPointer(_type, _parent->getNumChannels(), static_cast<void*>(data));
185
186
187
        }
        else {
            LERROR("Could not open file " << _url << " for reading.");
188
            return WeaklyTypedPointer(_type, _parent->getNumChannels(), 0);
189
190
191
192
193
194
195
196
        }

    }

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

197
198
    ImageRepresentationDisk* ImageRepresentationDisk::clone(ImageData* newParent) const {
        return new ImageRepresentationDisk(newParent, _url, _type, _offset, _endianess, _stride);
199
200
201
202
203
204
205
206
207
208
209
210
211
212
    }

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

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

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

213
214
215
216
217
    ImageRepresentationDisk* ImageRepresentationDisk::tryConvertFrom(const AbstractImageRepresentation* source) {
        // no conversion availible for now
        return 0;
    }

218
}