BitmapData* BitmapData::diff(const BitmapData* a, const BitmapData* b) { if (a->w != b->w || a->h != b->h) return NULL; BitmapData* result = lmNew(NULL) BitmapData( (size_t)a->w, (size_t)a->h ); if (result == NULL) { lmLogError(gGFXLogGroup, "Unable to allocate memory for BitmapData diff result"); return NULL; } rgba_t* pixelptr_a = reinterpret_cast<rgba_t*>(a->data); rgba_t* pixelptr_b = reinterpret_cast<rgba_t*>(b->data); rgba_t* resultptr = reinterpret_cast<rgba_t*>(result->data); for (size_t i = 0; i < a->w * a->h; i++) { Color ap(convertHostToBEndian(pixelptr_a[i])); Color bp(convertHostToBEndian(pixelptr_b[i])); Color rp(fabsf(ap.r - bp.r), fabsf(ap.g - bp.g), fabsf(ap.b - bp.b), 1.0f); rgba_t a = rp.getHex(); resultptr[i] = convertBEndianToHost(a); } return result; }
bool TerrainBlock::exportHeightMap( const UTF8 *filePath, const String &format ) const { GBitmap output( mFile->mSize, mFile->mSize, false, GFXFormatR5G6B5 ); // First capture the max height... we'll normalize // everything to this value. U16 maxHeight = 0; Vector<const U16>::iterator iBits = mFile->mHeightMap.begin(); for ( S32 y = 0; y < mFile->mSize; y++ ) { for ( S32 x = 0; x < mFile->mSize; x++ ) { if ( *iBits > maxHeight ) maxHeight = *iBits; ++iBits; } } // Now write out the map. iBits = mFile->mHeightMap.begin(); U16 *oBits = (U16*)output.getWritableBits(); for ( S32 y = 0; y < mFile->mSize; y++ ) { for ( S32 x = 0; x < mFile->mSize; x++ ) { // PNG expects big endian. U16 height = (U16)( ( (F32)(*iBits) / (F32)maxHeight ) * (F32)U16_MAX ); *oBits = convertHostToBEndian( height ); ++oBits; ++iBits; } } FileStream stream; if ( !stream.open( filePath, Torque::FS::File::Write ) ) { Con::errorf( "TerrainBlock::exportHeightMap() - Error opening file for writing: %s !", filePath ); return false; } if ( !output.writeBitmap( format, stream ) ) { Con::errorf( "TerrainBlock::exportHeightMap() - Error writing %s: %s !", format.c_str(), filePath ); return false; } // Print out the map size in meters, so that the user // knows what values to use when importing it into // another terrain tool. S32 dim = mSquareSize * mFile->mSize; S32 height = fixedToFloat( maxHeight ); Con::printf( "Saved heightmap with dimensions %d x %d x %d.", dim, dim, height ); return true; }
void BitmapData::setPixel(unsigned int x, unsigned int y, rgba_t color) { if (x < 0 || x >= w || y < 0 || y >= h) return; rgba_t* pixelptr = reinterpret_cast<rgba_t*>(data); pixelptr[x + y * w] = convertHostToBEndian(color); }
rgba_t BitmapData::getPixel(unsigned int x, unsigned int y) { if (x < 0 || x >= w || y < 0 || y >= h) return 0; rgba_t* pixelptr = reinterpret_cast<rgba_t*>(data); return convertHostToBEndian(pixelptr[x + y * w]); }