imagedatadisk.cpp 8.36 KB
Newer Older
schultezub's avatar
schultezub committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "imagedatadisk.h"

namespace TUMVis {
    const std::string ImageDataDisk::loggerCat_ = "TUMVis.core.datastructures.ImageDataDisk";

    ImageDataDisk::ImageDataDisk(const std::string& url, size_t dimensionality, const tgt::svec3& size, WeaklyTypedPointer::PointerType type, size_t offset /*= 0*/, EndianHelper::Endianness endianness /*= EndianHelper::LITTLE_ENDIAN*/, const tgt::svec3& stride /*= tgt::svec2::zero */)
        : ImageData(dimensionality, size)
        , _url(url)
        , _offset(offset)
        , _endianess(endianness)
        , _stride(stride)
    {
    }

    ImageDataDisk::~ImageDataDisk() {

    }

schultezub's avatar
schultezub committed
19
20
    ImageDataDisk* ImageDataDisk::getSubImage(const tgt::svec3& llf, const tgt::svec3& urb) const {
        tgtAssert(tgt::hand(tgt::lessThan(llf, urb)), "Coordinates in LLF must be component-wise smaller than the ones in URB!");
schultezub's avatar
schultezub committed
21
22
23
24

        tgt::svec3 newSize = urb - llf;
        if (newSize == _size) {
            // nothing has changed, just provide a copy:
schultezub's avatar
schultezub committed
25
            return clone();
schultezub's avatar
schultezub committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
        }

        size_t newOffset = _offset + WeaklyTypedPointer::numBytes(_type) * llf.x;
        // the stride doesn't change!
        tgt::svec3 newStride = (_stride == tgt::svec3::zero) ? getCanonicStride(_size) : _stride;
        return new ImageDataDisk(_url, _dimensionality, newSize,_type, newOffset, _endianess, newStride);
    }

    TUMVis::WeaklyTypedPointer ImageDataDisk::getImageData() const {
        size_t numElements = tgt::hmul(_size);
        size_t numBytesPerElement = WeaklyTypedPointer::numBytes(_type);
        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()) {
schultezub's avatar
schultezub committed
42
            size_t fileSize = static_cast<size_t>(file.tellg());
schultezub's avatar
schultezub committed
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
103
104
105
106
107
108
109
110
111
112
            if (fileSize < numBytes) {
                LERROR("File is smaller than expected.");
                return WeaklyTypedPointer(_type, 0);
            }

            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) {
schultezub's avatar
schultezub committed
113
114
                    case WeaklyTypedPointer::UINT8:
                    case WeaklyTypedPointer::INT8:
schultezub's avatar
schultezub committed
115
116
117
118
                        for (size_t i = 0; i < numElements; ++i)
                            data[i] = EndianHelper::swapEndian(data[i]);
                        break;

schultezub's avatar
schultezub committed
119
120
121
                    case WeaklyTypedPointer::UINT16:
                    case WeaklyTypedPointer::INT16: {
                        int16_t* tmp = reinterpret_cast<int16_t*>(data);
schultezub's avatar
schultezub committed
122
123
124
125
126
                        for (size_t i = 0; i < numElements; ++i)
                            tmp[i] = EndianHelper::swapEndian(tmp[i]);
                        break;
                        }

schultezub's avatar
schultezub committed
127
128
129
                    case WeaklyTypedPointer::UINT32:
                    case WeaklyTypedPointer::INT32: {
                        int32_t* tmp = reinterpret_cast<int32_t*>(data);
schultezub's avatar
schultezub committed
130
131
132
133
134
135
136
137
138
139
140
141
                        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;
                        }

schultezub's avatar
schultezub committed
142
143
144
                    case WeaklyTypedPointer::UINT64:
                    case WeaklyTypedPointer::INT64: {
                        int64_t* tmp = reinterpret_cast<int64_t*>(data);
schultezub's avatar
schultezub committed
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
                        for (size_t i = 0; i < numElements; ++i)
                            tmp[i] = EndianHelper::swapEndian(tmp[i]);
                        break;
                        }

                    case WeaklyTypedPointer::DOUBLE: {
                        double* tmp = reinterpret_cast<double*>(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;
                }
            }


            return WeaklyTypedPointer(_type, static_cast<void*>(data));
        }
        else {
            LERROR("Could not open file " << _url << " for reading.");
            return WeaklyTypedPointer(_type, 0);
        }

    }

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

schultezub's avatar
schultezub committed
178
    ImageDataDisk* ImageDataDisk::clone() const {
schultezub's avatar
schultezub committed
179
180
181
        return new ImageDataDisk(_url, _dimensionality, _size, _type, _offset, _endianess, _stride);
    }
}