showimage.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 08 Dec 2008 00:27:32 +0000
changeset 186 5fbbeabf647f
parent 145 6d65a8d8b1d1
child 187 3405ab551300
permissions -rw-r--r--
Updated copyright date
     1 /*
     2     showimage:  A test application for the SDL image loading library.
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 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     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #include <stdlib.h>
    24 #include <stdio.h>
    25 #include <string.h>
    26 
    27 #include "SDL.h"
    28 #include "SDL_image.h"
    29 
    30 /* #define XPM_INCLUDED and supply picture.xpm to test the XPM inclusion
    31    feature */
    32 
    33 #ifdef XPM_INCLUDED
    34 #include "picture.xpm"
    35 #endif
    36 
    37 /* Draw a Gimpish background pattern to show transparency in the image */
    38 void draw_background(SDL_Surface *screen)
    39 {
    40     Uint8 *dst = screen->pixels;
    41     int x, y;
    42     int bpp = screen->format->BytesPerPixel;
    43     Uint32 col[2];
    44     col[0] = SDL_MapRGB(screen->format, 0x66, 0x66, 0x66);
    45     col[1] = SDL_MapRGB(screen->format, 0x99, 0x99, 0x99);
    46     for(y = 0; y < screen->h; y++) {
    47 	for(x = 0; x < screen->w; x++) {
    48 	    /* use an 8x8 checkerboard pattern */
    49 	    Uint32 c = col[((x ^ y) >> 3) & 1];
    50 	    switch(bpp) {
    51 	    case 1:
    52 		dst[x] = (Uint8)c;
    53 		break;
    54 	    case 2:
    55 		((Uint16 *)dst)[x] = (Uint16)c;
    56 		break;
    57 	    case 3:
    58 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
    59 		dst[x * 3]     = (Uint8)(c);
    60 		dst[x * 3 + 1] = (Uint8)(c >> 8);
    61 		dst[x * 3 + 2] = (Uint8)(c >> 16);
    62 #else
    63 		dst[x * 3]     = (Uint8)(c >> 16);
    64 		dst[x * 3 + 1] = (Uint8)(c >> 8);
    65 		dst[x * 3 + 2] = (Uint8)(c);
    66 #endif
    67 		break;
    68 	    case 4:
    69 		((Uint32 *)dst)[x] = c;
    70 		break;
    71 	    }
    72 	}
    73 	dst += screen->pitch;
    74     }
    75 }
    76 
    77 int main(int argc, char *argv[])
    78 {
    79 	Uint32 flags;
    80 	SDL_Surface *screen, *image;
    81 	int i, depth, done;
    82 	SDL_Event event;
    83 
    84 	/* Check command line usage */
    85 	if ( ! argv[1] ) {
    86 		fprintf(stderr, "Usage: %s <image_file>\n", argv[0]);
    87 		return(1);
    88 	}
    89 
    90 	/* Initialize the SDL library */
    91 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    92 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    93 		return(255);
    94 	}
    95 
    96 	flags = SDL_SWSURFACE;
    97 	for ( i=1; argv[i]; ++i ) {
    98 		if ( strcmp(argv[i], "-fullscreen") == 0 ) {
    99 			SDL_ShowCursor(0);
   100 			flags |= SDL_FULLSCREEN;
   101 			continue;
   102 		}
   103 		/* Open the image file */
   104 #ifdef XPM_INCLUDED
   105 		image = IMG_ReadXPMFromArray(picture_xpm);
   106 #else
   107 		image = IMG_Load(argv[i]);
   108 #endif
   109 		if ( image == NULL ) {
   110 			fprintf(stderr, "Couldn't load %s: %s\n",
   111 			        argv[i], SDL_GetError());
   112 			continue;
   113 		}
   114 		SDL_WM_SetCaption(argv[i], "showimage");
   115 
   116 		/* Create a display for the image */
   117 		depth = SDL_VideoModeOK(image->w, image->h, 32, flags);
   118 		/* Use the deepest native mode, except that we emulate 32bpp
   119 		   for viewing non-indexed images on 8bpp screens */
   120 		if ( depth == 0 ) {
   121 			if ( image->format->BytesPerPixel > 1 ) {
   122 				depth = 32;
   123 			} else {
   124 				depth = 8;
   125 			}
   126 		} else
   127 		if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
   128 	    		depth = 32;
   129 		}
   130 		if(depth == 8)
   131 			flags |= SDL_HWPALETTE;
   132 		screen = SDL_SetVideoMode(image->w, image->h, depth, flags);
   133 		if ( screen == NULL ) {
   134 			fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
   135 				image->w, image->h, depth, SDL_GetError());
   136 			continue;
   137 		}
   138 
   139 		/* Set the palette, if one exists */
   140 		if ( image->format->palette ) {
   141 			SDL_SetColors(screen, image->format->palette->colors,
   142 			              0, image->format->palette->ncolors);
   143 		}
   144 
   145 		/* Draw a background pattern if the surface has transparency */
   146 		if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
   147 	    		draw_background(screen);
   148 
   149 		/* Display the image */
   150 		SDL_BlitSurface(image, NULL, screen, NULL);
   151 		SDL_UpdateRect(screen, 0, 0, 0, 0);
   152 
   153 		done = 0;
   154 		while ( ! done ) {
   155 			if ( SDL_PollEvent(&event) ) {
   156 				switch (event.type) {
   157 				    case SDL_KEYUP:
   158 					switch (event.key.keysym.sym) {
   159 					    case SDLK_LEFT:
   160 						if ( i > 1 ) {
   161 							i -= 2;
   162 							done = 1;
   163 						}
   164 						break;
   165 					    case SDLK_RIGHT:
   166 						if ( argv[i+1] ) {
   167 							done = 1;
   168 						}
   169 						break;
   170 					    case SDLK_ESCAPE:
   171 					    case SDLK_q:
   172 						argv[i+1] = NULL;
   173 						/* Drop through to done */
   174 					    case SDLK_SPACE:
   175 					    case SDLK_TAB:
   176 						done = 1;
   177 						break;
   178 					    default:
   179 						break;
   180 					}
   181 					break;
   182 				    case SDL_MOUSEBUTTONDOWN:
   183 					done = 1;
   184 					break;
   185                                     case SDL_QUIT:
   186 					argv[i+1] = NULL;
   187 					done = 1;
   188 					break;
   189 				    default:
   190 					break;
   191 				}
   192 			} else {
   193 				SDL_Delay(10);
   194 			}
   195 		}
   196 		SDL_FreeSurface(image);
   197 	}
   198 
   199 	/* We're done! */
   200 	SDL_Quit();
   201 	return(0);
   202 }