showimage.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 09:41:39 -0500
changeset 280 ec4ae96c100c
parent 208 a4d827c0b44d
child 320 aa5d55b11751
permissions -rw-r--r--
Happy New Year!
     1 /*
     2   showimage:  A test application for the SDL image loading library.
     3   Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 
    22 #include <stdlib.h>
    23 #include <stdio.h>
    24 #include <string.h>
    25 
    26 #include "SDL.h"
    27 #include "SDL_image.h"
    28 
    29 /* #define XPM_INCLUDED and supply picture.xpm to test the XPM inclusion
    30    feature */
    31 
    32 #ifdef XPM_INCLUDED
    33 #include "picture.xpm"
    34 #endif
    35 
    36 /* Draw a Gimpish background pattern to show transparency in the image */
    37 static void draw_background(SDL_Surface *screen)
    38 {
    39     Uint8 *dst = screen->pixels;
    40     int x, y;
    41     int bpp = screen->format->BytesPerPixel;
    42     Uint32 col[2];
    43     col[0] = SDL_MapRGB(screen->format, 0x66, 0x66, 0x66);
    44     col[1] = SDL_MapRGB(screen->format, 0x99, 0x99, 0x99);
    45     for(y = 0; y < screen->h; y++) {
    46 	for(x = 0; x < screen->w; x++) {
    47 	    /* use an 8x8 checkerboard pattern */
    48 	    Uint32 c = col[((x ^ y) >> 3) & 1];
    49 	    switch(bpp) {
    50 	    case 1:
    51 		dst[x] = (Uint8)c;
    52 		break;
    53 	    case 2:
    54 		((Uint16 *)dst)[x] = (Uint16)c;
    55 		break;
    56 	    case 3:
    57 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
    58 		dst[x * 3]     = (Uint8)(c);
    59 		dst[x * 3 + 1] = (Uint8)(c >> 8);
    60 		dst[x * 3 + 2] = (Uint8)(c >> 16);
    61 #else
    62 		dst[x * 3]     = (Uint8)(c >> 16);
    63 		dst[x * 3 + 1] = (Uint8)(c >> 8);
    64 		dst[x * 3 + 2] = (Uint8)(c);
    65 #endif
    66 		break;
    67 	    case 4:
    68 		((Uint32 *)dst)[x] = c;
    69 		break;
    70 	    }
    71 	}
    72 	dst += screen->pitch;
    73     }
    74 }
    75 
    76 int main(int argc, char *argv[])
    77 {
    78 	Uint32 flags;
    79 	SDL_Surface *screen, *image;
    80 	int i, depth, done;
    81 	SDL_Event event;
    82 #if 0
    83 	SDL_RWops* rw_ops;
    84 #endif
    85 
    86 	/* Check command line usage */
    87 	if ( ! argv[1] ) {
    88 		fprintf(stderr, "Usage: %s <image_file>\n", argv[0]);
    89 		return(1);
    90 	}
    91 
    92 	/* Initialize the SDL library */
    93 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    94 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    95 		return(255);
    96 	}
    97 
    98 	flags = SDL_SWSURFACE;
    99 	for ( i=1; argv[i]; ++i ) {
   100 		if ( strcmp(argv[i], "-fullscreen") == 0 ) {
   101 			SDL_ShowCursor(0);
   102 			flags |= SDL_FULLSCREEN;
   103 			continue;
   104 		}
   105 #if 0
   106 		rw_ops = SDL_RWFromFile(argv[1], "r");
   107 		
   108 		fprintf(stderr, "BMP:\t%d\n", IMG_isBMP(rw_ops));
   109 		fprintf(stderr, "GIF:\t%d\n", IMG_isGIF(rw_ops));
   110 		fprintf(stderr, "JPG:\t%d\n", IMG_isJPG(rw_ops));
   111 		fprintf(stderr, "PNG:\t%d\n", IMG_isPNG(rw_ops));
   112 		fprintf(stderr, "TIF:\t%d\n", IMG_isTIF(rw_ops));
   113 		/* fprintf(stderr, "TGA:\t%d\n", IMG_isTGA(rw_ops)); */
   114 		fprintf(stderr, "PCX:\t%d\n", IMG_isPCX(rw_ops));
   115 #endif
   116 
   117 		/* Open the image file */
   118 #ifdef XPM_INCLUDED
   119 		image = IMG_ReadXPMFromArray(picture_xpm);
   120 #else
   121 		image = IMG_Load(argv[i]);
   122 #endif
   123 		if ( image == NULL ) {
   124 			fprintf(stderr, "Couldn't load %s: %s\n",
   125 			        argv[i], SDL_GetError());
   126 			continue;
   127 		}
   128 		SDL_WM_SetCaption(argv[i], "showimage");
   129 
   130 		/* Create a display for the image */
   131 		depth = SDL_VideoModeOK(image->w, image->h, 32, flags);
   132 		/* Use the deepest native mode, except that we emulate 32bpp
   133 		   for viewing non-indexed images on 8bpp screens */
   134 		if ( depth == 0 ) {
   135 			if ( image->format->BytesPerPixel > 1 ) {
   136 				depth = 32;
   137 			} else {
   138 				depth = 8;
   139 			}
   140 		} else
   141 		if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
   142 	    		depth = 32;
   143 		}
   144 		if(depth == 8)
   145 			flags |= SDL_HWPALETTE;
   146 		screen = SDL_SetVideoMode(image->w, image->h, depth, flags);
   147 		if ( screen == NULL ) {
   148 			fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
   149 				image->w, image->h, depth, SDL_GetError());
   150 			continue;
   151 		}
   152 
   153 		/* Set the palette, if one exists */
   154 		if ( image->format->palette ) {
   155 			SDL_SetColors(screen, image->format->palette->colors,
   156 			              0, image->format->palette->ncolors);
   157 		}
   158 
   159 		/* Draw a background pattern if the surface has transparency */
   160 		if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
   161 	    		draw_background(screen);
   162 
   163 		/* Display the image */
   164 		SDL_BlitSurface(image, NULL, screen, NULL);
   165 		SDL_UpdateRect(screen, 0, 0, 0, 0);
   166 
   167 		done = 0;
   168 		while ( ! done ) {
   169 			if ( SDL_PollEvent(&event) ) {
   170 				switch (event.type) {
   171 				    case SDL_KEYUP:
   172 					switch (event.key.keysym.sym) {
   173 					    case SDLK_LEFT:
   174 						if ( i > 1 ) {
   175 							i -= 2;
   176 							done = 1;
   177 						}
   178 						break;
   179 					    case SDLK_RIGHT:
   180 						if ( argv[i+1] ) {
   181 							done = 1;
   182 						}
   183 						break;
   184 					    case SDLK_ESCAPE:
   185 					    case SDLK_q:
   186 						argv[i+1] = NULL;
   187 						/* Drop through to done */
   188 					    case SDLK_SPACE:
   189 					    case SDLK_TAB:
   190 						done = 1;
   191 						break;
   192 					    default:
   193 						break;
   194 					}
   195 					break;
   196 				    case SDL_MOUSEBUTTONDOWN:
   197 					done = 1;
   198 					break;
   199                                     case SDL_QUIT:
   200 					argv[i+1] = NULL;
   201 					done = 1;
   202 					break;
   203 				    default:
   204 					break;
   205 				}
   206 			} else {
   207 				SDL_Delay(10);
   208 			}
   209 		}
   210 		SDL_FreeSurface(image);
   211 	}
   212 
   213 	/* We're done! */
   214 	SDL_Quit();
   215 	return(0);
   216 }