gfxImageSurface::gfxImageSurface(const gfxIntSize& size, gfxImageFormat format) : mSize(size), mFormat(format) { mStride = ComputeStride(); if (!CheckSurfaceSize(size)) return; // if we have a zero-sized surface, just set mData to nsnull if (mSize.height * mStride > 0) { mData = (unsigned char *) malloc(mSize.height * mStride); if (!mData) return; } else { mData = nsnull; } mOwnsData = PR_TRUE; cairo_surface_t *surface = cairo_image_surface_create_for_data((unsigned char*)mData, (cairo_format_t)format, mSize.width, mSize.height, mStride); Init(surface); }
// TODO: Add meaningful return values int Surface::LoadBMP( const IDataStream& Stream ) { SBitmapFileHeader BMPFileHeader; SBitmapInfoHeader BMPInfoHeader; Stream.Read( sizeof( SBitmapFileHeader ), &BMPFileHeader ); Stream.Read( sizeof( SBitmapInfoHeader ), &BMPInfoHeader ); int Width = BMPInfoHeader.m_Width; int Height = BMPInfoHeader.m_Height; int Stride = ComputeStride( BMPInfoHeader.m_Width, BMPInfoHeader.m_BitCount ); Reset( Width, Height, BMPInfoHeader.m_BitCount ); #if BUILD_WINDOWS_NO_SDL Stream.Read( Stride * Height, m_pPixels ); #endif #if BUILD_SDL // SDL stores surfaces from top to bottom, so flip the rows byte* pPixels = m_pPixels + ( ( Height - 1 ) * Stride ); for( int Row = 0; Row < Height; ++Row, pPixels -= Stride ) { Stream.Read( Stride, pPixels ); } #endif return 0; }
void Surface::Reset( int Width, int Height, int BitCount ) { #if BUILD_WINDOWS_NO_SDL if( m_BMP ) { SelectObject( m_hDC, m_OldBMP ); DeleteObject( m_BMP ); } if( !m_hDC ) { m_hDC = CreateCompatibleDC( NULL ); // Could take a window's DC, but this should work for now } #endif m_BitmapInfo.m_Header.m_Size = sizeof( m_BitmapInfo.m_Header ); m_Width = m_BitmapInfo.m_Header.m_Width = Width; m_Height = m_BitmapInfo.m_Header.m_Height = Height; m_BitmapInfo.m_Header.m_Planes = 1; m_BitmapInfo.m_Header.m_BitCount = static_cast<uint16>( BitCount ); m_BitmapInfo.m_Header.m_Compression = 0; // BI_RGB m_Stride = ComputeStride( m_Width, BitCount ); m_Padding = ComputePadding(); #if BUILD_WINDOWS_NO_SDL // NOTE: This only supports 24-bit images! 32-bit was added for SDL icon support only. BITMAPINFO* const pBitmapInfo = reinterpret_cast<BITMAPINFO*>( &m_BitmapInfo ); m_BMP = CreateDIBSection( m_hDC, pBitmapInfo, DIB_RGB_COLORS, (void**)&m_pPixels, NULL, NULL ); m_OldBMP = (HBITMAP)SelectObject( m_hDC, m_BMP ); #endif #if BUILD_SDL m_Surface = SDL_CreateRGBSurface( 0, m_Width, m_Height, BitCount, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 ); ASSERT( m_Surface ); ASSERT( !SDL_MUSTLOCK( m_Surface ) ); // Surfaces are always in RAM now, so there's no reason they should need to be locked. m_pPixels = static_cast<byte*>( m_Surface->pixels ); #endif m_ViewportLeft = 0; m_ViewportTop = 0; m_ViewportRight = m_Width; m_ViewportBottom = m_Height; }