showimage.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 30 Sep 2009 20:45:01 +0000
changeset 208 a4d827c0b44d
parent 204 dcb38b0e78a6
child 280 ec4ae96c100c
permissions -rw-r--r--
Fixed VC2005 compiler warnings
     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 static 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 #if 0
    84 	SDL_RWops* rw_ops;
    85 #endif
    86 
    87 	/* Check command line usage */
    88 	if ( ! argv[1] ) {
    89 		fprintf(stderr, "Usage: %s <image_file>\n", argv[0]);
    90 		return(1);
    91 	}
    92 
    93 	/* Initialize the SDL library */
    94 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    95 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    96 		return(255);
    97 	}
    98 
    99 	flags = SDL_SWSURFACE;
   100 	for ( i=1; argv[i]; ++i ) {
   101 		if ( strcmp(argv[i], "-fullscreen") == 0 ) {
   102 			SDL_ShowCursor(0);
   103 			flags |= SDL_FULLSCREEN;
   104 			continue;
   105 		}
   106 #if 0
   107 		rw_ops = SDL_RWFromFile(argv[1], "r");
   108 		
   109 		fprintf(stderr, "BMP:\t%d\n", IMG_isBMP(rw_ops));
   110 		fprintf(stderr, "GIF:\t%d\n", IMG_isGIF(rw_ops));
   111 		fprintf(stderr, "JPG:\t%d\n", IMG_isJPG(rw_ops));
   112 		fprintf(stderr, "PNG:\t%d\n", IMG_isPNG(rw_ops));
   113 		fprintf(stderr, "TIF:\t%d\n", IMG_isTIF(rw_ops));
   114 		/* fprintf(stderr, "TGA:\t%d\n", IMG_isTGA(rw_ops)); */
   115 		fprintf(stderr, "PCX:\t%d\n", IMG_isPCX(rw_ops));
   116 #endif
   117 
   118 		/* Open the image file */
   119 #ifdef XPM_INCLUDED
   120 		image = IMG_ReadXPMFromArray(picture_xpm);
   121 #else
   122 		image = IMG_Load(argv[i]);
   123 #endif
   124 		if ( image == NULL ) {
   125 			fprintf(stderr, "Couldn't load %s: %s\n",
   126 			        argv[i], SDL_GetError());
   127 			continue;
   128 		}
   129 		SDL_WM_SetCaption(argv[i], "showimage");
   130 
   131 		/* Create a display for the image */
   132 		depth = SDL_VideoModeOK(image->w, image->h, 32, flags);
   133 		/* Use the deepest native mode, except that we emulate 32bpp
   134 		   for viewing non-indexed images on 8bpp screens */
   135 		if ( depth == 0 ) {
   136 			if ( image->format->BytesPerPixel > 1 ) {
   137 				depth = 32;
   138 			} else {
   139 				depth = 8;
   140 			}
   141 		} else
   142 		if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
   143 	    		depth = 32;
   144 		}
   145 		if(depth == 8)
   146 			flags |= SDL_HWPALETTE;
   147 		screen = SDL_SetVideoMode(image->w, image->h, depth, flags);
   148 		if ( screen == NULL ) {
   149 			fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
   150 				image->w, image->h, depth, SDL_GetError());
   151 			continue;
   152 		}
   153 
   154 		/* Set the palette, if one exists */
   155 		if ( image->format->palette ) {
   156 			SDL_SetColors(screen, image->format->palette->colors,
   157 			              0, image->format->palette->ncolors);
   158 		}
   159 
   160 		/* Draw a background pattern if the surface has transparency */
   161 		if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
   162 	    		draw_background(screen);
   163 
   164 		/* Display the image */
   165 		SDL_BlitSurface(image, NULL, screen, NULL);
   166 		SDL_UpdateRect(screen, 0, 0, 0, 0);
   167 
   168 		done = 0;
   169 		while ( ! done ) {
   170 			if ( SDL_PollEvent(&event) ) {
   171 				switch (event.type) {
   172 				    case SDL_KEYUP:
   173 					switch (event.key.keysym.sym) {
   174 					    case SDLK_LEFT:
   175 						if ( i > 1 ) {
   176 							i -= 2;
   177 							done = 1;
   178 						}
   179 						break;
   180 					    case SDLK_RIGHT:
   181 						if ( argv[i+1] ) {
   182 							done = 1;
   183 						}
   184 						break;
   185 					    case SDLK_ESCAPE:
   186 					    case SDLK_q:
   187 						argv[i+1] = NULL;
   188 						/* Drop through to done */
   189 					    case SDLK_SPACE:
   190 					    case SDLK_TAB:
   191 						done = 1;
   192 						break;
   193 					    default:
   194 						break;
   195 					}
   196 					break;
   197 				    case SDL_MOUSEBUTTONDOWN:
   198 					done = 1;
   199 					break;
   200                                     case SDL_QUIT:
   201 					argv[i+1] = NULL;
   202 					done = 1;
   203 					break;
   204 				    default:
   205 					break;
   206 				}
   207 			} else {
   208 				SDL_Delay(10);
   209 			}
   210 		}
   211 		SDL_FreeSurface(image);
   212 	}
   213 
   214 	/* We're done! */
   215 	SDL_Quit();
   216 	return(0);
   217 }