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