-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathguiimage.cpp
347 lines (318 loc) · 10.7 KB
/
guiimage.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
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
#include "gdcm.h"
#include "guiimage.h"
#include "tool.h"
#include <NiftiHeader.hpp>
#include <QDebug>
#include <QDebug>
#include <QPixmap>
#include <QRgb>
#include <QTime>
#include <QTime>
GuiImage::GuiImage( QString fname, QObject *parent ) : QObject( parent ), image( GDCM::OpenGImage(
fname.toStdString( ) ) ), m_fileName(
fname ) {
transform.resize( 4 );
m_equalizeHistogram = false;
bounding.insert( 0, 4, Bial::BBox( Bial::Point3D( 0, 0, 0 ), Bial::Point3D( image.size( 0 ), image.size( 1 ), 1 ) ) );
m_currentSlice.insert( 0, 4, 0 );
m_max = image.Maximum( );
if( image.Dims( ) == 3 ) {
COMMENT( "NIfTI image detected.", 2 );
m_modality = Modality::BW3D;
if( Bial::NiftiHeader::IsNiftiFile( fname.toStdString( ) ) ) {
{
COMMENT( "Generating Axial affine transform.", 2 );
transform[ 0 ].Rotate( 90.0, Bial::FastTransform::X ).Rotate( 90.0, Bial::FastTransform::Y );
transform[ 0 ].Scale( 1, -1, -1 );
updateBoundings( 0 );
}
{
COMMENT( "Generating Coronal affine transform.", 2 );
transform[ 1 ].Rotate( 180.0, Bial::FastTransform::Z ).Rotate( 90.0, Bial::FastTransform::Y );
transform[ 1 ].Scale( -1, 1, 1 );
updateBoundings( 1 );
}
{
COMMENT( "Generating Sagittal affine transform.", 2 );
transform[ 2 ].Rotate( 180.0, Bial::FastTransform::Z );
updateBoundings( 2 );
}
}
else {
{
COMMENT( "Generating Axial affine transform.", 2 );
transform[ 0 ].Rotate( 90.0, Bial::FastTransform::X ).Rotate( 90.0, Bial::FastTransform::Y );
updateBoundings( 0 );
}
{
COMMENT( "Generating Coronal affine transform.", 2 );
transform[ 1 ].Rotate( 90.0, Bial::FastTransform::Y );
updateBoundings( 1 );
}
{
COMMENT( "Generating Sagittal affine transform.", 2 );
transform[ 2 ].Rotate( 180.0, Bial::FastTransform::Z );
updateBoundings( 2 );
}
}
cachedPixmaps.resize( 3 );
needUpdate.insert( 0, 3, true );
for( int view = 0; view < m_currentSlice.size( ); ++view ) {
setCurrentSlice( view, depth( view ) / 2 );
}
m_currentToolPos = -1;
}
else if( ( image.Dims( ) == 2 ) && ( image.Channels( ) == 3 ) ) {
COMMENT( "PPM image detected.", 2 );
m_modality = Modality::RGB2D;
Bial::BBox box( Bial::Point3D( 0, 0, 0 ), Bial::Point3D( image.size( 0 ), image.size( 1 ), 1 ) );
bounding[ 0 ] = box;
cachedPixmaps.resize( 4 );
needUpdate.insert( 0, 4, true );
}
else if( ( image.Dims( ) == 2 ) && ( image.Channels( ) == 1 ) ) {
COMMENT( "Gray image detected.", 2 );
m_modality = Modality::BW2D;
Bial::BBox box( Bial::Point3D( 0, 0, 0 ), Bial::Point3D( image.size( 0 ), image.size( 1 ), 1 ) );
bounding[ 0 ] = box;
cachedPixmaps.resize( 1 );
needUpdate.push_back( true );
}
COMMENT( "Computing equalizaztion transform.", 2 );
histogram = Bial::Signal::ZeroStartHistogram( image );
Bial::Signal levi = histogram;
levi[ 0 ] = 0;
levi.Equalize( );
equalization.resize( levi.size( ) );
for( size_t val = 0; val < levi.size( ); ++val ) {
equalization[ val ] = std::round( levi[ val ] );
}
COMMENT( "Computing equalized histogram.", 2 );
equalized = Bial::Signal( histogram.size( ), 0.0, 1.0 );
for( size_t val = 0; val < equalized.size( ); ++val ) {
equalized[ equalization[ val ] ] = histogram[ val ];
}
COMMENT( "Image " << fileName( ).toStdString( ) << " size = (" << width( 0 ) << ", " << heigth( 0 ) << ", " <<
depth( 0 ) << ")", 0 );
}
GuiImage::~GuiImage( ) {
qDeleteAll( tools );
}
Tool* GuiImage::currentTool( ) {
if( tools.isEmpty( ) ) {
return( nullptr );
}
if( m_currentToolPos >= ( size_t ) tools.size( ) ) {
m_currentToolPos = 0;
}
return( tools.at( m_currentToolPos ) );
}
Modality GuiImage::modality( ) {
return( m_modality );
}
QString GuiImage::fileName( ) {
return( m_fileName );
}
QPixmap GuiImage::getSlice( size_t view ) {
size_t slice = currentSlice( view );
COMMENT( "GET SLICE: image = " << m_fileName.toStdString( ) << ", axis = " << view << ", slice = " << slice, 2 );
if( needUpdate[ view ] ) {
/*
* QTime timer;
* timer.start( );
*/
if( slice >= depth( view ) ) {
throw( std::out_of_range( BIAL_ERROR( QString( "Slice is out of range. Expected < %1" ).arg( depth( view ) ).
toStdString( ) ) ) );
}
const size_t xsize = width( view );
const size_t ysize = heigth( view );
QImage res( xsize, ysize, QImage::Format_ARGB32 );
double factor = 255.0 / ( double ) m_max;
const Bial::FastTransform &transf = transform[ view ];
if( ( modality( ) == Modality::BW3D ) || ( modality( ) == Modality::BW2D ) ) {
COMMENT( "Generating BW view.", 2 );
#pragma omp parallel for default(none) shared(transf, res) firstprivate(slice, factor)
for( size_t y = 0; y < ysize; ++y ) {
QRgb *scanLine = ( QRgb* ) res.scanLine( y );
for( size_t x = 0; x < xsize; ++x ) {
int pixel = 0;
int xx, yy, zz;
transf( x, y, slice, &xx, &yy, &zz );
pixel = image( xx, yy, zz );
if( m_equalizeHistogram ) {
pixel = equalization[ pixel ];
}
pixel *= factor;
scanLine[ x ] = qRgb( pixel, pixel, pixel );
}
}
}
else if( modality( ) == Modality::RGB2D ) {
if( needUpdate[ 0 ] ) {
COMMENT( "Generating RGB view.", 2 );
size_t disp1 = image.size( 0 ) * image.size( 1 );
size_t disp2 = disp1 * 2;
#pragma omp parallel for default(none) shared(transf, res) firstprivate(slice, factor, disp1, disp2)
for( size_t y = 0; y < ysize; ++y ) {
QRgb *scanLine = ( QRgb* ) res.scanLine( y );
for( size_t x = 0; x < xsize; ++x ) {
int xx, yy, zz;
transf( x, y, slice, &xx, &yy, &zz );
size_t pos = image.Position( xx, yy );
int r = image[ pos ];
int g = image[ pos + disp1 ];
int b = image[ pos + disp2 ];
if( m_equalizeHistogram ) {
r = equalization[ r ];
g = equalization[ g ];
b = equalization[ b ];
}
scanLine[ x ] = qRgb( r * factor, g * factor, b * factor );
}
}
cachedPixmaps[ 0 ] = QPixmap::fromImage( res );
needUpdate[ 0 ] = false;
}
if( view > 0 ) {
res = cachedPixmaps[ 0 ].toImage( );
int r( view == 1 ), g( view == 2 ), b( view == 3 );
for( size_t y = 0; y < ysize; ++y ) {
QRgb *scanLine = ( QRgb* ) res.scanLine( y );
for( size_t x = 0; x < xsize; ++x ) {
QRgb clr = scanLine[ x ];
scanLine[ x ] = qRgb( qRed( clr ) * r, qGreen( clr ) * g, qBlue( clr ) * b );
}
}
}
}
if( needUpdate[ view ] ) {
cachedPixmaps[ view ] = QPixmap::fromImage( res );
needUpdate[ view ] = false;
}
/* qDebug( ) << "Elapsed: " << timer.elapsed( ); */
}
return( cachedPixmaps[ view ] );
}
size_t GuiImage::width( size_t view = 0 ) {
return( abs( round( bounding.at( view ).pMax.x ) ) );
}
size_t GuiImage::heigth( size_t view = 0 ) {
return( abs( round( bounding.at( view ).pMax.y ) ) );
}
size_t GuiImage::depth( size_t view = 0 ) {
return( abs( round( bounding.at( view ).pMax.z ) ) );
}
void GuiImage::setCurrentSlice( size_t view, size_t slice ) {
size_t sz = m_currentSlice.size( );
if( view < sz ) {
if( ( m_currentSlice[ view ] != slice ) && ( slice < depth( view ) ) ) {
m_currentSlice[ view ] = slice;
needUpdate[ view ] = true;
emit imageUpdated( );
}
}
else {
throw std::out_of_range( BIAL_ERROR( "Axis out of range." ) );
}
}
Bial::Point3D GuiImage::getPosition( QPointF pos, size_t view ) {
Bial::Point3D point( pos.x( ), pos.y( ), ( double ) m_currentSlice[ view ] );
transform[ view ]( point, &point );
return( point );
}
Bial::FastTransform GuiImage::getTransform( size_t axis ) {
return( transform.at( axis ) );
}
const Bial::Image< int > &GuiImage::getImage( ) const {
return( image );
}
void GuiImage::rotate90( size_t view ) {
Bial::FastTransform transf;
transf.Rotate( -90.0, Bial::FastTransform::Z );
transform[ view ] = transf * transform[ view ].Inverse( );
updateBoundings( view );
needUpdate[ view ] = true;
emit imageUpdated( );
}
void GuiImage::flipH( size_t view ) {
Bial::FastTransform transf;
transf.Scale( -1, 1, 1 );
transform[ view ] = transf * transform[ view ].Inverse( );
updateBoundings( view );
needUpdate[ view ] = true;
emit imageUpdated( );
}
void GuiImage::flipV( size_t view ) {
Bial::FastTransform transf;
transf.Scale( 1, -1, 1 );
transform[ view ] = transf * transform[ view ].Inverse( );
updateBoundings( view );
needUpdate[ view ] = true;
emit imageUpdated( );
}
void GuiImage::rotateAll90( ) {
for( int axis = 0; axis < needUpdate.size( ); ++axis ) {
rotate90( axis );
}
}
int GuiImage::max( ) {
return( m_max );
}
size_t GuiImage::currentSlice( size_t view ) {
return( m_currentSlice[ view ] );
}
bool GuiImage::getEqualizeHistogram( ) const {
return( m_equalizeHistogram );
}
void GuiImage::setEqualizeHistogram( bool equalizeHistogram ) {
m_equalizeHistogram = equalizeHistogram;
for( int axis = 0; axis < needUpdate.size( ); ++axis ) {
needUpdate[ axis ] = true;
}
emit imageUpdated( );
}
const Bial::Signal &GuiImage::getHistogram( ) const {
if( m_equalizeHistogram ) {
return( equalized );
}
return( histogram );
}
int GuiImage::getPixel( int x, int y, int z ) {
int color = 0;
if( modality( ) == Modality::BW2D ) {
if( image.ValidPixel( x, y ) ) {
color = image.at( x, y );
}
}
else {
if( image.ValidPixel( x, y, z ) ) {
color = image.at( x, y, z );
}
}
if( m_equalizeHistogram ) {
return( equalization[ color ] );
}
return( color );
}
size_t GuiImage::currentToolPos( ) const {
return( m_currentToolPos );
}
void GuiImage::setCurrentToolPos( const size_t ¤tToolPos ) {
if( currentToolPos < tools.size( ) ) {
m_currentToolPos = currentToolPos;
}
}
void GuiImage::updateBoundings( size_t axis ) {
Bial::Point3D start;
Bial::Point3D end( image.size( 0 ) - 1, image.size( 1 ) - 1, 1 );
if( image.Dims( ) > 2 ) {
end = Bial::Point3D( image.size( 0 ) - 1, image.size( 1 ) - 1, image.size( 2 ) - 1 );
}
transform[ axis ]( start, &start );
transform[ axis ]( end, &end );
bounding[ axis ] = Bial::BBox( start, end );
transform[ axis ] = transform[ axis ].Inverse( );
transform[ axis ].Translate( bounding[ axis ].pMin.x, bounding[ axis ].pMin.y, bounding[ axis ].pMin.z );
bounding[ axis ] = bounding[ axis ].Normalized( );
}