src/video/ataricommon/SDL_atarigl.c
author Patrice Mandin
Fri, 26 Nov 2004 16:16:50 +0000
changeset 991 12b13601a544
parent 989 475166d13b44
child 992 0324ce32b2d9
permissions -rw-r--r--
Final touches to OSMesa OpenGL support on Atari, using loadable libraries. Hope SDL 1.2.8 is out soon.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2004 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* Atari OSMesa.ldg implementation of SDL OpenGL support */
    24 
    25 /*--- Includes ---*/
    26 
    27 #include <stdio.h>
    28 #include <stdlib.h>
    29 #include <string.h>
    30 #ifdef HAVE_OPENGL
    31 #include <GL/osmesa.h>
    32 #endif
    33 
    34 #include <mint/osbind.h>
    35 
    36 #include "SDL_video.h"
    37 #include "SDL_error.h"
    38 #include "SDL_endian.h"
    39 #include "SDL_atarigl_c.h"
    40 #ifdef ENABLE_OSMESA_SHARED
    41 #include "SDL_loadso.h"
    42 #endif
    43 
    44 /*--- Defines ---*/
    45 
    46 #define PATH_OSMESA_LDG	"osmesa.ldg"
    47 #define PATH_MESAGL_LDG	"mesa_gl.ldg"
    48 #define PATH_TINYGL_LDG	"tiny_gl.ldg"
    49 
    50 /*--- Functions prototypes ---*/
    51 
    52 static void SDL_AtariGL_UnloadLibrary(_THIS);
    53 
    54 static void CopyShadowNull(_THIS, SDL_Surface *surface);
    55 static void CopyShadowDirect(_THIS, SDL_Surface *surface);
    56 static void CopyShadow8888To555(_THIS, SDL_Surface *surface);
    57 static void CopyShadow8888To565(_THIS, SDL_Surface *surface);
    58 
    59 static void ConvertNull(_THIS, SDL_Surface *surface);
    60 static void Convert565To555be(_THIS, SDL_Surface *surface);
    61 static void Convert565To555le(_THIS, SDL_Surface *surface);
    62 static void Convert565le(_THIS, SDL_Surface *surface);
    63 static void ConvertBGRAToABGR(_THIS, SDL_Surface *surface);
    64 
    65 static int InitNew(_THIS, SDL_Surface *current);
    66 static int InitOld(_THIS, SDL_Surface *current);
    67 
    68 /*--- Public functions ---*/
    69 
    70 int SDL_AtariGL_Init(_THIS, SDL_Surface *current)
    71 {
    72 #ifdef HAVE_OPENGL
    73 	if (gl_oldmesa) {
    74 		gl_active = InitOld(this, current);		
    75 	} else {
    76 		gl_active = InitNew(this, current);		
    77 	}
    78 #endif
    79 
    80 	return (gl_active);
    81 }
    82 
    83 void SDL_AtariGL_Quit(_THIS)
    84 {
    85 #ifdef HAVE_OPENGL
    86 	if (!gl_active) {
    87 		return;
    88 	}
    89 
    90 	if (gl_oldmesa) {
    91 		/* Old mesa implementations */
    92 		if (this->gl_data->OSMesaDestroyLDG) {
    93 			this->gl_data->OSMesaDestroyLDG();
    94 		}
    95 		if (gl_shadow) {
    96 			Mfree(gl_shadow);
    97 			gl_shadow = NULL;
    98 		}
    99 	} else {
   100 		/* New mesa implementation */
   101 		if (gl_ctx) {
   102 			if (this->gl_data->OSMesaDestroyContext) {
   103 				this->gl_data->OSMesaDestroyContext(gl_ctx);
   104 			}
   105 			gl_ctx = NULL;
   106 		}
   107 	}
   108 
   109 	SDL_AtariGL_UnloadLibrary(this);
   110 
   111 #endif /* HAVE_OPENGL */
   112 	gl_active = 0;
   113 }
   114 
   115 int SDL_AtariGL_LoadLibrary(_THIS, const char *path)
   116 {
   117 #ifdef HAVE_OPENGL
   118 
   119 #ifdef ENABLE_OSMESA_SHARED
   120 	void *handle;
   121 
   122 	if (gl_active) {
   123 		SDL_SetError("OpenGL context already created");
   124 		return -1;
   125 	}
   126 
   127 	/* Unload previous driver */
   128 	SDL_AtariGL_UnloadLibrary(this);
   129 
   130 	/* Load library given by path */
   131 	handle = SDL_LoadObject(path);
   132 	if (handle == NULL) {
   133 		/* Try to load another one */
   134 		path = getenv("SDL_VIDEO_GL_DRIVER");
   135 		if ( path != NULL ) {
   136 			handle = SDL_LoadObject(path);
   137 		}
   138 
   139 		/* If it does not work, try some other */
   140 		if (handle == NULL) {
   141 			path = PATH_OSMESA_LDG;
   142 			handle = SDL_LoadObject(path);
   143 		}
   144 
   145 		if (handle == NULL) {
   146 			path = PATH_MESAGL_LDG;
   147 			handle = SDL_LoadObject(path);
   148 		}
   149 
   150 		if (handle == NULL) {
   151 			path = PATH_TINYGL_LDG;
   152 			handle = SDL_LoadObject(path);
   153 		}
   154 	}
   155 
   156 	if (handle == NULL) {
   157 		SDL_SetError("Could not load OpenGL library");
   158 		return -1;
   159 	}
   160 
   161 	/* Load functions pointers (osmesa.ldg) */
   162 	this->gl_data->OSMesaCreateContextExt = SDL_LoadFunction(handle, "OSMesaCreateContextExt");
   163 	this->gl_data->OSMesaDestroyContext = SDL_LoadFunction(handle, "OSMesaDestroyContext");
   164 	this->gl_data->OSMesaMakeCurrent = SDL_LoadFunction(handle, "OSMesaMakeCurrent");
   165 	this->gl_data->OSMesaPixelStore = SDL_LoadFunction(handle, "OSMesaPixelStore");
   166 	this->gl_data->OSMesaGetProcAddress = SDL_LoadFunction(handle, "OSMesaGetProcAddress");
   167 	this->gl_data->glGetIntegerv = SDL_LoadFunction(handle, "glGetIntegerv");
   168 
   169 	/* Load old functions pointers (mesa_gl.ldg, tiny_gl.ldg) */
   170 	this->gl_data->OSMesaCreateLDG = SDL_LoadFunction(handle, "OSMesaCreateLDG");
   171 	this->gl_data->OSMesaDestroyLDG = SDL_LoadFunction(handle, "OSMesaDestroyLDG");
   172 
   173 	gl_oldmesa = 0;
   174 
   175 	if ( (this->gl_data->OSMesaCreateContextExt == NULL) || 
   176 	     (this->gl_data->OSMesaDestroyContext == NULL) ||
   177 	     (this->gl_data->OSMesaMakeCurrent == NULL) ||
   178 	     (this->gl_data->OSMesaPixelStore == NULL) ||
   179 	     (this->gl_data->glGetIntegerv == NULL) ||
   180 	     (this->gl_data->OSMesaGetProcAddress == NULL)) {
   181 		/* Hum, maybe old library ? */
   182 		if ( (this->gl_data->OSMesaCreateLDG == NULL) || 
   183 		     (this->gl_data->OSMesaDestroyLDG == NULL)) {
   184 			SDL_SetError("Could not retrieve OpenGL functions");
   185 			return -1;
   186 		} else {
   187 			gl_oldmesa = 1;
   188 		}
   189 	}
   190 
   191 	this->gl_config.dll_handle = handle;
   192 	if ( path ) {
   193 		strncpy(this->gl_config.driver_path, path,
   194 			sizeof(this->gl_config.driver_path)-1);
   195 	} else {
   196 		strcpy(this->gl_config.driver_path, "");
   197 	}
   198 
   199 #endif
   200 	this->gl_config.driver_loaded = 1;
   201 
   202 	return 0;
   203 #else
   204 	return -1;
   205 #endif
   206 }
   207 
   208 void *SDL_AtariGL_GetProcAddress(_THIS, const char *proc)
   209 {
   210 	void *func = NULL;
   211 #ifdef HAVE_OPENGL
   212 
   213 	if (this->gl_config.dll_handle) {
   214 		func = SDL_LoadFunction(this->gl_config.dll_handle, (void *)proc);
   215 	} else if (this->gl_data->OSMesaGetProcAddress) {
   216 		func = this->gl_data->OSMesaGetProcAddress(proc);
   217 	}
   218 
   219 #endif
   220 	return func;
   221 }
   222 
   223 int SDL_AtariGL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
   224 {
   225 #ifdef HAVE_OPENGL
   226 	GLenum mesa_attrib;
   227 	SDL_Surface *surface;
   228 
   229 	if (this->gl_config.dll_handle) {
   230 		if (this->gl_data->glGetIntegerv == NULL) {
   231 			return -1;
   232 		}
   233 	}
   234 
   235 	if (!gl_active) {
   236 		return -1;
   237 	}
   238 
   239 	switch(attrib) {
   240 		case SDL_GL_RED_SIZE:
   241 			mesa_attrib = GL_RED_BITS;
   242 			break;
   243 		case SDL_GL_GREEN_SIZE:
   244 			mesa_attrib = GL_GREEN_BITS;
   245 			break;
   246 		case SDL_GL_BLUE_SIZE:
   247 			mesa_attrib = GL_BLUE_BITS;
   248 			break;
   249 		case SDL_GL_ALPHA_SIZE:
   250 			mesa_attrib = GL_ALPHA_BITS;
   251 			break;
   252 		case SDL_GL_DOUBLEBUFFER:
   253 			surface = this->screen;
   254 			*value = ((surface->flags & SDL_DOUBLEBUF)==SDL_DOUBLEBUF);
   255 			return 0;
   256 		case SDL_GL_DEPTH_SIZE:
   257 			mesa_attrib = GL_DEPTH_BITS;
   258 			break;
   259 		case SDL_GL_STENCIL_SIZE:
   260 			mesa_attrib = GL_STENCIL_BITS;
   261 			break;
   262 		case SDL_GL_ACCUM_RED_SIZE:
   263 			mesa_attrib = GL_ACCUM_RED_BITS;
   264 			break;
   265 		case SDL_GL_ACCUM_GREEN_SIZE:
   266 			mesa_attrib = GL_ACCUM_GREEN_BITS;
   267 			break;
   268 		case SDL_GL_ACCUM_BLUE_SIZE:
   269 			mesa_attrib = GL_ACCUM_BLUE_BITS;
   270 			break;
   271 		case SDL_GL_ACCUM_ALPHA_SIZE:
   272 			mesa_attrib = GL_ACCUM_ALPHA_BITS;
   273 			break;
   274 		default :
   275 			return -1;
   276 	}
   277 
   278 	this->gl_data->glGetIntegerv(mesa_attrib, value);
   279 	return 0;
   280 #else
   281 	return -1;
   282 #endif
   283 }
   284 
   285 int SDL_AtariGL_MakeCurrent(_THIS)
   286 {
   287 #ifdef HAVE_OPENGL
   288 	SDL_Surface *surface;
   289 	GLenum type;
   290 
   291 	if (gl_oldmesa && gl_active) {
   292 		return 0;
   293 	}
   294 
   295 	if (this->gl_config.dll_handle) {
   296 		if ((this->gl_data->OSMesaMakeCurrent == NULL) ||
   297 			(this->gl_data->OSMesaPixelStore == NULL)) {
   298 			return -1;
   299 		}
   300 	}
   301 
   302 	if (!gl_active) {
   303 		SDL_SetError("Invalid OpenGL context");
   304 		return -1;
   305 	}
   306 
   307 	surface = this->screen;
   308 	
   309 	if ((surface->format->BitsPerPixel == 15) || (surface->format->BitsPerPixel == 16)) {
   310 		type = GL_UNSIGNED_SHORT_5_6_5;
   311 	} else {
   312 		type = GL_UNSIGNED_BYTE;
   313 	}
   314 
   315 	if (!(this->gl_data->OSMesaMakeCurrent(gl_ctx, surface->pixels, type, surface->w, surface->h))) {
   316 		SDL_SetError("Can not make OpenGL context current");
   317 		return -1;
   318 	}
   319 
   320 	/* OSMesa draws upside down */
   321 	this->gl_data->OSMesaPixelStore(OSMESA_Y_UP, 0);
   322 
   323 	return 0;
   324 #else
   325 	return -1;
   326 #endif
   327 }
   328 
   329 void SDL_AtariGL_SwapBuffers(_THIS)
   330 {
   331 #ifdef HAVE_OPENGL
   332 	if (gl_active) {
   333 		gl_copyshadow(this, this->screen);
   334 		gl_convert(this, this->screen);
   335 	}
   336 #endif
   337 }
   338 
   339 void SDL_AtariGL_InitPointers(_THIS)
   340 {
   341 #if defined(HAVE_OPENGL)
   342 	this->gl_data->OSMesaCreateContextExt = OSMesaCreateContextExt;
   343 	this->gl_data->OSMesaDestroyContext = OSMesaDestroyContext;
   344 	this->gl_data->OSMesaMakeCurrent = OSMesaMakeCurrent;
   345 	this->gl_data->OSMesaPixelStore = OSMesaPixelStore;
   346 	this->gl_data->OSMesaGetProcAddress = OSMesaGetProcAddress;
   347 	this->gl_data->glGetIntegerv = glGetIntegerv;
   348 #endif
   349 }
   350 
   351 /*--- Private functions ---*/
   352 
   353 static void SDL_AtariGL_UnloadLibrary(_THIS)
   354 {
   355 #if defined(HAVE_OPENGL)
   356 	if (this->gl_config.dll_handle) {
   357 		SDL_UnloadObject(this->gl_config.dll_handle);
   358 		this->gl_config.dll_handle = NULL;
   359 
   360 		/* Restore pointers to static library */
   361 		this->gl_data->OSMesaCreateContextExt = OSMesaCreateContextExt;
   362 		this->gl_data->OSMesaDestroyContext = OSMesaDestroyContext;
   363 		this->gl_data->OSMesaMakeCurrent = OSMesaMakeCurrent;
   364 		this->gl_data->OSMesaPixelStore = OSMesaPixelStore;
   365 		this->gl_data->OSMesaGetProcAddress = OSMesaGetProcAddress;
   366 		this->gl_data->glGetIntegerv = glGetIntegerv;
   367 
   368 		this->gl_data->OSMesaCreateLDG = NULL;
   369 		this->gl_data->OSMesaDestroyLDG = NULL;
   370 	}
   371 #endif
   372 }
   373 
   374 /*--- Creation of an OpenGL context using new/old functions ---*/
   375 
   376 static int InitNew(_THIS, SDL_Surface *current)
   377 {
   378 	GLenum osmesa_format;
   379 	SDL_PixelFormat *pixel_format;
   380 	Uint32	redmask;
   381 
   382 	if (this->gl_config.dll_handle) {
   383 		if (this->gl_data->OSMesaCreateContextExt == NULL) {
   384 			return 0;
   385 		}
   386 	}
   387 
   388 	/* Init OpenGL context using OSMesa */
   389 	gl_convert = ConvertNull;
   390 	gl_copyshadow = CopyShadowNull;
   391 
   392 	pixel_format = current->format;
   393 	redmask = pixel_format->Rmask;
   394 	switch (pixel_format->BitsPerPixel) {
   395 		case 15:
   396 			/* 1555, big and little endian, unsupported */
   397 			gl_pixelsize = 2;
   398 			osmesa_format = OSMESA_RGB_565;
   399 			if (redmask == 31<<10) {
   400 				gl_convert = Convert565To555be;
   401 			} else {
   402 				gl_convert = Convert565To555le;
   403 			}
   404 			break;
   405 		case 16:
   406 			gl_pixelsize = 2;
   407 			if (redmask == 31<<11) {
   408 				osmesa_format = OSMESA_RGB_565;
   409 			} else {
   410 				/* 565, little endian, unsupported */
   411 				osmesa_format = OSMESA_RGB_565;
   412 				gl_convert = Convert565le;
   413 			}
   414 			break;
   415 		case 24:
   416 			gl_pixelsize = 3;
   417 			if (redmask == 255<<16) {
   418 				osmesa_format = OSMESA_RGB;
   419 			} else {
   420 				osmesa_format = OSMESA_BGR;
   421 			}
   422 			break;
   423 		case 32:
   424 			gl_pixelsize = 4;
   425 			if (redmask == 255<<16) {
   426 				osmesa_format = OSMESA_ARGB;
   427 			} else if (redmask == 255<<8) {
   428 				osmesa_format = OSMESA_BGRA;
   429 			} else if (redmask == 255<<24) {
   430 				osmesa_format = OSMESA_RGBA;
   431 			} else {
   432 				/* ABGR format unsupported */
   433 				osmesa_format = OSMESA_BGRA;
   434 				gl_convert = ConvertBGRAToABGR;
   435 			}
   436 			break;
   437 		default:
   438 			gl_pixelsize = 1;
   439 			osmesa_format = OSMESA_COLOR_INDEX;
   440 			break;
   441 	}
   442 
   443 	gl_ctx = this->gl_data->OSMesaCreateContextExt(
   444 		osmesa_format, this->gl_config.depth_size,
   445 		this->gl_config.stencil_size, this->gl_config.accum_red_size +
   446 		this->gl_config.accum_green_size + this->gl_config.accum_blue_size +
   447 		this->gl_config.accum_alpha_size, NULL );
   448 
   449 	return (gl_ctx != NULL);
   450 }
   451 
   452 static int InitOld(_THIS, SDL_Surface *current)
   453 {
   454 	GLenum osmesa_format;
   455 	SDL_PixelFormat *pixel_format;
   456 	Uint32	redmask;
   457 
   458 	if (this->gl_config.dll_handle) {
   459 		if (this->gl_data->OSMesaCreateLDG == NULL) {
   460 			return 0;
   461 		}
   462 	}
   463 
   464 	/* Init OpenGL context using OSMesa */
   465 	gl_convert = ConvertNull;
   466 	gl_copyshadow = CopyShadowNull;
   467 
   468 	pixel_format = current->format;
   469 	redmask = pixel_format->Rmask;
   470 	switch (pixel_format->BitsPerPixel) {
   471 		case 15:
   472 			/* 15 bits unsupported */
   473 			gl_pixelsize = 2;
   474 			osmesa_format = OSMESA_ARGB;
   475 			if (redmask == 31<<10) {
   476 				gl_copyshadow = CopyShadow8888To555;
   477 			} else {
   478 				gl_copyshadow = CopyShadow8888To565;
   479 				gl_convert = Convert565To555le;
   480 			}
   481 			break;
   482 		case 16:
   483 			/* 16 bits unsupported */
   484 			gl_pixelsize = 2;
   485 			osmesa_format = OSMESA_ARGB;
   486 			gl_copyshadow = CopyShadow8888To565;
   487 			if (redmask != 31<<11) {
   488 				/* 565, little endian, unsupported */
   489 				gl_convert = Convert565le;
   490 			}
   491 			break;
   492 		case 24:
   493 			gl_pixelsize = 3;
   494 			gl_copyshadow = CopyShadowDirect;
   495 			if (redmask == 255<<16) {
   496 				osmesa_format = OSMESA_RGB;
   497 			} else {
   498 				osmesa_format = OSMESA_BGR;
   499 			}
   500 			break;
   501 		case 32:
   502 			gl_pixelsize = 4;
   503 			gl_copyshadow = CopyShadowDirect;
   504 			if (redmask == 255<<16) {
   505 				osmesa_format = OSMESA_ARGB;
   506 			} else if (redmask == 255<<8) {
   507 				osmesa_format = OSMESA_BGRA;
   508 			} else if (redmask == 255<<24) {
   509 				osmesa_format = OSMESA_RGBA;
   510 			} else {
   511 				/* ABGR format unsupported */
   512 				osmesa_format = OSMESA_BGRA;
   513 				gl_convert = ConvertBGRAToABGR;
   514 			}
   515 			break;
   516 		default:
   517 			gl_pixelsize = 1;
   518 			gl_copyshadow = CopyShadowDirect;
   519 			osmesa_format = OSMESA_COLOR_INDEX;
   520 			break;
   521 	}
   522 
   523 	gl_shadow = this->gl_data->OSMesaCreateLDG(
   524 		osmesa_format, GL_UNSIGNED_BYTE, current->w, current->h
   525 	);
   526 
   527 	return (gl_shadow != NULL);
   528 }
   529 
   530 /*--- Conversions routines from shadow buffer to the screen ---*/
   531 
   532 static void CopyShadowNull(_THIS, SDL_Surface *surface)
   533 {
   534 }
   535 
   536 static void CopyShadowDirect(_THIS, SDL_Surface *surface)
   537 {
   538 	int y, srcpitch, dstpitch;
   539 	Uint8 *srcline, *dstline;
   540 
   541 	srcline = gl_shadow;
   542 	srcpitch = surface->w * gl_pixelsize;
   543 	dstline = surface->pixels;
   544 	dstpitch = surface->pitch;
   545 
   546 	for (y=0; y<surface->h; y++) {
   547 		memcpy(dstline, srcline, srcpitch);
   548 
   549 		srcline += srcpitch;
   550 		dstline += dstpitch;
   551 	}
   552 }
   553 
   554 static void CopyShadow8888To555(_THIS, SDL_Surface *surface)
   555 {
   556 	int x,y, srcpitch, dstpitch;
   557 	Uint16 *dstline, *dstcol;
   558 	Uint32 *srcline, *srccol;
   559 
   560 	srcline = (Uint32 *)gl_shadow;
   561 	srcpitch = surface->w;
   562 	dstline = surface->pixels;
   563 	dstpitch = surface->pitch >>1;
   564 
   565 	for (y=0; y<surface->h; y++) {
   566 		srccol = srcline;
   567 		dstcol = dstline;
   568 		for (x=0; x<surface->w; x++) {
   569 			Uint32 srccolor;
   570 			Uint16 dstcolor;
   571 			
   572 			srccolor = *srccol++;
   573 			dstcolor = (srccolor>>9) & (31<<10);
   574 			dstcolor |= (srccolor>>6) & (31<<5);
   575 			dstcolor |= (srccolor>>3) & 31;
   576 			*dstcol++ = dstcolor;
   577 		}
   578 
   579 		srcline += srcpitch;
   580 		dstline += dstpitch;
   581 	}
   582 }
   583 
   584 static void CopyShadow8888To565(_THIS, SDL_Surface *surface)
   585 {
   586 	int x,y, srcpitch, dstpitch;
   587 	Uint16 *dstline, *dstcol;
   588 	Uint32 *srcline, *srccol;
   589 
   590 	srcline = (Uint32 *)gl_shadow;
   591 	srcpitch = surface->w;
   592 	dstline = surface->pixels;
   593 	dstpitch = surface->pitch >>1;
   594 
   595 	for (y=0; y<surface->h; y++) {
   596 		srccol = srcline;
   597 		dstcol = dstline;
   598 
   599 		for (x=0; x<surface->w; x++) {
   600 			Uint32 srccolor;
   601 			Uint16 dstcolor;
   602 			
   603 			srccolor = *srccol++;
   604 			dstcolor = (srccolor>>8) & (31<<11);
   605 			dstcolor |= (srccolor>>5) & (63<<5);
   606 			dstcolor |= (srccolor>>3) & 31;
   607 			*dstcol++ = dstcolor;
   608 		}
   609 
   610 		srcline += srcpitch;
   611 		dstline += dstpitch;
   612 	}
   613 }
   614 
   615 /*--- Conversions routines in the screen ---*/
   616 
   617 static void ConvertNull(_THIS, SDL_Surface *surface)
   618 {
   619 }
   620 
   621 static void Convert565To555be(_THIS, SDL_Surface *surface)
   622 {
   623 	int x,y, pitch;
   624 	unsigned short *line, *pixel;
   625 
   626 	line = surface->pixels;
   627 	pitch = surface->pitch >> 1;
   628 	for (y=0; y<surface->h; y++) {
   629 		pixel = line;
   630 		for (x=0; x<surface->w; x++) {
   631 			unsigned short color = *pixel;
   632 
   633 			*pixel++ = (color & 0x1f)|((color>>1) & 0xffe0);
   634 		}
   635 
   636 		line += pitch;
   637 	}
   638 }
   639 
   640 static void Convert565To555le(_THIS, SDL_Surface *surface)
   641 {
   642 	int x,y, pitch;
   643 	unsigned short *line, *pixel;
   644 
   645 	line = surface->pixels;
   646 	pitch = surface->pitch >>1;
   647 	for (y=0; y<surface->h; y++) {
   648 		pixel = line;
   649 		for (x=0; x<surface->w; x++) {
   650 			unsigned short color = *pixel;
   651 
   652 			color = (color & 0x1f)|((color>>1) & 0xffe0);
   653 			*pixel++ = SDL_Swap16(color);
   654 		}
   655 
   656 		line += pitch;
   657 	}
   658 }
   659 
   660 static void Convert565le(_THIS, SDL_Surface *surface)
   661 {
   662 	int x,y, pitch;
   663 	unsigned short *line, *pixel;
   664 
   665 	line = surface->pixels;
   666 	pitch = surface->pitch >>1;
   667 	for (y=0; y<surface->h; y++) {
   668 		pixel = line;
   669 		for (x=0; x<surface->w; x++) {
   670 			unsigned short color = *pixel;
   671 
   672 			*pixel++ = SDL_Swap16(color);
   673 		}
   674 
   675 		line += pitch;
   676 	}
   677 }
   678 
   679 static void ConvertBGRAToABGR(_THIS, SDL_Surface *surface)
   680 {
   681 	int x,y, pitch;
   682 	unsigned long *line, *pixel;
   683 
   684 	line = surface->pixels;
   685 	pitch = surface->pitch >>2;
   686 	for (y=0; y<surface->h; y++) {
   687 		pixel = line;
   688 		for (x=0; x<surface->w; x++) {
   689 			unsigned long color = *pixel;
   690 
   691 			*pixel++ = (color<<24)|(color>>8);
   692 		}
   693 
   694 		line += pitch;
   695 	}
   696 }