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

📄 imlong.cpp

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

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

// value to assign when a divide-by-zero occurs
long mgcImageLong::zero_divide_result = 0;

//===========================================================================
mgcImageLong::
mgcImageLong () :
	mgcImage(mgcLong::type)
{
}
//---------------------------------------------------------------------------
mgcImageLong::
mgcImageLong (int _dimensions, const int* initial) :
	mgcImage(mgcLong::type,_dimensions,initial)
{
}
//---------------------------------------------------------------------------
mgcImageLong::mgcImageLong (int _dimensions, int initial_all) :
	mgcImage(mgcLong::type,_dimensions,initial_all)
{
}
//---------------------------------------------------------------------------
mgcImageLong::
mgcImageLong (int _dimensions, int initial0, int initial1, ...) :
	mgcImage(mgcLong::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(mgcLong::type,_dimensions,initial);
	delete[] initial;
}
//---------------------------------------------------------------------------
mgcImageLong::
mgcImageLong (const mgcImage& image) :
	mgcImage(mgcLong::type,image.Dimensions(),image.Bounds())
{
	*(mgcImage*)this = image;
}
//---------------------------------------------------------------------------
mgcImageLong::
mgcImageLong (const mgcLattice& lattice) :
	mgcImage(mgcLong::type,lattice.Dimensions(),lattice.Bounds())
{
}
//---------------------------------------------------------------------------
mgcImageLong::
mgcImageLong (char* fname) :
	mgcImage(mgcLong::type)
{
	Load(fname);
}
//---------------------------------------------------------------------------
mgcImage& mgcImageLong::
Load (char* fname, int trgt)
{
	if ( trgt == -1 )
		trgt = mgcLong::type;
	mgcImage::Load(fname,trgt);
	return *this;
}
//---------------------------------------------------------------------------
mgcImage& mgcImageLong::
Save (char* fname, int trgt)
{
	if ( trgt == -1 )
		trgt = mgcLong::type;
	mgcImage::Save(fname,trgt);
	return *this;
}
//---------------------------------------------------------------------------
mgcImage& mgcImageLong::
SaveAs (char* fname, int ff, int trgt)
{
	if ( trgt == -1 )
		trgt = mgcLong::type;
	mgcImage::SaveAs(fname,ff,trgt);
	return *this;
}
//---------------------------------------------------------------------------
mgcImageLong& mgcImageLong::
operator= (const mgcImageLong& image)
{
	if ( *(mgcLattice*)this != *(mgcLattice*)&image ) {
		*(mgcImage*)this = image;
		return *this;
	}

	memcpy(data,image.data,size*quantity);
	return *this;
}
//---------------------------------------------------------------------------
mgcImageLong& mgcImageLong::
operator= (const mgcImage& image)
{
	*(mgcImage*)this = image;
	return *this;
}
//---------------------------------------------------------------------------
mgcImageLong& mgcImageLong::
operator= (long value)
{
	if ( data )
		for (int i = 0; i < quantity; i++)
			(*this)[i] = value;
	else
		Report(null_pixel_pointer);
	return *this;
}
//---------------------------------------------------------------------------
int mgcImageLong::
operator== (const mgcImage& image)
{
	return *(mgcImage*)this == image;
}
//---------------------------------------------------------------------------
int mgcImageLong::
operator== (long 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;
}
//---------------------------------------------------------------------------
mgcImageLong& mgcImageLong::
operator+= (const mgcImageLong &image)
{
	if ( *(mgcLattice*)this != image ) {
		Report(incompatible_images);
		return *this;
	}

	for (int i = 0; i < quantity; i++)
		(*this)[i] += image[i];
	return *this;
}
//---------------------------------------------------------------------------
mgcImageLong& mgcImageLong::
operator-= (const mgcImageLong &image)
{
	if ( *(mgcLattice*)this != image ) {
		Report(incompatible_images);
		return *this;
	}

	for (int i = 0; i < quantity; i++)
		(*this)[i] -= image[i];
	return *this;
}
//---------------------------------------------------------------------------
mgcImageLong& mgcImageLong::
operator*= (const mgcImageLong &image)
{
	if ( *(mgcLattice*)this != image ) {
		Report(incompatible_images);
		return *this;
	}

	for (int i = 0; i < quantity; i++)
		(*this)[i] *= image[i];
	return *this;
}
//---------------------------------------------------------------------------
mgcImageLong& mgcImageLong::
operator/= (const mgcImageLong &image)
{
	if ( *(mgcLattice*)this != image ) {
		Report(incompatible_images);
		return *this;
	}

	for (int i = 0; i < quantity; i++)
		if ( image[i] != 0 )
			(*this)[i] /= image[i];
		else
			(*this)[i] = zero_divide_result;
	return *this;
}
//---------------------------------------------------------------------------
mgcImageLong& mgcImageLong::
operator+= (long value)
{
	for (int i = 0; i < quantity; i++)
		(*this)[i] += value;
	return *this;
}
//---------------------------------------------------------------------------
mgcImageLong& mgcImageLong::
operator-= (long value)
{
	for (int i = 0; i < quantity; i++)
		(*this)[i] -= value;
	return *this;
}
//---------------------------------------------------------------------------
mgcImageLong& mgcImageLong::
operator*= (long value)
{
	for (int i = 0; i < quantity; i++)
		(*this)[i] *= value;
	return *this;
}
//---------------------------------------------------------------------------
mgcImageLong& mgcImageLong::
operator/= (long value)
{
	for (int i = 0; i < quantity; i++)
		if ( value != 0 )
			(*this)[i] /= value;
		else
			(*this)[i] = zero_divide_result;
	return *this;
}
//---------------------------------------------------------------------------
mgcImageLong mgcImageLong::
operator- ()
{
	mgcImageLong result = *this;
	for (int i = 0; i < quantity; i++)
		result[i] = -result[i];
	return result;
}
//---------------------------------------------------------------------------
mgcImageLong mgcImageLong::
operator+ (const mgcImageLong &image)
{
	mgcImageLong result = *this;
	result += image;
	return result;
}
//---------------------------------------------------------------------------
mgcImageLong mgcImageLong::
operator- (const mgcImageLong &image)
{
	mgcImageLong result = *this;
	result -= image;
	return result;
}
//---------------------------------------------------------------------------
mgcImageLong mgcImageLong::
operator* (const mgcImageLong &image)
{
	mgcImageLong result = *this;
	result *= image;
	return result;
}
//---------------------------------------------------------------------------
mgcImageLong mgcImageLong::
operator/ (const mgcImageLong &image)
{
	mgcImageLong result = *this;
	result /= image;
	return result;
}
//---------------------------------------------------------------------------
mgcImageLong mgcImageLong::
operator+ (long value)
{
	mgcImageLong result = *this;
	result += value;
	return result;
}
//---------------------------------------------------------------------------
mgcImageLong mgcImageLong::
operator- (long value)
{
	mgcImageLong result = *this;
	result -= value;
	return result;
}
//---------------------------------------------------------------------------
mgcImageLong mgcImageLong::
operator* (long value)
{
	mgcImageLong result = *this;
	result *= value;
	return result;
}
//---------------------------------------------------------------------------
mgcImageLong mgcImageLong::
operator/ (long value)
{
	mgcImageLong result = *this;
	result /= value;
	return result;
}
//---------------------------------------------------------------------------
mgcImageLong
operator+ (long value, mgcImageLong &image)
{
	return image+value;
}
//---------------------------------------------------------------------------
mgcImageLong
operator- (long value, mgcImageLong &image)
{
	return -image+value;
}
//---------------------------------------------------------------------------
mgcImageLong
operator* (long value, mgcImageLong &image)
{
	return image*value;
}
//---------------------------------------------------------------------------
mgcImageLong
operator/ (long value, mgcImageLong &image)
{
	mgcImageLong result(image.Dimensions(),image.Bounds());
	result = value;
	result /= image;
	return result;
}
//---------------------------------------------------------------------------
void mgcImageLong::
ComputeMaxMin ()
{
	minimum = maximum = (*this)[0];

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

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

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


⌨️ 快捷键说明

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