00001 #include <iostream>
00002 #include <fstream>
00003
00004 #include "vuColourRGBa.h"
00005
00006 #include "Slicer.h"
00007
00008 #define min(a,b) (a<b ? a : b)
00009
00010
00011
00012
00013
00014 vu1112116::vu1112116() : m_Position(0,0,0), m_ImgScaleX(1), m_ImgScaleY(1)
00015 {
00016 }
00017
00018
00019
00020
00021
00022 vu1112116::vu1112116(const vu1112116& inst) : vu111211(inst)
00023 {
00024 m_Position = inst.m_Position;
00025 }
00026
00027
00028
00029
00030
00031 vu1112116::~vu1112116()
00032 {
00033 }
00034
00035
00036
00037
00038
00039 vu1112116& vu1112116::operator=(const vu1112116& rhs)
00040 {
00041 if (this != &rhs)
00042 {
00043 vu111211::operator=(rhs);
00044 }
00045 return *this;
00046 }
00047
00048
00049 void vu1112116::setImageSize(dword sx, dword sy)
00050 {
00051 m_ImgScaleX = (float)sx/m_Dim1Size;
00052 m_ImgScaleY = (float)sy/m_Dim2Size;
00053 #ifdef QUADRATIC_WIN
00054 if(m_ImgScaleX > m_ImgScaleY) m_ImgScaleX = m_ImgScaleY;
00055 else if(m_ImgScaleY > m_ImgScaleX) m_ImgScaleY = m_ImgScaleX;
00056 #endif
00057
00058 glPixelZoom(m_ImgScaleX, m_ImgScaleY);
00059
00060 glMatrixMode(GL_PROJECTION);
00061 glLoadIdentity();
00062 glOrtho(0,sx,0,sy,-1,1);
00063 glMatrixMode(GL_MODELVIEW);
00064 glLoadIdentity();
00065 }
00066
00067 void vu1112116::initOpenGL(void)
00068 {
00069
00070 }
00071
00072
00073
00074
00075
00076 bool vu1112116::read()
00077 {
00078 bool success = vu111211::read();
00079 if (!success) return false;
00080
00081 m_Image.init(m_Dim1Size,m_Dim2Size);
00082
00083 return true;
00084 }
00085
00086
00087
00088
00089
00090 bool vu1112116::readRaw(void)
00091 {
00092 dword len;
00093 ifstream in;
00094
00095 in.open(m_FileName, ios::in|ios::binary
00096 #ifdef IS_NOCREATE_NEEDED
00097 |ios::nocreate
00098 #endif
00099 );
00100 if (!in.is_open()) return false;
00101
00102 in.seekg(0, ios::end);
00103 len = in.tellg();
00104 in.seekg(0, ios::beg);
00105
00106 in >> m_Dim1Size >> m_Dim2Size >> m_Dim3Size;
00107 if (in.fail()) return false;
00108 m_DataSize = m_Dim1Size*m_Dim2Size*m_Dim3Size;
00109
00110 m_Data = new byte[m_DataSize];
00111 in.read((char *)m_Data, (int)m_DataSize);
00112 if (in.fail()) return false;
00113
00114 in.close();
00115
00116 m_Image.init(m_Dim1Size,m_Dim2Size);
00117 setImageSize(m_Dim1Size,m_Dim2Size);
00118
00119 return true;
00120 }
00121
00122
00123
00124
00125 #define USE_TFUNC
00126 void vu1112116::render(void)
00127 {
00128 byte tfunc[256][3];
00129 #ifdef USE_TFUNC
00130 for(int i=0;i<256;i++)
00131 {
00132 tfunc[i][0] = byte(255*m_TFunc[i][0]);
00133 tfunc[i][1] = byte(255*m_TFunc[i][1]);
00134 tfunc[i][2] = byte(255*m_TFunc[i][2]);
00135 }
00136 #else
00137 for(int i=0;i<256;i++)
00138 {
00139 tfunc[i][0] = i;
00140 tfunc[i][1] = i;
00141 tfunc[i][2] = i;
00142 }
00143 #endif
00144
00145
00146 dword mx,my;
00147 m_Image.get_extents((int&)mx,(int&)my);
00148 dword sx = min(mx,m_Dim1Size);
00149 dword sy = min(my,m_Dim2Size);
00150 dword addimg=0, addvol=0;
00151 if(sx<mx) addimg = 3*(mx-sx);
00152 else addvol = sx-mx;
00153
00154 byte *image = (byte*)m_Image.get_rgb();
00155 byte *data = getDataPointer(vuVector(0,0,m_Position[2]));
00156
00157 if(data)
00158 {
00159 for(dword y=0;y<sy;y++,image+=addimg, data+=addvol)
00160 for(dword x=0;x<sx;x++,data++)
00161 {
00162 *(image++)=tfunc[*data][0];
00163 *(image++)=tfunc[*data][1];
00164 *(image++)=tfunc[*data][2];
00165 }
00166 }
00167
00168
00169 if(mx && my) {
00170 glClear(GL_COLOR_BUFFER_BIT);
00171
00172 m_Image.blit();
00173 }
00174 }
00175
00176 dword vu1112116::getValue(const vuVector & where)
00177 {
00178 if(isInside(where))
00179 return m_Data[dword(where[0]/m_ImgScaleX)
00180 + dword(where[1]/m_ImgScaleY)*m_Dim1Size
00181 + dword(where[2])*m_Dim1Size*m_Dim2Size];
00182 else return 0;
00183 }
00184
00185 byte* vu1112116::getDataPointer(const vuVector & where)
00186 {
00187 if(isInside(where))
00188 return &m_Data[dword(where[0]/m_ImgScaleX)
00189 + dword(where[1]/m_ImgScaleY)*m_Dim1Size
00190 + dword(where[2])*m_Dim1Size*m_Dim2Size];
00191 else return NULL;
00192 }
00193
00194 bool vu1112116::isInside(const vuVector& pos)
00195 {
00196 return (pos[0]>=0 && pos[0]/m_ImgScaleX<m_Dim1Size &&
00197 pos[1]>=0 && pos[1]/m_ImgScaleX<m_Dim2Size &&
00198 pos[2]>=0 && pos[2]<m_Dim3Size);
00199 }
00200