showimage.c
author Sam Lantinga <slouken@lokigames.com>
Fri, 01 Sep 2000 00:45:57 +0000
changeset 4 b1bb33e907f8
parent 0 76be7dab668c
child 7 e1b6443ffb6b
permissions -rw-r--r--
Mattias Engdeg�rd - Wed Aug 9 20:32:22 MET DST 2000
* Removed the alpha flipping, made IMG_InvertAlpha() a noop
* Fixed nonexisting PCX alpha support
* Some TIFF bugfixes
* PNG greyscale images are loaded as 8bpp with a greyscale palette
     1 /*
     2     SHOW:  A test application for the SDL image loading library.
     3     Copyright (C) 1999  Sam Lantinga
     4 
     5     This program is free software; you can redistribute it and/or modify
     6     it under the terms of the GNU General Public License as published by
     7     the Free Software Foundation; either version 2 of the License, or
     8     (at your option) any later version.
     9 
    10     This program 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
    13     GNU General Public License for more details.
    14 
    15     You should have received a copy of the GNU General Public License
    16     along with this program; if not, write to the Free Software
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     5635-34 Springhouse Dr.
    21     Pleasanton, CA 94588 (USA)
    22     slouken@devolution.com
    23 */
    24 
    25 #include <stdlib.h>
    26 #include <stdio.h>
    27 
    28 #include "SDL.h"
    29 #include "SDL_image.h"
    30 
    31 
    32 /* Draw a Gimpish background pattern to show transparency in the image */
    33 void draw_background(SDL_Surface *screen)
    34 {
    35     Uint8 *dst = screen->pixels;
    36     int x, y;
    37     int bpp = screen->format->BytesPerPixel;
    38     Uint32 col[2];
    39     col[0] = SDL_MapRGB(screen->format, 0x66, 0x66, 0x66);
    40     col[1] = SDL_MapRGB(screen->format, 0x99, 0x99, 0x99);
    41     for(y = 0; y < screen->h; y++) {
    42 	for(x = 0; x < screen->w; x++) {
    43 	    /* use an 8x8 checkerboard pattern */
    44 	    Uint32 c = col[((x ^ y) >> 3) & 1];
    45 	    switch(bpp) {
    46 	    case 1:
    47 		dst[x] = c;
    48 		break;
    49 	    case 2:
    50 		((Uint16 *)dst)[x] = c;
    51 		break;
    52 	    case 3:
    53 		if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {
    54 		    dst[x * 3] = c;
    55 		    dst[x * 3 + 1] = c >> 8;
    56 		    dst[x * 3 + 2] = c >> 16;
    57 		} else {
    58 		    dst[x * 3] = c >> 16;
    59 		    dst[x * 3 + 1] = c >> 8;
    60 		    dst[x * 3 + 2] = c;
    61 		}
    62 		break;
    63 	    case 4:
    64 		((Uint32 *)dst)[x] = c;
    65 		break;
    66 	    }
    67 	}
    68 	dst += screen->pitch;
    69     }
    70 }
    71 
    72 int main(int argc, char *argv[])
    73 {
    74 	SDL_Surface *screen, *image;
    75 	int i, depth;
    76 
    77 	/* Check command line usage */
    78 	if ( ! argv[1] ) {
    79 		fprintf(stderr, "Usage: %s <image_file>\n", argv[0]);
    80 		exit(1);
    81 	}
    82 
    83 	/* Initialize the SDL library */
    84 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    85 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    86 		exit(255);
    87 	}
    88 	atexit(SDL_Quit);
    89 
    90 	/* Open the image file */
    91 	image = IMG_Load(argv[1]);
    92 	if ( image == NULL ) {
    93 		fprintf(stderr,"Couldn't load %s: %s\n",argv[1],SDL_GetError());
    94 		exit(2);
    95 	}
    96 	SDL_WM_SetCaption(argv[1], "showimage");
    97 
    98 	/* Create a display for the image */
    99 	depth = SDL_VideoModeOK(image->w, image->h, 32, SDL_SWSURFACE);
   100 	/* Use the deepest native mode, except that we emulate 32bpp for
   101 	   viewing non-indexed images on 8bpp screens */
   102 	if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
   103 	    depth = 32;
   104 	}
   105 	screen = SDL_SetVideoMode(image->w, image->h, depth, SDL_SWSURFACE);
   106 	if ( screen == NULL ) {
   107 		fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
   108 				image->w, image->h, depth, SDL_GetError());
   109 		exit(3);
   110 	}
   111 
   112 	/* Set the palette, if one exists */
   113 	if ( image->format->palette ) {
   114 		SDL_SetColors(screen, image->format->palette->colors,
   115 				0, image->format->palette->ncolors);
   116 	}
   117 
   118 	/* Draw a background pattern if the surface has transparency */
   119 	if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
   120 	    draw_background(screen);
   121 
   122 	/* Display the image */
   123 	SDL_BlitSurface(image, NULL, screen, NULL);
   124 	SDL_UpdateRect(screen, 0, 0, 0, 0);
   125 
   126 	/* Wait for any keyboard or mouse input */
   127 	for ( i=SDL_NOEVENT; i<SDL_NUMEVENTS; ++i ) {
   128 		switch (i) {
   129 		    case SDL_KEYDOWN:
   130 		    case SDL_MOUSEBUTTONDOWN:
   131 		    case SDL_QUIT:
   132 			/* Valid event, keep it */
   133 			break;
   134 		    default:
   135 			/* We don't want this event */
   136 			SDL_EventState(i, SDL_IGNORE);
   137 			break;
   138 		}
   139 	}
   140 	SDL_WaitEvent(NULL);
   141 
   142 	/* We're done! */
   143 	SDL_FreeSurface(image);
   144 	exit(0);
   145 }