-
Notifications
You must be signed in to change notification settings - Fork 0
/
volume.cpp
111 lines (102 loc) · 2.83 KB
/
volume.cpp
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
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
#include "volume.h"
// Release volume data if exists
void CVolume::clean()
{
if(m_data)
{
free(m_data);
m_data = nullptr;
}
}
// Constructor
CVolume::CVolume() : m_data(nullptr)
{
}
// Destructor
CVolume::~CVolume()
{
clean();
}
// Read a RAW/plain format (not ASCII-based) volume
// @param name name of the file to open.
// @param slices number of slices of the volume
// @param rows number of slices of the volume
// @param cols number of slices of the volume
//
// @return true if is valid, otherwise false
bool CVolume::readFile(std::string name, const int rows, const int cols, const int slices)
{
m_nrows = rows;
m_ncols = cols;
m_nslices = slices;
clean();
m_data = malloc3D(m_nrows, m_ncols, m_nslices);
FILE* open;
if(!(open = fopen(name.c_str(),"rb")))return false; //binary files
for(int k = 0; k < m_nslices; k++)
for(int i = 0; i < m_nrows; i++)
for(int j = 0; j < m_ncols; j++)
fread(&m_data[k][i][j],sizeof(uchar),1,open);
fclose(open);
return true;
}
// Obtain a slice [i][INDEX][j] from volume. It is not a "coronal" view, it depends of the volume orientation
// @param iIndex number of slice to extract from volume
//
// @return a RGB (gray scale) of a coronal slice
puchar CVolume::getCoronalSlice(int iIndex)
{
puchar data = (puchar)malloc(sizeof(uchar)*m_nslices*m_ncols * 3);
int x = 0;
for (int k = 0; k < m_nslices; k++)
for (int j = 0; j < m_ncols; j++)
{
data[x + 0] = m_data[k][iIndex][j];
data[x + 1] = m_data[k][iIndex][j];
data[x + 2] = m_data[k][iIndex][j];
x += 3;
}
_ASSERT(x == m_nslices * m_ncols * 3);
return data;
}
// Obtain a slice [i][j][INDEX] from volume. It is not a "sagittal" view, it depends of the volume orientation
// @param iIndex number of slice to extract from volume
//
// @return a RGB (gray scale) of a sagittal slice
puchar CVolume::getSagittalSlice(int iIndex)
{
puchar data = (puchar)malloc(sizeof(uchar)*m_nslices*m_nrows * 3);
int x = 0;
for (int k = 0; k < m_nslices; k++)
for (int i = 0; i < m_nrows; i++)
{
data[x + 0] = m_data[k][i][iIndex];
data[x + 1] = m_data[k][i][iIndex];
data[x + 2] = m_data[k][i][iIndex];
x += 3;
}
_ASSERT(x == m_nslices * m_nrows * 3);
return data;
}
// Obtain a slice [INDEX][i][j] from volume. It is not a "axial" view, it depends of the volume orientation
// @param iIndex number of slice to extract from volume
//
// @return a RGB (gray scale) of a axial slice
puchar CVolume::getAxialSlice(int iIndex)
{
puchar data = (puchar)malloc(sizeof(uchar)*m_nrows*m_ncols*3);
int x = 0;
for (int i = 0; i < m_nrows; i++)
for (int j = 0; j < m_ncols; j++)
{
data[x + 0] = m_data[iIndex][i][j];
data[x + 1] = m_data[iIndex][i][j];
data[x + 2] = m_data[iIndex][i][j];
x += 3;
}
_ASSERT(x == m_nrows * m_ncols * 3);
return data;
}