showimage.c
changeset 0 76be7dab668c
child 4 b1bb33e907f8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/showimage.c	Thu Aug 10 06:05:39 2000 +0000
     1.3 @@ -0,0 +1,145 @@
     1.4 +/*
     1.5 +    SHOW:  A test application for the SDL image loading library.
     1.6 +    Copyright (C) 1999  Sam Lantinga
     1.7 +
     1.8 +    This program is free software; you can redistribute it and/or modify
     1.9 +    it under the terms of the GNU General Public License as published by
    1.10 +    the Free Software Foundation; either version 2 of the License, or
    1.11 +    (at your option) any later version.
    1.12 +
    1.13 +    This program is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 +    GNU General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU General Public License
    1.19 +    along with this program; if not, write to the Free Software
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    5635-34 Springhouse Dr.
    1.24 +    Pleasanton, CA 94588 (USA)
    1.25 +    slouken@devolution.com
    1.26 +*/
    1.27 +
    1.28 +#include <stdlib.h>
    1.29 +#include <stdio.h>
    1.30 +
    1.31 +#include "SDL.h"
    1.32 +#include "SDL_image.h"
    1.33 +
    1.34 +
    1.35 +/* Draw a Gimpish background pattern to show transparency in the image */
    1.36 +void draw_background(SDL_Surface *image, SDL_Surface *screen)
    1.37 +{
    1.38 +    Uint8 *dst = screen->pixels;
    1.39 +    int x, y;
    1.40 +    int bpp = screen->format->BytesPerPixel;
    1.41 +    Uint32 col[2];
    1.42 +    col[0] = SDL_MapRGB(screen->format, 0x66, 0x66, 0x66);
    1.43 +    col[1] = SDL_MapRGB(screen->format, 0x99, 0x99, 0x99);
    1.44 +    for(y = 0; y < screen->h; y++) {
    1.45 +	for(x = 0; x < screen->w; x++) {
    1.46 +	    /* use an 8x8 checkerboard pattern */
    1.47 +	    Uint32 c = col[((x ^ y) >> 3) & 1];
    1.48 +	    switch(bpp) {
    1.49 +	    case 1:
    1.50 +		dst[x] = c;
    1.51 +		break;
    1.52 +	    case 2:
    1.53 +		((Uint16 *)dst)[x] = c;
    1.54 +		break;
    1.55 +	    case 3:
    1.56 +		if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {
    1.57 +		    dst[x * 3] = c;
    1.58 +		    dst[x * 3 + 1] = c >> 8;
    1.59 +		    dst[x * 3 + 2] = c >> 16;
    1.60 +		} else {
    1.61 +		    dst[x * 3] = c >> 16;
    1.62 +		    dst[x * 3 + 1] = c >> 8;
    1.63 +		    dst[x * 3 + 2] = c;
    1.64 +		}
    1.65 +		break;
    1.66 +	    case 4:
    1.67 +		((Uint32 *)dst)[x] = c;
    1.68 +		break;
    1.69 +	    }
    1.70 +	}
    1.71 +	dst += screen->pitch;
    1.72 +    }
    1.73 +}
    1.74 +
    1.75 +main(int argc, char *argv[])
    1.76 +{
    1.77 +	SDL_Surface *screen, *image;
    1.78 +	int i, depth;
    1.79 +
    1.80 +	/* Check command line usage */
    1.81 +	if ( ! argv[1] ) {
    1.82 +		fprintf(stderr, "Usage: %s <image_file>\n", argv[0]);
    1.83 +		exit(1);
    1.84 +	}
    1.85 +
    1.86 +	/* Initialize the SDL library */
    1.87 +	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    1.88 +		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    1.89 +		exit(255);
    1.90 +	}
    1.91 +	atexit(SDL_Quit);
    1.92 +
    1.93 +	/* Open the image file */
    1.94 +	image = IMG_Load(argv[1]);
    1.95 +	if ( image == NULL ) {
    1.96 +		fprintf(stderr,"Couldn't load %s: %s\n",argv[1],SDL_GetError());
    1.97 +		exit(2);
    1.98 +	}
    1.99 +	SDL_WM_SetCaption(argv[1], "showimage");
   1.100 +
   1.101 +	/* Create a display for the image */
   1.102 +	depth = SDL_VideoModeOK(image->w, image->h, 32, SDL_SWSURFACE);
   1.103 +	/* Use the deepest native mode, except that we emulate 32bpp for
   1.104 +	   viewing non-indexed images on 8bpp screens */
   1.105 +	if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
   1.106 +	    depth = 32;
   1.107 +	}
   1.108 +	screen = SDL_SetVideoMode(image->w, image->h, depth, SDL_SWSURFACE);
   1.109 +	if ( screen == NULL ) {
   1.110 +		fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
   1.111 +				image->w, image->h, depth, SDL_GetError());
   1.112 +		exit(3);
   1.113 +	}
   1.114 +
   1.115 +	/* Set the palette, if one exists */
   1.116 +	if ( image->format->palette ) {
   1.117 +		SDL_SetColors(screen, image->format->palette->colors,
   1.118 +				0, image->format->palette->ncolors);
   1.119 +	}
   1.120 +
   1.121 +	/* Draw a background pattern if the surface has transparency */
   1.122 +	if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
   1.123 +	    draw_background(image, screen);
   1.124 +
   1.125 +	/* Display the image */
   1.126 +	SDL_BlitSurface(image, NULL, screen, NULL);
   1.127 +	SDL_UpdateRect(screen, 0, 0, 0, 0);
   1.128 +
   1.129 +	/* Wait for any keyboard or mouse input */
   1.130 +	for ( i=SDL_NOEVENT; i<SDL_NUMEVENTS; ++i ) {
   1.131 +		switch (i) {
   1.132 +		    case SDL_KEYDOWN:
   1.133 +		    case SDL_MOUSEBUTTONDOWN:
   1.134 +		    case SDL_QUIT:
   1.135 +			/* Valid event, keep it */
   1.136 +			break;
   1.137 +		    default:
   1.138 +			/* We don't want this event */
   1.139 +			SDL_EventState(i, SDL_IGNORE);
   1.140 +			break;
   1.141 +		}
   1.142 +	}
   1.143 +	SDL_WaitEvent(NULL);
   1.144 +
   1.145 +	/* We're done! */
   1.146 +	SDL_FreeSurface(image);
   1.147 +	exit(0);
   1.148 +}