-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathImageLoader.cpp
More file actions
389 lines (299 loc) · 10.1 KB
/
ImageLoader.cpp
File metadata and controls
389 lines (299 loc) · 10.1 KB
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
32
33
34
35
36
37
38
39
40
41
42
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
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
184
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
////////////////////////////////////////
// ImageLoader.cpp
////////////////////////////////////////
#define THREAD_COUNT 6
#ifdef WIN32
#define NOMINMAX
#include <Shlwapi.h>
#endif
#include <dcmtk/dcmimgle/dcmimage.h>
#include <dcmtk/dcmdata/dctk.h>
#include <thread>
#include <algorithm>
#define STB_IMAGE_IMPLEMENTATION
#include <stb-master/stb_image.h>
#include "ImageLoader.h"
using namespace std;
////////////////////////////////////////////////////////////////////////////////
// File Helpers
string GetExt(const string& path) {
size_t k = path.rfind('.');
if (k == string::npos) return "";
return path.substr((int)k + 1);
}
string GetName(const string& path) {
char const* str = path.c_str();
int f = 0;
int l = 0;
for (int i = 0; i < path.length(); i++) {
if (str[i] == '\\' || str[i] == '/')
f = i + 1;
else if (str[i] == '.')
l = i;
}
return path.substr(f, l - f);
}
string GetFullPath(const string& str) {
#ifdef WIN32
char buf[MAX_PATH];
if (GetFullPathName(str.c_str(), 256, buf, nullptr) == 0) {
printf("Failed to get full file path of %s (%d)", str.c_str(), GetLastError());
return str;
}
return string(buf);
#endif
}
void GetFiles(const string& path, vector<string>& files) {
#ifdef WIN32
string d = path + "\\*";
WIN32_FIND_DATAA ffd;
HANDLE hFind = FindFirstFile(d.c_str(), &ffd);
if (hFind == INVALID_HANDLE_VALUE) {
assert(false);
return;
}
do {
if (ffd.cFileName[0] == L'.') continue;
string c = path + "\\" + ffd.cFileName;
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
// file is a directory
}
else {
string ext = GetExt(c);
if (ext == "dcm" || ext == "raw" || ext == "png")
files.push_back(GetFullPath(c));
}
} while (FindNextFileA(hFind, &ffd) != 0);
FindClose(hFind);
#endif
}
////////////////////////////////////////////////////////////////////////////////
// Texture Helpers
GLuint InitTexture2D(unsigned int width, unsigned int height,
GLenum internalFormat, GLenum format, GLenum type, GLenum filter, void* data) {
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, type, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
glBindTexture(GL_TEXTURE_2D, 0);
return textureID;
}
GLuint InitTexture3D(unsigned int width, unsigned int height, unsigned int depth,
GLenum internalFormat, GLenum format, GLenum type, GLenum filter, void* data) {
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_3D, textureID);
glTexImage3D(GL_TEXTURE_3D, 0, internalFormat, width, height, depth, 0, format, type, data);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, filter);
glBindTexture(GL_TEXTURE_3D, 0);
return textureID;
}
////////////////////////////////////////////////////////////////////////////////
// Dicom Readers
struct Slice {
DicomImage* image;
double location;
};
void ReadDicomSlice(DicomImage*& img, double& x, const string& file, double& spacingX, double& spacingY, double& thickness) {
OFCondition cnd;
DcmFileFormat fileFormat;
assert((cnd = fileFormat.loadFile(file.c_str())).good());
DcmDataset* dataset = fileFormat.getDataset();
double sx;
double sy;
double th;
cnd = dataset->findAndGetFloat64(DCM_PixelSpacing, sx, 0);
cnd = dataset->findAndGetFloat64(DCM_PixelSpacing, sy, 1);
cnd = dataset->findAndGetFloat64(DCM_SliceThickness, th, 0);
spacingX = std::max(sx, spacingX);
spacingY = std::max(sy, spacingY);
thickness = std::max(th, thickness);
cnd = dataset->findAndGetFloat64(DCM_SliceLocation, x, 0);
img = new DicomImage(file.c_str());
assert(img != NULL);
assert(img->getStatus() == EIS_Normal);
}
void ReadDicomImage(DicomImage* img, uint16_t* slice, int w, int h) {
img->setMinMaxWindow();
uint16_t* pixelData = (uint16_t*)img->getOutputData(16);
memcpy(slice, pixelData, sizeof(uint16_t) * w * h);
}
void ReadDicomImages(uint16_t* data, vector<Slice>& images, int start, int end, int w, int h) {
for (int sliceID = start; sliceID < end; sliceID++) {
if (sliceID >= (int)images.size())
break;
//printf("slice: %d, adding: %d\n", sliceID, (sliceID * w * h));
ReadDicomImage(images[sliceID].image, data + sliceID * w * h, w, h);
}
}
////////////////////////////////////////////////////////////////////////////////
// Dicom Loaders
GLuint ImageLoader::loadDicomImage() {
printf("Loading Dicom file: %s\n", _path.c_str());
// Get information
DicomImage* image = nullptr;
double x = 0.0;
double spacingX = 0.0;
double spacingY = 0.0;
double thickness = 0.0;
ReadDicomSlice(image, x, _path, spacingX, spacingY, thickness);
unsigned int w = image->getWidth();
unsigned int h = image->getHeight();
unsigned int d = 1;
_imgDims.x = w;
_imgDims.y = h;
_imgDims.z = d;
// volume size in meters
_size.x = .001f * (float)spacingX * w;
_size.y = .001f * (float)spacingY * h;
_size.z = .001f * (float)thickness;
_imageData = new uint16_t[w * h * d];
memset(_imageData, 0xFF, w * h * d * sizeof(uint16_t));
ReadDicomImage(image, _imageData, w, h);
// get the raw data
//_dataTest = (uint16_t*)_imageData;
//_dataTest = (uint16_t*)image->getOutputData(16);
//_dataTest = (uint16_t*)image->getInterData();
// Get the Min/Max values for the Dicom Image
int mode = 0; // mode = used pixel values vs 1 = possible pixelvalues
image->getMinMaxValues(_minPixelVal, _maxPixelVal, mode);
GLuint tex = InitTexture2D(w, h, GL_R16, GL_RED, GL_UNSIGNED_SHORT, GL_LINEAR, _imageData);
//delete[] data;
cerr << "Dicom loaded: (" << tex << ")\n\n";
return tex;
}
GLuint ImageLoader::loadDicomVolume(const vector<string>& files) {
std::cerr << "Loading DICOM Folder\n";
vector<Slice> images;
// Get information
double spacingX = 0.0;
double spacingY = 0.0;
double thickness = 0.0;
for (unsigned int i = 0; i < (int)files.size(); i++) {
DicomImage* img;
double x;
ReadDicomSlice(img, x, files[i], spacingX, spacingY, thickness);
images.push_back({ img, x });
}
std::sort(images.begin(), images.end(), [](const Slice& a, const Slice& b) {
return a.location < b.location;
});
unsigned int w = images[0].image->getWidth();
unsigned int h = images[0].image->getHeight();
unsigned int d = (unsigned int)images.size();
_imgDims.x = w;
_imgDims.y = h;
_imgDims.z = d;
// volume size in meters
_size.x = .001f * (float)spacingX * w;
_size.y = .001f * (float)spacingY * h;
_size.z = .001f * (float)thickness * images.size();
printf("%fm x %fm x %fm\n", _size.x, _size.y, _size.z);
_imageData = new uint16_t[w * h * d];
memset(_imageData, 0xFFFF, w * h * d * sizeof(uint16_t));
if (THREAD_COUNT > 1) {
printf("reading %d slices\n", d);
vector<thread> threads;
int s = ((int)images.size() + THREAD_COUNT - 1) / THREAD_COUNT;
// each thread processes s slices
for (int i = 0; i < (int)images.size(); i += s) {
threads.push_back(thread(ReadDicomImages, _imageData, images, i, i + s, w, h));
}
for (int i = 0; i < (int)threads.size(); i++) {
threads[i].join();
}
}
else {
ReadDicomImages(_imageData, images, 0, (int)images.size(), w, h);
}
GLuint tex = InitTexture3D(w, h, d, GL_R16, GL_RED, GL_UNSIGNED_SHORT, GL_LINEAR, _imageData);
printf("Dicom Volume loaded: (%d)\n\n", tex);
return tex;
}
////////////////////////////////////////////////////////////////////////////////
// Image/Volume Loaders
GLuint ImageLoader::loadImage() {
#ifdef WIN32
if (!PathFileExists(_path.c_str())) {
#endif
printf("%s Does not exist!\n", _path.c_str());
return 0;
}
string ext = GetExt(_path.c_str());
if (ext == "dcm")
return loadDicomImage();
else
return 0;
}
GLuint ImageLoader::loadVolume() {
#ifdef WIN32
if (!PathFileExists(_path.c_str())) {
#endif
printf("%s Does not exist!\n", _path.c_str());
return 0;
}
vector<string> files;
GetFiles(_path, files);
if (files.size() == 0) return 0;
string ext = GetExt(files[0]);
if (ext == "dcm")
return loadDicomVolume(files);
else
return 0;
}
GLuint ImageLoader::loadMask() {
std::cerr << "Loading Mask Folder\n";
#ifdef WIN32
if (!PathFileExists(_maskPath.c_str())) {
#endif
printf("%s Does not exist!\n\n", _maskPath.c_str());
return 0;
}
// get the mask files in sorted order
vector<string> files;
GetFiles(_maskPath, files);
std::sort(files.data(), files.data() + files.size(), [](const string& a, const string& b) {
return atoi(GetName(a).c_str()) > atoi(GetName(b).c_str());
});
int width, height, channel;
unsigned char* maskData = new unsigned char[_imgDims.x * _imgDims.y * _imgDims.z];
memset(maskData, 0, _imgDims.x * _imgDims.y * _imgDims.z * sizeof(unsigned char));
for (int currSlice = 0; currSlice < files.size(); currSlice++) {
//stbi_set_flip_vertically_on_load(true);
unsigned char* pixelData = stbi_load(files.at(currSlice).c_str(), &width, &height, &channel, STBI_grey);
unsigned char* slice = maskData + (currSlice * _imgDims.x * _imgDims.y);
memcpy(slice, pixelData, sizeof(unsigned char) * _imgDims.x * _imgDims.y);
stbi_image_free(pixelData);
}
// make volume texture for the mask data
GLuint tex = InitTexture3D(_imgDims.x, _imgDims.y, _imgDims.z, GL_R8, GL_RED, GL_UNSIGNED_BYTE, GL_LINEAR, maskData);
printf("Masks loaded: (%d) - (%d, %d, %d)\n\n", tex, _imgDims.x, _imgDims.y, _imgDims.z);
delete[] maskData;
return tex;
}
////////////////////////////////////////////////////////////////////////////////
// Constructor/Destructor
ImageLoader::ImageLoader(string& path, string& maskpath, bool isImg) {
_path = path; _maskPath = maskpath;
if (isImg) {
cerr << "Load Image\n";
_textureID = loadImage();
}
else {
cerr << "Load Volume\n";
_textureID = loadVolume();
_maskID = loadMask();
}
}
ImageLoader::~ImageLoader() {
delete[] _imageData;
}
////////////////////////////////////////////////////////////////////////////////