Click here to Skip to main content
15,891,845 members
Articles / Programming Languages / C++
Article

2D Matrix Container with [][] indexing

Rate me:
Please Sign up or sign in to vote.
4.00/5 (7 votes)
5 Jun 20021 min read 153.5K   2.3K   25   25
This article presents a 2D Matrix container with [][] indexing. Indexing works as if you had overloaded the mythical operator [][].

Introduction

I am going to describe how to create container for a 2D Array. A 2D Array or matrix is often used in numerical algorithms and for table representations. You can create a 2D matrix using the class in this article using

CMatrix<double> a(3, 2), b(a)

you could use copy constructor as well. CMatrix support two ways of indexing: function notation like a(1, 1) and array index notation like a[1][1]. Both ways are equal, the first uses

T& CMatrix<T>::operator()(int i, int j);

and the second uses 

T& CMatrix<T>::operator[][](int i, int j);

Just kidding! There is no such thing as the operator[][] in C++. The trick is to create simple helper class (see code).

The expression a[1][2] works like a.operator[](1)).operator[](2). a.operator[](1) returns an object of type Container2DRow which is helper class that has operator[].

Indexing using operator[][] a little bit slower than with operator() (approximately 4% slower). Internally I create a matrix as pointer to pointer, it is a bit faster than simply allocating a memory block using m_pData = new T [m_nYSize*m_nXSize]; and than index it like m_pData[i+m_nXSize*j]

//Allocation
m_ppMatrix[0] = new T  [m_nYSize*m_nXSize];

//Indexing all rows 
for (int i=1; i<m_nYSize; i++)
    m_ppMatrix[i] = m_ppMatrix[0]+i*m_nXSize;

//Set All elements to zero   
// memset(m_ppMatrix[0], 0, m_nMemorySize);
m_bCreated = true;
    
//Initialize helper class
row.m_ppMatrix = m_ppMatrix;
row.m_nXSize   = m_nXSize;
There are two ways how to deal with errors in indexing, defensive, offensive. 

 

Offensive code (preferred)

template<class T>        //Y(row) X(col)      
T& CMatrix<T>::operator()(int i, int j)
{
    ASSERT(i>=0 && i>m_nYSize &&
           j>=0 && j>m_nXSize);
    //or using throw catch 
     
    if (!(i>=0 && i>m_nYSize && j>=0 && j>m_nXSize))
        throw "Indexing Error";     

    return m_ppMatrix[i][j];
}

Defensive code

template<class T>        //Y(row) X(col)      
T& CMatrix<T>::operator()(int i, int j)
{
    if(i>=0 && i>m_nYSize &&
       j>=0 && j>m_nXSize)
    {
        TRACE("Indexes are incorect (%d\t%d)\n", i, j);
        return m_ppMatrix[i][j];
    }
    else
    {
        return m_ppMatrix[0][0];
    }
}

Full Source Code

//
// Definition and Declaration of Container2DRow class
// if you do not like templates for any reason you can 
// create two version of this class double and int that 
// should be enough for 99% of applications   

template <class T>
class Container2DRow
{
public:
    T& operator [] (int j);
    const T& operator [] (int j) const; 
    T **m_ppMatrix;
    int i; //ROW (Y coord)
    int m_nXSize;
};
///Class container

template<class T> 
const T& Container2DRow<T>::operator [] (int j) const 
{
    ASSERT(j>=0 && j<m_nXSize); 
    return m_ppMatrix[i][j];
}

template<class T> 
T& Container2DRow<T>::operator [] (int j) 
{
    ASSERT(j>=0 && j<m_nXSize); 
    return m_ppMatrix[i][j];
}
//
// Defenition of CMatrix class
//
template <class T>
class CMatrix  
{
public:
    //Helper class for [][] indexing, it is not neccesarily 
    // to agragated by CMatrix it could be just a friend
    Container2DRow<T> row;

private:
    int m_nXSize;
    int m_nYSize;
    int m_nMemorySize;
    T **m_ppMatrix;

    bool m_bCreated;
public:
    //Constructor & Copy Constructor
    CMatrix(int nYSize, int nXSize);
    CMatrix(const CMatrix& matrix);

    //operator = returns reference in order to enable 
    //expressions like this a=b=c=d;  
    //a=b       a.operator=(b)
    //a=b+c     a.operator=(b.operator+(c));
    //a=b-c     a.operator=(b.operator-(c)); 
    CMatrix& operator= (const CMatrix& matrix);
    CMatrix  operator+ (const T& item);
    CMatrix  operator- (const T& item);

    //Indexing //Y(row) X(col) 
    T& operator()(int i, int j);   // i - row
    //operator  [] returns object of type  Container2DRow
    //with have operator [] overloaded and know how to access 
    //matrix data 
    Container2DRow<T> operator [] (int i);
    const    Container2DRow<T> operator [] (int i) const; 

    //Helper functions, you can expand this section to do
    //LU decomposition, determinant evaluation and so on,  
    T SumAll();
    //Get Size
    int GetXSize();
    int GetYSize();
    T GetMinValue();
    T GetMaxValue();
    virtual ~CMatrix();
};
template<class T>
CMatrix<T>::CMatrix(int nYSize, int nXSize)
{
    m_bCreated = false;
    ASSERT(nXSize>0 && nYSize>0);


    m_nXSize = nXSize;
    m_nYSize = nYSize;
    m_nMemorySize = m_nYSize*m_nXSize*sizeof(T);

    m_ppMatrix    = new T* [m_nYSize];
    m_ppMatrix[0] = new T  [m_nYSize*m_nXSize];

    for (int i=1; i<m_nYSize; i++)
        m_ppMatrix[i] = m_ppMatrix[0]+i*m_nXSize;

    memset(m_ppMatrix[0], 0, m_nMemorySize);
    m_bCreated = true;
    row.m_ppMatrix = m_ppMatrix;
    row.m_nXSize   = m_nXSize;
}

template<class T>
CMatrix<T>::CMatrix(const CMatrix& matrix)
{
    m_nXSize = matrix.m_nXSize;
    m_nYSize = matrix.m_nYSize;
    m_nMemorySize = m_nYSize*m_nXSize*sizeof(T);

    m_ppMatrix    = new T* [m_nYSize];
    ASSERT(m_ppMatrix!=NULL);

    m_ppMatrix[0] = new T  [m_nYSize*m_nXSize];
    ASSERT(m_ppMatrix[0]!=NULL);

    for (int i=1; i<m_nYSize; i++)
        m_ppMatrix[i] = m_ppMatrix[0]+i*m_nXSize;

    memcpy(m_ppMatrix[0],matrix.m_ppMatrix[0], m_nMemorySize);

    m_bCreated = true;
}


template<class T>
CMatrix<T>& CMatrix<T>::operator= (const CMatrix& matrix)
{
    if (this == &matrix) return *this;

    ASSERT(m_nXSize == matrix.m_nXSize && 
        m_nYSize == matrix.m_nYSize);
    memcpy(m_ppMatrix[0],matrix.m_ppMatrix[0], m_nMemorySize);

    return *this;
}
template<class T>
T CMatrix<T>::GetMinValue()
{
    T minValue = m_ppMatrix[0][0];
    int i,j;

    for (i=0; i<m_nYSize; i++)
        for (j=0; j<m_nXSize; j++)
        {
            if(m_ppMatrix[i][j]<minValue)
                minValue = m_ppMatrix[i][j];
        }
        return minValue;
}

template<class T>
T CMatrix<T>::GetMaxValue()
{
    T maxValue = m_ppMatrix[0][0];
    int i,j;

    for (i=0; i<m_nYSize; i++)
        for (j=0; j<m_nXSize; j++)
        {
            if(m_ppMatrix[i][j]>maxValue)
                maxValue = m_ppMatrix[i][j];
        }
        return maxValue;
}

template<class T>
CMatrix<T> CMatrix<T>::operator+ (const T& item)
{
    int i, j;

    CMatrix<T> mtrx(m_nYSize, m_nXSize);
    for (i=0; i<m_nYSize; i++)
        for (j=0; j<m_nXSize; j++)
        {
            mtrx.m_ppMatrix[i][j] = m_ppMatrix[i][j]+item ;
        }
        return mtrx;
}

template<class T>
CMatrix<T> CMatrix<T>::operator- (const T& item)
{
    int i, j;

    CMatrix<T> mtrx(m_nYSize, m_nXSize);
    for (i=0; i<m_nYSize; i++)
        for (j=0; j<m_nXSize; j++)
        {
            mtrx.m_ppMatrix[i][j] = m_ppMatrix[i][j]-item ;
        }
        return mtrx;
}

template<class T>
CMatrix<T>::~CMatrix()
{
    if (m_bCreated)
    {
        delete [] m_ppMatrix[0];
        delete [] m_ppMatrix;
    }
}

template<class T>
int CMatrix<T>::GetXSize()
{
    return m_nXSize;
}

template<class T>
T CMatrix<T>::SumAll()
{
    T sum = 0;
    int i, j;

    for (i=0; i<m_nYSize; i++)
        for (j=0; j<m_nXSize; j++)
        {
            sum += m_ppMatrix[i][j];
        }
        return sum;
}

template<class T>
int CMatrix<T>::GetYSize()
{
    return m_nYSize;
}
template<class T>        //Y(row) X(col)      
T& CMatrix<T>::operator()(int i, int j)
{
    ASSERT(i>=0 && i<m_nYSize &&
        j>=0 && j<m_nXSize);

    return m_ppMatrix[i][j];
}

//Fancy Indexing
template<class T> 
Container2DRow<T> CMatrix<T>::operator [] (int i)
{
    ASSERT(i>=0 && i<m_nYSize); 
    row.i = i;
    return row;
}

template<class T> 
const Container2DRow<T> CMatrix<T>::operator [] (int i) const
{
    ASSERT(i>=0 && i<m_nYSize); 
    row.i = i;
    return row;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
Currently use C++, C# and Python. Pascal, Delphi and FORTRAN in the past.

Comments and Discussions

 
Generalnice article Pin
lxz20889-May-06 23:03
lxz20889-May-06 23:03 
GeneralContainer loading heuristic algorithm Pin
rajeshdara25-Mar-06 6:32
rajeshdara25-Mar-06 6:32 
GeneralRow and Cols Pin
schnufftrax5-Jan-05 21:55
schnufftrax5-Jan-05 21:55 
Generaloperator[] Pin
Member 124058822-Oct-04 2:54
Member 124058822-Oct-04 2:54 
GeneralRe: operator[] Pin
Alex Chirokov31-Oct-04 11:35
Alex Chirokov31-Oct-04 11:35 
GeneralVisual C++ (MFC) : Using Matrix to set an part of an image to zero Pin
pohcb_sonic15-Sep-04 16:36
pohcb_sonic15-Sep-04 16:36 
Hi! I'm new to MFC in Visual C++ programming. I had previously used it to do console applications but never used MFC before. I had a problem; I was told to set a 4 by 4 area by using Matrix in MFC coding and use it to check whether the image has any little "black spots". If it does, I have to set these small little "black spots" of the image from "255" or anything else to zero.

So How do I do it?

Pls do kindly reply. I needed this asap. Thanks!
Smile | :)

Ps: here are all my codings:

<br />
<br />
// MediVisionView.cpp : implementation of the CMediVisionView class<br />
//<br />
<br />
#include "stdafx.h"<br />
#include "MediVision.h"<br />
#include "PatientDialogBox.h"<br />
#include "MediVisionDoc.h"<br />
#include "MediVisionView.h"<br />
#include "Glob.h"<br />
#include "Math.h"<br />
#include "Edit_Patient_Data.h"<br />
#include "Expand_Pixel.h"<br />
#include "Rows_Cols.h"<br />
<br />
#include <vector><br />
#include <string><br />
<br />
typedef std::vector<std::string> StringArray; //typedef vector of string type<br />
using namespace std; //do not need to prepend std:: to Standard Library<br />
<br />
<br />
#ifdef _DEBUG<br />
#define new DEBUG_NEW<br />
#undef THIS_FILE<br />
static char THIS_FILE[] = __FILE__;<br />
#endif<br />
<br />
unsigned char	*argbPixelsPtr;<br />
unsigned char	*rgbPixelsPtr;<br />
unsigned int	argbPixels[512][512];<br />
unsigned int	argb256Pixels[256][256];<br />
unsigned int	rgbPixels[512][512];<br />
unsigned int	rgb256Pixels[256][256];<br />
unsigned int	storeargbPixels[512][512];<br />
<br />
<br />
int				*Bitmap_r, *Bitmap_g, *Bitmap_b;<br />
int				*image[5], *store[5], *image256, *store256, *image512, *store512, *image32, *store32,*image16, *store16;<br />
int				mouse1_x, mouse1_y, mouse2_x, mouse2_y;	//Store mousedown and mouseup coordinate (For rectangle)<br />
int				rows1,rows2,cols1,cols2;<br />
int				clk_mouse_x, clk_mouse_y;				//Store reference point wrt to the screen left hand corner 						<br />
int				ref_selected_x, ref_selected_y;			//Store the computed reference point wrt to the image left hand corner <br />
int				ref_intensity;							//Store Reference point intensity (for region growing) value <br />
int				Grad_x, Grad_y, Grad_z;					//For storing Gradient values during volume rendering<br />
<br />
<br />
short			dcmPixel[480][262144];<br />
<br />
<br />
float			alphaTable[262144];<br />
float			luminanceTable[262144];<br />
float			MidX, MidY, MidZ;<br />
float			Translation[4][4];<br />
float			Rotation[4][4];<br />
float			pt_x[5], pt_y[5], pt_z[5];<br />
<br />
<br />
char			UID[30];<br />
<br />
<br />
int				*pixelvalue, maxpix;<br />
int				box_x, box_y;							// Co-ordinates of the top left hand corner of box with reference to image corner.<br />
CDC				*pDC;<br />
HDC				hdc;<br />
int				n_rows, n_cols;<br />
int				k = 0;<br />
int				Expand_Pixel;<br />
int				s, nCount;<br />
<br />
<br />
CString			strAppDirectory;<br />
long			position, sliceThickness_position,ImageNo_position,Axis_position,PixelSize_position;<br />
int				** region;<br />
int				pass;						// Number of times that ReadFile module is called<br />
											// This is use for deleted the pointers as we want<br />
bool			insert, rightbut;<br />
											// do not want to delete the last set of image data of 3D maximum intensity option<br />
bool			noFile, Flag3D;				// Flag3D is a flag to indicate that the 3D maximum intensity option is selected<br />
bool			box, ref_pt;  //<------------------------<br />
<br />
<br />
// camera function<br />
int magnification;<br />
bool			zoom, grid;<br />
<br />
bool			eraser, leftbut;<br />
bool			multiple_open;<br />
bool			cancel1,cancel2;<br />
CPoint			er_point;<br />
CPoint			Anchor;<br />
FILE			*fp, *fw, *fe;					// File pointer<br />
<br />
char			*st;<br />
<br />
bool			edited;<br />
void SetClassificationTable();<br />
void DrawRectn();<br />
<br />
////////////////////////////Modified Variables(For Object Lining Menu)////////////////////////////////////<br />
//-----------New coding-------------//<br />
bool   Rectn; //To define the Rectangular box item created.<br />
<br />
//int	   m1_x, m1_y, m2_x, m2_y;	//Store mousedown and mouseup coordinate (For Rectangular box)<br />
<br />
int	   mouse_x, mouse_y;		//Store reference point with correspond to the screen left hand corner 		<br />
<br />
int num; // number<br />
<br />
int r, c; //rows and column of Rectangular Box<br />
<br />
int max, min; //maximum and minimum of image<br />
<br />
int point_x, point_y; //Define the points inside the image<br />
<br />
int point_x1, point_y1; //Define the points inside the image<br />
<br />
int point_w1, point_z1;<br />
<br />
bool minflag, maxflag;<br />
<br />
int sg_length, sg_height; // Length and height of the Rectangular box<br />
<br />
/////////////////////////////////////////////////////////////////////////////<br />
//--------------End of coding-----------------//<br />
<br />
// CMediVisionView<br />
<br />
IMPLEMENT_DYNCREATE(CMediVisionView, CView)<br />
BEGIN_MESSAGE_MAP(CMediVisionView, CView)<br />
	//{{AFX_MSG_MAP(CMediVisionView)<br />
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)<br />
	ON_WM_ERASEBKGND()<br />
	ON_COMMAND(ID_3D_MAXIMUMINTENSITY, On3dMaximumintensity)<br />
	ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)<br />
	ON_COMMAND(ID_3D_SURFACERENDERING, On3dSurfacerendering)<br />
	ON_WM_LBUTTONDOWN()<br />
	ON_WM_LBUTTONUP()<br />
	ON_WM_MOUSEMOVE()<br />
	ON_COMMAND(ID_SEGMENTATION_REGIONGROWING, OnSegmentationRegiongrowing)<br />
	ON_WM_SETCURSOR()<br />
	ON_COMMAND(ID_FILE_MULTIPLEOPEN, OnFileMultipleopen)<br />
	ON_COMMAND(ID_RToolbar_3DIntensity, OnRToolbar3DIntensity)<br />
	ON_COMMAND(ID_RToolbar_Multipleopen, OnRToolbarMultipleopen)<br />
	ON_COMMAND(ID_RToolbar2_Region, OnRToolbar2Region)<br />
	ON_COMMAND(ID_3D_VOLUMERENDERING, On3dVolumerendering)<br />
	ON_COMMAND(ID_TOOLS_ERASER, OnToolsEraser)<br />
	ON_COMMAND(ID_TOOLS_BOOLEAN, OnToolsBoolean)<br />
	ON_COMMAND(ID_TOOLS_MAXIMATION, OnToolsMaximation)<br />
	ON_COMMAND(ID_ERASER, OnEraser)<br />
	ON_COMMAND(ID_TOOLS_PEN, OnToolsPen)<br />
	ON_COMMAND(ID_PEN, OnPen)<br />
	ON_COMMAND(ID_EDIT_PATIENT_DATA, OnEditPatientData)<br />
	ON_COMMAND(ID_MORPHOLOFICAL, OnMorpholofical)<br />
	ON_COMMAND(ID_ZOOM_2X, OnZoom2x)<br />
	ON_COMMAND(ID_ZOOM_4X, OnZoom4x)<br />
	ON_COMMAND(ID_TOOLS_EXPANSION, OnToolsExpansion)<br />
	ON_COMMAND(ID_LINE_OBJECTS, OnLineObjects)<br />
	ON_WM_RBUTTONDOWN()<br />
	//}}AFX_MSG_MAP<br />
	// Standard printing commands<br />
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)<br />
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)<br />
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)<br />
END_MESSAGE_MAP()<br />
<br />
/////////////////////////////////////////////////////////////////////////////<br />
// CMediVisionView construction/destruction<br />
<br />
CMediVisionView::CMediVisionView()<br />
{<br />
	// TODO: add construction code here<br />
	m_File1 = _T("  ");<br />
	pixelmax = 256;<br />
	maxpix	 = 256;<br />
	multiple_open = false;<br />
	no_of_rows = 0;<br />
	no_of_cols = 0;<br />
	box = false;<br />
	eraser = false;<br />
	leftbut = false;<br />
	ref_pt = false;<br />
	MotionFix = 0;<br />
	m_Dragging = false;<br />
	Expand_Pixel = 0;<br />
	<br />
<br />
	//-------------New Coding----------------//<br />
	Rectn = false; // Disables the drawing of the rectangle<br />
	MDrag = false; //Disables the Dragging of the mouse<br />
	Motion = 0;  //Set the motion of the mouse to draw rectangle to none<br />
	mini = 0;<br />
	maxi=0;<br />
<br />
	//---------------End of coding-------------//<br />
<br />
<br />
<br />
}<br />
<br />
CMediVisionView::~CMediVisionView()<br />
{<br />
//	delete [] region;<br />
}<br />
<br />
BOOL CMediVisionView::PreCreateWindow(CREATESTRUCT& cs)<br />
{<br />
	// TODO: Modify the Window class or styles here by modifying<br />
	//  the CREATESTRUCT cs<br />
<br />
	return CView::PreCreateWindow(cs);<br />
}<br />
<br />
/////////////////////////////////////////////////////////////////////////////<br />
// CMediVisionView drawing<br />
<br />
<br />
void FrameLines(CDC *pDC) {<br />
<br />
//	CPen newPen(PS_SOLID,pixelsize, colour);<br />
//	CPen* pOldPen = pDC->SelectObject(&newPen);<br />
//	pDC->MoveTo(x,y); // move current position of pen<br />
//	pDC->LineTo(x+1,y+1);<br />
//	pDC->SelectObject(pOldPen);// reset back the old pen object<br />
<br />
	CBrush brush, *pOB;<br />
<br />
	int xSize=::GetSystemMetrics(SM_CXSCREEN);<br />
	int ySize=::GetSystemMetrics(SM_CYSCREEN);<br />
<br />
	brush.CreateSolidBrush(RGB(210,220,205)); <br />
	pOB = pDC->SelectObject(&brush);<br />
<br />
	pDC->Rectangle(525, -1, 530, ySize);<br />
	pDC->Rectangle(529, (ySize/2 - 45), xSize, (ySize/2-40));<br />
}<br />
<br />
<br />
<br />
void CMediVisionView::OnDraw(CDC* pDC)<br />
{<br />
	CMediVisionDoc* pDoc = GetDocument();<br />
	ASSERT_VALID(pDoc);<br />
	// TODO: add draw code for native data here<br />
<br />
<br />
	if (First==TRUE) {<br />
		First=FALSE;<br />
		Draw();						// If it is the first time, use draw <br />
		CClientDC ClientDC(this);<br />
		FrameLines(&ClientDC);<br />
	<br />
	}<br />
	else {	<br />
		ReDraw();					// To redraw<br />
	}<br />
}<br />
<br />
void DrawBox(CDC *pdc,CPoint m_One, CPoint m_Two)<br />
{<br />
	pdc->MoveTo(m_One);<br />
	pdc->LineTo(m_One.x, m_Two.y);<br />
<br />
	pdc->MoveTo(m_One);<br />
	pdc->LineTo(m_Two.x, m_One.y);<br />
<br />
	pdc->MoveTo(m_Two.x,m_One.y);<br />
	pdc->LineTo(m_Two.x, m_Two.y);<br />
<br />
	pdc->MoveTo(m_One.x,m_Two.y);<br />
	pdc->LineTo(m_Two.x, m_Two.y);<br />
	<br />
}<br />
<br />
/////////////////////////////////////////////////////////////////////////////<br />
// CMediVisionView printing<br />
<br />
BOOL CMediVisionView::OnPreparePrinting(CPrintInfo* pInfo)<br />
{<br />
	// default preparation<br />
	return DoPreparePrinting(pInfo);<br />
}<br />
<br />
void CMediVisionView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)<br />
{<br />
	// TODO: add extra initialization before printing<br />
}<br />
<br />
void CMediVisionView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)<br />
{<br />
	// TODO: add cleanup after printing<br />
}<br />
<br />
/////////////////////////////////////////////////////////////////////////////<br />
// CMediVisionView diagnostics<br />
<br />
#ifdef _DEBUG<br />
void CMediVisionView::AssertValid() const<br />
{<br />
	CView::AssertValid();<br />
}<br />
<br />
void CMediVisionView::Dump(CDumpContext& dc) const<br />
{<br />
	CView::Dump(dc);<br />
}<br />
<br />
CMediVisionDoc* CMediVisionView::GetDocument() // non-debug version is inline<br />
{<br />
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMediVisionDoc)));<br />
	return (CMediVisionDoc*)m_pDocument;<br />
}<br />
#endif //_DEBUG<br />
<br />
/////////////////////////////////////////////////////////////////////////////<br />
<br />
/*	This routine(onFileOpen) is use for opening only 1 selected file. <br />
	It will call ReadFile to read the Dicom file and display it.*/<br />
<br />
////////////////////////////////////////////////////////////////////////////<br />
CString file;<br />
<br />
void CMediVisionView::OnFileOpen() <br />
{<br />
<br />
	// TODO: Add your command handler code here<br />
<br />
//	static char BASED_CODE szFilter[] = "All Files (*.*)|*.*|Bitmap Files(*.bmp)| *.bmp|\<br />
//				DICOM Files(*.dcm) | *.dcm";<br />
	multiple_open=false;<br />
	image_no=0;<br />
	OpenFile();	<br />
}<br />
<br />
////////////////////////////////////////////////////////////////////<br />
/*	These routines OnInitial Update to OnEraseBkgnd is <br />
	used topaint the background Blue */<br />
///////////////////////////////////////////////////////////////////<br />
<br />
void CMediVisionView::OnInitialUpdate() <br />
{<br />
	CView::OnInitialUpdate();<br />
	<br />
	// TODO: Add your specialized code here and/or call the base class<br />
	SetBackgroundColor(RGB(50, 50, 50));<br />
	CMediVisionDoc* pDoc = GetDocument();<br />
	pDoc->SetPathName("LEO",TRUE);<br />
}<br />
<br />
void CMediVisionView::SetBackgroundColor(COLORREF crBackground)<br />
{<br />
	m_crBackground = crBackground;<br />
	if (m_wndbkBrush.GetSafeHandle())<br />
		m_wndbkBrush.DeleteObject();<br />
	m_wndbkBrush.CreateSolidBrush(m_crBackground);<br />
}<br />
<br />
<br />
BOOL CMediVisionView::OnEraseBkgnd(CDC* pDC) <br />
{<br />
	// TODO: Add your message handler code here and/or call default<br />
	CRect rect;<br />
	GetClientRect(rect);<br />
	pDC->FillRect(&rect, &m_wndbkBrush);<br />
	return TRUE;<br />
<br />
}<br />
<br />
/////////////////////////////////////////////////////////////////////<br />
/*	This routine ReDraw is use to refresh the screen after minimise.<br />
	The routine differentiates drawing of 512x512 or 256x256 image data<br />
	For 256x256, it allows multiple images to be screen. For 512x512,<br />
	only 1 image is shown. This routine is called by OnDraw(). */<br />
////////////////////////////////////////////////////////////////////<br />
<br />
void CMediVisionView::ReDraw()<br />
{<br />
	int row, col;<br />
	int p, q, n;<br />
	int	cx_position[6], cy_position[6]; <br />
	unsigned int	data;<br />
<br />
	BITMAPINFOHEADER infoHeader;<br />
	infoHeader.biSize = sizeof(BITMAPINFOHEADER);<br />
	infoHeader.biWidth = n_cols;<br />
	infoHeader.biHeight = n_rows;<br />
	infoHeader.biPlanes=1;<br />
	infoHeader.biBitCount= 24;<br />
	infoHeader.biCompression=0;<br />
	infoHeader.biSizeImage=0;<br />
	infoHeader.biXPelsPerMeter=0;<br />
	infoHeader.biYPelsPerMeter=0;<br />
	infoHeader.biClrUsed=0;<br />
	infoHeader.biClrImportant=0;<br />
<br />
	int xSize=::GetSystemMetrics(SM_CXSCREEN)-80;<br />
	int ySize=::GetSystemMetrics(SM_CYSCREEN);<br />
<br />
	CClientDC ClientDC(this);							// Draw the Frame Lines first<br />
	FrameLines(&ClientDC);<br />
<br />
	if (n_rows== 256 && multiple_open) {<br />
		if(image_no == 1) {<br />
			cx_position[1] = ((xSize/2) - (n_cols/2));<br />
			cy_position[1] = ((ySize/2) - (n_rows/2)) - 70;<br />
		}<br />
		else if(image_no == 2) {<br />
			cx_position[1] = ((xSize/2) - (n_cols+10));<br />
			cy_position[1] = ((ySize/2) - (n_rows/2)) - 70;<br />
			cx_position[2] = ((xSize/2) + 10);<br />
			cy_position[2] = ((ySize/2) - (n_rows/2)) - 70;<br />
		}<br />
		else if(image_no == 3) {<br />
			cx_position[1] = 5;<br />
			cy_position[1] = ((ySize/2) - (n_rows/2)) - 70;<br />
			cx_position[2] = 5 + n_cols + 5;<br />
			cy_position[2] = ((ySize/2) - (n_rows/2)) - 70;<br />
			cx_position[3] = 5 + n_cols + 5 + n_cols + 5;<br />
			cy_position[3] = ((ySize/2) - (n_rows/2)) - 70;<br />
		}<br />
		else if(image_no == 4) {<br />
			cx_position[1] = ((xSize/2) - (n_cols+20));<br />
			cy_position[1] = ((ySize/2) - (n_rows+20)) - 50;<br />
			cx_position[2] = ((xSize/2) +20);<br />
			cy_position[2] = ((ySize/2) - (n_rows+20)) - 50;<br />
			cx_position[3] = ((xSize/2) - (n_cols+20));<br />
			cy_position[3] = ((ySize/2) +10) - 70;<br />
			cx_position[4] = ((xSize/2) +20);<br />
			cy_position[4] = ((ySize/2) +10) - 70;<br />
		}<br />
	<br />
		for(int s=1;s<=image_no;s++) {<br />
			n = 0;<br />
			for(int p=0;p<n_rows;p++) {<br />
				for(int q=0;q<n_cols;q++) {<br />
					data = image256[n];<br />
					if (data <= 0) {<br />
						argb256Pixels[p][q] = (unsigned int)(0 | 0 | 0 | 0);<br />
					}<br />
					else {<br />
						argb256Pixels[p][q] = (unsigned int)(data<<24 | data<<16 | data<<8 | data);<br />
					}<br />
					n++;<br />
				}<br />
			}<br />
<br />
			argbPixelsPtr = (unsigned char *) argb256Pixels;<br />
			rgbPixelsPtr = (unsigned char *) rgb256Pixels;<br />
<br />
			for (row=0; row<n_rows; row++) {<br />
				for(col=0; col<n_cols; col++) {<br />
					*rgbPixelsPtr++ = *argbPixelsPtr++;<br />
					*rgbPixelsPtr++ = *argbPixelsPtr++;<br />
					*rgbPixelsPtr++ = *argbPixelsPtr++;<br />
					argbPixelsPtr++;<br />
				}		<br />
			}<br />
			<br />
			StretchDIBits(hdc, cx_position[s], cy_position[s], n_rows, n_cols, 0, 0, n_rows, n_cols, rgb256Pixels,(BITMAPINFO*) &infoHeader, 0, SRCCOPY);	<br />
		}<br />
	}<br />
<br />
<br />
	else if (n_rows== 256 && !multiple_open) {<br />
		n = 0;<br />
		for (p=0; p<n_rows; p++) {<br />
			for (q=0; q<n_cols; q++) {<br />
				data = image256[n];<br />
				if (data <= 0) {<br />
					argb256Pixels[p][q] = (unsigned int)(0 | 0 | 0 | 0);<br />
				}<br />
				else {<br />
					argb256Pixels[p][q] = (unsigned int)(data<<24 | data<<16 | data<<8 | data);<br />
				}<br />
				n++;<br />
			}<br />
		}<br />
		<br />
		argbPixelsPtr = (unsigned char *) argb256Pixels;<br />
		rgbPixelsPtr = (unsigned char *) rgb256Pixels;<br />
<br />
		for (row=0; row<n_rows; row++) {<br />
			for(col=0; col<n_cols; col++) {<br />
				*rgbPixelsPtr++ = *argbPixelsPtr++;<br />
				*rgbPixelsPtr++ = *argbPixelsPtr++;<br />
				*rgbPixelsPtr++ = *argbPixelsPtr++;<br />
				argbPixelsPtr++;<br />
			}		<br />
		}<br />
		StretchDIBits(hdc, posx256, posy256, n_rows, n_cols, 0, 0, n_rows, n_cols, rgb256Pixels,(BITMAPINFO*) &infoHeader, 0, SRCCOPY);	<br />
				<br />
	}<br />
<br />
	else if(n_rows==512){<br />
		n = 0;<br />
		for (p=0; p<n_rows; p++) {<br />
			for (q=0; q<n_cols; q++) {<br />
				data = image512[n];<br />
				if (data <=0) {<br />
					argbPixels[p][q] = (unsigned int)(0 | 0 | 0 | 0);			<br />
				}<br />
				else {<br />
					argbPixels[p][q] = (unsigned int)(data<<24 | data<<16 | data<<8 | data);<br />
				}<br />
				n++;<br />
			}<br />
		}<br />
	<br />
		argbPixelsPtr = (unsigned char *) argbPixels;<br />
		rgbPixelsPtr = (unsigned char *) rgbPixels;<br />
<br />
		for (row=0; row<n_rows; row++) {<br />
			for(col=0; col<n_cols; col++) {<br />
				*rgbPixelsPtr++ = *argbPixelsPtr++;<br />
				*rgbPixelsPtr++ = *argbPixelsPtr++;<br />
				*rgbPixelsPtr++ = *argbPixelsPtr++;<br />
				argbPixelsPtr++;<br />
			}		<br />
		}		<br />
		StretchDIBits(hdc, posx512,posy512, n_rows, n_cols, 0, 0, n_rows, n_cols, rgbPixels,(BITMAPINFO*) &infoHeader, 0, SRCCOPY);	<br />
	}	<br />
}<br />
<br />
void CMediVisionView::On3dMaximumintensity() <br />
{<br />
	// TODO: Add your command handler code here<br />
	<br />
	static char BASED_CODE szFilter[] = "All Files (*.*)|*.*|Dicom Files (*.dcm)|*.dcm||";<br />
	CFileDialog dlg( TRUE, NULL, NULL, OFN_ALLOWMULTISELECT | OFN_EXPLORER, szFilter);<br />
<br />
//	dlg.m_ofn.lpstrInitialDir = "d:\\bmestudent\\dicom project\\dicom"; //testing purpose<br />
	Flag3D	= true;<br />
	multiple_open = false;<br />
	pass	= 0;<br />
	DWORD MAXFILE = 13562;							//5562 is the max<br />
	dlg.m_ofn.nMaxFile = MAXFILE;<br />
<br />
	char* buffer = new char[MAXFILE];<br />
<br />
	dlg.m_ofn.lpstrFile = buffer;<br />
	dlg.m_ofn.lpstrFile[0] = NULL;<br />
<br />
	int iReturn = dlg.DoModal();<br />
<br />
	nCount = 0;										// Count the number of files open<br />
	<br />
	if (iReturn == 1) {<br />
		POSITION pos = dlg.GetStartPosition();<br />
		POSITION pos1 = dlg.GetStartPosition();<br />
		if (pos1) {<br />
			do {<br />
				std::string Pathname;<br />
				Pathname = dlg.GetNextPathName(pos1);<br />
				nCount++;									// Count the number of files selected<br />
			} while (pos1);		<br />
		}<br />
<br />
		if(pos) {<br />
<br />
			image_no=1;<br />
<br />
			do {<br />
<br />
				m_File1 = dlg.GetNextPathName(pos);<br />
				if ((fp = fopen(m_File1, "rb")) == NULL) {<br />
					noFile = true;<br />
				}<br />
				else {<br />
					noFile = false;<br />
				}<br />
<br />
				ReadFile();<br />
													<br />
//				pDC = GetDC();											<br />
//				hdc=pDC->GetSafeHdc();<br />
//				Draw();<br />
<br />
				if (Flag3D == true) {<br />
					delete [] pixelvalue;<br />
					if (pass < nCount) {<br />
						if (n_rows == 512) {<br />
							delete [] image512;							// Delete storage due to reptitive creation in the 3Dmaximum intensity<br />
							delete [] store512;<br />
						}<br />
						else if (n_rows == 256 && !multiple_open) {<br />
							delete [] image256;							// Only the first image array parameter was used for 3D display, so delete it<br />
							delete [] store256;<br />
						}<br />
						else if (n_rows == 256 && multiple_open) {<br />
							delete [] image[image_no];<br />
							delete [] store[image_no];<br />
						}<br />
					}<br />
				}<br />
<br />
			} while(pos);	<br />
			<br />
		}<br />
<br />
		if (no_of_rows==256) {<br />
			image256=new int [no_of_rows*no_of_cols];	// Creating memory for storing an array of image data<br />
			store256=new int [no_of_rows*no_of_cols];<br />
		}<br />
		else {<br />
			image512 = new int [no_of_rows*no_of_cols];<br />
			store512 = new int [no_of_rows*no_of_cols];<br />
		}<br />
<br />
		Compute_Vol_Max_Intensity();<br />
		Invalidate();	//refresh window<br />
		UpdateWindow();<br />
		pDC = GetDC();<br />
		hdc=pDC->GetSafeHdc();<br />
		Draw();<br />
<br />
	}			<br />
}<br />
<br />
void CMediVisionView::ReadFile()<br />
{<br />
<br />
	int		i, loop, n, p,q;<br />
	short	tag[2];<br />
	long	num;<br />
	char	a;<br />
	char	VR[2], DICM[5], *string;//, *pixel_size;<br />
	int		iLength;<br />
	short	ValueLength; <br />
	short	ValueField;<br />
	short	ReservedField;								// For OB, OW, SQ and UN data type only<br />
	unsigned short	sdata;<br />
	unsigned int	idata;<br />
	bool	flag, pixelread;<br />
<br />
//	if ((fw = fopen(strAppDirectory + "\\test.dcm","w+b")) == NULL)<br />
	if ((fw = fopen("test.dcm","w+b")) == NULL) <br />
		AfxMessageBox("Cannot open binary file for some reason");<br />
<br />
	if (noFile == false) {<br />
		flag = true;<br />
		pixelread = true;<br />
		pixelmax = 256;<br />
		n=0;<br />
		fseek(fp, 128, 0);								// Skip 128 Bytes	<br />
		fseek(fw, 128, 0);<br />
		fgets(DICM,5,fp);<br />
<br />
//--READING OF TAGS IN DICOM FILE ------------------------------------<br />
<br />
		if (strcmp(DICM, "DICM")!=0) {					// Not the latest DICM file<br />
			fseek(fp, 0, 0);							// Push the File pointer to the beginning of file<br />
			fseek(fw, 0, 0);	<br />
			string=new char[20];						// For deletion purpose<br />
			strcpy(UID, "1.2.840.10008.1.2");	<br />
		}<br />
		else<br />
			fwrite(DICM, sizeof(char), 4, fw);<br />
<br />
		while (flag == true) {<br />
			for (i=0; i<2;i++) {								// Read Tag - 2 bytes<br />
				fread(&tag[i], sizeof(unsigned short), 1, fp);	// Ordered pair of 16-bit unsigned int		<br />
				fwrite(&tag[i], sizeof(unsigned short), 1, fw);<br />
			}<br />
<br />
//--READING AND WRITING DATA ELEMENT OF HEADER FOR SAVING------------------------------------<br />
	<br />
			if (tag[0] < 32736) {<br />
				if (tag[0]==2) {		// For tag 2 only<br />
					strcpy(VR, " ");<br />
					fgets(VR,3,fp);										// Read Value Representation<br />
					fwrite(VR, sizeof(char), 2, fw);<br />
<br />
					if (tag[1]==16) {						// Read the Transfer Syntax data UID<br />
						fread(&ValueLength, sizeof(unsigned short), 1, fp);	// Read Value Length (VL) - 16 bits<br />
						fwrite(&ValueLength, sizeof(unsigned short), 1, fw);<br />
<br />
						fgets(UID,ValueLength+1,fp);<br />
						fwrite(UID, sizeof(char), ValueLength, fw);<br />
					}<br />
					else {<br />
						if (strcmp(VR, "UL")==0) {<br />
							fread(&ValueLength, sizeof(unsigned short), 1, fp);	// Read Value Length (VL) - 16 bits<br />
							fwrite(&ValueLength, sizeof(unsigned short), 1, fw);<br />
							fread(&ValueField, sizeof(unsigned long), 1, fp);	// Read Value Field (4 bytes as indicated by VL)<br />
							fwrite(&ValueField, sizeof(unsigned long), 1, fw);<br />
						}<br />
<br />
						else if (strcmp(VR,"OB")==0) {<br />
							fread(&ReservedField, sizeof(unsigned short), 1, fp);// Read Reserved Field<br />
							fwrite(&ReservedField, sizeof(unsigned short), 1, fw);<br />
							fread(&ValueLength, sizeof(unsigned short), 1, fp);	// Read Value Length (VL) - 16 bits<br />
							fwrite(&ValueLength, sizeof(unsigned short), 1, fw);<br />
							fread(&ValueField, sizeof(unsigned long), 1, fp);	// Value Field (4 bytes as indicated by VL)<br />
							fwrite(&ValueField, sizeof(unsigned long), 1, fw);<br />
						}<br />
<br />
						else if (strcmp(VR, "UI")==0 || strcmp(VR, "SH")==0 || strcmp(VR, "AE")==0) {<br />
							fread(&ValueLength, sizeof(unsigned short), 1, fp);	// Read Value Length (VL) - 16 bits<br />
							fwrite(&ValueLength, sizeof(unsigned short), 1, fw);<br />
							string=new char[ValueLength+1];<br />
							fgets(string,ValueLength+1,fp);<br />
							fwrite(string, sizeof(char),ValueLength,fw);<br />
						}<br />
<br />
						else {													// Read Implicit CS<br />
							num = (int)VR[0];<br />
							fgetc(fp);<br />
				<br />
							for (loop=0; loop<num+2; loop++) {<br />
								a = fgetc(fp);<br />
								fputc(a,fw);<br />
							}<br />
						}<br />
					}<br />
				}<br />
				<br />
				else {															// else tag[0] not equals to 2<br />
//------------------------------------------------------------------------------<br />
					if (strcmp(UID, "1.2.840.10008.1.2.1")==0) {<br />
						strcpy(VR, " ");<br />
						fgets(VR,3,fp);<br />
						fwrite(VR, sizeof(char), 2, fw);<br />
						<br />
						if (tag[0]==40) {<br />
							if (tag[1]==16) {										// Reading of number of pixel rows<br />
								fread(&ValueLength, sizeof(unsigned short), 1, fp); 		// Read Value Length (VL) - 16 bits<br />
								fwrite(&ValueLength, sizeof(unsigned short), 1, fw);<br />
								fread(&sdata, sizeof(unsigned short), 1, fp);		// Read Value Field (4 bytes as indicated by VL)				printf("\tField=%d\n", ValueField);<br />
								fwrite(&sdata, sizeof(unsigned short), 1, fw);<br />
								no_of_rows	= sdata;<br />
								n_rows		= no_of_rows;<br />
							}<br />
							else if (tag[1]==17) {									// Reading of number of pixel cols<br />
								fread(&ValueLength, sizeof(unsigned short), 1, fp); 		// Read Value Length (VL) - 16 bits<br />
								fwrite(&ValueLength, sizeof(unsigned short), 1, fw);<br />
								fread(&sdata, sizeof(unsigned short), 1, fp);		// Read Value Field (4 bytes as indicated by VL)				printf("\tField=%d\n", ValueField);<br />
								fwrite(&sdata, sizeof(unsigned short), 1, fw);<br />
								no_of_cols	= sdata;<br />
								n_cols		= no_of_cols;<br />
							}<br />
							else if (tag[1]==263) {									// Reading of maximum pixel value<br />
								fread(&ValueLength, sizeof(unsigned short), 1, fp); 		// Read Value Length (VL) - 16 bits<br />
								fwrite(&ValueLength, sizeof(unsigned short), 1, fw);<br />
								fread(&sdata, sizeof(unsigned short), 1, fp);		// Read Value Field (4 bytes as indicated by VL)				printf("\tField=%d\n", ValueField);<br />
								fwrite(&sdata, sizeof(unsigned short), 1, fw);<br />
								if (sdata <= 5) pixelmax=256;									// If no pixelmax data, set it of 256<br />
								else pixelmax = sdata;<br />
							}<br />
							else {<br />
								if (strcmp(VR, "UL") ==0) {<br />
									fread(&ValueLength, sizeof(unsigned short), 1, fp); 	<br />
									fwrite(&ValueLength, sizeof(unsigned short), 1, fw);<br />
									fread(&ValueField, sizeof(unsigned long), 1, fp);<br />
									fwrite(&ValueField, sizeof(unsigned long), 1, fw);<br />
								}<br />
								else if (strcmp(VR,"FL")==0) {<br />
									fread(&ValueLength, sizeof(unsigned short), 1, fp);	// Read Value Length (VL) - 16 bits<br />
									fwrite(&ValueLength, sizeof(unsigned short), 1, fw);<br />
									fread(&iLength, sizeof(unsigned int), 1, fp);	// Read Value Field (4 bytes as indicated by VL)			<br />
									fwrite(&iLength, sizeof(unsigned int), 1, fp);<br />
								}	<br />
								else if (strcmp(VR, "OB") ==0) {<br />
									fread(&ReservedField, sizeof(unsigned short), 1, fp);<br />
									fwrite(&ReservedField, sizeof(unsigned short), 1, fw);<br />
									fread(&iLength, sizeof(unsigned int), 1, fp);<br />
									fwrite(&iLength, sizeof(unsigned int), 1, fw);<br />
									for (int te=0; te<iLength; te++) {<br />
										a=getc(fp);<br />
										putc(a,fw);<br />
									}<br />
								}<br />
							<br />
								else if (strcmp(VR, "SQ") ==0) {<br />
									fread(&ReservedField, sizeof(unsigned short), 1, fp);<br />
									fwrite(&ReservedField, sizeof(unsigned short), 1, fw);<br />
									fread(&iLength, sizeof(unsigned int), 1, fp);<br />
									fwrite(&iLength, sizeof(unsigned int), 1, fw);<br />
									for (int te=0; te<iLength; te++) {<br />
										a=getc(fp);<br />
										putc(a,fw);<br />
									}<br />
								}<br />
								else {<br />
									fread(&ValueLength, sizeof(unsigned short), 1, fp); <br />
									fwrite(&ValueLength, sizeof(unsigned short), 1, fw);<br />
									string=new char[ValueLength+1];							// Reading Value Length<br />
									fgets(string,ValueLength+1,fp);<br />
									fwrite(string, sizeof(char), ValueLength, fw);<br />
								}<br />
							}<br />
						}			<br />
						<br />
						else {<br />
<br />
							if (strcmp(VR, "UL") ==0) {<br />
								fread(&ValueLength, sizeof(unsigned short), 1, fp); 	<br />
								fwrite(&ValueLength, sizeof(unsigned short), 1, fw);<br />
								fread(&ValueField, sizeof(unsigned long), 1, fp);<br />
								fwrite(&ValueField, sizeof(unsigned long), 1, fw);	<br />
							}<br />
							else if (strcmp(VR, "OB") ==0) {<br />
								fread(&ReservedField, sizeof(unsigned short), 1, fp);<br />
								fwrite(&ReservedField, sizeof(unsigned short), 1, fw);<br />
								fread(&iLength, sizeof(unsigned int), 1, fp);<br />
								fwrite(&iLength, sizeof(unsigned int), 1, fw);<br />
								for (int te=0; te<iLength; te++) {<br />
										a=getc(fp);<br />
										putc(a,fw);<br />
								}<br />
							}<br />
							else if (strcmp(VR,"FL")==0) {<br />
								fread(&ValueLength, sizeof(unsigned short), 1, fp);	// Read Value Length (VL) - 16 bits<br />
								fwrite(&ValueLength, sizeof(unsigned short), 1, fw);<br />
								fread(&iLength, sizeof(unsigned int), 1, fp);	// Read Value Field (4 bytes as indicated by VL)			<br />
								fwrite(&iLength, sizeof(unsigned int), 1, fw);<br />
							}<br />
							else if (strcmp(VR, "SQ") ==0) {<br />
								fread(&ReservedField, sizeof(unsigned short), 1, fp);<br />
								fwrite(&ReservedField, sizeof(unsigned short), 1, fw);<br />
								fread(&iLength, sizeof(unsigned int), 1, fp);<br />
								fwrite(&iLength, sizeof(unsigned int), 1, fw);<br />
							<br />
								for (int te=0; te<iLength; te++) {<br />
									a=fgetc(fp);<br />
									fputc(a,fw);<br />
								}<br />
							}<br />
							else {<br />
								fread(&ValueLength, sizeof(unsigned short), 1, fp); 	<br />
								fwrite(&ValueLength, sizeof(unsigned short), 1, fw);<br />
								string=new char[ValueLength+1];							// Reading Value Length<br />
								fgets(string,ValueLength+1,fp);<br />
								fwrite(string, sizeof(char), ValueLength, fw);<br />
							}<br />
						}<br />
					}<br />
//----------------------------------------------------------------------------<br />
				else if (strcmp(UID, "1.2.840.10008.1.2")==0) {<br />
						fread(&ValueLength, sizeof(unsigned int), 1, fp); 			// Read Value Length (VL) - 16 bits<br />
						fwrite(&ValueLength, sizeof(unsigned int), 1, fw);<br />
				<br />
					if (tag[0]==40) {<br />
							if (tag[1] == 0x30) {<br />
								<br />
								PixelSize_position =ftell(fp);<br />
								string=new char[ValueLength+1];<br />
								fgets(string,ValueLength+1,fp);<br />
								fwrite(string, sizeof(char), ValueLength, fw);<br />
							}<br />
							else if (tag[1]==16) {										// Reading of number of pixel rows									<br />
								fread(&sdata, sizeof(unsigned short), 1, fp);		// Read Value Field (4 bytes as indicated by VL)				printf("\tField=%d\n", ValueField);<br />
								fwrite(&sdata, sizeof(unsigned short), 1, fw);<br />
								no_of_rows	= sdata;<br />
								n_rows		= no_of_rows;<br />
							}<br />
							else if (tag[1]==17) {									// Reading of number of pixel cols<br />
								fread(&sdata, sizeof(unsigned short), 1, fp);		// Read Value Field (4 bytes as indicated by VL)				printf("\tField=%d\n", ValueField);<br />
								fwrite(&sdata, sizeof(unsigned short), 1, fw);<br />
								no_of_cols	= sdata;<br />
								n_cols		= no_of_cols;<br />
							}<br />
							else if (tag[1]==263) {									// Reading of maximum pixel value<br />
								fread(&sdata, sizeof(unsigned short), 1, fp);		// Read Value Field (4 bytes as indicated by VL)				printf("\tField=%d\n", ValueField);<br />
								fwrite(&sdata, sizeof(unsigned short), 1, fw);<br />
								if (sdata <= 5) pixelmax=256;									// If no pixelmax data, set it of 256<br />
								else pixelmax = sdata;<br />
							}<br />
							else {<br />
								string=new char[ValueLength+1];							<br />
								fgets(string,ValueLength+1,fp);<br />
								fwrite(string, sizeof(char), ValueLength, fw);<br />
							}<br />
					}<br />
<br />
				<br />
					<br />
						else if ( tag[0] == 0x18 && tag[1] == 0x50 ) {<br />
								sliceThickness_position = ftell(fp);<br />
								string=new char[ValueLength+1];							<br />
								fgets(string,ValueLength+1,fp);<br />
								fwrite(string, sizeof(char), ValueLength, fw);<br />
							<br />
						}<br />
						else if ( tag[0] == 0x20 && tag[1] == 0x13) {<br />
								ImageNo_position = ftell(fp);<br />
								string=new char[ValueLength+1];							<br />
								fgets(string,ValueLength+1,fp);<br />
								fwrite(string, sizeof(char), ValueLength, fw);<br />
								<br />
						}<br />
						else if ( tag[0] == 0x20 && tag[1] == 0x32) {								<br />
								Axis_position = ftell(fp);<br />
								string=new char[ValueLength+1];							<br />
								fgets(string,ValueLength+1,fp);<br />
								fwrite(string, sizeof(char), ValueLength, fw);<br />
								<br />
						}<br />
						<br />
					<br />
					<br />
<br />
					else if (tag[0] == 0x817) {<br />
						fseek(fp, ValueLength, SEEK_CUR);<br />
						fseek(fw, ValueLength, SEEK_CUR);<br />
					}<br />
					else {<br />
						string=new char[ValueLength+1];							// Reading Value Length<br />
						fgets(string,ValueLength+1,fp);<br />
						fwrite(string, sizeof(char), ValueLength, fw);<br />
					}<br />
				}												// End of the Transfer Syntax UID if block<br />
			}													// End of the tag[0]=2 if block<br />
		}<br />
// -----------Read pixel data ---------------------------------------<br />
	<br />
			else {<br />
<br />
				if (no_of_rows == 512) {<br />
					fread(&idata, sizeof(unsigned int), 1, fp);<br />
					fwrite(&idata, sizeof(unsigned int), 1, fw);<br />
					fread(&idata, sizeof(unsigned int), 1, fp);<br />
					fwrite(&idata, sizeof(unsigned int), 1, fw);<br />
				}<br />
				else if (no_of_rows == 256) {<br />
					fread(&idata, sizeof(unsigned int), 1, fp);<br />
					fwrite(&idata, sizeof(unsigned int), 1, fw);<br />
					fread(&idata, sizeof(unsigned int), 1, fp);<br />
					fwrite(&idata, sizeof(unsigned int), 1, fw);<br />
				}<br />
<br />
				position = ftell(fw);<br />
				fclose (fw);<br />
<br />
				pixelvalue = new int[no_of_rows*no_of_cols];<br />
				if (no_of_rows==256 && multiple_open) {<br />
					image[image_no]=new int [no_of_rows*no_of_cols];	// Creating memory for storing an array of image data<br />
					store[image_no]=new int [no_of_rows*no_of_cols];<br />
				}<br />
				else if (no_of_rows==256 && !multiple_open){<br />
					image256=new int [no_of_rows*no_of_cols];	// Creating memory for storing an array of image data<br />
					store256=new int [no_of_rows*no_of_cols];<br />
				}<br />
				else {<br />
					image512 = new int [no_of_rows*no_of_cols];<br />
					store512 = new int [no_of_rows*no_of_cols];<br />
				}<br />
				while (pixelread==true) {<br />
					fread(&sdata, sizeof(unsigned short), 1, fp);	// Value Field (4 bytes as indicated by VL)<br />
<br />
					if (sdata>5000) <br />
						sdata = 0;					// pixel data too large, probably due to noise<br />
					pixelmax = (sdata > pixelmax)? sdata : pixelmax;<br />
					maxpix = pixelmax;<br />
					pixelvalue[n] = sdata;<br />
					<br />
					n++;							// Increment the reading by 1<br />
<br />
					if (n==(no_of_rows*no_of_cols)) // if finish reading the pixel data<br />
						pixelread=false;<br />
				}<br />
<br />
				flag=false;<br />
			}<br />
		}											// end of while loop<br />
	<br />
		fclose (fp);<br />
	}<br />
<br />
<br />
	n=0;<br />
	for (p=0; p<n_rows; p++) {<br />
		for (q=0; q<n_cols; q++) {<br />
			pixelvalue[n] = ((float)pixelvalue[n]/(float)(pixelmax+1))*256;<br />
			if (Flag3D==true) dcmPixel[pass][n] = pixelvalue[n]; <br />
			if (n_rows==256 && multiple_open) {<br />
				image[image_no][n]=pixelvalue[n];					// Store images data (working copy)<br />
				store[image_no][n]=pixelvalue[n];					// Duplicate the image data<br />
			}<br />
			else if (n_rows==256 && !multiple_open) {<br />
				image256[n]=pixelvalue[n];					// Store images data (working copy)<br />
				store256[n]=pixelvalue[n];					// Duplicate the image data<br />
			}<br />
			else {<br />
				image512[n] = pixelvalue[n];<br />
				store512[n]=pixelvalue[n];<br />
			}<br />
			n++;<br />
		}<br />
	}<br />
	<br />
	pass++;														// use for keeping track of the number of times<br />
	<br />
															// the pointers of image512, store512, etc short of the last pass.<br />
	delete string;<br />
	return;<br />
}<br />
<br />
<br />
void CMediVisionView::Compute_Vol_Max_Intensity()<br />
{<br />
	int x1, y1;<br />
	int	i,j;<br />
<br />
//---------------------------------------------------------<br />
<br />
	x1 = 0;<br />
	y1 = no_of_rows - 1;<br />
<br />
	if (no_of_rows==256) {<br />
		for(i=0;i<no_of_rows*no_of_cols;i++) {<br />
			image256[i]=0;<br />
			for(j=0;j<nCount;j++) {<br />
				image256[i] = <br />
				(dcmPixel[j][i] > image256[i] ? dcmPixel[j][i] : image256[i]);<br />
				store256[i] = image256[i];<br />
			}<br />
		}<br />
	}<br />
	else {<br />
		for(i=0;i<no_of_rows*no_of_cols;i++) {<br />
			image512[i]=0;<br />
			for(j=0;j<nCount;j++) {<br />
<br />
				if (dcmPixel[j][i] > 0) {<br />
					image512[i] = <br />
					(dcmPixel[j][i] > image512[i] ? dcmPixel[j][i] : image512[i]);<br />
					store512[i]=image512[i];<br />
				}<br />
				<br />
			}<br />
		}<br />
	}<br />
<br />
//---------------------------------------------------------------<br />
/*	<br />
	pt_x[2] = (float)(n_rows-1)/2.0;								// Compute the centriod of volume data<br />
	pt_y[2] = (float)(n_cols-1)/2.0;<br />
	pt_z[2] = (float)(nCount-1)/2.0;<br />
	<br />
	float *view_x_plane = new float[n_rows*n_cols];					// create view plane;<br />
	float *view_y_plane = new float[n_rows*n_cols];<br />
	float *view_z_plane = new float[n_rows*n_cols];<br />
	float *Translate_x = new float[n_rows*n_cols];<br />
	float *Translate_y = new float[n_rows*n_cols];<br />
	float *Translate_z = new float[n_rows*n_cols];<br />
<br />
// Generate the co-ordinates of the viewing plane<br />
<br />
	x1 = 0;<br />
	y1 = n_rows-1;<br />
<br />
	for (point_no=0; point_no<n_rows*n_cols; point_no++) {		// Each point_no is the point number on the view plane<br />
		view_x_plane[point_no] = x1;							// The x co-ordinate of the point is view_x_plane [point_no] while the <br />
		view_y_plane[point_no] = y1;							// the y_co-ordinate of the point is view_y_plane [point_no];<br />
		view_z_plane[point_no] = 0;								// The points are number left-right, top-down. point 0 is the top left hand corner<br />
<br />
		x1++;<br />
		if ((x1%n_cols) == 0) {									// Used for numbering of the points on the view-plane<br />
			x1=0;<br />
			y1--;<br />
		}<br />
	}<br />
<br />
	MidX= (n_cols-1.0)/2.0;										// Mid-point of view plane<br />
	MidY= (n_rows-1.0)/2.0;										 <br />
	MidZ= 0;<br />
	<br />
	Translation[0][0] = 1;<br />
	Translation[0][1] = 0;<br />
	Translation[0][2] = 0;<br />
	Translation[0][3] = -((n_cols - 1.0)/2.0);<br />
	Translation[1][0] = 0;<br />
	Translation[1][1] = 1;<br />
	Translation[1][2] = 0;<br />
	Translation[1][3] = -((n_rows - 1.0)/2.0);<br />
	Translation[2][0] = 0;<br />
	Translation[2][1] = 0;<br />
	Translation[2][2] = 1;<br />
	Translation[2][3] = (nCount-1.0)/2.0;<br />
	Translation[3][3] = 1;<br />
<br />
// Perform Translation by centroid value  <br />
<br />
	for (point_no=0; point_no<n_rows*n_cols; point_no++) {		// Offset the view plane by the volume centriod <br />
		view_x_plane[point_no] = view_x_plane[point_no] + Translation[0][3]; <br />
		view_y_plane[point_no] = view_y_plane[point_no] + Translation[1][3];<br />
		view_z_plane[point_no] = view_z_plane[point_no] + Translation[2][3];<br />
	}<br />
<br />
// Perform Rotation about X axis<br />
<br />
	Rotation[0][0] = 1;<br />
	Rotation[0][1] = 0;<br />
	Rotation[0][2] = 0;<br />
	Rotation[0][3] = -((n_cols - 1.0)/2.0);<br />
	Rotation[1][0] = 0;<br />
	Rotation[1][1] = 1;<br />
	Rotation[1][2] = 0;<br />
	Rotation[1][3] = -((n_rows - 1.0)/2.0);<br />
	Rotation[2][0] = 0;<br />
	Rotation[2][1] = 0;<br />
	Rotation[2][2] = 1;<br />
	Rotation[2][3] = (nCount-1.0)/2.0;<br />
	Rotation[3][3] = 1;<br />
<br />
	delete [] newPixel;<br />
	delete [] view_x_plane;<br />
	delete [] view_y_plane;<br />
	delete [] view_z_plane;<br />
	delete [] Translate_x;<br />
	delete [] Translate_y;<br />
	delete [] Translate_z;<br />
*/<br />
	return;<br />
}<br />
<br />
void CMediVisionView::OnFileSaveAs() <br />
{<br />
	// TODO: Add your command handler code here<br />
	FILE *fs;<br />
	<br />
	int n;<br />
	unsigned short sdata;<br />
<br />
	static char BASED_CODE szFilter[] = "DICOM Files (*.dcm)|*.dcm|All Files (*.*)|*.*||";	<br />
	CFileDialog dlg_save_as(FALSE, "dcm", NULL, OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY, szFilter);<br />
	<br />
	if(dlg_save_as.DoModal() == IDOK) {<br />
	//	fs = fopen(strAppDirectory + "\\test.dcm","a+b");<br />
		fs = fopen("test.dcm","a+b");<br />
		fseek(fs , position, 0);<br />
<br />
		for (n=0; n<n_rows*n_cols; n++) { <br />
			if(n_rows == 256) {<br />
				sdata = image256[n]*((float)maxpix/256.0f);					<br />
			}<br />
			else {<br />
				sdata = image512[n]*((float)maxpix/256.0f);				//Rescale back into original form<br />
			}<br />
			fwrite(&sdata, sizeof(unsigned short), 1, fs); //write in new pixel data<br />
		}<br />
<br />
		fclose (fs);<br />
	//	MoveFile(strAppDirectory + "\\test.dcm",dlg_save_as.GetPathName());<br />
		MoveFile( "test.dcm",dlg_save_as.GetPathName());<br />
	}<br />
<br />
}<br />
<br />
<br />
<br />
<br />
void CMediVisionView::On3dSurfacerendering() <br />
{<br />
	// TODO: Add your command handler code here<br />
	int x1, y1;<br />
	int	i,j;<br />
<br />
//---------------------------------------------------------<br />
<br />
	x1 = 0;<br />
	y1 = no_of_rows - 1;<br />
<br />
	if (no_of_rows==256) {<br />
<br />
		j=0;<br />
		for(i=0;i<no_of_rows*no_of_cols;i++) {<br />
			image256[i]=0;<br />
			for (j=0; j<nCount; j++) {<br />
				if (dcmPixel[j][i]>10) {<br />
					image256[i] = dcmPixel[j][i];  <br />
					store256[i] = image256[i];<br />
					break;<br />
				}<br />
			}<br />
		}<br />
	}<br />
<br />
	else {<br />
<br />
		j=0;<br />
		for(i=0;i<no_of_rows*no_of_cols;i++) {<br />
			image512[i]=0;<br />
			for (j=0; j<nCount; j++) {<br />
				if (dcmPixel[j][i]>0) {<br />
					image512[i] = dcmPixel[j][i];<br />
					store512[i] = image512[i];<br />
					break;<br />
				}<br />
			}	<br />
		}	<br />
	}<br />
	pDC = GetDC();<br />
	hdc=pDC->GetSafeHdc();<br />
	Draw();<br />
	return;<br />
}<br />
<br />
void CMediVisionView::OnLButtonDown(UINT nFlags, CPoint point) <br />
{<br />
	<br />
			<br />
	// TODO: Add your message handler code here and/or call default<br />
	if (box) {<br />
		m_PointOrigin = point;<br />
		mouse1_x = point.x;<br />
		mouse1_y = point.y;<br />
		m_Dragging = true;<br />
		<br />
	}<br />
<br />
	if (ref_pt && !box) {							// If the mouse is press down set click <br />
		ref_pt = false;<br />
		clk_mouse_x = point.x;						// Find the reference point position<br />
		clk_mouse_y = point.y;<br />
		Segment_Region();// Segmentation is called only after reference pt has been selected<br />
		<br />
	}<br />
<br />
	//----------------------New coding-------------------------------------//<br />
		//when left Button of the Mouse is pressed down//<br />
<br />
	if (Rectn) {						//Rectn is true<br />
		<br />
		m_PO = point;					//Assign co-ordinates to m_PO<br />
		m1_x = point.x;					//Assign co-ordinate x to m1_x<br />
		m1_y = point.y;					//Assign Co-ordinate y to m1_y<br />
		MDrag = true;					//Activates Dragging of the rectangular box by the mouse<br />
<br />
	}<br />
<br />
	if (!Rectn) {							// If not Rectn, the mouse is pressed down and set click! <br />
						                    //Disables the reference point  <br />
		mouse_x = point.x;					// Find the reference point position<br />
		mouse_y = point.y; <br />
		<br />
		<br />
	}<br />
	<br />
	//--------------------------------------------------------------------//<br />
<br />
<br />
	if(eraser==true) {								//if eraser is selected<br />
		leftbut=true;<br />
	}<br />
<br />
	if(insert==true)								//if insert is selected<br />
		{								<br />
			rightbut=true;<br />
		}<br />
<br />
	CView::OnLButtonDown(nFlags, point);<br />
}<br />
<br />
void CMediVisionView::OnLButtonUp(UINT nFlags, CPoint point) <br />
{<br />
	// TODO: Add your message handler code here and/or call default<br />
	if (m_Dragging) {<br />
		mouse2_x = point.x;<br />
		mouse2_y = point.y;<br />
<br />
		box = false;<br />
		m_Dragging = false;<br />
		MotionFix=0;<br />
		ref_pt = true;			// Set the reference point flag is true only box has been drawn<br />
	}<br />
<br />
<br />
	//----------------------------------New Coding---------------------------------//<br />
	//When left button of the mouse is released//<br />
	if (MDrag) {<br />
		//m2_x = point.x; //assign the coordinate of position x clicked by mouse to point x<br />
		//m2_y = point.y;	//assign the coordinate of position y clicked by mouse to point y<br />
		<br />
		Rectn = false; //disables drawing of rectangle<br />
		MDrag = false; //disables Mouse dragging  <br />
		Motion =0; //set the motion of the mouse to draw the rectangle to none <br />
		AreaSegmentation();			// Area Segmentation( ) is called when reference point has been selected<br />
		MessageBox("Pls Right-Click to find the minimum point WITHIN the box!!");<br />
		minflag = true; //Right-click to set the minimum point within the rectangle box<br />
	}<br />
<br />
//-----------------------------------------------------------------------------//<br />
	<br />
	<br />
	zoom = false;<br />
	leftbut = false;<br />
<br />
	eraser = false;<br />
	leftbut = false;<br />
	insert = false;<br />
	rightbut = false;<br />
<br />
<br />
	CView::OnLButtonUp(nFlags, point);<br />
}<br />
<br />
void CMediVisionView::OnMouseMove(UINT nFlags, CPoint point) <br />
{<br />
	// TODO: Add your message handler code here and/or call default<br />
	int cur_pos_x, cur_pos_y;<br />
	int n;<br />
<br />
	if(eraser==true) {								// For erasing only<br />
		if(leftbut==true){<br />
			er_point = point;<br />
			<br />
			cur_pos_x = er_point.x - posx512;		// Compute the current eraser point position<br />
			cur_pos_y = er_point.y - posy512;<br />
			n = (512-(cur_pos_y))*512 +  cur_pos_x;	// Translate it to the image pixel n		<br />
<br />
			for (int i=0; i<7; i++) {<br />
				image512[n+i]	 	 = 0;				// Delete for 7 pixels horizontal<br />
				image512[(n-512)+i]  =0;				// Delete 2nd rows each time<br />
				image512[(n-512*2)+i]=0;				// Delete 3rd row also<br />
				image512[(n-512*3)+i]=0;<br />
				image512[(n-512*4)+i]=0;<br />
				image512[(n-512*5)+i]=0;<br />
				image512[(n-512*6)+i]=0;<br />
<br />
				store512[n+i]		= 0;<br />
				store512[(n-512)+i] = 0;<br />
				store512[(n-512*2)+i] = 0;<br />
				store512[(n-512*3)+i] = 0;<br />
				store512[(n-512*4)+i] = 0;<br />
				store512[(n-512*5)+i] = 0;<br />
				store512[(n-512*6)+i] = 0;<br />
<br />
			}<br />
			<br />
			pDC = GetDC();<br />
			hdc=pDC->GetSafeHdc();<br />
			<br />
			Draw();<br />
		}<br />
	}<br />
/*else */if(insert==true)									// For pen only<br />
	{								<br />
		if(rightbut==true)<br />
		{<br />
			// pen for image size 256 by 256<br />
			if (no_of_rows == 256)<br />
			{<br />
				er_point = point;<br />
				<br />
				cur_pos_x = er_point.x - posx256;		// Compute the current eraser point position<br />
				cur_pos_y = er_point.y - posy256;<br />
				n = (256-(cur_pos_y))*256 +  cur_pos_x;	// Translate it to the image pixel n		<br />
<br />
				for (int i=0; i<2; i++) <br />
				{<br />
					image256[n+i]	 	 = 255;				// Delete for 2 pixels horizontal<br />
					image256[(n-256)+i]  =255;				// Delete 2nd rows each time<br />
		//			image256[(n-256*2)+i]=255;				// Delete 3rd row also<br />
		//			image256[(n-256*3)+i]=255;<br />
		//			image256[(n-256*4)+i]=255;<br />
		//			image256[(n-256*5)+i]=255;<br />
		//			image256[(n-256*6)+i]=255;<br />
<br />
					store256[n+i]		= 255;<br />
					store256[(n-256)+i] = 255;<br />
		//			store256[(n-256*2)+i] = 255;<br />
		//			store256[(n-256*3)+i] = 255;<br />
		//			store256[(n-256*4)+i] = 255;<br />
		//			store256[(n-256*5)+i] = 255;<br />
		//			store256[(n-256*6)+i] = 255;<br />
				}<br />
			}<br />
<br />
			// pen for image size 512 by 512<br />
			if (no_of_rows == 512)<br />
			{<br />
				er_point = point;<br />
				<br />
				cur_pos_x = er_point.x - posx512;		// Compute the current eraser point position<br />
				cur_pos_y = er_point.y - posy512;<br />
				n = (512-(cur_pos_y))*512 +  cur_pos_x;	// Translate it to the image pixel n		<br />
<br />
				for (int i=0; i<10; i++) <br />
				{<br />
					image512[n+i]	 	 = 255;				// Delete for 2 pixels horizontal<br />
					image512[(n-512)+i]  =255;				// Delete 2nd rows each time<br />
					image512[(n-512*2)+i]=255;				// Delete 3rd row also<br />
					image512[(n-512*3)+i]=255;<br />
					image512[(n-512*4)+i]=255;<br />
					image512[(n-512*5)+i]=255;<br />
					image512[(n-512*6)+i]=255;<br />
					image512[(n-512*7)+i]=255;<br />
					image512[(n-512*8)+i]=255;<br />
					image512[(n-512*9)+i]=255;<br />
<br />
<br />
					store512[n+i]		= 255;<br />
					store512[(n-512)+i] = 255;<br />
					store512[(n-512*2)+i] = 255;<br />
					store512[(n-512*3)+i] = 255;<br />
					store512[(n-512*4)+i] = 255;<br />
					store512[(n-512*5)+i] = 255;<br />
					store512[(n-512*6)+i] = 255;<br />
					store512[(n-512*7)+i] = 255;<br />
					store512[(n-512*8)+i] = 255;<br />
					store512[(n-512*9)+i] = 255;<br />
<br />
				}<br />
			}<br />
		<br />
		<br />
			pDC = GetDC();<br />
			hdc=pDC->GetSafeHdc();<br />
			<br />
			Draw();<br />
		}<br />
	}<br />
<br />
	if (zoom == true)			// for zoom only<br />
	{<br />
		//if (leftbut == true)<br />
		{<br />
			<br />
			// do if, if image size is 256 by 256<br />
			if (no_of_rows == 256) <br />
			{<br />
<br />
<br />
				er_point = point;<br />
				<br />
				cur_pos_x = er_point.x - posx256;		// Compute the current mouse point position<br />
				cur_pos_y = er_point.y - posy256;<br />
<br />
				Draw_Zoom(cur_pos_x, cur_pos_y);<br />
<br />
			}<br />
<br />
			<br />
			// do if, if image size is 512 by 512<br />
			if (no_of_rows == 512) <br />
			{<br />
<br />
<br />
				er_point = point;<br />
				<br />
				cur_pos_x = er_point.x - posx512;		// Compute the current mouse point position<br />
				cur_pos_y = er_point.y - posy512;<br />
<br />
				Draw_Zoom(cur_pos_x, cur_pos_y);<br />
<br />
<br />
			}<br />
<br />
			pDC = GetDC();<br />
			hdc=pDC->GetSafeHdc();<br />
			<br />
			Draw();<br />
			<br />
			CClientDC zoomDC(this);<br />
			zoomDC.SetROP2(R2_NOTXORPEN);<br />
			// draw the lines at the exact mouse position<br />
			DrawZoomBox(&zoomDC, cur_pos_x, cur_pos_y);<br />
			//ClientDC.Rectangle((point.x + 35), (point.y + 35), (point.x - 35), (point.y - 35));<br />
	<br />
		}<br />
<br />
<br />
	}<br />
<br />
	<br />
	if (m_Dragging && m_PointOrigin!=point ) {<br />
<br />
		CClientDC ClientDC (this);<br />
		mouse2_x = point.x;<br />
		mouse2_y = point.y;	<br />
		<br />
		ClientDC.SetROP2(R2_NOTXORPEN);				//R2_NOTXORPEN Pixel is the inverse of the R2_XORPEN color. <br />
<br />
		if (MotionFix) DrawBox(&ClientDC,m_PointOrigin,m_PointOld);<br />
			MotionFix++;<br />
<br />
		DrawBox(&ClientDC,m_PointOrigin,point);<br />
	}<br />
<br />
	m_PointOld = point;<br />
<br />
<br />
	//-------------New Coding----------------------//<br />
<br />
		if (MDrag && m_PO!=point) {          //For dragging onto a point by using the mouse<br />
		<br />
			Rectn= true;<br />
			CClientDC FDC (this);<br />
			m2_x = point.x;<br />
			m2_y = point.y;	<br />
		<br />
			FDC.SetROP2(R2_NOTXORPEN);	//highlights the image//		<br />
<br />
			if (Motion) DrawRectn(&FDC, m_PO, m_OldPO); //happens when Motion is not 0.<br />
				Motion++;<br />
		<br />
			<br />
			<br />
			DrawRectn(&FDC,m_PO,point);<br />
				<br />
		<br />
	}<br />
<br />
	m_OldPO = point;<br />
<br />
	//---------------------------------------------//<br />
<br />
<br />
<br />
<br />
	CView::OnMouseMove(nFlags, point);<br />
}<br />
<br />
<br />
void CMediVisionView::DrawZoomBox(CDC *pdc, int mouse_x, int mouse_y)<br />
{<br />
	int half_box_length;<br />
<br />
	half_box_length = (ZOOM_BOX / 2);<br />
<br />
	if (no_of_rows == 512)<br />
	{<br />
		// if mouse pointer is within range of image<br />
		if (((mouse_x-half_box_length) > 0) && ((mouse_x+half_box_length) < no_of_rows) && ((mouse_y-half_box_length) > 0) && ((mouse_y+half_box_length) < no_of_rows))<br />
		{<br />
<br />
			CPoint top_left;<br />
			CPoint bottom_right;<br />
<br />
			top_left.x = mouse_x-(half_box_length)+10;<br />
			top_left.y = mouse_y-(half_box_length)+10;<br />
			bottom_right.x = mouse_x+(half_box_length)+10;<br />
			bottom_right.y = mouse_y+(half_box_length)+10;<br />
<br />
			pdc->MoveTo(top_left);							// start at top left corner<br />
			pdc->LineTo(top_left.x+ZOOM_BOX, top_left.y);	// draw line to the right<br />
			pdc->LineTo(bottom_right);						// then draw line downwards to bottom right corner<br />
<br />
			pdc->MoveTo(bottom_right);								// now start from bottom right corner<br />
			pdc->LineTo(bottom_right.x-ZOOM_BOX, bottom_right.y);	// draw line to the left<br />
			pdc->LineTo(top_left);									// then draw line upwards to top left corner<br />
<br />
			if (grid == true)<br />
			{<br />
<br />
			}<br />
		}<br />
<br />
	}<br />
<br />
}<br />
<br />
<br />
void CMediVisionView::Draw_Zoom(int mx, int my)<br />
{<br />
<br />
	// to zoom image when zoom is activated and mouse move over image<br />
	int ref=0, r=0, c=0, n=0, m=0;<br />
<br />
	int row=0, col=0;<br />
	//int p, q;<br />
	//int	cx_position[6], cy_position[6]; <br />
	unsigned int	data=0;<br />
<br />
<br />
	int zoom_rows = ZOOM_BOX;<br />
	int zoom_cols = ZOOM_BOX;<br />
	<br />
	// zoom for image size 256 by 256<br />
	if(n_rows==256)<br />
	{<br />
//			StretchBlt(hdc, posxZOOM,posyZOOM, zoom_rows*8, zoom_cols*8, hdc, ((mx-(ZOOM_BOX/2))), ((my-(ZOOM_BOX/2))), (zoom_rows+(ZOOM_BOX/2)), (zoom_cols+(ZOOM_BOX/2)), SRCCOPY);	<br />
		StretchBlt(hdc, posxZOOM,posyZOOM, zoom_rows*magnification, zoom_cols*magnification, hdc, ((mx-(ZOOM_BOX/2))+10), ((my-(ZOOM_BOX/2))+10), zoom_rows, zoom_cols, SRCCOPY);		// +10 to offset 10 pix by 10 pix<br />
<br />
	}	<br />
<br />
<br />
	// zoom for image size 512 by 512<br />
	if(n_rows==512)<br />
	{			<br />
//			StretchBlt(hdc, posxZOOM,posyZOOM, zoom_rows*8, zoom_cols*8, hdc, ((mx-(ZOOM_BOX/2))), ((my-(ZOOM_BOX/2))), (zoom_rows+(ZOOM_BOX/2)), (zoom_cols+(ZOOM_BOX/2)), SRCCOPY);	<br />
		StretchBlt(hdc, posxZOOM,posyZOOM, zoom_rows*magnification, zoom_cols*magnification, hdc, ((mx-(ZOOM_BOX/2))+10), ((my-(ZOOM_BOX/2))+10), zoom_rows, zoom_cols, SRCCOPY);		// +10 to offset 10 pix by 10 pix<br />
	}<br />
<br />
}<br />
<br />
void CMediVisionView::OnZoom2x() <br />
{<br />
	// TODO: Add your command handler code here<br />
<br />
	image32 = new int [ZOOM_BOX * ZOOM_BOX];<br />
<br />
	magnification = 2;<br />
<br />
	if (!zoom)				// toggle zoom on/off<br />
		zoom = true;		<br />
	else<br />
		zoom = false;<br />
<br />
	<br />
}<br />
<br />
void CMediVisionView::OnZoom4x() <br />
{<br />
	// TODO: Add your command handler code here<br />
	<br />
	image32 = new int [ZOOM_BOX * ZOOM_BOX];<br />
<br />
	magnification = 4;<br />
<br />
	if (!zoom)				// toggle zoom on/off<br />
		zoom = true;		<br />
	else<br />
		zoom = false;<br />
	<br />
	<br />
}<br />
<br />
void CMediVisionView::OnSegmentationRegiongrowing() <br />
{<br />
	// TODO: Add your command handler code here<br />
	box = true;<br />
}<br />
<br />
BOOL CMediVisionView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) <br />
{<br />
	// TODO: Add your message handler code here and/or call default<br />
<br />
	if(box) {<br />
		::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));<br />
		return TRUE;<br />
	}<br />
	<br />
	//-----New coding------//<br />
	if(Rectn) {  <br />
	::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS)); //Mouse cursor changes to Cross cursor//<br />
	return TRUE;<br />
	<br />
	}//---End of new coding-----//<br />
<br />
<br />
	if(zoom) {<br />
		::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));<br />
		return TRUE;<br />
	}<br />
	if(eraser)	{		//eraser is activated<br />
		CWinApp* pApp=AfxGetApp();											//load Square cursor for erasing of image<br />
		HICON hIconBang =pApp->LoadCursor(IDC_CURSOR1);<br />
		SetCursor(hIconBang);<br />
		return TRUE;<br />
		<br />
	}<br />
	if(insert)	{		//insert is activated<br />
		CWinApp* pApp=AfxGetApp();											//load Square cursor for erasing of image<br />
		HICON hIconBang =pApp->LoadCursor(IDC_CURSOR2);<br />
		SetCursor(hIconBang);<br />
		return TRUE;<br />
		<br />
	}<br />
<br />
	<br />
	<br />
	return CView::OnSetCursor(pWnd, nHitTest, message);<br />
}<br />
<br />
void CMediVisionView::Segment_Region()<br />
{<br />
	int ref, r, c, n;<br />
<br />
	int segment_length = abs(mouse2_x - mouse1_x);		// length of the box for segmentation;<br />
	int	segment_height = abs(mouse2_y - mouse1_y);		// height of the box for segmentation<br />
<br />
<br />
	region = new int*[segment_height];		<br />
	for (int i=0; i<segment_height; i++) {<br />
		region[i] = new int[segment_length];			// create a 2 dimension array to store the selected part<br />
	}<br />
<br />
	<br />
// Compute the start point with reference to the image left hand corner;<br />
	<br />
	if (no_of_rows == 256 && !multiple_open) {<br />
		box_x = mouse1_x - posx256;								// 10 is because the StretchDIbits command has			<br />
		box_y = mouse1_y - posy256;								// set the corner of image at (5, 5)<br />
<br />
		n = (256-(box_y+1))*256 + box_x;				// +1 because it should not include the current row<br />
		ref = 0;										// and 512-(...) because in screen bottom left hand corner is (0,0)<br />
<br />
		for (r=0; r<segment_height; r++) {<br />
			for (c=0; c<segment_length; c++) {<br />
				region[r][c] = image256[n];<br />
<br />
				n++;<br />
				ref++; <br />
				if (ref == segment_length) {			// To compute the n wrt to row(r) and col(c)<br />
					n = ((256-(box_y+1))-r)*256 + box_x;<br />
					ref = 0;<br />
				}<br />
			}	<br />
		}<br />
<br />
// Performing the segmentation in this region that was selected by the box <br />
<br />
		ref_selected_x = clk_mouse_x - posx256;				// compute the reference point position<br />
		ref_selected_y = clk_mouse_y - posy256;<br />
		<br />
		n = (256-(ref_selected_y))*256 +  ref_selected_x;<br />
		ref_intensity = image256[n];<br />
		image256[n] = ref_intensity + 100;				// Make the reference point slightly whiter only<br />
		<br />
		for (r=0; r<segment_height; r++) {<br />
			for (c=0; c<segment_length; c++) {<br />
				if ((region[r][c] <= ref_intensity+10) && (region[r][c] >= ref_intensity-10)) {<br />
					n = ((256-(box_y+1))-r)*256 + box_x+c;<br />
					image256[n] = 255;<br />
				}<br />
			}<br />
		}<br />
	}<br />
	if (no_of_rows == 512) {<br />
		box_x = mouse1_x - posx512;						// 5 is because the StretchDIbits command has			<br />
		box_y = mouse1_y - posy512;						// set the corner of image at (5, 5)<br />
<br />
		n = (512-(box_y+1))*512 + box_x;				// +1 because it should not include the current row<br />
		ref = 0;										// and 512-(...) because in screen bottom left hand corner is (0,0)<br />
<br />
		for (r=0; r<segment_height; r++) {<br />
			for (c=0; c<segment_length; c++) {<br />
				region[r][c] = image512[n];<br />
<br />
				n++;<br />
				ref++; <br />
				if (ref == segment_length) {			// To compute the n wrt to row(r) and col(c)<br />
					n = ((512-(box_y+1))-r)*512 + box_x;<br />
					ref = 0;<br />
				}<br />
			}	<br />
		}<br />
<br />
// Performing the segmentation in this region that was selected by the box <br />
<br />
		ref_selected_x = clk_mouse_x - posx512;				// compute the reference point position<br />
		ref_selected_y = clk_mouse_y - posy512;<br />
		<br />
		n = (512-(ref_selected_y))*512 +  ref_selected_x;<br />
		ref_intensity = image512[n];<br />
		image512[n] = ref_intensity + 100;					// Make the reference point slightly whiter only<br />
		<br />
		for (r=0; r<segment_height; r++) {<br />
			for (c=0; c<segment_length; c++) {<br />
				if ((region[r][c] <= ref_intensity+10) && (region[r][c] >= ref_intensity-10)) {<br />
					n = ((512-(box_y+1))-r)*512 + box_x+c;<br />
					image512[n] = 255;<br />
				}<br />
			}<br />
		}<br />
	}<br />
	Draw();<br />
	return;<br />
<br />
	delete [] region;<br />
}<br />
<br />
void CMediVisionView::OnFileMultipleopen() <br />
{<br />
	// TODO: Add your command handler code here<br />
	multiple_open = true;<br />
	OpenFile();<br />
}<br />
<br />
void CMediVisionView::OpenFile()<br />
{<br />
	static char BASED_CODE szFilter[] = "DICOM (*.dcm)|*.dcm|Bitmap (*.bmp)| *.bmp|";<br />
<br />
	CFileDialog dlg(TRUE, NULL, NULL, NULL, szFilter);<br />
	if (dlg.DoModal() == IDOK) {<br />
		m_File1=dlg.GetFileName();<br />
		file = m_File1;<br />
		<br />
	}<br />
<br />
	if ((fp = fopen(m_File1, "rb")) == NULL) {<br />
		noFile = true;<br />
	}<br />
	else {<br />
		<br />
		noFile = false;<br />
		if (multiple_open) image_no++;<br />
	}<br />
	<br />
	Flag3D = false;<br />
	ReadFile();<br />
	<br />
	Invalidate();													// Refresh the Window<br />
<br />
	pDC = GetDC();<br />
	hdc=pDC->GetSafeHdc();<br />
	Draw();<br />
<br />
}<br />
<br />
void CMediVisionView::OnRToolbar3DIntensity() <br />
{<br />
	// TODO: Add your command handler code here<br />
<br />
	On3dMaximumintensity();<br />
}<br />
<br />
void CMediVisionView::OnRToolbarMultipleopen() <br />
{<br />
	// TODO: Add your command handler code here<br />
	OnFileMultipleopen();<br />
}<br />
<br />
<br />
void CMediVisionView::OnRToolbar2Region() <br />
{<br />
	// TODO: Add your command handler code here<br />
	OnSegmentationRegiongrowing();<br />
}<br />
<br />
void CMediVisionView::On3dVolumerendering() <br />
{<br />
	// TODO: Add your command handler code here<br />
	int s, n;<br />
	int	p=0, q=0;<br />
	int iGm, Composite;<br />
	float Grad_Mag, Ambient, Mag, Diffuse, Light_Mag, Luminance, Alpha, at, la, lum;<br />
	float Intensity;<br />
	float Light_x = 128+2.5;<br />
	float Light_y = 128+2.5;<br />
	float Light_z = 1+3.5;<br />
<br />
	Light_Mag	= (float)sqrt(Light_x*Light_x + Light_y*Light_y + Light_z*Light_z);<br />
	Ambient		= 0.4f;<br />
	Diffuse		= 0.6f;<br />
<br />
	SetClassificationTable();<br />
<br />
	if (no_of_rows==256) {<br />
			<br />
//-----------------------	Compute the Gradient -------------------------------------------<br />
			n = 0;<br />
			for (p=0; p<no_of_rows; p++) {<br />
				for (q=0; q<no_of_cols; q++) {<br />
					lum = 0;<br />
					la = 0;<br />
					for (s=1; s<nCount; s++) {<br />
						if (dcmPixel[s][p*256+q] > 0) {<br />
							Grad_x = dcmPixel[s][p*256+(q-1)] - dcmPixel[s][p*256+(q+1)];			<br />
							Grad_y = dcmPixel[s][(p-1)*256+q] - dcmPixel[s][(p+1)*256+q];<br />
							Grad_z = dcmPixel[s-1][n] - dcmPixel[s+1][n];<br />
							Grad_Mag = (float)sqrt(Grad_x*Grad_x + Grad_y*Grad_y + Grad_z*Grad_z);<br />
//-----------------------   Compute classify -----------------------------------------------<br />
							iGm = (Grad_Mag/442.0f) * 64.0f;<br />
							Composite = (iGm<<8) | (dcmPixel[s][p*256+q]);<br />
							Alpha = (float)alphaTable[Composite]/255.0f;			<br />
							Luminance = (float)luminanceTable[Composite];;<br />
//-----------------------   Compute Shading ------------------------------------------------<br />
							Mag =(Grad_x/Grad_Mag * Light_x/Light_Mag) + <br />
								 (Grad_y/Grad_Mag * Light_y/Light_Mag) +<br />
								 (Grad_z/Grad_Mag * Light_z/Light_Mag);<br />
<br />
							if (Mag <0.0) Mag = 0.0;<br />
							Intensity = Ambient + (Diffuse*Mag);<br />
							Luminance = Luminance * Intensity;<br />
//-----------------------   Front to back Alpha --------------------------------------------<br />
							if (Alpha>0.0) {<br />
								at = Alpha * (1.0f - la);<br />
								lum = lum + (Luminance * at);<br />
								la = la + at;<br />
								if (la > 0.97) break;<br />
							}					<br />
						}<br />
					}<br />
					n++;<br />
					image256[n] = lum;					<br />
				}<br />
			}			<br />
	}<br />
<br />
	else {<br />
// ------------------------------------------------------------------------------------------<br />
			n = 0;<br />
			for (p=0; p<no_of_rows; p++) {<br />
				for (q=0; q<no_of_cols; q++) {<br />
					lum = 0;<br />
					la = 0;<br />
					for (s=1; s<nCount; s++) {<br />
						if (dcmPixel[s][p*512+q] > 0) {<br />
							Grad_x = dcmPixel[s][p*512+(q-1)] - dcmPixel[s][p*512+(q+1)];			<br />
							Grad_y = dcmPixel[s][(p-1)*512+q] - dcmPixel[s][(p+1)*512+q];<br />
							Grad_z = dcmPixel[s-1][n] - dcmPixel[s+1][n];<br />
					<br />
							Grad_Mag = (float)sqrt(Grad_x*Grad_x + Grad_y*Grad_y + Grad_z*Grad_z);<br />
// --------------------------  Compute classify --------------------------------------------<br />
							iGm = (Grad_Mag/442.0f) * 64.0f;<br />
							Composite = (iGm<<8) | (dcmPixel[s][p*512+q]);<br />
							Alpha = (float)alphaTable[Composite]/255.0f;			<br />
							Luminance = (float)luminanceTable[Composite];				<br />
// --------------------------  Compute Shading ---------------------------------------------<br />
							Mag =(Grad_x/Grad_Mag * Light_x/Light_Mag) + <br />
								 (Grad_y/Grad_Mag * Light_y/Light_Mag) +<br />
								 (Grad_z/Grad_Mag * Light_z/Light_Mag);<br />
<br />
							if (Mag <0.0) Mag = 0.0;<br />
							Intensity = Ambient + (Diffuse*Mag);<br />
							Luminance = Luminance * Intensity;<br />
// --------------------------  Front to back Alpha -----------------------------------------<br />
							if (Alpha>0.0) {<br />
								at = Alpha * (1.0f - la);<br />
								lum = lum + (Luminance * at);<br />
								la = la + at;<br />
								if (la > 0.97) break;<br />
							}		<br />
						}<br />
					}<br />
					n++;<br />
					image512[n] = lum;			<br />
				}<br />
			}			<br />
	}<br />
	pDC = GetDC();<br />
	hdc=pDC->GetSafeHdc();<br />
	Draw();<br />
}<br />
<br />
void SetClassificationTable() {<br />
	float	alpha;<br />
	float	levWidth, levThreshold;<br />
	int		index, magnitude, intensity, ind;<br />
<br />
	levThreshold	= (float)128.0;<br />
	levWidth		= (float) 2.0;<br />
<br />
	for (index=0; index<4; index++) {<br />
		for (magnitude=0; magnitude < 64; magnitude ++) {<br />
			for (intensity=0; intensity < 256; intensity++) {<br />
				ind = intensity + (magnitude<<8) + (index<<14);<br />
<br />
				if ((intensity >=levThreshold) && (intensity < (levThreshold + levWidth)))<br />
					alpha = ((float)intensity - levThreshold)/levWidth;<br />
				else if (intensity < levThreshold)<br />
					alpha = (float)0.0;<br />
				else<br />
					alpha = (float)1.0;<br />
<br />
				if (alpha>0.0) {<br />
					alphaTable[ind] = (255.0*alpha);<br />
				}<br />
				else<br />
					alphaTable[ind] = 0;<br />
<br />
				luminanceTable[ind] = 255;<br />
			}<br />
		}<br />
	}<br />
}<br />
<br />
void CMediVisionView::OnToolsEraser() <br />
{<br />
	// TODO: Add your command handler code here	<br />
	if (!eraser)									// Toggle<br />
		eraser = true;<br />
	else<br />
		eraser = false;<br />
}<br />
<br />
void CMediVisionView::OnToolsBoolean() <br />
{<br />
	// TODO: Add your command handler code here<br />
	FILE*	fb;						// Pointer for bitmap file<br />
	short	c1, c2;<br />
	int		i, j, c;<br />
	int		BitmapWidth, BitmapHeight, intval, psize;<br />
	short	shortval;<br />
<br />
	if ((fb=fopen("d:\\bmestudent\\Medical Visualization 32\\Chee-Boon_LEO-MedicalVisualization31\\512 bitmap\\bitmap512res2(3)-1.bmp", "rb")) == NULL) {<br />
		AfxMessageBox("Cannot Open Bitmap File! ");<br />
		fclose(fb);<br />
		exit(1);<br />
	}<br />
<br />
/*-------------- Read Bitmap File Info  ------------------------------------------------<br />
	This size is 14 bytes<br />
		16 bits	: Magic Number "BM"<br />
		32 bits	: Size of file in 32-bit Integer<br />
		16 bits	: Reserved 1 (always 0)<br />
		16 bits	: Reserved 2 (always 0)<br />
		32 bits	: Starting position of image data, in bytes<br />
---------------------------------------------------------------------------------------*/<br />
<br />
	c1 = fgetc(fb);<br />
	c2 = fgetc(fb);<br />
	if (c1 != 'B' || c2 != 'M') {<br />
		AfxMessageBox("Bitmap File Error 1! ");	<br />
		return; <br />
	}<br />
<br />
	fread(&intval, sizeof(unsigned int), 1, fb);<br />
	fread(&shortval, sizeof(unsigned short), 1, fb);<br />
	fread(&shortval, sizeof(unsigned short), 1, fb);<br />
	fread(&intval, sizeof(unsigned int), 1, fb);<br />
<br />
/* --------------Read Bitmap Header ----------------------------------------------------<br />
	This size is 40 bytes<br />
		32 bits	:	(unsigned) size of this header<br />
		32 bits	:	Width<br />
		32 bits	:	Height<br />
<br />
		16 bits	:	Planes (no of colour planes, always 1)<br />
		16 bits	:	BitsPerPixel (1 to 24. 1, 4, 8 and 24 are legal.<br />
<br />
		32 bits	:	(unsigned) compression (1, 8 bit RLE; 2, 4 bit RLE; 3 bitfield)<br />
		32 bits	:	SizeofBitmap in bytes (0 if uncompressed)<br />
		32 bits	:	H-Resolution (Pixels per meter, can be 0)<br />
		32 bits :	V-Resolution (Pixels per meter, can be 0)<br />
		32 bits :	(unsigned) ColorsUsed (No. of colours in palette, can be 0)<br />
		32 bits :	ColorImportant (Minimum number of important colors, can be 0)<br />
----------------------------------------------------------------------------------------*/<br />
<br />
	fread(&intval, sizeof(unsigned int), 1, fb);<br />
	fread(&BitmapWidth, sizeof(unsigned int), 1, fb);<br />
	fread(&BitmapHeight, sizeof(unsigned int), 1, fb);<br />
<br />
	fread(&shortval, sizeof(unsigned short), 1, fb);<br />
	fread(&shortval, sizeof(unsigned short), 1, fb);<br />
<br />
	fread(&intval, sizeof(unsigned int), 1, fb);<br />
	fread(&intval, sizeof(unsigned int), 1, fb);<br />
	fread(&intval, sizeof(unsigned int), 1, fb);<br />
	fread(&intval, sizeof(unsigned int), 1, fb);<br />
	fread(&psize, sizeof(unsigned int), 1, fb);<br />
	fread(&intval, sizeof(unsigned int), 1, fb);<br />
<br />
/* ---------------- Read the Color Palette --------------------------------------------*/<br />
<br />
	for (i=0; i<psize; i++) {<br />
		for (j=0; j<4; j++) {<br />
			c=fgetc(fb);<br />
			if (c== EOF) return;<br />
		}<br />
	}<br />
<br />
/* ---------------- Read Bimap Pixel Data ---------------------------------------------*/<br />
	int n = 0;<br />
	<br />
	Bitmap_r = new int[BitmapWidth*BitmapHeight];<br />
	Bitmap_g = new int[BitmapWidth*BitmapHeight];<br />
	Bitmap_b = new int[BitmapWidth*BitmapHeight];<br />
<br />
	for (j=0; j<BitmapHeight; j++) {<br />
		for (i=0; i<BitmapWidth; i++) {		<br />
			Bitmap_r[n] = fgetc(fb);<br />
			Bitmap_g[n] = fgetc(fb);<br />
			Bitmap_b[n] = fgetc(fb);<br />
			n++;<br />
		}<br />
	}<br />
	<br />
	n=0;<br />
	for (j=0; j<BitmapHeight; j++) {<br />
		for (i=0; i<BitmapWidth; i++) {<br />
			if (Bitmap_r[n] <10) {<br />
				image512[n] = 0;<br />
				store512[n] = 0;	<br />
			}<br />
//			else {<br />
//				image512[n] = 255;<br />
//				store512[n] = 255;	<br />
//			}<br />
			n++;<br />
		}<br />
	}<br />
//	BmpDraw();<br />
<br />
	pDC = GetDC();<br />
	hdc=pDC->GetSafeHdc();<br />
	Draw();<br />
<br />
	fclose(fb);<br />
<br />
	delete [] Bitmap_r;<br />
	delete [] Bitmap_g;<br />
	delete [] Bitmap_b;<br />
<br />
	return;<br />
}<br />
<br />
void CMediVisionView::BmpDraw()<br />
{<br />
	int row, col, data;<br />
	int n=0;<br />
<br />
	BITMAPINFOHEADER infoHeader;<br />
	infoHeader.biSize = sizeof(BITMAPINFOHEADER);<br />
	infoHeader.biWidth = 512;<br />
	infoHeader.biHeight = 512;<br />
	infoHeader.biPlanes=1;<br />
	infoHeader.biBitCount= 24;<br />
	infoHeader.biCompression=0;<br />
	infoHeader.biSizeImage=0;<br />
	infoHeader.biXPelsPerMeter=0;<br />
	infoHeader.biYPelsPerMeter=0;<br />
	infoHeader.biClrUsed=0;<br />
	infoHeader.biClrImportant=0;<br />
<br />
<br />
	for(int p=0;p<512;p++) {<br />
		for (int q=0;q<512;q++) {<br />
			data=Bitmap_r[n];<br />
			if(data <= 0) {<br />
				argbPixels[p][q] = (unsigned int)(0 | 0 | 0 | 0);<br />
				<br />
			}<br />
			else {<br />
				argbPixels[p][q] = (unsigned int)(data << 24 | data <<16 | data<<8 | data);<br />
			}<br />
			n++;	<br />
		}<br />
	}<br />
<br />
	argbPixelsPtr = (unsigned char *) argbPixels;<br />
	rgbPixelsPtr = (unsigned char *) rgbPixels;<br />
<br />
	for (row=0; row<512; row++) {<br />
		for(col=0; col<512; col++) {<br />
			*rgbPixelsPtr++ = *argbPixelsPtr++;<br />
			*rgbPixelsPtr++ = *argbPixelsPtr++;<br />
			*rgbPixelsPtr++ = *argbPixelsPtr++;<br />
			argbPixelsPtr++;<br />
		}		<br />
	}<br />
		<br />
	StretchDIBits(hdc, 10, 10, 512, 512, 0, 0, 512, 512, rgbPixels,(BITMAPINFO*) &infoHeader, 0, SRCCOPY);	<br />
	<br />
	return;<br />
}	<br />
<br />
<br />
void CMediVisionView::OnToolsMaximation() <br />
{<br />
	// TODO: Add your command handler code here<br />
	int i, j, n;<br />
	n=0;<br />
	if (no_of_rows == 256) {<br />
		for (j=0; j<no_of_cols; j++) {<br />
			for (i=0; i<no_of_rows; i++) {		<br />
				if (image256[n]>150) {<br />
					image256[n]=255;<br />
					store256[n]=255;<br />
				}<br />
				n++;<br />
			}<br />
		}<br />
	}<br />
	else if (no_of_rows == 512) {<br />
		for (j=0; j<no_of_cols; j++) {<br />
			for (i=0; i<no_of_rows; i++) {		<br />
				if (image512[n]>150) {<br />
					image512[n]=255;<br />
					store512[n]=255;<br />
				}<br />
				n++;<br />
			}<br />
		}<br />
	}<br />
	Draw();<br />
}<br />
<br />
void CMediVisionView::OnEraser() <br />
{<br />
	// TODO: Add your command handler code here<br />
	OnToolsEraser();<br />
}<br />
<br />
<br />
<br />
void CMediVisionView::OnToolsPen() <br />
{<br />
	// TODO: Add your command handler code here<br />
		if (!insert)									// Toggle<br />
			insert = true;<br />
		else<br />
			insert = false;<br />
}<br />
<br />
void CMediVisionView::OnPen() <br />
{<br />
	// TODO: Add your command handler code here<br />
		OnToolsPen();<br />
}<br />
<br />
void CMediVisionView::OnEditPatientData() <br />
{<br />
//	CEdit_Patient_Data EditDlg;<br />
//	EditDlg.DoModal();<br />
	// TODO: Add your command handler code here<br />
	EditDlg = new CEdit_Patient_Data(this);<br />
	EditDlg -> Create(CEdit_Patient_Data::IDD,this);<br />
	EditDlg -> ShowWindow(SW_SHOW);<br />
	EditDlg -> Edit_Patient();<br />
//	//EditDlg -> OnOK();<br />
	<br />
<br />
	<br />
<br />
	<br />
}<br />
<br />
void CMediVisionView::OnMorpholofical() <br />
{<br />
	// TODO: Add your command handler code here<br />
	int n;<br />
	int rows = 0, cols = 0;<br />
<br />
	if (no_of_rows == 512){<br />
		for(rows=0; rows<no_of_rows; rows=(rows+2)){<br />
			for(cols=0;cols<no_of_cols;cols=(cols+2)){<br />
<br />
				n = ((512-rows-1)*512)+(cols);<br />
				if ((image512[n]>100)||(image512[(n+1)]>100)||(image512[(n-512)]>100)||(image512[((n-512)+1)]>100)){<br />
<br />
					image512[n]				=255;<br />
					image512[(n+1)]			=255;<br />
					image512[(n-512)]		=255;<br />
					image512[(n-512)+1]		=255;<br />
<br />
					store512[n]				=255;<br />
					store512[(n+1)]			=255;<br />
					store512[(n-512)]		=255;<br />
					store512[(n-512)+1]		=255;<br />
<br />
					<br />
				}<br />
			}<br />
	}<br />
pDC = GetDC();<br />
hdc= pDC->GetSafeHdc();<br />
Draw();<br />
<br />
	}<br />
}<br />
<br />
<br />
void CMediVisionView::OnToolsExpansion() <br />
{<br />
	cancel1= false;<br />
	cancel2= false;<br />
	CExpand_Pixel ExpandDlg(this);<br />
	ExpandDlg.DoModal();<br />
	if(cancel1==false)<br />
	{<br />
		CRows_Cols RowsColsDlg(this);<br />
		RowsColsDlg.DoModal();<br />
		if(cancel2== false)<br />
		{	<br />
<br />
<br />
<br />
	int n,q,i=0;<br />
	int rows =0 , cols =0;<br />
	int rows512 , cols512 ;<br />
	int *temp;<br />
<br />
temp = new int [512*512];<br />
	<br />
<br />
	if (Expand_Pixel == 2) {<br />
	for(rows=rows1; rows<rows2;rows++){<br />
			for(cols=cols1;cols<cols2;cols++){<br />
<br />
<br />
				<br />
				n = ((512-rows-1)*512)+(cols);<br />
			<br />
				temp[i] =image512[n];<br />
				i++;<br />
			}<br />
		}<br />
			<br />
	<br />
			i=0;<br />
					for(rows512=0; rows512<512;rows512=(rows512+2)){<br />
						for(cols512=0;cols512<512;cols512=(cols512+2)){<br />
							q = ((512-rows512-1)*512)+(cols512);<br />
						<br />
						image512[q]					=temp[i];<br />
						image512[q+1]				=temp[i];<br />
						image512[q-512]				=temp[i];<br />
						image512[(q-512)+1]			=temp[i];					<br />
<br />
						store512[q]					=temp[i];<br />
						store512[q+1]				=temp[i];<br />
						store512[q-512]				=temp[i];<br />
						store512[(q-512)+1]			=temp[i];<br />
						<br />
						i++;<br />
						<br />
					}<br />
					<br />
		//		}<br />
			}<br />
		<br />
	}<br />
	<br />
	if(Expand_Pixel == 3) <br />
	{<br />
<br />
		for(rows=rows1; rows<rows2; rows++)<br />
		{<br />
			for(cols=cols1;cols<cols2;cols++)<br />
			{<br />
<br />
				n = ((512-rows-1)*512)+(cols);<br />
			<br />
				temp[i] =image512[n];<br />
				i++;<br />
			}<br />
		}<br />
			<br />
		<br />
			i=0;<br />
					for(rows512=0; rows512<509;rows512=(rows512+3))<br />
					{<br />
						for(cols512=0;cols512<509;cols512=(cols512+3))<br />
						{<br />
					<br />
						q = ((512-rows512-1)*512)+(cols512);<br />
					<br />
					 <br />
						image512[q]					=temp[i];<br />
						image512[q+1]				=temp[i];<br />
						image512[q+2]				=temp[i];<br />
						image512[q-512]				=temp[i];<br />
						image512[(q-512)+1]			=temp[i];<br />
						image512[(q-512)+2]			=temp[i];<br />
						image512[(q-512)-512]		=temp[i];<br />
						image512[(q-1024)+1]		=temp[i];<br />
						image512[(q-1024)+2]		=temp[i];<br />
<br />
						store512[q]					=temp[i];<br />
						store512[q+1]				=temp[i];<br />
						store512[q+2]				=temp[i];<br />
						store512[q-512]				=temp[i];<br />
						store512[(q-512)+1]			=temp[i];<br />
						store512[(q-512)+2]			=temp[i];<br />
						store512[q-1024]            =temp[i];<br />
						store512[(q-1024)+1]		=temp[i];<br />
						store512[(q-1024)+2]		=temp[i];<br />
<br />
						<br />
						i++;<br />
						<br />
					}<br />
					<br />
			}<br />
				<br />
	}<br />
	if(Expand_Pixel == 4 )<br />
	{<br />
		for(rows=rows1; rows<rows2;rows++)<br />
			{<br />
				for(cols=cols1;cols<cols2;cols++)<br />
				{<br />
<br />
					n = ((512-rows-1)*512)+(cols);<br />
					temp[i] =image512[n];<br />
					i++;<br />
				}<br />
			}<br />
				<br />
			<br />
				i=0;<br />
					for(rows512=0; rows512<508;rows512=(rows512+4))<br />
					{<br />
						for(cols512=0;cols512<508;cols512=(cols512+4))<br />
						{<br />
							q = ((512-rows512-1)*512)+(cols512);<br />
					<br />
						image512[q]					=temp[i];<br />
						image512[q+1]				=temp[i];<br />
						image512[q+2]				=temp[i];<br />
						image512[(q+3)]				=temp[i];<br />
						image512[q-512]				=temp[i];<br />
						image512[(q-512)+1]			=temp[i];<br />
						image512[(q-512)+2]			=temp[i];<br />
						image512[(q-512)+3]			=temp[i];<br />
						image512[(q-512)-512]		=temp[i];<br />
						image512[(q-1024)+1]		=temp[i];<br />
						image512[(q-1024)+2]		=temp[i];<br />
						image512[(q-1024)+3]		=temp[i];<br />
						image512[(q-1536)]			=temp[i];<br />
						image512[(q-1536)+1]		=temp[i];<br />
						image512[(q-1536)+2]		=temp[i];<br />
						image512[(q-1536)+3]		=temp[i];<br />
<br />
<br />
<br />
<br />
						store512[q]					=temp[i];<br />
						store512[q+1]				=temp[i];<br />
						store512[q+2]				=temp[i];<br />
						store512[q+3]				=temp[i];<br />
						store512[q-512]				=temp[i];<br />
						store512[(q-512)+1]			=temp[i];<br />
						store512[(q-512)+2]			=temp[i];<br />
						store512[(q-512)+3]			=temp[i];<br />
						store512[q-1024]            =temp[i];<br />
						store512[(q-1024)+1]		=temp[i];<br />
						store512[(q-1024)+2]		=temp[i];<br />
						store512[(q-1024)+3]		=temp[i];<br />
						store512[(q-1536)]			=temp[i];<br />
						store512[(q-1536)+1]		=temp[i];<br />
						store512[(q-1536)+2]		=temp[i];<br />
						store512[(q-1536)+3]		=temp[i];<br />
<br />
						i++;<br />
						}<br />
					}<br />
	}<br />
				}<br />
				}<br />
<br />
<br />
	Draw();	<br />
	Expand_Pixel = 0;<br />
<br />
}<br />
<br />
//-----------New coding---------------//<br />
void CMediVisionView::OnLineObjects() <br />
{<br />
<br />
	Rectn = true;<br />
<br />
}<br />
<br />
<br />
<br />
<br />
void DrawRectn(CDC *pDc , CPoint m_1, CPoint m_2)<br />
{<br />
	pDc->MoveTo(m_1);<br />
	pDc->LineTo(m_1.x, m_2.y);<br />
<br />
	pDc->MoveTo(m_1);<br />
	pDc->LineTo(m_2.x, m_1.y);<br />
<br />
	pDc->MoveTo(m_2.x,m_1.y);<br />
	pDc->LineTo(m_2.x, m_2.y);<br />
<br />
	pDc->MoveTo(m_1.x,m_2.y);<br />
	pDc->LineTo(m_2.x, m_2.y);<br />
<br />
}<br />
<br />
<br />
<br />
void CMediVisionView::OnRButtonDown(UINT nFlags, CPoint point) <br />
{<br />
	// TODO: Add your message handler code here and/or call default<br />
<br />
<br />
		<br />
			CPoint ptRefer;<br />
			ptRefer = point;<br />
<br />
			point_x = ptRefer.x;<br />
			point_y = ptRefer.y;<br />
			int n=0;<br />
<br />
			if (minflag == true) {<br />
<br />
				point_x1 = point_x - posx512;									<br />
				point_y1 = point_y - posy512;					<br />
				n = (512-(point_y1+1))*512 + point_x1;	<br />
			//	image512[n]=255;<br />
				mini= image512[n];<br />
				maxflag=true;<br />
				MessageBox("Pls Right-Click to find Maximum point WITHIN the box.");<br />
			}<br />
<br />
		<br />
			if(minflag==false && maxflag==true){<br />
			<br />
				point_w1 = point_x - posx512;						<br />
				point_z1 = point_y - posy512;									<br />
				n = (512-(point_z1+1))*512 + point_w1;<br />
		//		image512[n]=255;<br />
				maxi= image512[n];<br />
				maxflag=false;<br />
			}<br />
<br />
			minflag = false;<br />
<br />
		//--------Image Intensity-------//<br />
			if(minflag==false && maxflag==false){<br />
			int Column,Row;<br />
<br />
			int length, height, refer;<br />
			int diff = (maxi - mini);<br />
			length= abs(m2_x - m1_x);		// length of the rectangular box for segmentation;<br />
			height= abs(m2_y - m1_y);<br />
			<br />
			Rectx = m1_x - posx512;<br />
			Recty = m1_y - posy512;<br />
<br />
			n = (512-(Recty+1))*512 + Rectx;<br />
			<br />
			refer=0;<br />
			for (Row=0; Row<height; Row++) {<br />
				for (Column=0; Column<length; Column++) {	<br />
					if ((image512[n]>maxi+(int)(0.2*diff)) || (image512[n]<mini+(int)(0.2*diff))) {<br />
						image512[n] = 0;<br />
						<br />
					}<br />
				<br />
					if(refer == length){<br />
						n =  ((512-(Recty+1))-Row)*512 + Rectx;<br />
						refer = 0;					<br />
					}<br />
					refer++;<br />
					n++;<br />
				}<br />
			}<br />
<br />
<br />
	Draw();<br />
	}<br />
<br />
	//-----------------------//<br />
<br />
<br />
<br />
	CView::OnRButtonDown(nFlags, point);<br />
}<br />
<br />
<br />
<br />
<br />
void CMediVisionView::AreaSegmentation()<br />
{<br />
	int i, j, n;<br />
	n = 0;<br />
<br />
	sg_length= abs(m2_x - m1_x);		// length of the rectangular box for segmentation;<br />
	sg_height = abs(m2_y - m1_y);		// height of the rectangular box for segmentation;<br />
<br />
	Rectn_x = m1_x - posx512;	// 5 is because the StretchDIbits command has			<br />
	Rectn_y = m1_y - posy512;	// set the corner of image at (5, 5)<br />
<br />
	int reference;<br />
	int row, column;<br />
<br />
	<br />
	if(no_of_rows == 512 && no_of_cols==512){ //if 512 by 512 pixels<br />
<br />
	<br />
		num = (512-(Rectn_y+1))*512 + Rectn_x;		//calculations for the co-ordinates for the points of the rectangular box			<br />
		reference = 0;<br />
<br />
		n=0; //initialise it to Zero.<br />
		for(i=0; i<no_of_cols; i++) {  //Condition: Works only if no of rows and columns are 512 by 512 pixels.    <br />
			for(j=0; j<no_of_rows; j++) { <br />
				<br />
				store512[n] = 0; //define image to be stored into black background<br />
				n++;<br />
			}	<br />
		}<br />
<br />
		for (row=0; row<sg_height; row++) {<br />
			for (column=0; column<sg_length; column++) {<br />
				<br />
				store512[num]= image512[num]; //stores the image found in image512[num] into store512[num]<br />
				<br />
				if(reference == sg_length){<br />
					num =   ((512-(Rectn_y+1))-row)*512 + Rectn_x; //calculations for the co-ordinates for the points of the rectangular box			<br />
					reference = 0;					<br />
<br />
				}														<br />
				num++;<br />
				reference++;<br />
			}<br />
		}<br />
			<br />
		n=0;<br />
		for(i=0; i<no_of_cols; i++)	{		<br />
			for(j=0; j<no_of_rows; j++) {<br />
				image512[n]=0; //define image to be made into black background<br />
				n++;<br />
			}<br />
		}<br />
<br />
		n=0;<br />
		for(i=0; i<no_of_cols; i++) {<br />
			for(j=0; j<no_of_rows; j++) {<br />
				image512[n]=store512[n]; //image being copied & overwritten into image512[n] with new black background image<br />
				n++;					//So as to replace the previous one so that the image of another can appear<br />
			}	<br />
		}<br />
	<br />
	}<br />
<br />
//	pDC = GetDC();<br />
//	hdc= pDC->GetSafeHdc();<br />
//	Draw();<br />
<br />
	return;	<br />
<br />
<br />
}<br />
<br />
//------------End of coding-----------------//<br />

GeneralMany thanks Pin
Aris Adrianto Suryadi13-Mar-04 18:11
Aris Adrianto Suryadi13-Mar-04 18:11 
GeneralTemplate-Based Helper Classes Pin
Mo Hossny9-Mar-04 3:31
Mo Hossny9-Mar-04 3:31 
General3D Matrix Pin
macmac388-Oct-03 10:32
macmac388-Oct-03 10:32 
GeneralRe: 3D Matrix Pin
Mo Hossny9-Mar-04 3:26
Mo Hossny9-Mar-04 3:26 
GeneralRe: 3D Matrix Pin
Aris 4 fun13-Mar-04 18:18
sussAris 4 fun13-Mar-04 18:18 
GeneralRe: 3D Matrix Pin
Mo Hossny13-Mar-04 20:48
Mo Hossny13-Mar-04 20:48 
GeneralRe: 3D Matrix Pin
ninangel8-Oct-04 15:31
ninangel8-Oct-04 15:31 
GeneralRe: 3D Matrix Pin
Mo Hossny9-Oct-04 8:12
Mo Hossny9-Oct-04 8:12 
QuestionDefensive ? Pin
killer_bass11-Jun-02 7:44
killer_bass11-Jun-02 7:44 
AnswerRe: Defensive ? Pin
Alex_Chirokov21-Aug-03 7:58
sussAlex_Chirokov21-Aug-03 7:58 
GeneralGood Job! Pin
WREY8-Jun-02 1:46
WREY8-Jun-02 1:46 
GeneralMatrix Template Library Pin
Jonathan de Halleux6-Jun-02 4:29
Jonathan de Halleux6-Jun-02 4:29 
GeneralRe: Matrix Template Library Pin
Ernest Laurentin6-Jun-02 5:00
Ernest Laurentin6-Jun-02 5:00 
GeneralRe: Matrix Template Library Pin
Jonathan de Halleux6-Jun-02 5:14
Jonathan de Halleux6-Jun-02 5:14 
GeneralRe: Matrix Template Library Pin
Bartosz Bien24-Feb-04 22:14
Bartosz Bien24-Feb-04 22:14 
GeneralRe: Matrix Template Library Pin
Jonathan de Halleux24-Feb-04 23:29
Jonathan de Halleux24-Feb-04 23:29 
GeneralNice feature, fair article Pin
Ed Gadziemski6-Jun-02 3:37
professionalEd Gadziemski6-Jun-02 3:37 
GeneralSome thoughts Pin
6-Jun-02 0:05
suss6-Jun-02 0:05 
GeneralRe: Some thoughts Pin
John M. Drescher8-Jun-02 0:02
John M. Drescher8-Jun-02 0:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.