Main Page   Alphabetical List   Compound List   File List   Compound Members   Related Pages  

MDISlice.cpp

00001 //----------------------------------------------------------------------------
00002 //Borland C++Builder
00003 //Copyright (c) 1987, 1998-2002 Borland International Inc. All Rights Reserved.
00004 //----------------------------------------------------------------------------
00005 //---------------------------------------------------------------------------
00006 #include <vcl.h>
00007 #include <math.h>
00008 #pragma hdrstop
00009 
00010 #include "MDISlice.h"
00011 #include "MDIEdit.h"
00012 #include "MDIFrame.h"
00013 #include "DContrast.h"
00014 //---------------------------------------------------------------------------
00015 #pragma resource "*.dfm"
00016 TSliceForm *SliceForm;
00017 
00018 rgba *sliceview;
00019 
00020 //---------------------------------------------------------------------------
00021 __fastcall TSliceForm::TSliceForm(TComponent* Owner)
00022     : TForm(Owner)
00023 {
00024 
00025 }
00026 //---------------------------------------------------------------------------
00027 void __fastcall TSliceForm::SetPixelFormatDescriptor()
00028 {
00029     PIXELFORMATDESCRIPTOR pfd = {
00030         sizeof(PIXELFORMATDESCRIPTOR),
00031         1,
00032         PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
00033         PFD_TYPE_RGBA,
00034         24,
00035         0,0,0,0,0,0,
00036         0,0,
00037         0,0,0,0,0,
00038         32,
00039         0,
00040         0,
00041         PFD_MAIN_PLANE,
00042         0,
00043         0,0,
00044     };
00045     PixelFormat = ChoosePixelFormat(hdc, &pfd);
00046     SetPixelFormat(hdc, PixelFormat, &pfd);
00047 }
00048 //---------------------------------------------------------------------
00049 void __fastcall TSliceForm::FormClose(TObject *Sender, TCloseAction &Action)
00050 {
00051 Action=caFree;
00052 FrameForm->Slices->Checked = false;
00053 delete [] sliceview;
00054 memusage = memusage - sizeof(rgba)*512*512;
00055 FrameForm->memupdate();
00056 wglDeleteContext(hrc);
00057 ReleaseDC(Handle,hdc);
00058 }
00059 //---------------------------------------------------------------------
00060 void __fastcall TSliceForm::FormCreate(TObject *Sender)
00061 {
00062     int i,a;
00063 
00064     sliceview = new rgba[512*512];
00065     memusage = memusage + sizeof(rgba)*512*512;
00066     FrameForm->memupdate();
00067 
00068     hdc = GetDC(Handle);
00069     SetPixelFormatDescriptor();
00070     hrc = wglCreateContext(hdc);
00071     if(hrc == NULL) ShowMessage("Could not create DC!");
00072     wglMakeCurrent(hdc, hrc);
00073 
00074     w = ClientWidth;
00075     h = ClientHeight;
00076 
00077     glEnable(GL_DEPTH_TEST);
00078     glEnable(GL_CULL_FACE);
00079 
00080     glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
00081     glOrtho (-50.0,50.0,-50.0,50.0,-50.0,50.0);
00082 
00083     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00084     glFlush();
00085     SwapBuffers(hdc);
00086     FormResize(Sender);
00087     pagesize = sizeX*sizeY;
00088         for(int a = 0; a<sizeY; a+=1) {
00089         for(int i = 0; i<sizeX; i+=1) {
00090                 sliceview[a*sizeX+i].r =
00091                         sliceview[a*sizeX+i].g =
00092                         sliceview[a*sizeX+i].b =
00093                                 contr->getVal(data[pagesize*TrackBar->Position+a*sizeX+i]);
00094         }
00095         }
00096     TrackBar->Max = sizeZ-1;
00097 }
00098 //---------------------------------------------------------------------
00099 void __fastcall TSliceForm::FormResize(TObject *Sender)
00100 {
00101         tPanel->Height = ClientHeight-34;
00102         TrackBar->Height = ClientHeight-36;
00103         kontrast->Height = ClientHeight-188;
00104         kontrastpanel->Height = ClientHeight-235;
00105         CbTransfer->Top = ClientHeight-211;
00106         PrepareGLWindow();
00107         PaintGL();
00108 }
00109 //---------------------------------------------------------------------------
00110 void __fastcall TSliceForm::FormPaint(TObject *Sender)
00111 {
00112         //histo->drawTo(HistoArea);
00113         PaintGL();
00114 }
00115 //---------------------------------------------------------------------------
00116 void __fastcall TSliceForm::PaintGL()
00117 {
00118         float i;
00119 
00120         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00121         PrepareGLWindow();
00122         glColor3f(0.6f,0.6f,0.6f);
00123         glLineWidth(3.0f);
00124         glBegin(GL_LINE_STRIP);
00125         glVertex3f(-50,-50,0);
00126         glVertex3f(50,-50,0);
00127         glVertex3f(50,50,0);
00128         glVertex3f(-50,50,0);
00129         glVertex3f(-50,-50,0);
00130         glEnd();
00131 
00132 
00133 //        glDrawPixels(sizeX, sizeY, GL_RGB, GL_UNSIGNED_BYTE,
00134 //                     color+(sizeX*sizeY*TrackBar->Position));
00135 if (RadioButton1->Checked) glDrawPixels(sizeX, sizeY, GL_RGBA, GL_UNSIGNED_BYTE, sliceview);
00136 if (RadioButton2->Checked) glDrawPixels(sizeX, sizeZ, GL_RGBA, GL_UNSIGNED_BYTE, sliceview);
00137 if (RadioButton3->Checked) glDrawPixels(sizeY, sizeZ, GL_RGBA, GL_UNSIGNED_BYTE, sliceview);
00138                         // we use unsigned byte (char) to meet speed issues.
00139                         // 16 bit data (__int16) slows down drawing to much.
00140 
00141         glFlush();
00142         SwapBuffers(hdc);
00143 }
00144 
00145 void __fastcall TSliceForm::PrepareGLWindow()
00146 {
00147         wglMakeCurrent(hdc, hrc);
00148 
00149         GLfloat nRange = 50.0;
00150         GLfloat xRange, yRange;
00151         float yrat;
00152 
00153         w = ClientWidth-200;
00154         h = ClientHeight;
00155 
00156         if(h == 0) h = 1;
00157         if(w == 0) w = 1;
00158 
00159         glViewport(200, 0, w, h);
00160 
00161         glMatrixMode(GL_PROJECTION);
00162         glLoadIdentity();
00163 if (RadioButton1->Checked) {
00164         yrat = (float)sizeX/sizeY;
00165         if (w <= h*yrat)
00166         {        glOrtho (-nRange, nRange, -nRange*h*yrat/w, nRange*h*yrat/w, -nRange, nRange);
00167                 glPixelZoom((float)w/(float)sizeX, (float)w/((float)sizeY*yrat));}
00168          else
00169         {        glOrtho (-nRange*w/(h*yrat), nRange*w/(h*yrat), -nRange, nRange, -nRange, nRange);
00170                 glPixelZoom((float)h*yrat/(float)sizeX, (float)h/((float)sizeY));}
00171 }
00172 if (RadioButton2->Checked) {
00173         yrat = (float)sizeX/sizeZ;
00174         if (w <= h*yrat)
00175         {        glOrtho (-nRange, nRange, -nRange*h*yrat/w, nRange*h*yrat/w, -nRange, nRange);
00176                 glPixelZoom((float)w/(float)sizeX, (float)w/((float)sizeZ*yrat));}
00177          else
00178         {        glOrtho (-nRange*w/(h*yrat), nRange*w/(h*yrat), -nRange, nRange, -nRange, nRange);
00179                 glPixelZoom((float)h*yrat/(float)sizeX, (float)h/(float)sizeZ);}
00180 }
00181 if (RadioButton3->Checked) {
00182         yrat = (float)sizeY/sizeZ;
00183         if (w <= h*yrat)
00184         {        glOrtho (-nRange, nRange, -nRange*h*yrat/w, nRange*h*yrat/w, -nRange, nRange);
00185                 glPixelZoom((float)w/(float)sizeY, (float)w/((float)sizeZ*yrat));}
00186          else
00187         {        glOrtho (-nRange*w/(h*yrat), nRange*w/(h*yrat), -nRange, nRange, -nRange, nRange);
00188                 glPixelZoom((float)h*yrat/(float)sizeY, (float)h/(float)sizeZ);}
00189 }
00190 
00191         glRasterPos2d(-50,-50);
00192         glMatrixMode(GL_MODELVIEW);
00193         glLoadIdentity();
00194 }
00195 //---------------------------------------------------------------------------
00196 void __fastcall TSliceForm::TrackBarChange(TObject *Sender)
00197 {
00198 if (CbTransfer->Checked)
00199 {
00200 
00201 if (RadioButton1->Checked)
00202 {
00203         //float ratio = (float)0xFF / (float)4096;
00204         for(int a = 0; a<sizeY; a+=1) {
00205         for(int i = 0; i<sizeX; i+=1) {
00206                 sliceview[a*sizeX+i] =
00207                         transe->getColor(data[pagesize*TrackBar->Position+a*sizeX+i]);
00208                 sliceview[a*sizeX+i].r =
00209                         (int)((float)sliceview[a*sizeX+i].r*(float)sliceview[a*sizeX+i].a/(float)0xff);
00210                 sliceview[a*sizeX+i].g =
00211                         (int)((float)sliceview[a*sizeX+i].g*(float)sliceview[a*sizeX+i].a/(float)0xff);
00212                 sliceview[a*sizeX+i].b =
00213                         (int)((float)sliceview[a*sizeX+i].b*(float)sliceview[a*sizeX+i].a/(float)0xff);
00214         }
00215         }
00216 }
00217 if (RadioButton2->Checked)
00218 {
00219         for(int a = 0; a<sizeZ; a+=1) {
00220         for(int i = 0; i<sizeX; i+=1) {
00221                 //sliceview[a*sizeX+i].r = sliceview[a*sizeX+i].g = sliceview[a*sizeX+i].b = color[pagesize*a+TrackBar->Position*sizeX+i].b;
00222                 sliceview[a*sizeX+i] =
00223                         transe->getColor(data[pagesize*a+TrackBar->Position*sizeX+i]);
00224                 sliceview[a*sizeX+i].r =
00225                         (int)((float)sliceview[a*sizeX+i].r*(float)sliceview[a*sizeX+i].a/(float)0xff);
00226                 sliceview[a*sizeX+i].g =
00227                         (int)((float)sliceview[a*sizeX+i].g*(float)sliceview[a*sizeX+i].a/(float)0xff);
00228                 sliceview[a*sizeX+i].b =
00229                         (int)((float)sliceview[a*sizeX+i].b*(float)sliceview[a*sizeX+i].a/(float)0xff);
00230         }
00231         }
00232 }
00233 if (RadioButton3->Checked)
00234 {
00235         for(int a = 0; a<sizeZ; a+=1) {
00236         for(int i = 0; i<sizeY; i+=1) {
00237                 //sliceview[a*sizeY+i].r = sliceview[a*sizeY+i].g = sliceview[a*sizeY+i].b = color[TrackBar->Position+i*sizeX+a*pagesize].b;
00238                 sliceview[a*sizeY+i] = transe->getColor(data[TrackBar->Position+i*sizeX+a*pagesize]);
00239                 sliceview[a*sizeY+i].r =
00240                         (int)((float)sliceview[a*sizeY+i].r*(float)sliceview[a*sizeY+i].a/(float)0xff);
00241                 sliceview[a*sizeY+i].g =
00242                         (int)((float)sliceview[a*sizeY+i].g*(float)sliceview[a*sizeY+i].a/(float)0xff);
00243                 sliceview[a*sizeY+i].b =
00244                         (int)((float)sliceview[a*sizeY+i].b*(float)sliceview[a*sizeY+i].a/(float)0xff);
00245         }
00246         }
00247 }
00248 
00249 } else {
00250 
00251 if (RadioButton1->Checked)
00252 {
00253         //float ratio = (float)0xFF / (float)4096;
00254         for(int a = 0; a<sizeY; a+=1) {
00255         for(int i = 0; i<sizeX; i+=1) {
00256                 //sliceview[a*sizeX+i].r = sliceview[a*sizeX+i].g = sliceview[a*sizeX+i].b = color[pagesize*TrackBar->Position+a*sizeX+i].b;
00257                 sliceview[a*sizeX+i].r =
00258                         sliceview[a*sizeX+i].g =
00259                         sliceview[a*sizeX+i].b =
00260                                 contr->getVal(data[pagesize*TrackBar->Position+a*sizeX+i]);
00261         }
00262         }
00263 }
00264 if (RadioButton2->Checked)
00265 {
00266         for(int a = 0; a<sizeZ; a+=1) {
00267         for(int i = 0; i<sizeX; i+=1) {
00268                 //sliceview[a*sizeX+i].r = sliceview[a*sizeX+i].g = sliceview[a*sizeX+i].b = color[pagesize*a+TrackBar->Position*sizeX+i].b;
00269                 sliceview[a*sizeX+i].r =
00270                         sliceview[a*sizeX+i].g =
00271                         sliceview[a*sizeX+i].b =
00272                                 contr->getVal(data[pagesize*a+TrackBar->Position*sizeX+i]);
00273         }
00274         }
00275 }
00276 if (RadioButton3->Checked)
00277 {
00278         for(int a = 0; a<sizeZ; a+=1) {
00279         for(int i = 0; i<sizeY; i+=1) {
00280                 //sliceview[a*sizeY+i].r = sliceview[a*sizeY+i].g = sliceview[a*sizeY+i].b = color[TrackBar->Position+i*sizeX+a*pagesize].b;
00281                 sliceview[a*sizeY+i].r =
00282                         sliceview[a*sizeY+i].g =
00283                         sliceview[a*sizeY+i].b =
00284                                 contr->getVal(data[TrackBar->Position+i*sizeX+a*pagesize]);
00285         }
00286         }
00287 }
00288 
00289 }
00290 lslice->Caption = IntToStr(TrackBar->Position) + " / " + IntToStr(TrackBar->Max);
00291 PaintGL();
00292 }
00293 //---------------------------------------------------------------------------
00294 
00295 void __fastcall TSliceForm::RadioButton1Click(TObject *Sender)
00296 {
00297 TrackBar->Max = sizeZ-1;
00298 TrackBarChange(this);
00299 }
00300 //---------------------------------------------------------------------------
00301 
00302 void __fastcall TSliceForm::RadioButton2Click(TObject *Sender)
00303 {
00304 TrackBar->Max = sizeY-1;
00305 TrackBarChange(this);
00306 }
00307 //---------------------------------------------------------------------------
00308 
00309 void __fastcall TSliceForm::RadioButton3Click(TObject *Sender)
00310 {
00311 TrackBar->Max = sizeX-1;
00312 TrackBarChange(this);
00313 }
00314 //---------------------------------------------------------------------------
00315 
00316 void __fastcall TSliceForm::FormActivate(TObject *Sender)
00317 {
00318         wglMakeCurrent(hdc, hrc);
00319         PaintGL();
00320 }
00321 //---------------------------------------------------------------------------
00322 void __fastcall TSliceForm::HistoAreaDblClick(TObject *Sender)
00323 {
00324         ContrastForm = new TContrastForm(this, histo, contr);
00325         ContrastForm->Show();
00326 }
00327 //---------------------------------------------------------------------------
00328 
00329 void __fastcall TSliceForm::HistoAreaPaint(TObject *Sender)
00330 {
00331         histo->drawTo(HistoArea);
00332         int l = (int)(contr->Lower*(float)histo->size/histo->getHPP(HistoArea));
00333         int u = (int)(contr->Upper*(float)histo->size/histo->getHPP(HistoArea));
00334         HistoArea->Canvas->Pen->Color = clBlack;
00335         if (histo->horizontal) {
00336                 HistoArea->Canvas->MoveTo(l,0);
00337                 HistoArea->Canvas->LineTo(l,HistoArea->Height);
00338                 HistoArea->Canvas->MoveTo(u,0);
00339                 HistoArea->Canvas->LineTo(u,HistoArea->Height);
00340         } else {
00341                 HistoArea->Canvas->MoveTo(0,l);
00342                 HistoArea->Canvas->LineTo(HistoArea->Width,l);
00343                 HistoArea->Canvas->MoveTo(0,u);
00344                 HistoArea->Canvas->LineTo(HistoArea->Width,u);
00345         }
00346 }
00347 //---------------------------------------------------------------------------
00348 

Generated on Thu Jan 23 06:17:38 2003 for Vol by doxygen1.2.18