imagerepresentationdisk.cpp 10 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
// 
// ================================================================================================

#include "imagerepresentationdisk.h"
26

27
28
#include "tgt/filesystem.h"

29

30
31
32
namespace campvis {
    const std::string ImageRepresentationDisk::loggerCat_ = "CAMPVis.core.datastructures.ImageRepresentationDisk";

33
34
    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);
35
36
37
38
        toReturn->addToParent();
        return toReturn;
    }

39
    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*/)
40
        : GenericAbstractImageRepresentation<ImageRepresentationDisk>(parent)
41
42
43
44
45
        , _url(url)
        , _offset(offset)
        , _type(type)
        , _endianess(endianness)
        , _stride(stride)
46
        , _multichannelSideBySide(multichannelSideBySide)
47
48
49
50
51
52
53
54
55
    {
    }

    ImageRepresentationDisk::~ImageRepresentationDisk() {
    }

    campvis::WeaklyTypedPointer ImageRepresentationDisk::getImageData() const {
        const tgt::svec3& size = getSize();
        size_t numElements = tgt::hmul(size);
56
        size_t numBytesPerElement = WeaklyTypedPointer::numBytes(_type, _parent->getNumChannels());
57
58
59
60
61
62
63
64
        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.");
65
                return WeaklyTypedPointer(_type, _parent->getNumChannels(), 0);
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
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
            }

            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) {
133
                    case WeaklyTypedPointer::UINT8: // fallthrough
134
                    case WeaklyTypedPointer::INT8:
135
                        // nothing to do here.
136
137
                        break;

138
                    case WeaklyTypedPointer::UINT16: // fallthrough
139
140
                    case WeaklyTypedPointer::INT16: {
                        for (size_t i = 0; i < numElements; ++i)
141
                            EndianHelper::swapEndian<2>(data + (2*i));
142
143
144
                        break;
                        }

145
146
                    case WeaklyTypedPointer::UINT32: // fallthrough
                    case WeaklyTypedPointer::INT32: // fallthrough
147
148
                    case WeaklyTypedPointer::FLOAT: {
                        for (size_t i = 0; i < numElements; ++i)
149
                            EndianHelper::swapEndian<4>(data + (4*i));
150
151
152
153
154
155
156
157
158
159
                        break;
                        }

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

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
            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;
            }
175

176
            return WeaklyTypedPointer(_type, _parent->getNumChannels(), static_cast<void*>(data));
177
178
179
        }
        else {
            LERROR("Could not open file " << _url << " for reading.");
180
            return WeaklyTypedPointer(_type, _parent->getNumChannels(), 0);
181
182
183
184
185
186
187
188
        }

    }

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

189
    ImageRepresentationDisk* ImageRepresentationDisk::clone(ImageData* newParent) const {
190
        return ImageRepresentationDisk::create(newParent, _url, _type, _offset, _endianess, _stride);
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    }

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

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

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

205
206
207
208
209
    ImageRepresentationDisk* ImageRepresentationDisk::tryConvertFrom(const AbstractImageRepresentation* source) {
        // no conversion availible for now
        return 0;
    }

210
}