imfloat.cpp
来自「《3D游戏引擎设计》的源码」· C++ 代码 · 共 406 行
CPP
406 行
#include <fstream.h>
#include <string.h>
#include "imfloat.h"
// error handling
int mgcImageFloat::verbose = 0;
unsigned mgcImageFloat::error = 0;
const unsigned mgcImageFloat::zero_dimension = 0x00000001;
const unsigned mgcImageFloat::invalid_dimension = 0x00000002;
const unsigned mgcImageFloat::null_pixel_pointer = 0x00000004;
const unsigned mgcImageFloat::incompatible_images = 0x00000008;
const unsigned mgcImageFloat::get_data_failed = 0x00000010;
const unsigned mgcImageFloat::put_data_failed = 0x00000020;
const unsigned mgcImageFloat::load_failed = 0x00000040;
const unsigned mgcImageFloat::save_failed = 0x00000080;
const char* mgcImageFloat::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
float mgcImageFloat::zero_divide_result = 0;
//===========================================================================
mgcImageFloat::
mgcImageFloat () :
mgcImage(mgcFloat::type)
{
}
//---------------------------------------------------------------------------
mgcImageFloat::
mgcImageFloat (int _dimensions, const int* initial) :
mgcImage(mgcFloat::type,_dimensions,initial)
{
}
//---------------------------------------------------------------------------
mgcImageFloat::mgcImageFloat (int _dimensions, int initial_all) :
mgcImage(mgcFloat::type,_dimensions,initial_all)
{
}
//---------------------------------------------------------------------------
mgcImageFloat::
mgcImageFloat (int _dimensions, int initial0, int initial1, ...) :
mgcImage(mgcFloat::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(mgcFloat::type,_dimensions,initial);
delete[] initial;
}
//---------------------------------------------------------------------------
mgcImageFloat::
mgcImageFloat (const mgcImage& image) :
mgcImage(mgcFloat::type,image.Dimensions(),image.Bounds())
{
*(mgcImage*)this = image;
}
//---------------------------------------------------------------------------
mgcImageFloat::
mgcImageFloat (const mgcLattice& lattice) :
mgcImage(mgcFloat::type,lattice.Dimensions(),lattice.Bounds())
{
}
//---------------------------------------------------------------------------
mgcImageFloat::
mgcImageFloat (char* fname) :
mgcImage(mgcFloat::type)
{
Load(fname);
}
//---------------------------------------------------------------------------
mgcImage& mgcImageFloat::
Load (char* fname, int trgt)
{
if ( trgt == -1 )
trgt = mgcFloat::type;
mgcImage::Load(fname,trgt);
return *this;
}
//---------------------------------------------------------------------------
mgcImage& mgcImageFloat::
Save (char* fname, int trgt)
{
if ( trgt == -1 )
trgt = mgcFloat::type;
mgcImage::Save(fname,trgt);
return *this;
}
//---------------------------------------------------------------------------
mgcImage& mgcImageFloat::
SaveAs (char* fname, int ff, int trgt)
{
if ( trgt == -1 )
trgt = mgcFloat::type;
mgcImage::SaveAs(fname,ff,trgt);
return *this;
}
//---------------------------------------------------------------------------
mgcImageFloat& mgcImageFloat::
operator= (const mgcImageFloat& image)
{
if ( *(mgcLattice*)this != *(mgcLattice*)&image ) {
*(mgcImage*)this = image;
return *this;
}
memcpy(data,image.data,size*quantity);
return *this;
}
//---------------------------------------------------------------------------
mgcImageFloat& mgcImageFloat::
operator= (const mgcImage& image)
{
*(mgcImage*)this = image;
return *this;
}
//---------------------------------------------------------------------------
mgcImageFloat& mgcImageFloat::
operator= (float value)
{
if ( data )
for (int i = 0; i < quantity; i++)
(*this)[i] = value;
else
Report(null_pixel_pointer);
return *this;
}
//---------------------------------------------------------------------------
int mgcImageFloat::
operator== (const mgcImage& image)
{
return *(mgcImage*)this == image;
}
//---------------------------------------------------------------------------
int mgcImageFloat::
operator== (float 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;
}
//---------------------------------------------------------------------------
mgcImageFloat& mgcImageFloat::
operator+= (const mgcImageFloat &image)
{
if ( *(mgcLattice*)this != image ) {
Report(incompatible_images);
return *this;
}
for (int i = 0; i < quantity; i++)
(*this)[i] += image[i];
return *this;
}
//---------------------------------------------------------------------------
mgcImageFloat& mgcImageFloat::
operator-= (const mgcImageFloat &image)
{
if ( *(mgcLattice*)this != image ) {
Report(incompatible_images);
return *this;
}
for (int i = 0; i < quantity; i++)
(*this)[i] -= image[i];
return *this;
}
//---------------------------------------------------------------------------
mgcImageFloat& mgcImageFloat::
operator*= (const mgcImageFloat &image)
{
if ( *(mgcLattice*)this != image ) {
Report(incompatible_images);
return *this;
}
for (int i = 0; i < quantity; i++)
(*this)[i] *= image[i];
return *this;
}
//---------------------------------------------------------------------------
mgcImageFloat& mgcImageFloat::
operator/= (const mgcImageFloat &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;
}
//---------------------------------------------------------------------------
mgcImageFloat& mgcImageFloat::
operator+= (float value)
{
for (int i = 0; i < quantity; i++)
(*this)[i] += value;
return *this;
}
//---------------------------------------------------------------------------
mgcImageFloat& mgcImageFloat::
operator-= (float value)
{
for (int i = 0; i < quantity; i++)
(*this)[i] -= value;
return *this;
}
//---------------------------------------------------------------------------
mgcImageFloat& mgcImageFloat::
operator*= (float value)
{
for (int i = 0; i < quantity; i++)
(*this)[i] *= value;
return *this;
}
//---------------------------------------------------------------------------
mgcImageFloat& mgcImageFloat::
operator/= (float value)
{
for (int i = 0; i < quantity; i++)
if ( value != 0 )
(*this)[i] /= value;
else
(*this)[i] = zero_divide_result;
return *this;
}
//---------------------------------------------------------------------------
mgcImageFloat mgcImageFloat::
operator- ()
{
mgcImageFloat result = *this;
for (int i = 0; i < quantity; i++)
result[i] = -result[i];
return result;
}
//---------------------------------------------------------------------------
mgcImageFloat mgcImageFloat::
operator+ (const mgcImageFloat &image)
{
mgcImageFloat result = *this;
result += image;
return result;
}
//---------------------------------------------------------------------------
mgcImageFloat mgcImageFloat::
operator- (const mgcImageFloat &image)
{
mgcImageFloat result = *this;
result -= image;
return result;
}
//---------------------------------------------------------------------------
mgcImageFloat mgcImageFloat::
operator* (const mgcImageFloat &image)
{
mgcImageFloat result = *this;
result *= image;
return result;
}
//---------------------------------------------------------------------------
mgcImageFloat mgcImageFloat::
operator/ (const mgcImageFloat &image)
{
mgcImageFloat result = *this;
result /= image;
return result;
}
//---------------------------------------------------------------------------
mgcImageFloat mgcImageFloat::
operator+ (float value)
{
mgcImageFloat result = *this;
result += value;
return result;
}
//---------------------------------------------------------------------------
mgcImageFloat mgcImageFloat::
operator- (float value)
{
mgcImageFloat result = *this;
result -= value;
return result;
}
//---------------------------------------------------------------------------
mgcImageFloat mgcImageFloat::
operator* (float value)
{
mgcImageFloat result = *this;
result *= value;
return result;
}
//---------------------------------------------------------------------------
mgcImageFloat mgcImageFloat::
operator/ (float value)
{
mgcImageFloat result = *this;
result /= value;
return result;
}
//---------------------------------------------------------------------------
mgcImageFloat
operator+ (float value, mgcImageFloat &image)
{
return image+value;
}
//---------------------------------------------------------------------------
mgcImageFloat
operator- (float value, mgcImageFloat &image)
{
return -image+value;
}
//---------------------------------------------------------------------------
mgcImageFloat
operator* (float value, mgcImageFloat &image)
{
return image*value;
}
//---------------------------------------------------------------------------
mgcImageFloat
operator/ (float value, mgcImageFloat &image)
{
mgcImageFloat result(image.Dimensions(),image.Bounds());
result = value;
result /= image;
return result;
}
//---------------------------------------------------------------------------
void mgcImageFloat::
ComputeMaxMin ()
{
minimum = maximum = (*this)[0];
for (int i = 1; i < quantity; i++) {
float value = (*this)[i];
if ( value < minimum )
minimum = value;
else if ( value > maximum )
maximum = value;
}
}
//---------------------------------------------------------------------------
int mgcImageFloat::
Number (unsigned single_error)
{
int result;
for (result = -1; single_error; single_error >>= 1)
result++;
return result;
}
//---------------------------------------------------------------------------
void mgcImageFloat::
Report (unsigned single_error)
{
if ( verbose || mgcImage::verbose || mgcLattice::verbose )
cout << "mgcImageFloat: " << message[Number(single_error)] << endl;
else
ofstream("imfloat.err",ios::out|ios::app)
<< "mgcImageFloat: " << message[Number(single_error)] << endl;
error |= single_error;
}
//---------------------------------------------------------------------------
void mgcImageFloat::
Report (ostream& ostr)
{
for (unsigned single_error = 1; single_error; single_error <<= 1)
if ( error & single_error )
ostr << "mgcImageFloat: " << message[Number(single_error)]
<< endl;
error = 0;
}
//===========================================================================
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?