Dear friends:
I want to draw the sparse matrix sturucture to a file. The attachment shows an example.
Could you please suggest me some methods to achieve this goal.
Your Sincerely

Recommended Answers

All 4 Replies

Member Avatar

Achieve what? An algorithm or how to draw to a file?

i do not know how to write a bmp file using c++.

Member Avatar

Never done it per se, but using a library would be the way forward.

Wouldn't be able to recommend the *best* ones because I've never done such a thing in c++. Maybe someone else can point you in the right direction.

Some code I wrote a long time ago do dump my sparse matrix data to a tga so I could analyze it.



if ( (GetKeyState( '0' ) & 0x80)
	WriteMatrixToTGA<DMatrix>(A, "Amat20x20.tga");

template<typename MATRIX> 
inline void WriteMatrixToTGA(const MATRIX& mat, const char* fileName)
	int testSize = mat.GetNumRows();
	unsigned int* image = new unsigned int[ testSize * testSize ];
	memset( image, 0xff, 4 * testSize * testSize );
	for (int rr=0; rr<(int)mat.GetNumRows(); ++rr)
		for (int cc=0; cc<(int)mat.GetNumCols(); ++cc)
				if ( mat.Get(rr,cc) != 0.0f )
					const int index = rr + cc*testSize;
					DBG_ASSERT(index>=0 && index<testSize*testSize);
					image[ index ] = 0xffff0000;

	WriteDataToTGA( fileName, image, testSize, testSize );
	delete[] image;

// Code for writing to a tga

typedef unsigned char  uint08;
typedef unsigned short uint16 ;

// tga header structure.
#pragma pack(1)
typedef struct _tgaheader
		uint08 idlength;					// Misc header
		uint08 colourmap;
		uint08 imagetype;
		uint16 colourmap_origin;			// Colour map spec
		uint16 colourmap_length;
		uint08 colourmap_type;
		uint16 xorigin;						// Image spec
		uint16 yorigin;
		uint16 width;
		uint16 height;
		uint08 pixelsize;
		uint08 descriptor;
} TGAHeader, *PTGAHeader;
#pragma pack()
void WriteDataToTGA(const char *fileName, const unsigned int* argb, unsigned int width, unsigned int height)
	TGAHeader Header = {0,0,2, 0,0,32, 0,0,width,height,32,0x00000008};
	DWORD dwNumWritten = 0;
	WriteFile(TGAoutput, &Header, sizeof(Header), &dwNumWritten, 0);

	unsigned int *fileBuffer = new unsigned int[ width * height ];

	for (unsigned int i=0; i<height; i++)
		for (unsigned int j=0; j<width; j++)
			const int index = j+((width-1-i)*height);
			DBG_ASSERT(index>=0 && index<(int)(width*height));
			fileBuffer[index] = *argb;
		}// End inner loop
	}// End outer loop
	//memcpy( fileBuffer, argb, width * height * 4 );

	dwNumWritten = 0;
	WriteFile(TGAoutput, fileBuffer, width*height*4, &dwNumWritten, 0);
	DBG_ASSERT(dwNumWritten== width*height*4);

	delete[] fileBuffer;

}// End WriteDataToTGA(..)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.