imrgb8.cpp

来自「《3D游戏引擎设计》的源码」· C++ 代码 · 共 195 行

CPP
195
字号
#include <fstream.h>
#include <string.h>
#include "imrgb8.h"

// error handling
int mgcImageRGB8::verbose = 0;
unsigned mgcImageRGB8::error = 0;
const unsigned mgcImageRGB8::zero_dimension      = 0x00000001;
const unsigned mgcImageRGB8::invalid_dimension   = 0x00000002;
const unsigned mgcImageRGB8::null_pixel_pointer  = 0x00000004;
const unsigned mgcImageRGB8::get_data_failed     = 0x00000008;
const unsigned mgcImageRGB8::put_data_failed     = 0x00000010;
const unsigned mgcImageRGB8::load_failed         = 0x00000020;
const unsigned mgcImageRGB8::save_failed         = 0x00000040;
const char* mgcImageRGB8::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"
};

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

    memcpy(data,image.data,size*quantity);
    return *this;
}
//---------------------------------------------------------------------------
mgcImageRGB8& mgcImageRGB8::
operator= (const mgcImage& image)
{
    *(mgcImage*)this = image;
    return *this;
}
//---------------------------------------------------------------------------
mgcImageRGB8& mgcImageRGB8::
operator= (unsigned int value)
{
    if ( data )
        for (int i = 0; i < quantity; i++)
            (*this)[i] = value;
    else
        Report(null_pixel_pointer);
    return *this;
}
//---------------------------------------------------------------------------
int mgcImageRGB8::
operator== (const mgcImage& image)
{
    return *(mgcImage*)this == image;
}
//---------------------------------------------------------------------------
int mgcImageRGB8::
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;
}
//---------------------------------------------------------------------------
int mgcImageRGB8::
Number (unsigned single_error)
{
    int result;
    for (result = -1; single_error; single_error >>= 1)
        result++;
    return result;
}
//---------------------------------------------------------------------------
void mgcImageRGB8::
Report (unsigned single_error)
{
    if ( verbose || mgcImage::verbose || mgcLattice::verbose )
        cout << "mgcImageRGB8: " << message[Number(single_error)] << endl;
    else
        ofstream("imrgb8.err",ios::out|ios::app)
             << "mgcImageRGB8: " << message[Number(single_error)] << endl;

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

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

⌨️ 快捷键说明

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