00001 #ifdef HAVE_CONFIG_H
00002 # include <simgear_config.h>
00003 #endif
00004
00005 #ifdef WIN32
00006 # include <windows.h>
00007 #endif
00008
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <limits.h>
00012 #include <string.h>
00013
00014 #include "GLBitmaps.h"
00015
00016 GlBitmap::GlBitmap( GLenum mode, GLint width, GLint height, GLubyte *bitmap )
00017 : m_bytesPerPixel(mode==GL_RGB?3:4), m_width(width), m_height(height), m_bitmap(NULL)
00018 {
00019 m_bitmapSize = m_bytesPerPixel*m_width*m_height;
00020 if ( !m_bitmapSize )
00021 {
00022 GLint vp[4];
00023 glGetIntegerv( GL_VIEWPORT, vp );
00024 m_width = vp[2];
00025 m_height = vp[3];
00026 m_bitmapSize = m_bytesPerPixel*m_width*m_height;
00027 }
00028 m_bitmap = (GLubyte *)malloc( m_bitmapSize );
00029 if ( bitmap ) memcpy( m_bitmap, bitmap, m_bitmapSize );
00030 else glReadPixels( 0,0, m_width,m_height, mode, GL_UNSIGNED_BYTE, m_bitmap );
00031 }
00032
00033 GlBitmap::~GlBitmap( )
00034 {
00035 if ( m_bitmap ) free( m_bitmap );
00036 }
00037
00038 GLubyte *GlBitmap::getBitmap()
00039 {
00040 return m_bitmap;
00041 }
00042
00043 void GlBitmap::copyBitmap( GlBitmap *from, GLint at_x, GLint at_y )
00044 {
00045 GLint newWidth = at_x + from->m_width;
00046 GLint newHeight = at_y + from->m_height;
00047 if ( newWidth < m_width ) newWidth = m_width;
00048 if ( newHeight < m_height ) newHeight = m_height;
00049 m_bitmapSize = m_bytesPerPixel*newWidth*newHeight;
00050 GLubyte *newBitmap = (GLubyte *)malloc( m_bitmapSize );
00051 GLint x,y;
00052 for ( y=0; y<m_height; y++ )
00053 {
00054 GLubyte *s = m_bitmap + m_bytesPerPixel * (y * m_width);
00055 GLubyte *d = newBitmap + m_bytesPerPixel * (y * newWidth);
00056 memcpy( d, s, m_bytesPerPixel * m_width );
00057 }
00058 m_width = newWidth;
00059 m_height = newHeight;
00060 free( m_bitmap );
00061 m_bitmap = newBitmap;
00062 for ( y=0; y<from->m_height; y++ )
00063 {
00064 GLubyte *s = from->m_bitmap + from->m_bytesPerPixel * (y * from->m_width);
00065 GLubyte *d = m_bitmap + m_bytesPerPixel * ((at_y+y) * m_width + at_x);
00066 for ( x=0; x<from->m_width; x++ )
00067 {
00068 d[0] = s[0];
00069 d[1] = s[1];
00070 d[2] = s[2];
00071 if ( m_bytesPerPixel == 4 )
00072 {
00073 d[3] = (from->m_bytesPerPixel == 4) ? s[3] : 0;
00074 }
00075 s += from->m_bytesPerPixel;
00076 d += m_bytesPerPixel;
00077 }
00078 }
00079 }
00080