00001 #include "stdafx.h"
00002 #include "slice.h"
00003
00004 Slice::Slice()
00005 {
00006 m_depth=0;
00007 m_height=0;
00008 m_width=0;
00009 m_type=0;
00010 slice_data = NULL;
00011 m_dSliceNumber = -1;
00012 m_bZoom = false;
00013 m_bUseTransfer = false;
00014 m_tTransferfunction = NULL;
00015 }
00016
00017
00018 Slice::~Slice()
00019 {
00020 if(slice_data != NULL) delete slice_data;
00021 }
00022
00023 bool
00024 Slice::SetType(int tp)
00025 {
00026 if ((tp < 0)||(tp > 2)) return false;
00027 m_type = tp;
00028 return true;
00029 }
00030
00031 int
00032 Slice::GetType()
00033 {
00034 return m_type;
00035 }
00036
00037
00038 int Slice::GetSliceNumber(Data *dat) {
00039 if (dat == NULL)
00040 return m_dSliceNumber;
00041
00042 switch (m_type) {
00043 case XY:
00044 m_dSliceNumber = dat->GetZDim() - 1;
00045 break;
00046 case XZ:
00047 m_dSliceNumber = dat->GetYDim() - 1;
00048 break;
00049 case YZ:
00050 m_dSliceNumber = dat->GetXDim() - 1;
00051 break;
00052 }
00053 return m_dSliceNumber;
00054 }
00055
00056
00057 bool
00058 Slice::SetPixels(Transfunc *tf, Data *dat, int dp)
00059 {
00060 if (m_bUseTransfer && tf == NULL)
00061 m_bUseTransfer = false;
00062
00063 int size = 0;
00064 if(slice_data != NULL) delete slice_data;
00065 m_depth = dp;
00066 int density;
00067
00068 if(m_type == XY)
00069 {
00070 m_width = (int)dat->GetXDim();
00071 m_height = (int)dat->GetYDim();
00072 size = m_width*m_height;
00073 slice_data = new rgb[size];
00074 for(int i = 0; i < m_height; i++)
00075 {
00076 for(int j = 0; j < m_width; j++)
00077 {
00078 if (m_bUseTransfer) {
00079 density = dat->GetDensity(j,i,m_depth);
00080 slice_data[i*m_width+j] = tf->GetDensityColor(density);
00081 }
00082 else {
00083 density = dat->GetDensity(j, i, m_depth) / 16;
00084 density = CLAMP0255(density);
00085 rgb col;
00086 col.r = density;
00087 col.g = density;
00088 col.b = density;
00089 slice_data[i * m_width + j] = col;
00090 }
00091 }
00092 }
00093 }
00094
00095 if(m_type == XZ)
00096 {
00097 m_width = dat->GetXDim();
00098 m_height = dat->GetZDim();
00099 size = m_width*m_height;
00100 slice_data = new rgb[size];
00101 for(int i = 0; i < m_height; i++)
00102 {
00103 for(int j = 0; j < m_width; j++)
00104 {
00105 if (m_bUseTransfer) {
00106 density = dat->GetDensity(j,m_depth,i);
00107 slice_data[i*m_width+j] = tf->GetDensityColor(density);
00108 }
00109 else {
00110 density = dat->GetDensity(j, m_depth, i) / 16;
00111 density = CLAMP0255(density);
00112 rgb col;
00113 col.r = density;
00114 col.g = density;
00115 col.b = density;
00116 slice_data[i * m_width + j] = col;
00117 }
00118 }
00119 }
00120 }
00121
00122 if(m_type == YZ)
00123 {
00124 m_width = dat->GetYDim();
00125 m_height = dat->GetZDim();
00126 size = m_width*m_height;
00127 slice_data = new rgb[size];
00128 for(int i = 0; i < m_height; i++)
00129 {
00130 for(int j = 0; j < m_width; j++)
00131 {
00132 if (m_bUseTransfer) {
00133 density = dat->GetDensity(m_depth,j,i);
00134 slice_data[i*m_width+j] = tf->GetDensityColor(density);
00135 }
00136 else {
00137 density = dat->GetDensity(m_depth, j, i) / 16;
00138 density = CLAMP0255(density);
00139 rgb col;
00140 col.r = density;
00141 col.g = density;
00142 col.b = density;
00143 slice_data[i * m_width + j] = col;
00144 }
00145 }
00146 }
00147 }
00148
00149 return true;
00150 }
00151
00152
00153 #pragma warning(push)
00154 #pragma warning(disable : 4244)
00155
00156 bool
00157 Slice::DrawSlice(int width, int height)
00158 {
00159 float zoomx = (float)width / (float)m_width;
00160 float zoomy = (float)height / (float)m_height;
00161 float zoom;
00162
00163
00164 if (zoomx < zoomy) {
00165 zoom = zoomx;
00166 m_x = 0;
00167 m_y = height / 2 - (m_height * zoom) / 2;
00168 }
00169 else {
00170 zoom = zoomy;
00171 m_y = 0;
00172 m_x = width / 2 - (m_width * zoom) / 2;
00173 }
00174
00175
00176 if (!m_bZoom) {
00177 zoom = 1.0f;
00178 m_x = width / 2 - m_width / 2;
00179 m_y = height / 2 - m_height / 2;
00180 }
00181
00182
00183 if(slice_data == NULL) return false;
00184 glRasterPos2i(m_x, m_y);
00185 glPixelStorei(GL_UNPACK_ALIGNMENT,1);
00186 glPixelZoom(zoom, zoom);
00187 glDrawPixels(m_width, m_height, GL_RGB, GL_UNSIGNED_BYTE, slice_data);
00188 return true;
00189 }
00190
00191 #pragma warning(pop)
00192
00193
00194 void Slice::GetImageInfo(int &width, int &height, int &x, int &y, int &plane, int &depth) {
00195 width = m_width;
00196 height = m_height;
00197 x = m_x;
00198 y = m_y;
00199 plane = m_type;
00200 depth = m_depth;
00201 }