Пример #1
0
// static
bool PortableFloatMapIO::write( const std::string& filename, Array2DReadView< Vector4f > image )
{
    int w = image.width();
    int h = image.height();

    // use "wb" binary mode to ensure that on Windows,
    // newlines in the header are written out as '\n'
    FILE* pFile = fopen( filename.c_str(), "wb" );
    if( pFile == nullptr )
    {
        return false;
    }

    // write header
    int nCharsWritten = fprintf( pFile, "PF4\n%d %d\n-1\n", w, h );
    if( nCharsWritten < 0 )
    {
        fclose( pFile );
        return false;
    }

    // All at once.
    if( image.packed() )
    {
        fwrite( image.rowPointer( 0 ), sizeof( Vector4f ), image.numElements(), pFile );
    }
    // Row by Row.
    else if( image.elementsArePacked() )
    {
        for( int y = 0; y < h; ++y )
        {
            fwrite( image.rowPointer( y ), sizeof( Vector4f ), image.width(), pFile );
        }
    }
    // Element by element.
    else
    {
        for( int y = 0; y < h; ++y )
        {
            for( int x = 0; x < w; ++x )
            {
                fwrite( image.elementPointer( { x, y } ), sizeof( Vector4f ), 1, pFile );
            }
        }
    }

    fclose( pFile );
    return true;
}