www.pudn.com > Ì廿֯Âë.rar > transfuncwin.cpp, change:2001-12-09,size:7362b
#include <math.h>
#include "transfunc.h"
#include "transfuncwin.h"
TransferFunctionWindow::TransferFunctionWindow()
{
m_curcolor = RGB(255,255,255);
for (int i=0; i<TFWIDTH; i++)
{
m_colors[i].r = 255;
m_colors[i].g = 255;
m_colors[i].b = 255;
m_colors[i].a = 255;
m_wincolors[i] = m_curcolor;
m_opacity[i] = 1.0f;
m_histogram[i] = 0.0f;
}
m_curbackgroundcolor = RGB(0,0,100);
}
TransferFunctionWindow::~TransferFunctionWindow()
{
}
void
TransferFunctionWindow::SetData(TransferFunction *tf)
{
for (int i=0; i<TFWIDTH-1; i++)
{
rgba c0 = m_colors[i];
rgba c1 = m_colors[i+1];
float o0 = m_opacity[i];
float o1 = m_opacity[i+1];
for (int j=0; j<8; j++)
{
float o = ((o0*(8-j) + o1*j)/8);
rgba c = {((c0.r*(8-j) + c1.r*j)/8),
((c0.g*(8-j) + c1.g*j)/8),
((c0.b*(8-j) + c1.b*j)/8),
((c0.a*(8-j) + c1.a*j)/8)};
tf->SetOpacity((i<<3)+j, o);
tf->SetColor((i<<3)+j, c);
}
}
rgba col;
col.r = GetRValue(m_curbackgroundcolor);
col.g = GetGValue(m_curbackgroundcolor);
col.b = GetBValue(m_curbackgroundcolor);
col.a = 255;
tf->SetBackgroundColor(col);
}
void
TransferFunctionWindow::GetData(TransferFunction *tf)
{
for (int i=0; i<TFWIDTH; i++)
{
tf->GetOpacity((i<<3), m_opacity[i]);
tf->GetColor((i<<3), m_colors[i]);
m_wincolors[i] = RGB(m_colors[i].r, m_colors[i].g, m_colors[i].b);
}
rgba col;
tf->GetBackgroundColor(col);
m_curbackgroundcolor = RGB(col.r, col.g, col.b);
}
void
TransferFunctionWindow::GetThreshold(int &threshold)
{
threshold = m_threshold;
}
void
TransferFunctionWindow::SetThreshold(int threshold)
{
m_threshold = threshold;
}
void
TransferFunctionWindow::SetCurrentColor(COLORREF color)
{
m_curcolor = color;
}
void
TransferFunctionWindow::SetCurrentAsBackgroundColor()
{
m_curbackgroundcolor = m_curcolor;
}
void
TransferFunctionWindow::SetHistogram(Volume *vv)
{
for (int i=0; i<TFWIDTH; i++)
{
m_histogram[i] = vv->GetHistogramRange((i<<3), (i<<3)+7);
}
}
void
TransferFunctionWindow::DrawOpacity(HWND hWnd)
{
HBRUSH WhiteBrush = CreateSolidBrush(RGB(255,255,255));
HPEN BlackPen = CreatePen(PS_SOLID, 1, RGB(0,0,0));
HPEN WhitePen = CreatePen(PS_SOLID, 1, RGB(255,255,255));
HPEN BluePen = CreatePen(PS_SOLID, 1, RGB(0,0,255));
HPEN LightGrayPen = CreatePen(PS_SOLID, 1, RGB(191,191,191));
HDC hDC = GetDC(hWnd);
int x1 = TFWNDX;
int y1 = TFWNDY;
int x2 = TFWNDX + TFWNDW;
int y2 = TFWNDY + TFWNDH;
int xd = x2 - x1 + 1;
int yd = y2 - y1 + 1;
int i;
SelectObject(hDC, WhiteBrush);
SelectObject(hDC, BlackPen);
Rectangle(hDC, x1-1, y1-1, x2+1, y2+2);
// ----- threshold -----
SelectObject(hDC, BluePen);
MoveToEx(hDC, x1 + (m_threshold >> 3), y1, NULL);
LineTo(hDC, x1 + (m_threshold >> 3), y2+1);
// ----- histogram -----
SelectObject(hDC, LightGrayPen);
for (i=0; i<TFWIDTH; i++)
{
MoveToEx(hDC, x1+i, y2, NULL);
int h = (int)(yd * m_histogram[i]);
if (h > yd) h = yd;
LineTo(hDC, x1+i, y2-h);
}
// ------ opacity -------
SelectObject(hDC, BlackPen);
MoveToEx(hDC, x1, y2 - (int)(yd * m_opacity[0]), NULL);
for (i=1; i<TFWIDTH; i++)
{
LineTo(hDC, x1+i, y2 - (int)(yd * m_opacity[i]));
}
//----------------------
ReleaseDC(hWnd, hDC);
DeleteObject(WhiteBrush);
DeleteObject(BlackPen);
DeleteObject(BluePen);
DeleteObject(WhitePen);
DeleteObject(LightGrayPen);
}
void
TransferFunctionWindow::DrawColorBar(HWND hWnd)
{
HDC hDC = GetDC(hWnd);
// ----- color bar frame -----
HBRUSH WhiteBrush = CreateSolidBrush(RGB(255,255,255));
HPEN BlackPen = CreatePen(PS_SOLID, 1, RGB(0,0,0));
SelectObject(hDC, WhiteBrush);
SelectObject(hDC, BlackPen);
int cbx1 = CBWNDX;
int cby1 = CBWNDY;
int cbx2 = CBWNDX+CBWNDW;
int cby2 = CBWNDY+CBWNDH;
Rectangle(hDC, cbx1-1, cby1-1, cbx2+1, cby2+2);
// ----- color bar -----
HPEN OldPen = 0;
HPEN Pen = 0;
for (int i=0; i<TFWIDTH; i++)
{
if (OldPen != 0) DeleteObject(OldPen);
Pen = CreatePen(PS_SOLID, 1, m_wincolors[i]);
OldPen = (HPEN)SelectObject(hDC, Pen);
MoveToEx(hDC, CBWNDX+i, CBWNDY, NULL);
LineTo(hDC, CBWNDX+i, CBWNDY+CBWNDH+1);
}
// ---------------------
ReleaseDC(hWnd, hDC);
if (OldPen != 0) DeleteObject(OldPen);
DeleteObject(Pen);
DeleteObject(WhiteBrush);
DeleteObject(BlackPen);
}
void
TransferFunctionWindow::DrawCurrentColor(HWND hWnd)
{
// ----- current color -----
HBRUSH CurrentColorBrush = CreateSolidBrush(m_curcolor);
HPEN BlackPen = CreatePen(PS_SOLID, 1, RGB(0,0,0));
HDC hDC = GetDC(hWnd);
SelectObject(hDC, CurrentColorBrush);
SelectObject(hDC, BlackPen);
int ccx1 = CCWNDX;
int ccy1 = CCWNDY;
int ccx2 = CCWNDX+CCWNDW;
int ccy2 = CCWNDY+CCWNDH;
Rectangle(hDC, ccx1-1, ccy1-1, ccx2+1, ccy2+2);
ReleaseDC(hWnd, hDC);
DeleteObject(CurrentColorBrush);
DeleteObject(BlackPen);
}
void
TransferFunctionWindow::DrawBackgroundColor(HWND hWnd)
{
// ----- current color -----
HBRUSH BackgroundColorBrush = CreateSolidBrush(m_curbackgroundcolor);
HPEN BlackPen = CreatePen(PS_SOLID, 1, RGB(0,0,0));
HDC hDC = GetDC(hWnd);
SelectObject(hDC, BackgroundColorBrush);
SelectObject(hDC, BlackPen);
int bcx1 = BCWNDX;
int bcy1 = BCWNDY;
int bcx2 = BCWNDX+BCWNDW;
int bcy2 = BCWNDY+BCWNDH;
Rectangle(hDC, bcx1-1, bcy1-1, bcx2+1, bcy2+2);
ReleaseDC(hWnd, hDC);
DeleteObject(BackgroundColorBrush);
DeleteObject(BlackPen);
}
void
TransferFunctionWindow::ApplyColor(HWND hWnd, int min, int max)
{
int h;
if (max min)
{
h = max;
max = min;
min = h;
}
if ((max 0) || (min >= TFWIDTH)) return;
if (max >= TFWIDTH) max = TFWIDTH-1;
if (min 0) min = 0;
HPEN CurrentColorPen = CreatePen(PS_SOLID, 1, m_curcolor);
HDC hDC = GetDC(hWnd);
SelectObject(hDC, CurrentColorPen);
for (int i=min; i<max; i++)
{
m_colors[i].r = GetRValue(m_curcolor);
m_colors[i].g = GetGValue(m_curcolor);
m_colors[i].b = GetBValue(m_curcolor);
m_colors[i].a = 255;
m_wincolors[i] = m_curcolor;
// ----- draw color -----
MoveToEx(hDC, CBWNDX+i, CBWNDY, NULL);
LineTo(hDC, CBWNDX+i, CBWNDY+CBWNDH+1);
}
ReleaseDC(hWnd, hDC);
DeleteObject(CurrentColorPen);
}
void
TransferFunctionWindow::ApplyOpacity(HWND hWnd, int min, int max, float opacity)
{
int h;
if (max min)
{
h = max;
max = min;
min = h;
}
if ((max 0) || (min >= TFWIDTH)) return;
if (max > TFWIDTH-1) max = TFWIDTH-1;
if (min 0) min = 0;
if (opacity 0) opacity = 0;
if (opacity > 1) opacity = 1;
for (int i=min; i=max; i++)
{
m_opacity[i] = opacity;
}
DrawOpacity(hWnd);
}