itkreader.cpp 16.9 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
31
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
// 
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// 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
// 
// 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.
// 
// ================================================================================================

#include "itkreader.h"

#include "modules/itk/core/genericimagerepresentationitk.h"

#include "core/datastructures/imagedata.h"
#include "core/datastructures/genericimagerepresentationlocal.h"

32
33
#include "core/tools/stringutils.h"
#include <iomanip>
34
35
36
37
38
39
40
41


namespace campvis {

    const std::string ItkReader::loggerCat_ = "CAMPVis.modules.io.ItkReader";

    ItkReader::ItkReader()
        : AbstractProcessor()
42
43
44
        , p_url("sourcefile", "Source File", "", StringProperty::OPEN_FILENAME)
        , p_imageSeries("imageseries", "Image Series", false)
        , p_lastUrl("lastsourcefile", "Last Series File", "", StringProperty::OPEN_FILENAME)
45
46
47
        , p_targetImageID("OutoutImage", "Image Output Name", "itkImage", DataNameProperty::WRITE)
    {
        addProperty(p_url, INVALID_RESULT);
48
49
        addProperty(p_imageSeries, INVALID_RESULT);
        addProperty(p_lastUrl, INVALID_RESULT);
50
51
52
53
54
55
56
57
58
59
        addProperty(p_targetImageID, INVALID_RESULT);
    }

    ItkReader::~ItkReader() {

    }


    void ItkReader::updateResult(DataContainer& data)  {

60
61
62
63
64
65
66
67
68
69
        if (p_imageSeries.getValue()) {
            ReadImageSeries(data);
        }
        else {
            ReadImageDirect(data);
        }
        validate(INVALID_RESULT);
    }

    void ItkReader::ReadImageDirect(DataContainer& data) {
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
133
134
        typedef itk::ImageIOBase::IOComponentType ScalarPixelType;

        itk::ImageIOBase::Pointer imageIO =
            itk::ImageIOFactory::CreateImageIO(p_url.getValue().c_str(), itk::ImageIOFactory::ReadMode);

        if (imageIO.IsNotNull())
        {
            WeaklyTypedPointer wtp;

            imageIO->SetFileName(p_url.getValue());
            imageIO->ReadImageInformation();

            const ScalarPixelType pixelType = imageIO->GetComponentType();
            const size_t numDimensions = imageIO->GetNumberOfDimensions();

            LDEBUG("Reading Image with Reader " << imageIO->GetNameOfClass());
            LDEBUG("Pixel Type is " << imageIO->GetComponentTypeAsString(pixelType));
            LDEBUG("numDimensions: " << numDimensions);

            if (numDimensions > 3) {
                LERROR("Error: Dimensions higher than 3 not supported!");
                return;
            }

            itk::ImageIORegion ioRegion(numDimensions);
            itk::ImageIORegion::IndexType ioStart = ioRegion.GetIndex();
            itk::ImageIORegion::SizeType ioSize = ioRegion.GetSize();

            tgt::vec3 imageOffset(0.f);
            tgt::vec3 voxelSize(1.f);
            tgt::ivec3 size_i(1);

            //we assured above that numDimensions is < 3
            for (int i = 0; i < numDimensions; i++) {
                size_i[i] = imageIO->GetDimensions(i);
                imageOffset[i] = imageIO->GetOrigin(i);
                voxelSize[i] = imageIO->GetSpacing(i);
                ioStart[i] = 0;
                ioSize[i] = size_i[i];
            }

            tgt::svec3 size(size_i);
            size_t dimensionality = (size_i[2] == 1) ? ((size_i[1] == 1) ? 1 : 2) : 3;

            LDEBUG("Image Size is " << size);
            LDEBUG("Voxel Size is " << voxelSize);
            LDEBUG("Image Offset is " << imageOffset);
            LDEBUG("component size: " << imageIO->GetComponentSize());
            LDEBUG("components: " << imageIO->GetNumberOfComponents());
            LDEBUG("pixel type (string): " << imageIO->GetPixelTypeAsString(imageIO->GetPixelType())); // 'vector'
            LDEBUG("pixel type: " << imageIO->GetPixelType()); // '5'

            switch (pixelType) {
            case ScalarPixelType::CHAR:
                wtp._baseType = WeaklyTypedPointer::INT8; break;
            case ScalarPixelType::UCHAR:
                wtp._baseType = WeaklyTypedPointer::UINT8; break;
            case ScalarPixelType::SHORT:
                wtp._baseType = WeaklyTypedPointer::INT16; break;
            case ScalarPixelType::USHORT:
                wtp._baseType = WeaklyTypedPointer::UINT16; break;
            case ScalarPixelType::INT:
                wtp._baseType = WeaklyTypedPointer::INT32; break;
            case ScalarPixelType::UINT:
                wtp._baseType = WeaklyTypedPointer::UINT32; break;
135
136
            case ScalarPixelType::DOUBLE:
                LWARNING("Pixel Type is DOUBLE. Conversion to float may result in loss of precision!");
137
138
            case ScalarPixelType::FLOAT:
                wtp._baseType = WeaklyTypedPointer::FLOAT; break;
139
140


141
142
143
144
145
146
147
148
149
150
151
152
            default:
                LERROR("Error while loading ITK image: unsupported type: " << pixelType);
                return;
            }

            wtp._numChannels = imageIO->GetNumberOfComponents();

            //Setup the image region to read
            ioRegion.SetIndex(ioStart);
            ioRegion.SetSize(ioSize);
            imageIO->SetIORegion(ioRegion);

153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
            if (pixelType != ScalarPixelType::DOUBLE) {
                //Finally, allocate buffer and read the image data
                wtp._pointer = new uint8_t[imageIO->GetImageSizeInBytes()];
                imageIO->Read(wtp._pointer);
            }
            else {
                //convert float volume to double volume
                double * inputBuf = new double[imageIO->GetImageSizeInComponents()];
                wtp._pointer = new uint8_t[imageIO->GetImageSizeInComponents() * sizeof(float)];
                imageIO->Read(inputBuf);

                double * dptr = inputBuf;
                float * fptr = reinterpret_cast<float*>(wtp._pointer);
                for (int i = 0, s = imageIO->GetImageSizeInComponents(); i < s; ++i) {
                    *fptr = *dptr;
                    fptr++;
                    dptr++;
                }
                delete[] inputBuf;
            }
173
174
175
176
177
178
179
180
181
182
183
184

            ImageData* image = new ImageData(dimensionality, size, wtp._numChannels);
            ImageRepresentationLocal::create(image, wtp);

            image->setMappingInformation(ImageMappingInformation(size, imageOffset/* + p_imageOffset.getValue()*/, voxelSize /** p_voxelSize.getValue()*/));
            data.addData(p_targetImageID.getValue(), image);
        }
        else {
            LWARNING("Unable to create ImageIO Instance; No suitable reader found!");
        }
    }

185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414

    void ItkReader::ReadImageSeries(DataContainer& data) {
        typedef itk::ImageIOBase::IOComponentType ScalarPixelType;

        std::vector<std::string> imageFileNames = GetImageFileNames();

        if (!imageFileNames.size())
            return;

        itk::ImageIOBase::Pointer imageIO =
            itk::ImageIOFactory::CreateImageIO(imageFileNames[0].c_str(), itk::ImageIOFactory::ReadMode);

        const int numSlices = imageFileNames.size();

        if (imageIO.IsNotNull())
        {
            WeaklyTypedPointer wtp;

            imageIO->SetFileName(imageFileNames[0]);
            imageIO->ReadImageInformation();

            const ScalarPixelType pixelType = imageIO->GetComponentType();
            const size_t numDimensions = imageIO->GetNumberOfDimensions();

            LDEBUG("Reading Image with Reader " << imageIO->GetNameOfClass());
            LDEBUG("Pixel Type is " << imageIO->GetComponentTypeAsString(pixelType));
            LDEBUG("numDimensions: " << numDimensions);

            if (numDimensions > 3) {
                LERROR("Error: Dimensions higher than 3 not supported!");
                return;
            }

            itk::ImageIORegion ioRegion(numDimensions);
            itk::ImageIORegion::IndexType ioStart = ioRegion.GetIndex();
            itk::ImageIORegion::SizeType ioSize = ioRegion.GetSize();

            tgt::vec3 imageOffset(0.f);
            tgt::vec3 voxelSize(1.f);
            tgt::ivec3 size_i(1);

            //we assured above that numDimensions is < 3
            for (int i = 0; i < numDimensions; i++) {
                size_i[i] = imageIO->GetDimensions(i);
                imageOffset[i] = imageIO->GetOrigin(i);
                voxelSize[i] = imageIO->GetSpacing(i);
                ioStart[i] = 0;
                ioSize[i] = size_i[i];
            }

            tgt::svec3 size(size_i);
            size_t dimensionality = (size_i[2] == 1) ? ((size_i[1] == 1) ? 1 : 2) : 3;
            if (dimensionality > 2) {
                LERROR("Error: Cannot load image series with more than two dimensions!");
                return;
            }

            LDEBUG("Image Size is " << size);
            LDEBUG("Voxel Size is " << voxelSize);
            LDEBUG("Image Offset is " << imageOffset);
            LDEBUG("component size: " << imageIO->GetComponentSize());
            LDEBUG("components: " << imageIO->GetNumberOfComponents());
            LDEBUG("pixel type (string): " << imageIO->GetPixelTypeAsString(imageIO->GetPixelType()));
            LDEBUG("pixel type: " << imageIO->GetPixelType());

            switch (pixelType) {
            case ScalarPixelType::CHAR:
                wtp._baseType = WeaklyTypedPointer::INT8; break;
            case ScalarPixelType::UCHAR:
                wtp._baseType = WeaklyTypedPointer::UINT8; break;
            case ScalarPixelType::SHORT:
                wtp._baseType = WeaklyTypedPointer::INT16; break;
            case ScalarPixelType::USHORT:
                wtp._baseType = WeaklyTypedPointer::UINT16; break;
            case ScalarPixelType::INT:
                wtp._baseType = WeaklyTypedPointer::INT32; break;
            case ScalarPixelType::UINT:
                wtp._baseType = WeaklyTypedPointer::UINT32; break;
            case ScalarPixelType::DOUBLE:
                LWARNING("Pixel Type is DOUBLE. Conversion to float may result in loss of precision!");
            case ScalarPixelType::FLOAT:
                wtp._baseType = WeaklyTypedPointer::FLOAT; break;


            default:
                LERROR("Error while loading ITK image: unsupported type: " << pixelType);
                return;
            }

            wtp._numChannels = imageIO->GetNumberOfComponents();

            //Setup the image region to read
            ioRegion.SetIndex(ioStart);
            ioRegion.SetSize(ioSize);
            imageIO->SetIORegion(ioRegion);

            //allocate a temporary buffer if necessary
            double * inputBuf = (pixelType == ScalarPixelType::DOUBLE) ? new double[imageIO->GetImageSizeInComponents()] : nullptr;
            size_t sliceSize = (pixelType == ScalarPixelType::DOUBLE) ? imageIO->GetImageSizeInComponents() * sizeof(float) : imageIO->GetImageSizeInBytes();
            wtp._pointer = new uint8_t[numSlices * sliceSize];
            for(size_t idx=0; idx < numSlices; idx++) {
                itk::ImageIOBase::Pointer fileIO = imageIO;
                    //itk::ImageIOFactory::CreateImageIO(imageFileNames[idx].c_str(), itk::ImageIOFactory::ReadMode);
                fileIO->SetFileName(imageFileNames[idx]);
                fileIO->ReadImageInformation();
                fileIO->SetIORegion(ioRegion);

                size_t currentSliceSize = (pixelType == ScalarPixelType::DOUBLE) ? imageIO->GetImageSizeInComponents() * sizeof(float) : fileIO->GetImageSizeInBytes();
                if (currentSliceSize != sliceSize) {
                    LERROR("Image " << imageFileNames[idx] << " has different dimensionality or data type!");
                    delete wtp._pointer;
                    delete inputBuf;
                    wtp._pointer = nullptr;
                    return;
                }

                uint8_t * sliceBuffer = reinterpret_cast<uint8_t*>(wtp._pointer) + idx * sliceSize;

                if (pixelType != ScalarPixelType::DOUBLE) {
                    // directly read slice into buffer
                    fileIO->Read(sliceBuffer);
                }
                else {
                    //convert float volume to double volume
                    fileIO->Read(inputBuf);

                    double * dptr = inputBuf;
                    float * fptr = reinterpret_cast<float*>(sliceBuffer);
                    for (int i = 0, s = fileIO->GetImageSizeInComponents(); i < s; ++i) {
                        *fptr = static_cast<float>(*dptr);
                        fptr++;
                        dptr++;
                    }
                }
            }
            delete[] inputBuf;

            size[2] = numSlices;
            //series adds one dimension
            ImageData* image = new ImageData(dimensionality+1, size, wtp._numChannels);
            ImageRepresentationLocal::create(image, wtp);

            image->setMappingInformation(ImageMappingInformation(size, imageOffset/* + p_imageOffset.getValue()*/, voxelSize /** p_voxelSize.getValue()*/));
            data.addData(p_targetImageID.getValue(), image);
        }
        else {
            LWARNING("Unable to create ImageIO Instance; No suitable reader found!");
        }
    }


    std::vector<std::string> ItkReader::GetImageFileNames() {
        std::vector<std::string> filenames;

        std::string first = p_url.getValue();
        std::string last = p_lastUrl.getValue();

        //either one is empty - we cant do anything
        if (!first.size() || !last.size())
            return filenames;

        //this is a pretty naive scheme to find out the naming convention of the files
        //we first scan both filenames from left to find the first position where they diverge
        size_t diverge_left = 0;
        for (; diverge_left < first.size(); ++diverge_left) {
            //check if we are outside last string
            if (diverge_left >= last.size())
                break;

            if (first[diverge_left] != last[diverge_left]) break;
        }
        std::string fileBegin = first.substr(0, diverge_left);

        //strings are equal?
        if (diverge_left == first.size() && diverge_left == last.size()) {
            LWARNING("First and last filenames are equal!");
            filenames.push_back(first);
            return filenames;
        }


        // now we assume the diverging letters are a number, which we skip forward
        int digitFirstEnd = diverge_left;
        if (!isdigit(first[digitFirstEnd])) {
            LERROR("Cannot find sequence between first and last filename!");
            return filenames;
        }
        while (isdigit(first[digitFirstEnd])) digitFirstEnd++;
        int digitLastEnd = diverge_left;
        if (!isdigit(last[digitLastEnd])) {
            LERROR("Cannot find sequence between first and last filename!");
            return filenames;
        }
        while (isdigit(last[digitLastEnd])) digitLastEnd++;

        
        std::string fileEnd = first.substr(digitFirstEnd, std::string::npos);
        if (fileEnd != last.substr(digitLastEnd, std::string::npos)) {
            LERROR("Filename Tails mismatch!");
            return filenames;
        }

        //now, we have diverge_left and diverge_right
        LINFO("Diverge Left: " << diverge_left << "; digit end position: " << digitFirstEnd);
        LINFO("String begin: " << fileBegin << "; end: " << fileEnd);

        //the substrings of first and last filename are converted to numbers to get the numerical range
        int numFirst = StringUtils::fromString<int>(first.substr(diverge_left, digitFirstEnd - diverge_left));
        int numLast = StringUtils::fromString<int>(last.substr(diverge_left, digitLastEnd - diverge_left));

        LINFO("Indices from " << first.substr(diverge_left, digitFirstEnd - diverge_left) << "(" << numFirst << ") to " << numLast);


        //then we step through all numbers in the range and generate filenames by replacing the diverged substring
        //with the generated number
        for (int imgIdx = numFirst; imgIdx <= numLast; ++imgIdx) {
            std::stringstream s;
            if (first.size() == last.size()) {
                s << std::setfill('0');
                s << std::setw(digitFirstEnd - diverge_left);
            }
            s.clear();
            s << imgIdx;
            filenames.push_back(fileBegin + s.str() + fileEnd);
            //LINFO(filenames.back());
        }


        return filenames;
    }
415
}