bool GLTextureCubeMap::set( GLCubeMapFace face, Array2DReadView< uint8x4 > data, GLImageFormat format, const Vector2i& dstOffset ) { if( dstOffset.x + data.width() > sideLength() || dstOffset.y + data.height() > sideLength() ) { return false; } if( format != GLImageFormat::RGBA && format != GLImageFormat::BGRA ) { return false; } //glPushClientAttribDefaultEXT( GL_CLIENT_PIXEL_STORE_BIT ); // TODO: alignment, strides, ..., has to be packed // DSA treats a cube map as a 2D array texture. glTextureSubImage3D ( id(), 0, dstOffset.x, dstOffset.y, static_cast< int >( face ), data.width(), data.height(), 1, // 1 face static_cast< GLenum >( format ), GL_UNSIGNED_BYTE, data.pointer() ); //glPopClientAttrib(); return true; }
// 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; }
void extractBackgroundColor( Array2DReadView< uint8x4 > composite, Array2DReadView< uint8x4 > foreground, Array2DWriteView< uint8x4 > background ) { for( int y = 0; y < composite.height(); ++y ) { for( int x = 0; x < composite.width(); ++x ) { Vector4f cRGBA = toFloat( composite[ { x, y } ] ); Vector4f fRGBA = toFloat( foreground[ { x, y } ] ); Vector4f bRGBA = extractBackgroundColor( cRGBA, fRGBA ); background[ { x, y } ] = toUInt8( bRGBA ); } } }
// static bool PNGIO::write( const std::string& filename, Array2DReadView< uint16_t > image ) { // TODO: use BitPacking::byteSwap16 on the buffer once it supports a // destination. Array2D< uint16_t > tmpImage( image.size() ); libcgt::core::arrayutils::map( image, tmpImage.writeView(), [&] ( uint16_t z ) { return byteSwap16( z ); } ); const uint8_t* srcPointer = reinterpret_cast< const uint8_t* >( tmpImage.pointer() ); unsigned int errVal = lodepng::encode( filename, srcPointer, image.width(), image.height(), LCT_GREY, 16U ); bool succeeded = ( errVal == 0 ); return succeeded; }
void over( Array2DReadView< Vector4f > foreground, Array2DReadView< Vector4f > background, Array2DWriteView< Vector4f > output ) { for( int y = 0; y < foreground.height(); ++y ) { for( int x = 0; x < foreground.width(); ++x ) { Vector4f f = foreground[ { x, y } ]; Vector4f b = background[ { x, y } ]; float fa = f.w; float ba = b.w; Vector3f compositeColor = fa * f.xyz + ( 1.f - fa ) * ( b.xyz ); float compositeAlpha = fa + ba * ( 1 - fa ); output[ { x, y } ] = Vector4f( compositeColor, compositeAlpha ); } } }
// static bool PNGIO::write( const std::string& filename, Array2DReadView< uint8x4 > image ) { Array2D< uint8x4 > tmpImage; const uint8_t* srcPointer; if( !image.packed() ) { tmpImage.resize( image.size() ); copy< uint8x4 >( image, tmpImage ); srcPointer = reinterpret_cast< const uint8_t* >( tmpImage.pointer() ); } else { srcPointer = reinterpret_cast< const uint8_t* >( image.pointer() ); } unsigned int errVal = lodepng::encode( filename, srcPointer, image.width(), image.height(), LCT_RGBA ); bool succeeded = ( errVal == 0 ); return succeeded; }
void extractBackgroundColor( Array2DReadView< Vector4f > composite, Array2DReadView< Vector4f > foreground, Array2DWriteView< Vector4f > background ) { // red channel: // c_r = f_a * f_r + ( 1 - f_a ) * b_r // b_r = ( c_r - f_a * f_r ) / ( 1 - f_a ) // // alpha channel: // c_a = f_a + b_a * ( 1 - f_a ) // b_a = ( c_a - f_a ) / ( 1 - f_a ) for( int y = 0; y < composite.height(); ++y ) { for( int x = 0; x < composite.width(); ++x ) { Vector4f cRGBA = composite[ { x, y } ]; Vector4f fRGBA = foreground[ { x, y } ]; Vector4f bRGBA = extractBackgroundColor( cRGBA, fRGBA ); background[ { x, y } ] = bRGBA; } } }