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