⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imuint.cpp

📁 3D Game Engine Design Source Code非常棒
💻 CPP
字号:
#include <fstream.h>
#include <string.h>
#include "imuint.h"

// error handling
int mgcImageUint::verbose = 0;
unsigned mgcImageUint::error = 0;
const unsigned mgcImageUint::zero_dimension      = 0x00000001;
const unsigned mgcImageUint::invalid_dimension   = 0x00000002;
const unsigned mgcImageUint::null_pixel_pointer  = 0x00000004;
const unsigned mgcImageUint::get_data_failed     = 0x00000008;
const unsigned mgcImageUint::put_data_failed     = 0x00000010;
const unsigned mgcImageUint::load_failed         = 0x00000020;
const unsigned mgcImageUint::save_failed         = 0x00000040;
const char* mgcImageUint::message[7] = {
	"zero dimension in argument list",
	"invalid dimension in variable argument list",
	"null pointer in assignment",
	"attempt to read file data failed",
	"attempt to write file data failed",
	"attempt to load file failed",
	"attempt to save file failed"
};

//===========================================================================
mgcImageUint::
mgcImageUint () :
	mgcImage(mgcUint::type)
{
}
//---------------------------------------------------------------------------
mgcImageUint::
mgcImageUint (int _dimensions, const int* initial) :
	mgcImage(mgcUint::type,_dimensions,initial)
{
}
//---------------------------------------------------------------------------
mgcImageUint::mgcImageUint (int _dimensions, int initial_all) :
	mgcImage(mgcUint::type,_dimensions,initial_all)
{
}
//---------------------------------------------------------------------------
mgcImageUint::
mgcImageUint (int _dimensions, int initial0, int initial1, ...) :
	mgcImage(mgcUint::type)
{
	if ( _dimensions < 2 ) {
		Report(invalid_dimension);
		return;
	}

	int* initial = new int[_dimensions];
	initial[0] = initial0;
	initial[1] = initial1;

	va_list ap;
	va_start(ap,initial1);
	for (int d = 2; d < _dimensions; d++)
		initial[d] = va_arg(ap,int);
	va_end(ap);

	*(mgcImage*)this = mgcImage(mgcUint::type,_dimensions,initial);
	delete[] initial;
}
//---------------------------------------------------------------------------
mgcImageUint::
mgcImageUint (const mgcImage& image) :
	mgcImage(mgcUint::type,image.Dimensions(),image.Bounds())
{
	*(mgcImage*)this = image;
}
//---------------------------------------------------------------------------
mgcImageUint::
mgcImageUint (const mgcLattice& lattice) :
	mgcImage(mgcUint::type,lattice.Dimensions(),lattice.Bounds())
{
}
//---------------------------------------------------------------------------
mgcImageUint::
mgcImageUint (char* fname) :
	mgcImage(mgcUint::type)
{
	Load(fname);
}
//---------------------------------------------------------------------------
mgcImage& mgcImageUint::
Load (char* fname, int trgt)
{
	if ( trgt == -1 )
		trgt = mgcUint::type;
	mgcImage::Load(fname,trgt);
	return *this;
}
//---------------------------------------------------------------------------
mgcImage& mgcImageUint::
Save (char* fname, int trgt)
{
	if ( trgt == -1 )
		trgt = mgcUint::type;
	mgcImage::Save(fname,trgt);
	return *this;
}
//---------------------------------------------------------------------------
mgcImage& mgcImageUint::
SaveAs (char* fname, int ff, int trgt)
{
	if ( trgt == -1 )
		trgt = mgcUint::type;
	mgcImage::SaveAs(fname,ff,trgt);
	return *this;
}
//---------------------------------------------------------------------------
mgcImageUint& mgcImageUint::
operator= (const mgcImageUint& image)
{
	if ( *(mgcLattice*)this != *(mgcLattice*)&image ) {
		*(mgcImage*)this = image;
		return *this;
	}

	memcpy(data,image.data,size*quantity);
	return *this;
}
//---------------------------------------------------------------------------
mgcImageUint& mgcImageUint::
operator= (const mgcImage& image)
{
	*(mgcImage*)this = image;
	return *this;
}
//---------------------------------------------------------------------------
mgcImageUint& mgcImageUint::
operator= (unsigned int value)
{
	if ( data )
		for (int i = 0; i < quantity; i++)
			(*this)[i] = value;
	else
		Report(null_pixel_pointer);
	return *this;
}
//---------------------------------------------------------------------------
int mgcImageUint::
operator== (const mgcImage& image)
{
	return *(mgcImage*)this == image;
}
//---------------------------------------------------------------------------
int mgcImageUint::
operator== (unsigned int value)
{
	if ( data == 0 ) {
		Report(null_pixel_pointer);
		return 0;
	}

	for (int i = 0; i < quantity; i++)
		if ( (*this)[i] != value )
			return 0;
	return 1;
}
//---------------------------------------------------------------------------
void mgcImageUint::
ComputeMaxMin ()
{
	minimum = maximum = (*this)[0];

	for (int i = 1; i < quantity; i++) {
		unsigned int value = (*this)[i];
		if ( value < minimum )
			minimum = value;
		else if ( value > maximum )
			maximum = value;
	}
}
//---------------------------------------------------------------------------
int mgcImageUint::
Number (unsigned single_error)
{
	int result;
	for (result = -1; single_error; single_error >>= 1)
		result++;
	return result;
}
//---------------------------------------------------------------------------
void mgcImageUint::
Report (unsigned single_error)
{
	if ( verbose || mgcImage::verbose || mgcLattice::verbose )
		cout << "mgcImageUint: " << message[Number(single_error)] << endl;
	else
		ofstream("imuint.err",ios::out|ios::app)
			 << "mgcImageUint: " << message[Number(single_error)] << endl;

	error |= single_error;
}
//---------------------------------------------------------------------------
void mgcImageUint::
Report (ostream& ostr)
{
	for (unsigned single_error = 1; single_error; single_error <<= 1)
		if ( error & single_error )
			ostr << "mgcImageUint: " << message[Number(single_error)]
				 << endl;

	error = 0;
}
//===========================================================================


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -